04-06-2018, 07:10 AM
(This post was last modified: 04-06-2018, 07:13 AM by Guido1982.
Edit Reason: Edit 1: turn new validation file into function
)
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
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
modules/SalesOrder/ValidateStatus.php
My questions
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($r, 0, '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($r, 0, '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($r, 0, '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($r, 0, '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?