1. Fabrik 3.8 has been released. As usual, we strongly recommend that you backup your site (using Akeeba) before upgrading. Report any issues in the forums, we will answer promptly.

PHP list plugin

Jul 4, 2017
PHP list plugin

  • Front end example(top)

    This plug-in adds a button in a list row.
    Depending on list / Links / "Render buttons as" option, it is rendered as a floating tooltip, or an inline button.
    The power of Fabrik allows you to add as many buttons as you want !
    Floating.PNG Inline.PNG

    Settings (top)

    php-options.png
    • Access - Which users can see the php button
    • Button label - the label to apply to the php button
    • PHP file- the file that should be run when the php button is pressed.
      • 2.x: Located in components/com_fabrik/plugins/table/tablephp/scripts
      • 3.x: Location in plugins/fabrik_list/php/scripts
    • PHP code - If no file selected then the php code entered in this textarea will be run. Do not encase your PHP code with "<?php ?>".
      IMPORTANT: placeholders like {element___table}, {rowid} etc are not usable here and in php file, neither
      Status -
      if you need to show a status message in AJAX mode, you can set the $statusMsg variable, which Fabrik will pass back to the browser and display.
    • JS code - Any JavaScript code code entered here will be run when the plugin button is clicked (at the end of this plugin buttonAction() method).
      If your code returns false, the button will not fire the request to run the PHP code.
    • Success message - The message to be shown after the PHP script has run

    Sample code (top)


    A couple of useful Javascript variables: (top)

    Code (Javascript):

    console.log(ids); //outputs an array of the selected row ids
    console.log(rows); // outputs a object key'ed on the selected row ids, each value is an object containing the row data.
     

    Accessing list data in Javascript(top)


    You can get at the selected row data by iterating through the 'rows' variable:

    Code (Text):

    jQuery.each(rows, function(rowid, row) {
       // echo the value of yourtable___yourelement to the console
       fconsole(row.yourtable___yourelement);
    })
     

    Get selected row id(s) (top)

    As more than one row can be selected, in a list plugin we are getting an array of posted ids. To retreive them, use :
    PHP:
    $app = JFactory::getApplication();
    $ids = $app->input->get('ids', array(), 'array');

    Accessing the list model object(top)

    The list model is defined by the variable $model, so for example to get the basic list information you can do:

    PHP:
    $item = $model->getTable();
    echo "<pre>";print_r($item);exit;
     

    Accessing the selected rows' data(top)

    PHP:
    foreach ($ids AS $myid)
    {
        $row = $model->getRow($myid);
        $element = $row->your-full-elementname;
        $element_raw = $row->your-full-elementname_raw;
    //echo "<pre>";print_r($row);
    }
    //exit;
     

    Using list php plugin to update Wordpress blog post in other database(top)

    This solution's purpose is the collaboration when creating a press release that would be published in a Wordpress blog site.

    Two lists are used - one of them shows blog post drafts from another database (used by Wordpress blog) and second is related to it, containing comments and suggestions for blog post drafts. Details view of a post draft shows with content plugin in view's footer also the list with comments and suggestions that are related to this post. People can add to each row
    1) excerpt from post draft (exact copy-paste)
    2) suggestion how should it actually written (only the alternate version, nothing else)
    3) comment about this suggestion.

    When the main author approves fully the suggestion, he/she choose a button "Approved" in list view (or whatever that should run the php plugin) and click on it. As result the part of the blog post draft would be replaced with a new version.

    When using the code from code box don't include php tags.

    PHP:
    <?php

    $item = $model->getTable();
    $app = JFactory::getApplication();

    // getting array of current Fabrik list's main table pk values and imploding them to comma separated list:

    $ids = $app->input->get('ids', array(), 'array');
    $id = implode(',', $ids);

    // connecting to the default database:

    $db = FabrikWorker::getDbo();

    // getting current list's main db table name

    $curtab = $db->quoteName($item->db_table_name);

    // Defining needed fields. $db->quoteName ensures that the field names would be surrounded by appropriate quotes ``:

    $c_id = $db->quoteName('id');
    $c_parent = $db->quoteName('parent_id');
    $c_orig = $db->quoteName('orig_text');
    $c_sugg = $db->quoteName('suggested');

    // Let's query

    $query = "SELECT $c_parent, $c_orig, $c_sugg FROM $curtab WHERE $c_id IN($id)";
    $db->setQuery($query);

    // As this query is set to return multiple rows of data from multiple fields:

    $rows = $db->loadObjectList();

    /*
        * We got needed data.
        * Now let's connect to the other database where WP blog data resides (Fabrik connection to this db should be set! In this case its id = 2):
    */


    $db2 = FabrikWorker::getDbo(false, 2);

    // Defining table and needed fields, again using $db->quoteName

    $other = $db->quoteName('wp_posts');
    $o_id = $db->quoteName('ID');
    $o_post = $db->quoteName('post_content');

    // Using results of the previous query, generating own UPDATE query for each selected row:

    foreach ($rows as $row)
    {
        $q = $db2->getQuery(true);

        /*
            * Setting new value for `post_content` field data = replace in `post_content` field data the string $row->orig_text with the string $row->suggested:
            * IMPORTANT: $db2->quote is here the only possible way to ensure that the string values would be surrounded by right quotes in query.
              See i.e thread http://fabrikar.com/forums/index.php?threads/list-php-plugin-query-execution-fails.37517
        */


        $fields = array(
            $o_post . ' = REPLACE(' . $o_post . ', '. $db2->quote($row->orig_text) . ', ' . $db2->quote($row->suggested) . ')'
        );
        $conditions = array(
            $o_id . ' = ' . $row->parent_id
        );

        // Generating the UPDATE query

        $q->update($other)->set($fields)->where($conditions);
        $db2->setQuery($q);

        // processing query/queries

        $db2->execute();
    }
    ?>

    Additional help (top)

    The following forum thread provides additional help with using this plug-in : http://fabrikar.com/forums/showthread.php?t=20094

    Note that JRequest::getVar() is now deprecated. Use instead :
    PHP:
    $app = JFactory::getApplication();
    $ids = $app->input->get('ids', array(), 'array');

    Programming constraints(top)

    Because AJAX ... < to be explained... >
    some PHP can't be done :
    • Redirection to an URL
    • Load a specific Form
    For these purposes, use Javascript Plugin.
  • Loading...