Page 1 of 1

custom condition of display for an action button

PostPosted: Mon Jun 23, 2008 1:59 am
by Jean
Hi Steve,

I have a button to order items. I would like to hide/display according to the user. I could not find the solution in the forum. Could you help me ?

My action is :

Code: Select all
[command_selected]
   url="javascript:commandSelected('result_list')"
   label="Commander"
   description="Commander les besoins sélectionnés"
   category=selected_result_actions
   permission=edit
   icon="{$dataface_url}/images/edit.gif"


Thank you
Jean

PostPosted: Mon Jun 23, 2008 8:30 am
by shannah
Hi Jean,

There are a couple of options here:
1. Use the permission parameter.
2. Use the condition parameter.

To use the permission parameter, you would need to deny the user the permission needed to see the action. Currently your action uses the edit permission. You could just create a new permission in the permissions.ini for your action, and deny him that....

A more direct approach is to use the condition parameter. This allows you to specify a php expression that evaluates to a boolean.

e.g.
Code: Select all
[command_selected]
   url="javascript:commandSelected('result_list')"
   label="Commander"
   description="Commander les besoins sélectionnés"
   category=selected_result_actions
   permission=edit
   icon="{$dataface_url}/images/edit.gif"
   condition="canOrderItems();"



Then just define a function somewhere in your PHP code called canOrderItems() that returns true if the current user can order the items, and false otherwise.

-Steve

PostPosted: Tue Jun 24, 2008 7:11 am
by Jean
Steve,
I add the function into my class in the table (xata_needs.php)

Code: Select all
function canOrderItems(){
      $auth =& Dataface_AuthenticationTool::getInstance();
$user =& $auth->getLoggedInUser(); // returns a Dataface_Record object
      $app =& Dataface_Application::getInstance();
$db = $app->db(); // database resource handle...
    $record =& $app->getRecord();
$usager=trim(strtolower($user));
    $sql="select * from glpi_users where LOWER('name') LIKE '$usager' AND (location='492111728' OR location='492023960')";
    if ($res=mysql_query($sql, $db)){
    return true;
    }
      return false;
      }


Now the button does not appear in any case.
Do you have an idea ?
Jean

PostPosted: Wed Jul 09, 2008 1:50 am
by Jean
Hi Steve,

It is as if the function was not loaded. I tried to insert it into xata_needs.php in the table directory xata_needs, into ApplicationDelegate.php in the conf directory, into the order.php that is the action itself but nothing happens.

The condition works if I put 1==1...

Jean

PostPosted: Wed Jul 09, 2008 9:29 am
by shannah
One pesky thing about the conditions for actions is that errors are suppressed with executing the content, so if there is an error in your function it may not show up.

You may want to try testing your function to make sure that it is bug free by calling it somewhere after it is defined (so you know that it will run).

PostPosted: Thu Jul 10, 2008 5:41 am
by Jean
I tried to have my function return only true to see if the button appears, it does not. My function is placed inside DelegateClass.php in the conf directory. I tried in other files (xata_needs.php in the xata_needs directory, in the action file commander.php) but with the same result.
Jean :?

PostPosted: Fri Jul 11, 2008 10:58 am
by shannah
What i'm thinking is that there might be some kind of error ocurring inside the function when it is called. When functions are called inside actions the errors are suppressed so you may not be able to see the error.

Try calling your function in your index.php file (some time after the function has been defined) to test and make sure it returns properly.

-Steve

PostPosted: Tue Jul 15, 2008 5:55 am
by Jean
Well Steve, I have followed your advice and my function returns 1.
Here is my function :
Code: Select all
function isAdmin(){
    $auth =& Dataface_AuthenticationTool::getInstance();
    $user =& $auth->getLoggedInUser();
     $app =& Dataface_Application::getInstance();
    $db = $app->db(); // database resource handle...
    $usager=$user->strval('identifiant');
    $sql="select * from glpi_users where name LIKE '$usager' AND (location='492111728' OR location='492023960')";
    if ($res=mysql_query($sql, $db)){
    return true;
    }
    return false;
}

Then I put it into the myAction.php file but it is as if my function was not loaded. The button never appears.
Jean

PostPosted: Tue Jul 15, 2008 9:21 am
by shannah
Then I put it into the myAction.php file

Does this mean that the function is defined in your myAction.php file, or that you are calling it in your myAction.php file.

If it is defined in the myAction.php file then that is your problem. All actions.ini expressions are executed before any individual php action files are loaded. Best to place functions like this in some central file that is included at the beginning at your index.php file.

-Steve

PostPosted: Wed Jul 16, 2008 7:18 am
by Jean
I tried so many directions, I am getting lost. May be to-morrow I'll be clear to solve the whole of it.
Jean