Ajax request

Hi,

i've added a checkbox in my image.php and i need to setup an ajax request to send the checkbox value when it's clicked and update the current image information in database.
I've already added the necessary fields to database, get and set functions to class-image.php no problem with that.
What's the best file/url to call for my request?
I need then to call my class-image.php set function to save the necessary data of the current image, probably i'll need to create an image object for that, right?

Comments

  • acrylian Administrator, Developer
    I've already added the necessary fields to database, get and set functions to class-image.php no problem with that.
    If you mean you have both change the main images database table and the core image class I strongely suggest to rethink that way.
    If you need extra methods for the image class you should use object orientation to extend the class with what you need extra.

    As for the database table you should be using the plugin_storage table for that reason.

    As for the ajax request you should not be needing to call the core class. You should use a plugin or theme functionality for that. A custom theme page also would work.
  • Hmm.. i followed this post http://www.zenphoto.org/support/topic.php?id=1217#post-6820
    i know it is very old but i didn't find out anything more recent.
    I found it strange be able to add fields to image database table..
    Can i add fields to plugin_storage table or i have to use the current columns?
    It makes more sense the way you're saying because it will be a pain to update zp in the future the way i'm doing it.
  • acrylian Administrator, Developer
    A lot changed since the early days of Zenphoto. The plugin_storage table was exactly introduce to have theme or plugins authors have a place for custom storage.

    It is recommended to not add fields to the plugin_storage table and use the ones provide. It is also part of Zenphoto. Some recomended usage of the three fields:
    1. An identifier of your action ( like "mycustomplugin">
    2. the identifier of the item to attach the data to ('myitem').
    3. The actual value. If you need more fields you can use serialization for example or just more rows per item.
    Since this table can be used for anything it is not covered by the object model. You have to use plain sql queries.

    Of course if you need really special things you can always create complete custom tables and write something to use them.
  • So i have to develop a plugin! I think i can use the plugin_storage table.
    Can you recommend some example of plugin to follow?
    Best practices to develop plugins to zenphoto?
    Thank you.
  • acrylian Administrator, Developer
    Technically you might be able to do it as a theme custom function as well. But maybe what you want to do is of use for others so you might want to make it available as a plugin later on.

    There is a tutorial about plugins:
    http://www.zenphoto.org/news/zenphoto-plugin-architecture
    Within that a plugin ndemo file is linked which shows a basic structure:
    https://github.com/zenphoto/DevTools/tree/master/demo_plugin-and-theme

    A plugin that uses the plugin_storage table is for example the multiple_layouts one.

    Best you should also have read the theming tutorial and the object model tutorial.
  • Well i think what i need is too specific to be done as a general plugin, if i can do as a theme function to begin it would be great. Later on i can try to make it a plugin.
    I just have to create the php files i need inside theme folder?
    Thank you.
  • acrylian Administrator, Developer
    Themes support a `functions.php` file that loads automatically if present. Unless you want to add checkboxes or else to the backend pages you don't need a plugin. Of course it does not hurt to do it like that even if it is not generally usable for others.

    Besides the default theme structure you can create anything additionally within your theme. Even that structure you can leave if you have the knowledge.
  • Nice, i'll go that way.
    Unless you want to add checkboxes or else to the backend pages you don't need a plugin.
    No, i just need a checkbox in image.php for certain users be able to use as a picture approval system.
    Thank you very much for your help.
  • acrylian Administrator, Developer
    A picture approval system? A kind of clients selecting what they like to buy or something? Then you should maybe take a look at the favorites plugin. Might server your purpose already. Also the rating plugin could server as a similar purpose (e.g five stars = approved).
  • Yes i know that, but i need to save and expose the date it was approved. I really need it works this way.
    I did as you said added the functions i needed to themes folder functions.php. I've created a controller.php in themes folder to handle with checkbox ajax request call the desired functions.php function and update the plugin_storage table. I've included the functions.php in controller.php but when i do the ajax request to controller.php error is thrown "Call to undefined function zp_register_filter() in functions.php on line 2".
    Of course this is related how the files are included..
    How to make sure my controller.php has all preceding includes needed so i can use functions.php functions?
  • acrylian Administrator, Developer
    Ok. If you wish your controller.php to feature the standard Zenphoto functions you need to call it as a custom page: `/page/controller`
  • Yes! It's all working now. Thank you
  • @jalves can you share the code when you are finished I am interested in this.
  • acrylian Administrator, Developer
    Actually it is quite simple if using jQuery. Here some basic examples:

    To send something to a page that has ZP stuff available:
    `
    $.ajax({
    url: "/page/"
    });
    `
    http://api.jquery.com/jQuery.ajax/

    To load somehting here a custom theme page as well so the function are all avaiable:
    `$("#element-to-load-to").load("/page/");`
    http://api.jquery.com/load/
Sign In or Register to comment.