/** * Use the condition statements and processed table to build query to retrieve beans to be actioned */ function get_flow_beans() { global $beanList, $app_list_strings, $sugar_config; if ($beanList[$this->flow_module]) { $module = new $beanList[$this->flow_module](); $where = ''; $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']); if (isset($app_list_strings['aow_sql_operator_list'][$condition->operator])) { $where_set = false; if ($where != '') { $where .= ' AND '; } $data = $module->field_defs[$condition->field]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $condition->field = $data['id_name']; } if (isset($data['source']) && $data['source'] == 'custom_fields') { $field = $module->table_name . '_cstm.' . $condition->field; } else { $field = $module->table_name . '.' . $condition->field; } switch ($condition->value_type) { case 'Field': $data = $module->field_defs[$condition->value]; if ($data['type'] == 'relate' && isset($data['id_name'])) { $condition->value = $data['id_name']; } if (isset($data['source']) && $data['source'] == 'custom_fields') { $value = $module->table_name . '_cstm.' . $condition->value; } else { $value = $module->table_name . '.' . $condition->value; } break; case 'Date': $params = unserialize(base64_decode($condition->value)); if ($params[0] == 'now') { if ($sugar_config['dbconfig']['db_type'] == 'mssql') { $value = 'GetDate()'; } else { $value = 'NOW()'; } } else { $data = $module->field_defs[$params[0]]; if (isset($data['source']) && $data['source'] == 'custom_fields') { $value = $module->table_name . '_cstm.' . $params[0]; } else { $value = $module->table_name . '.' . $params[0]; } } if ($params[1] != 'now') { switch ($params[3]) { case 'business_hours': //business hours not implemented for query, default to hours $params[3] = 'hours'; default: if ($sugar_config['dbconfig']['db_type'] == 'mssql') { $value = "DATEADD(" . $params[3] . ", " . $app_list_strings['aow_date_operator'][$params[1]] . " {$params['2']}, {$value})"; } else { $value = "DATE_ADD({$value}, INTERVAL " . $app_list_strings['aow_date_operator'][$params[1]] . " {$params['2']} " . $params[3] . ")"; } break; } } break; case 'Multi': $sep = ' AND '; if ($condition->operator == 'Equal_To') { $sep = ' OR '; } $multi_values = unencodeMultienum($condition->value); if (!empty($multi_values)) { $value = '('; foreach ($multi_values as $multi_value) { if ($value != '(') { $value .= $sep; } $value .= $field . ' ' . $app_list_strings['aow_sql_operator_list'][$condition->operator] . " '" . $multi_value . "'"; } $value .= ')'; } $where .= $value; $where_set = true; break; case 'Value': default: $value = "'" . $condition->value . "'"; break; } if (!$where_set) { $where .= $field . ' ' . $app_list_strings['aow_sql_operator_list'][$condition->operator] . ' ' . $value; } } } if (!$this->multiple_runs) { if ($where != '') { $where .= ' AND '; } $where .= "NOT EXISTS (SELECT * FROM aow_processed WHERE aow_processed.aow_workflow_id='" . $this->id . "' AND aow_processed.parent_id=" . $module->table_name . ".id AND aow_processed.status = 'Complete' AND aow_processed.deleted = 0)"; } $query = $module->create_new_list_query('', $where, array(), array(), 0, '', false, $this); return $module->process_full_list_query($query); } return null; }
/** * Advanced OpenWorkflow, Automating SugarCRM. * @package Advanced OpenWorkflow for SugarCRM * @copyright SalesAgility Ltd http://www.salesagility.com * * This program is free software; you can redistribute it and/or modify * it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE as published by * the Free Software Foundation; either version 3 of the License, or * (at your option) any later version. * * This program is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU AFFERO GENERAL PUBLIC LICENSE * along with this program; if not, see http://www.gnu.org/licenses * or write to the Free Software Foundation,Inc., 51 Franklin Street, * Fifth Floor, Boston, MA 02110-1301 USA * * @author SalesAgility <*****@*****.**> */ function display_condition_lines($focus, $field, $value, $view) { global $locale, $app_list_strings, $mod_strings; $html = ''; if (!is_file('cache/jsLanguage/AOW_Conditions/' . $GLOBALS['current_language'] . '.js')) { require_once 'include/language/jsLanguage.php'; jsLanguage::createModuleStringsCache('AOW_Conditions', $GLOBALS['current_language']); } $html .= '<script src="cache/jsLanguage/AOW_Conditions/' . $GLOBALS['current_language'] . '.js"></script>'; if ($view == 'EditView') { $html .= '<script src="modules/AOW_Conditions/conditionLines.js"></script>'; $html .= "<table border='0' cellspacing='4' width='100%' id='conditionLines'></table>"; $html .= "<div style='padding-top: 10px; padding-bottom:10px;'>"; $html .= "<input type=\"button\" tabindex=\"116\" class=\"button\" value=\"" . $mod_strings['LBL_ADD_CONDITION'] . "\" id=\"btn_ConditionLine\" onclick=\"insertConditionLine()\" disabled/>"; $html .= "</div>"; if (isset($focus->flow_module) && $focus->flow_module != '') { require_once "modules/AOW_WorkFlow/aow_utils.php"; $html .= "<script>"; $html .= "flow_rel_modules = \"" . trim(preg_replace('/\\s+/', ' ', getModuleRelationships($focus->flow_module))) . "\";"; $html .= "flow_module = \"" . $focus->flow_module . "\";"; $html .= "document.getElementById('btn_ConditionLine').disabled = '';"; if ($focus->id != '') { $sql = "SELECT id FROM aow_conditions WHERE aow_workflow_id = '" . $focus->id . "' AND deleted = 0 ORDER BY condition_order ASC"; $result = $focus->db->query($sql); while ($row = $focus->db->fetchByAssoc($result)) { $condition_name = new AOW_Condition(); $condition_name->retrieve($row['id']); $condition_name->module_path = unserialize(base64_decode($condition_name->module_path)); if ($condition_name->module_path == '') { $condition_name->module_path = $focus->flow_module; } $html .= "flow_fields = \"" . trim(preg_replace('/\\s+/', ' ', getModuleFields(getRelatedModule($focus->flow_module, $condition_name->module_path[0])))) . "\";"; if ($condition_name->value_type == 'Date') { $condition_name->value = unserialize(base64_decode($condition_name->value)); } $condition_item = json_encode($condition_name->toArray()); $html .= "loadConditionLine(" . $condition_item . ");"; } } $html .= "flow_fields = \"" . trim(preg_replace('/\\s+/', ' ', getModuleFields($focus->flow_module))) . "\";"; $html .= "</script>"; } } else { if ($view == 'DetailView') { $html .= '<script src="modules/AOW_Conditions/conditionLines.js"></script>'; $html .= "<table border='0' cellspacing='0' width='100%' id='conditionLines'></table>"; if (isset($focus->flow_module) && $focus->flow_module != '') { require_once "modules/AOW_WorkFlow/aow_utils.php"; $html .= "<script>"; $html .= "flow_rel_modules = \"" . trim(preg_replace('/\\s+/', ' ', getModuleRelationships($focus->flow_module))) . "\";"; $html .= "flow_module = \"" . $focus->flow_module . "\";"; $sql = "SELECT id FROM aow_conditions WHERE aow_workflow_id = '" . $focus->id . "' AND deleted = 0 ORDER BY condition_order ASC"; $result = $focus->db->query($sql); while ($row = $focus->db->fetchByAssoc($result)) { $condition_name = new AOW_Condition(); $condition_name->retrieve($row['id']); $condition_name->module_path = unserialize(base64_decode($condition_name->module_path)); if (empty($condition_name->module_path)) { $condition_name->module_path[0] = $focus->flow_module; } $html .= "flow_fields = \"" . trim(preg_replace('/\\s+/', ' ', getModuleFields(getRelatedModule($focus->flow_module, $condition_name->module_path[0])))) . "\";"; if ($condition_name->value_type == 'Date') { $condition_name->value = unserialize(base64_decode($condition_name->value)); } $condition_item = json_encode($condition_name->toArray()); $html .= "loadConditionLine(" . $condition_item . ");"; } $html .= "</script>"; } } } return $html; }
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 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; }