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,