1. Fabrik 3.8.1 has been released. It is mostly bug fixes and feature enhancements, but does include two new plugins (push notifications, and the sequence element). As usual we strongly recommend testing the new release on a sandbox if your application is mission critical, and always do an Akeeba backup before updating.

Anatomy of a basic phptable plugin

Discussion in 'Hints, Tips and Tricks' started by achartier, Feb 7, 2011.

  1. achartier

    achartier Active Member

    Level: Community
    I am creating this post so that others can save some time using the phptable plugin. This tutorial consolidates information I obtained from multiple fabrik posts as well as many google searches. I am not a fabrik expert and am a neophyte when it comes to mysql. I hope this helps others like me.

    My problem: I use DocMan on my website to manage hundreds of pdf files for my users.I chose DocMan because it made it easy to assign viewing priviledges by document. We made a decision to change the viewing rights of most of the documents to all users from all registered users. This represented about 300 documents. DocMan has no automated way to do this and to do each manually involves opening each individually, changing the permission and then saving it. A very labor intensive and time consuming process.

    It occurred to me I could use Fabrik to connect to the docman table and create a table php plugin to walk through the records and change the viewers field. There are probably other ways to solve this problem, but hey, I wanted to learn how to make a phptable plugin so why not.

    Step 1
    Obtain the phptable plugin (you must be a bronze or silver member to get it) http://fabrikar.com/project/downloads/doc_download/14-run-php-table-plugin
    Install the plugin from the plugins tab in Fabrik.

    Step 2
    Create a new table in fabrik, in my case I called it Documents, and publish it. On the Data tab select your table from the "Link to table" dropdown. In my case this was jos_docman. Save the table.

    Step 3
    Create the plugin. The first problem I ran into was where the plugin lived; it wasn't obvious (at least to me). Open the table again and click on the Plug-ins tab. Click the Add button and select Run PHP. In my case this is a backend only function so I set the Access to Manager and the Button Label to Everyone (meaning that I will be assigning access to these documents to Everyone).

    Creating this type of plugin will create a button with the button label you define. This button will be located at the bottom of the page when viewing the table in table view. Clicking on the button runs the php code from a file (as selected in the PHP File dropdown) or directly from the PHP Code box.

    In my case I used the PHP code box and placed the following code into it (I will go through each line later):

    $db =& JFactory::getDBO();
    $ids = JRequest::getVar('ids', array());
    $count = 0;
    foreach ($ids AS $id) {
    $update = "UPDATE jos_docman SET dmowner='-1' WHERE id=" . (int)$id;
    $db->setQuery($update);
    $db->query();
    $count++;
    }
    $msg = JText::_('Changed viewers to Everybody on ' . (int)$count . ' records.');
    $params =& $this->getParams();
    $params->set('table_php_msg', $msg);

    Save the table.

    Step 4
    Click on the view data link for the table to open the table in table view. If there are too many fields being displayed you may wish to go to the elements for the table and disable some number from table view to make it more managable. All you really need to see here are the fields necessary to allow you to determine which records you want to change.

    Click on the checkbox at the far right of each record that you want to run the table plugin against. If there are no checkboxes then you did not define editor access for the table, go back into the table and on the Access tab select an appropriate value for Edit records. In my case I selected Manager as this was to be a backend function.

    Once you have selected the records you want click on your new button at the bottom of the page and the table plugin runs the php code! Simple, yes?

    A walk through the code I used.

    This information may also serve useful for anyone creating this type of plugin. I will include some DocMan specific details only because someone may come here with the same requirement and these details may be useful.

    $db =& JFactory::getDBO();
    This line creates a connection to your site database. You need this to do any work with the database.

    $ids = JRequest::getVar('ids', array());
    This line gets an array of records ids for each of the records you have selected. In my case it returned the key values for the docman records.

    $count = 0;
    Just a counter to be used when I display a count of records updated.

    foreach ($ids AS $id) {
    This is a loop directive that will perform the loop for each item in the array of ids and places the value of the array item (the record id) into the $id variable for use in the loop.

    $update = "UPDATE jos_docman SET dmowner='-1' WHERE id=" . (int)$id;
    Here I am creating an sql statement to update the database. The table name is jos_docman and I am setting the dmowner value to '-1'. This value to docman represents Everybody. (DocMan note - 0 is for All Registered Users and -12 is for Special. There are others.) I want to ensure I update the correct record so we set the "WHERE id=" to the item id from the array which is the record key.

    $db->setQuery($update);
    $db->query();

    Set the query and execute it.

    $count++;
    Increment our little counter.

    $msg = JText::_('Changed viewers to Everybody on ' . (int)$count . ' records.');
    Create a nice little custom message to be displayed when we are done. Note: if you don't need anything to be returned from the function, like the count I have, you can add some text to the Success Message field in the plugin, otherwise it simply returns Code Run.

    $params =& $this->getParams();
    We need access to the table params so we can override the success message.

    $params->set('table_php_msg', $msg);
    Set the message.

    And that is it. There may be other ways to accomplish the same thing, and there are probably enhancements that can be made to this, but it works and did want I wanted it to do.
     
    waqas07, dimoss, rackem and 7 others like this.
  2. rob

    rob Administrator Staff Member

    Level: Community
    thanks! A very clear an complete walk though !
     
    waqas07 and mr_snappyz like this.

Share This Page