Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Migrating 'old style' custom validation to a Business Map validation
#1
I want to migrate a validation I have in a custom validation file to a Business Map Validation. The validation I have now looks like this:

SalesOrderValidation.php
PHP Code:
<?php

global $log,$currentModule,$adb,$current_user;

$screen_values json_decode($_REQUEST['structure'],true);

$q "SELECT vtiger_groups.groupname FROM vtiger_groups INNER JOIN vtiger_users2group ON vtiger_groups.groupid = vtiger_users2group.groupid WHERE vtiger_users2group.userid = ?";
$p = array($current_user->id);
$r $adb->pquery($q$p);

$groups = array();

while (
$groupname $adb->fetch_array($r)) {
 
$groups[] = $groupname['groupname'];
}

$q "SELECT sostatus FROM vtiger_salesorder WHERE salesorderid = ?";
$p = array(vtlib_purify($screen_values['record']));
$r $adb->pquery($q$p);
$current_status $adb->query_result($r0'sostatus');

$q "SELECT vtiger_role.rolename FROM vtiger_role INNER JOIN vtiger_user2role ON vtiger_role.roleid = vtiger_user2role.roleid WHERE vtiger_user2role.userid = ?";
$p = array($current_user->id);
$r $adb->pquery($q$p);
$rolename $adb->query_result($r0'rolename');

if (!
in_array('Debiteurenadministratie'$groups) && $current_status == 'Wacht op betaling' && $screen_values['sostatus'] != 'Wacht op betaling') {
 echo 
'U mag de status niet van \'Wacht op betaling\' af halen'// Translation: You are not allowed to change the status from 'Wacht op betaling' to anything else
 
die();
} else if (
$rolename != 'Directeur' && $screen_values['sostatus'] == 'Cancelled' && $current_status != 'Cancelled') {
 echo 
'U mag deze order niet annuleren, alleen de Directeur mag dat'// Translation: You are not allowed to cancel an order, only the CEO is.
 
die();
}

echo 
'%%%OK%%%'

This basically checks the user's role and group to see what they can and cannot do to specific SalesOrder statuses. Now I want to migrate this to a Busines map validation. I was thinking along these lines:

Business Map
Code:
<map>
 <originmodule>
   <originname>SalesOrder</originname>
 </originmodule>
 <fields>
   <field>
     <fieldname>sostatus</fieldname>
     <validations>
       <validation>
         <rule>custom</rule>
         <restrictions>
         <restriction>modules/SalesOrder/ValidateStatus.php</restriction>
         <restriction>check_sostatus_permissions</restriction>
         <restriction>validate_sostatus_permissions</restriction>
         </restrictions>
       </validation>
     </validations>
   </field>
 </fields>
</map>

modules/SalesOrder/ValidateStatus.php

PHP Code:
<?php

function validate_sostatus_permissions() {

global 
$log,$currentModule,$adb,$current_user;

$screen_values json_decode($_REQUEST['structure'],true);

$q "SELECT vtiger_groups.groupname FROM vtiger_groups INNER JOIN vtiger_users2group ON vtiger_groups.groupid = vtiger_users2group.groupid WHERE vtiger_users2group.userid = ?";
$p = array($current_user->id);
$r $adb->pquery($q$p);

$groups = array();

while (
$groupname $adb->fetch_array($r)) {
 
$groups[] = $groupname['groupname'];
}

$q "SELECT sostatus FROM vtiger_salesorder WHERE salesorderid = ?";
$p = array(vtlib_purify($screen_values['record']));
$r $adb->pquery($q$p);
$current_status $adb->query_result($r0'sostatus');

$q "SELECT vtiger_role.rolename FROM vtiger_role INNER JOIN vtiger_user2role ON vtiger_role.roleid = vtiger_user2role.roleid WHERE vtiger_user2role.userid = ?";
$p = array($current_user->id);
$r $adb->pquery($q$p);
$rolename $adb->query_result($r0'rolename');

if (!
in_array('Debiteurenadministratie'$groups) && $current_status == 'Wacht op betaling' && $screen_values['sostatus'] != 'Wacht op betaling') {
 return 
'U mag de status niet van \'Wacht op betaling\' af halen'// Translation: You are not allowed to change the status from 'Wacht op betaling' to anything else
 
die();
} else if (
$rolename != 'Directeur' && $screen_values['sostatus'] == 'Cancelled' && $current_status != 'Cancelled') {
 return 
'U mag deze order niet annuleren, alleen de Directeur mag dat'// Translation: You are not allowed to cancel an order, only the CEO is.
 
die();
}

return 
true;


My questions
  • Do I have the same 'screen_values' array at my disposal?
  • Am I correct to transform my "echo's" into "returns" and expect it to work?
  • Am I doing something else terribly wrong?
Reply
#2
You will be able to access the $_REQUEST and get screen_values as usual.

No, these functions must return TRUE or FALSE. The output message is the fourth <restriction>, the fourth, if given, is a label that will be translated in the context of the module

A quick read and a few comments:

- control the case of inline edit where you do not have all the field values in screen_values
- you don't need the die() after the return
Joe
TSolucio
Reply
#3
Ah, so it would be better to create multiple validations for each use case I think? I now use one script that checks multiple possible errors, but it would be better to split them I think?

Quote:control the case of inline edit where you do not have all the field values in screen_values
Do you mean the 'old' field value versus the value the user is trying to change to, or do you mean not all the record values?

Quote:you don't need the die() after the return
Thanks for the heads up, that was a remnant from when the script used 'echo' in stead of 'return'.
Reply
#4
I mean, not all the record values as in detail view you only get the field that is being changed

Yes, I think the idea is to have individual validations
Joe
TSolucio
Reply
#5
That field get automatically passed into the function you point to in the business map right? You just have to setup the function to accept the argument.
Reply
#6
Yes, the validation function receives the information it needs, I think it is the field, the value and some other information.
Joe
TSolucio
Reply


Forum Jump:


Users browsing this thread: 1 Guest(s)