Create action on selected related records

102 views
Skip to first unread message

hp.ros...@gmail.com

unread,
Apr 21, 2017, 5:44:55 AM4/21/17
to Xataface
Hi Steve,

I've found the following in xataface-global.js (in g2 module)

function actOnSelected(tableid, action, beforeHook, vals){
var ids = getSelectedIds(tableid);
if ( ids.length == 0 ){
alert("First you must select the rows you wish to modify.");
return false;
}
if ( typeof(beforeHook) != 'undefined' ){
if ( !beforeHook() ) return false;
}
var form = document.getElementById("result_list_selected_items_form");
form.elements['--selected-ids'].value = ids.join("\n");
form.elements['-action'].value = action;
form.submit();
return false;

}

Could you please explain how it can be used for creating "action" which act on selected record, especially in a related record list

Thanks
Pierrick

hp.ros...@gmail.com

unread,
Apr 28, 2017, 2:11:29 AM4/28/17
to Xataface
Hi,

I finally manage to do what I want, but in another way:

(1) Follow the indications given in http://xataface.com/wiki/Selected_Records_Actions

it is stated that:
  • selected_result_actions : Actions that operate on the currently checked records in the result list. This is used only in the old theme. The g2 theme uses a different mechanism for operating on selected records based on the class directive.
  • selected_related_result_actions : Actions operating on checked rows in related lists. Used only in the old theme. The g2 theme uses a different mechanism for operating on selected records based on the class directive.
(3) The class directive is to be added in the action.ini for the regarded action is 
class="related-selected-action"
(above example is for selected related record)

Please do not hesitate if any questions.

Pierrick

Ti Tou

unread,
Feb 16, 2022, 9:37:04 AM2/16/22
to Xataface
Hi,

Thanks for the share.

I followed the indications and adapt for a related list action but it seems the given code

    $records = df_get_selected_records($query);

returns nothing.

I tested with no selection, one and more selected rows.


xataface 3.0.0 (19 oct 2021) with the default theme (not G2) / PHP 7.4.3 / with admin profil (=> xataface role = ADMIN)

(actions.ini)
  [reserver]
    condition="$query['-table'] == 'pret'"
    category=selected_related_result_actions
    class="related-selected-action"
    materialIcon=done
;;    confirm="Êtes-vous sûr de réserver ce(s) machine(s) ?" /* no javascript pop-up, doesn't work ? */
    label="Réserver"
    description="Réserver ce(s) machine(s)"
;;    url="{$record->getURL('-action=reserver')}"
    url="{$this->url('-action=reserver')}"


(reserver.php)
  <?php
  class actions_reserver {
      function handle(&$params){
          // First get the selected records
          $app =& Dataface_Application::getInstance();
          $query =& $app->getQuery();

          // selection (relationships)
          $records = df_get_selected_records($query);

          if ( $query['-table'] != 'pret' ){
              return PEAR::raiseError("specific action for this table");
          }

          $updated = 0;  // Count the number of records we update
          $errs = array();   // Log the errors we encounter

          foreach ($records as $rec){ /* $records => count => 0 */
            ...

Result
  • 0 record selected ($updated = 0 / count($records) = 0)
  • No errors occurred ($errs = empty array)
  • error_reporting(E_ALL); => no PHP error in log

have you encountered and solved a similar problem?


Best regards,

hp.ros...@gmail.com

unread,
Feb 18, 2022, 6:23:25 AM2/18/22
to Xataface
Hi,

I add recently the same issue.
In order to identify the issue, I added some code in order to log the content of the "$records = df_get_selected_records($query);"
=> it was empty, whatever I did.

I've solved the issue just adding a # before the url, like this:
 #url="{$this->url('-action=reserver')}"

Please let me know whether it solve the issue also for you.

To Steve,
I would be very pleased if you could explain why this # make this work.

Thanks !

Ti Tou

unread,
Feb 24, 2022, 5:46:37 AM2/24/22
to Xataface
Hi,

Thanks for your help.

I try the # but the behavior seems the same : no error and $records is empty


For my needs, I find an other way to implement an action on a related record.

workaround

I use a relation table rel_pret_machine.

tables/rel_pret_machine/fields.ini.php
disable the link on the visible fielods on the related list :

    grafted field "lien" + noLinkFromListView = 1

(and all the other visible fields on the list have noLinkFromListView property = 1 to avoid user errors)

tables/rel_pret_machine/rel_pret_machine.php

function lien__renderCell( &$record ){
  return  '<a href=' . $record->getURL('-action=reserver') . ' target="_blank"> Réserver </a>';
 }

actions/reserver.php

    function handle(&$params){
        // First get the selected records
        $app =& Dataface_Application::getInstance();
        //$query =& $app->getQuery();

        // sélection (relations)
        //$records = df_get_selected_records($query);
        $rec = $app->getRecord(); // with grafted field/link action : only one selected record
/* @test
        if ( $query['-table'] != 'rel_pret_machines_candidates' ){
                            return PEAR::raiseError("fonctionnalité dédiée aux demandes de prêts");
        }
*/

        $updated = 0;  // Count the number of records we update
        $errs = array();   // Log the errors we encounter

//        foreach ($records as $rec){ useless iteration
          $record = df_get_record('pret', array('pr_id'=>'='.$rec->val('pm_pr_id'))); // ($record) is the "parent" record of this related record ($rec)

            /*if ( !$rec->checkPermission('edit'), array('field'=>'approved')) ){
                $errs[] = Dataface_Error::permissionDenied(
                    "You do not have permission to approve '".
                    $rec->getTitle().
                    "' because you do not have the 'edit' permission.");
                continue;
            }
            $rec->setValue('approved', 1);
            */

          $record->setValue('pr_ma_id', $rec->val('pm_pr_ma_id') );
          $res = $record->save(true);

            if ( PEAR::isError($res) ) $errs[] = $res->getMessage();
            else $updated++;

           unset($rec);
//        } useless iteration

        if ( $errs ){
            // Errors occurred.  Let's let the user know.
            // The $_SESSION['--msg'] content will be displayed to the user as a message
            // in the next page request.
            $_SESSION['--msg'] = 'Errors Occurred:<br/> '.implode('<br/> ', $errs);
        } else {
            $_SESSION['--msg'] = "No errors occurred";
        }


        //$url = $app->url('-action=list');   // A default URL in case no redirect was supplied
        $url = $record->getURL('-action=browse');   // A default URL in case no redirect was supplied
        if ( @$_POST['--redirect'] ) $url = base64_decode($_POST['--redirect']);
        $url .= '&--msg='.urlencode($updated.' machine(s) a(ont) été affectée(s).');

        // Redirect back to the previous page
        header('Location: '.$url);
        exit;
    }

Pro/cons
  • With a action button (unsuccessful trial), I need to test if there is at last one selected record (if not, I need a iteration for all the related records or a user warning) => test + iteration +/- user warning
  • With a grafted field/link action (workaround), there is only one selected record => no test of the selection + no iteration

Best regards,

Reply all
Reply to author
Forward
0 new messages