function check_valid_bean(SugarBean &$bean) { global $app_list_strings, $timedate; require_once 'modules/AOW_Processed/AOW_Processed.php'; $processed = new AOW_Processed(); if (!$this->multiple_runs) { $processed->retrieve_by_string_fields(array('aow_workflow_id' => $this->id, 'parent_id' => $bean->id)); if ($processed->status == 'Complete') { //has already run so return false return false; } } if (!isset($bean->date_entered)) { $bean->date_entered = $bean->fetched_row['date_entered']; } if ($this->flow_run_on) { switch ($this->flow_run_on) { case 'New_Records': if (strtotime($bean->date_entered) < strtotime($this->date_entered)) { return false; } break; case 'Modified_Records': if (strtotime($bean->date_modified) < strtotime($this->date_entered) && strtotime($bean->date_modified) != strtotime($bean->date_entered)) { return false; } break; } } $sql = "SELECT id FROM aow_conditions WHERE aow_workflow_id = '" . $this->id . "' AND deleted = 0 ORDER BY condition_order ASC"; $result = $this->db->query($sql); while ($row = $this->db->fetchByAssoc($result)) { $condition = new AOW_Condition(); $condition->retrieve($row['id']); $field = $condition->field; $value = $condition->value; $path = unserialize(base64_decode($condition->module_path)); $condition_bean = $bean; //TODO Add related module implementation, until return false if (isset($path[0]) && $path[0] != $bean->module_dir) { return false; } $dateFields = array('date', 'datetime', 'datetimecombo'); if (isset($app_list_strings['aow_sql_operator_list'][$condition->operator])) { $data = $condition_bean->field_defs[$field]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $field = $data['id_name']; $condition->field = $data['id_name']; } $field = $condition_bean->{$field}; if (in_array($data['type'], $dateFields)) { $field = strtotime($field); } switch ($condition->value_type) { case 'Field': $data = $condition_bean->field_defs[$value]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $value = $data['id_name']; } $value = $condition_bean->{$value}; if (in_array($data['type'], $dateFields)) { $value = strtotime($value); } break; case 'Any_Change': $value = $condition_bean->fetched_row[$condition->field]; if (in_array($data['type'], $dateFields)) { $value = strtotime($value); } switch ($condition->operator) { case 'Not_Equal_To': $condition->operator = 'Equal_To'; break; case 'Equal_To': default: $condition->operator = 'Not_Equal_To'; break; } break; case 'Date': $params = unserialize(base64_decode($value)); if ($params[0] == 'now') { $value = date('Y-m-d H:i:s'); } else { $value = $condition_bean->{$params}[0]; } if ($params[1] != 'now') { switch ($params[3]) { case 'business_hours': if (file_exists('modules/AOBH_BusinessHours/AOBH_BusinessHours.php')) { require_once 'modules/AOBH_BusinessHours/AOBH_BusinessHours.php'; $businessHours = new AOBH_BusinessHours(); $amount = $params[2]; if ($params[1] != "plus") { $amount = 0 - $amount; } $value = $businessHours->addBusinessHours($amount, $timedate->fromDb($value)); $value = strtotime($timedate->asDb($value)); break; } //No business hours module found - fall through. $params[3] = 'hours'; default: $value = strtotime($value, $app_list_strings['aow_date_operator'][$params[1]] . " {$params['2']} " . $params[3]); break; } } else { $value = strtotime($value); } break; case 'Multi': $value = unencodeMultienum($value); if ($data['type'] == 'multienum') { $field = unencodeMultienum($field); } switch ($condition->operator) { case 'Not_Equal_To': $condition->operator = 'Not_Contains'; break; case 'Equal_To': default: $condition->operator = 'Contains'; break; } break; case 'SecurityGroup': if (file_exists('modules/SecurityGroups/SecurityGroup.php')) { $sg_module = $condition_bean->module_dir; if (isset($data['module']) && $data['module'] != '') { $sg_module = $data['module']; } $value = $this->check_in_group($field, $sg_module, $value); $field = true; break; } case 'Value': default: if (in_array($data['type'], $dateFields) && trim($value) != '') { $value = strtotime($value); } break; } if (!$this->compare_condition($field, $value, $condition->operator)) { return false; } } } return true; }
function set_record(SugarBean $record, SugarBean $bean, $params = array(), $in_save = false) { global $app_list_strings, $timedate; $record_vardefs = $record->getFieldDefinitions(); if (isset($params['field'])) { foreach ($params['field'] as $key => $field) { if ($field == '') { continue; } switch ($params['value_type'][$key]) { case 'Field': if ($params['value'][$key] == '') { continue; } $data = $bean->field_defs[$params['value'][$key]]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $params['value'][$key] = $data['id_name']; } $value = $bean->{$params}['value'][$key]; break; case 'Date': $dformat = 'Y-m-d H:i:s'; if ($record_vardefs[$field]['type'] == 'date') { $dformat = 'Y-m-d'; } switch ($params['value'][$key][3]) { case 'business_hours': if (file_exists('modules/AOBH_BusinessHours/AOBH_BusinessHours.php')) { require_once 'modules/AOBH_BusinessHours/AOBH_BusinessHours.php'; $businessHours = new AOBH_BusinessHours(); $dateToUse = $params['value'][$key][0]; $sign = $params['value'][$key][1]; $amount = $params['value'][$key][2]; if ($sign != "plus") { $amount = 0 - $amount; } if ($dateToUse == "now") { $value = $businessHours->addBusinessHours($amount); } else { if ($dateToUse == "field") { $dateToUse = $params['field'][$key]; $value = $businessHours->addBusinessHours($amount, $timedate->fromDb($bean->{$dateToUse})); } else { $value = $businessHours->addBusinessHours($amount, $timedate->fromDb($bean->{$dateToUse})); } } $value = $timedate->asDb($value); break; } $params['value'][$key][3] = 'hours'; //No business hours module found - fall through. //No business hours module found - fall through. default: if ($params['value'][$key][0] == 'now') { $date = gmdate($dformat); } else { if ($params['value'][$key][0] == 'field') { $date = $record->fetched_row[$params['field'][$key]]; } else { $date = $bean->fetched_row[$params['value'][$key][0]]; } } if ($params['value'][$key][1] != 'now') { $value = date($dformat, strtotime($date . ' ' . $app_list_strings['aow_date_operator'][$params['value'][$key][1]] . $params['value'][$key][2] . ' ' . $params['value'][$key][3])); } else { $value = date($dformat, strtotime($date)); } break; } break; case 'Round_Robin': case 'Least_Busy': case 'Random': switch ($params['value'][$key][0]) { case 'security_group': if (file_exists('modules/SecurityGroups/SecurityGroup.php')) { require_once 'modules/SecurityGroups/SecurityGroup.php'; $security_group = new SecurityGroup(); $security_group->retrieve($params['value'][$key][1]); $group_users = $security_group->get_linked_beans('users', 'User'); $users = array(); $r_users = array(); if ($params['value'][$key][2] != '') { require_once 'modules/ACLRoles/ACLRole.php'; $role = new ACLRole(); $role->retrieve($params['value'][$key][2]); $role_users = $role->get_linked_beans('users', 'User'); foreach ($role_users as $role_user) { $r_users[$role_user->id] = $role_user->name; } } foreach ($group_users as $group_user) { if ($params['value'][$key][2] != '' && !isset($r_users[$group_user->id])) { continue; } $users[$group_user->id] = $group_user->name; } break; } //No Security Group module found - fall through. //No Security Group module found - fall through. case 'role': require_once 'modules/ACLRoles/ACLRole.php'; $role = new ACLRole(); $role->retrieve($params['value'][$key][2]); $role_users = $role->get_linked_beans('users', 'User'); $users = array(); foreach ($role_users as $role_user) { $users[$role_user->id] = $role_user->name; } break; case 'all': default: $users = get_user_array(false); break; } // format the users array $users = array_values(array_flip($users)); if (empty($users)) { $value = ''; } else { if (sizeof($users) == 1) { $value = $users[0]; } else { switch ($params['value_type'][$key]) { case 'Round_Robin': $value = getRoundRobinUser($users, $this->id); break; case 'Least_Busy': $user_id = 'assigned_user_id'; if (isset($record_vardefs[$field]['id_name']) && $record_vardefs[$field]['id_name'] != '') { $user_id = $record_vardefs[$field]['id_name']; } $value = getLeastBusyUser($users, $user_id, $record); break; case 'Random': default: shuffle($users); $value = $users[0]; break; } } } setLastUser($value, $this->id); break; case 'Value': default: $value = $params['value'][$key]; break; } if ($record_vardefs[$field]['type'] == 'relate' && isset($record_vardefs[$field]['id_name'])) { $field = $record_vardefs[$field]['id_name']; } $record->{$field} = $value; } } $bean_processed = isset($record->processed) ? $record->processed : false; if ($in_save) { global $current_user; $record->processed = true; $check_notify = $record->assigned_user_id != $current_user->id && $record->assigned_user_id != $record->fetched_row['assigned_user_id']; } else { $check_notify = $record->assigned_user_id != $record->fetched_row['assigned_user_id']; } $record->process_save_dates = false; $record->new_with_id = false; $record->save($check_notify); $record->processed = $bean_processed; }
function check_valid_bean(SugarBean &$bean) { global $app_list_strings, $timedate; require_once 'modules/AOW_Processed/AOW_Processed.php'; $processed = new AOW_Processed(); if (!$this->multiple_runs) { $processed->retrieve_by_string_fields(array('aow_workflow_id' => $this->id, 'parent_id' => $bean->id)); if ($processed->status == 'Complete') { //has already run so return false return false; } } if (!isset($bean->date_entered)) { $bean->date_entered = $bean->fetched_row['date_entered']; } if ($this->flow_run_on) { // database time correction with the user's time-zoneqq $beanDateEnteredTimestamp = strtotime($timedate->asUser(new DateTime($timedate->fromDb($bean->date_entered)))); $beanDateModifiedTimestamp = strtotime($timedate->asUser(new DateTime($timedate->fromDb($bean->date_modified)))); $thisDateEnteredTimestamp = strtotime($this->date_entered); switch ($this->flow_run_on) { case 'New_Records': // it is an invalid bean if the user modify it now because the affection need on new records only! if (!empty($bean->fetched_row) || $beanDateEnteredTimestamp < $thisDateEnteredTimestamp) { return false; } break; case 'Modified_Records': // it isn't a valid bean if the user create it now because the affection need on already exists records only! if (empty($bean->fetched_row) || $beanDateModifiedTimestamp < $thisDateEnteredTimestamp && $beanDateModifiedTimestamp != $beanDateEnteredTimestamp) { return false; } break; } } $sql = "SELECT id FROM aow_conditions WHERE aow_workflow_id = '" . $this->id . "' AND deleted = 0 ORDER BY condition_order ASC"; $result = $this->db->query($sql); $query_array = array(); while ($row = $this->db->fetchByAssoc($result)) { $condition = new AOW_Condition(); $condition->retrieve($row['id']); $path = unserialize(base64_decode($condition->module_path)); $condition_bean = $bean; if (isset($path[0]) && $path[0] != $bean->module_dir) { $query_array = $this->build_query_where($condition, $condition_bean, $query_array); continue; } $field = $condition->field; $value = $condition->value; $dateFields = array('date', 'datetime', 'datetimecombo'); if (isset($app_list_strings['aow_sql_operator_list'][$condition->operator])) { $data = $condition_bean->field_defs[$field]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $field = $data['id_name']; $condition->field = $data['id_name']; } $field = $condition_bean->{$field}; if (in_array($data['type'], $dateFields)) { $field = strtotime($field); } switch ($condition->value_type) { case 'Field': $data = $condition_bean->field_defs[$value]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $value = $data['id_name']; } $value = $condition_bean->{$value}; if (in_array($data['type'], $dateFields)) { $value = strtotime($value); } break; case 'Any_Change': $value = $condition_bean->fetched_row[$condition->field]; if (in_array($data['type'], $dateFields)) { $value = strtotime($value); } switch ($condition->operator) { case 'Not_Equal_To': $condition->operator = 'Equal_To'; break; case 'Equal_To': default: $condition->operator = 'Not_Equal_To'; break; } break; case 'Date': $params = unserialize(base64_decode($value)); $dateType = 'datetime'; if ($params[0] == 'now') { $value = date('Y-m-d H:i:s'); } else { if ($params[0] == 'today') { $dateType = 'date'; $value = date('Y-m-d'); $field = strtotime(date('Y-m-d', $field)); } else { $value = $condition_bean->{$params}[0]; } } if ($params[1] != 'now') { switch ($params[3]) { case 'business_hours': if (file_exists('modules/AOBH_BusinessHours/AOBH_BusinessHours.php')) { require_once 'modules/AOBH_BusinessHours/AOBH_BusinessHours.php'; $businessHours = new AOBH_BusinessHours(); $amount = $params[2]; if ($params[1] != "plus") { $amount = 0 - $amount; } $value = $businessHours->addBusinessHours($amount, $timedate->fromDb($value)); $value = strtotime($timedate->asDbType($value, $dateType)); break; } //No business hours module found - fall through. $params[3] = 'hours'; default: $value = strtotime($value . ' ' . $app_list_strings['aow_date_operator'][$params[1]] . " {$params['2']} " . $params[3]); if ($dateType == 'date') { $value = strtotime(date('Y-m-d', $value)); } break; } } else { $value = strtotime($value); } break; case 'Multi': $value = unencodeMultienum($value); if ($data['type'] == 'multienum') { $field = unencodeMultienum($field); } switch ($condition->operator) { case 'Not_Equal_To': $condition->operator = 'Not_One_of'; break; case 'Equal_To': default: $condition->operator = 'One_of'; break; } break; case 'SecurityGroup': if (file_exists('modules/SecurityGroups/SecurityGroup.php')) { $sg_module = $condition_bean->module_dir; if (isset($data['module']) && $data['module'] != '') { $sg_module = $data['module']; } $value = $this->check_in_group($field, $sg_module, $value); $field = true; break; } case 'Value': default: if (in_array($data['type'], $dateFields) && trim($value) != '') { $value = strtotime($value); } break; } if (!$this->compare_condition($field, $value, $condition->operator)) { return false; } } } if (isset($query_array['where'])) { $query = 'SELECT ' . $bean->table_name . '.id AS id FROM ' . $bean->table_name . ' '; if (isset($query_array['join'])) { foreach ($query_array['join'] as $join) { $query .= $join; } } $query_where = ''; $query_array['where'][] = $bean->table_name . '.id = ' . "'" . $bean->id . "'"; foreach ($query_array['where'] as $where) { $query_where .= ($query_where == '' ? 'WHERE ' : ' AND ') . $where; } $query .= ' ' . $query_where; $rel_check = $bean->db->getOne($query); if ($rel_check == '') { return false; } } return true; }