• Hello Fabrik Community

    Fabrik is now in the hands of the development team that brought you Fabrik for Joomla 4. We have recently transitioned the Fabrik site over to a new server and are busy trying to clean it up. We have upgraded the site to Joomla 4 and are running the latest version of Fabrik 4. We have also upgraded the Xenforo forum software to the latest version. Many of the widgets you might have been used to on the forum are no longer operational, many abandoned by the developers. We hope to bring back some of the important ones as we have time.

    Exciting times to be sure.

    The Fabrik 4.0 Official release is now available. In addition, the Fabrik codebase is now available in a public repository. See the notices about these in the announcements section

    We wish to shout out a very big Thank You to all of you who have made donations. They have really helped. But we can always use more...wink..wink..

    Also a big Thank You to those of you who have been assisting others in the forum. This takes a very big burden off of us as we work on bugs, the website and the future of Fabrik.

PHP list plugin

  • Views Views: 41,074
  • Last updated Last updated:

Navigation

  • 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​

    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​


    A couple of useful Javascript variables:​

    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

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

    Code:

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

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

    Accessing the list model object​
    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​
    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;

    updating the selected rows' data​
    PHP:

    $app = JFactory::getApplication();
    $ids = $app->input->get('ids', array(), 'array');
    foreach($ids as $id)
    {

    $couponcode = "hello";

    $model->updateRow($id, "courses.teachername", $couponcode);
    }
    //this code update Teachername column of your Courses table with checked row in fabrik list. you must use your own table and column instead of "courses.teachername" and also change $couponcode with your own variable

    Using list php plugin to add new random coupon to hikashop​
    with this sample you can generate random coupon in hikashop via fabrik and get result (coupon id and coupon code) back to the appropriate fabrik column and update you fabrik list
    PHP:

    $coupon= substr(base_convert(sha1(uniqid(mt_rand())), 16, 36), 0, 10);
    //in my case you must installed hikaserial but it seems you can edit this line for hikashop only installed
    require_once(JPATH_ADMINISTRATOR.'/components/com_hikaserial/helpers/helper.php');

    $discountClass = hikaserial::get('shop.class.discount');
    $data= new stdClass();
    $data->discount_code = $coupon;
    //$data->discount_flat_amount = 250;
    $data->discount_published = 1;//use your own value
    $data->discount_used_times = 1;//use your own value
    //$data->discount_flat_amount= 1000;
    $data->discount_percent_amount= 10;//use your own value
    $data->discount_currency_id = 183;//use your own value
    $data->discount_type = "coupon";

    $date = new DateTime(null, new DateTimeZone('Asia/Tehran'));//use your own value
    $dateTimeZone = new DateTimeZone("Asia/Tehran");//use your own value
    $dateTime = new DateTime("now", $dateTimeZone);//use your own value
    $timeOffset = $dateTimeZone->getOffset($dateTime);
    $newTime = time() + $timeOffset;

    $data->discount_start = $newTime;
    $data->discount_coupon_nodoubling=2;//use your own value
    $data->discount_quota = 1;//use your own value

    $result = $discountClass->save($data);
    dump($result, 'result');
    // dump(get_defined_vars(), 'vars');
    $app = JFactory::getApplication();
    $ids = $app->input->get('ids', array(), 'array');

    foreach($ids as $id)
    {
    //$row = $model->getRow($id);
    // dump($row, 'row');
    $db = JFactory::getDbo();
    $query = $db->getQuery(true);
    $query->select($db->quoteName('discount_code'));
    $query->from($db->quoteName('#__hikashop_discount'));
    $query->where($db->quoteName('discount_id') . ' = ' . $db->quote($result));
    $db->setQuery($query);
    $couponcode = $db->loadResult();
    // dump($query->__toString(), 'query');
    // dump($couponcode, 'couponcode');
    $model->updateRow($id, "courses.teachername", $couponcode);//use your own variable and column and etc
    }

    Using list php plugin to update Wordpress blog post in other database​
    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:

    $mydb = FabrikWorker::getDbo();

    // getting current list's main db table name

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

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

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

    // Let's query

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

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

    $rows = $mydb->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 $mydb->quoteName

    $other = $mydb->quoteName('wp_posts');
    $o_id = $mydb->quoteName('ID');
    $o_post = $mydb->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 https://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​

    The following forum thread provides additional help with using this plug-in : https://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​

    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.
Back
Top