private function getAssignableUsers() { if ($this->distributionMethod == 'singleUser') { return array(); } $distributionOptions = $this->getDistributionOptions(); if (empty($distributionOptions)) { return array(); } switch ($distributionOptions[0]) { case 'security_group': if (file_exists('modules/SecurityGroups/SecurityGroup.php')) { require_once 'modules/SecurityGroups/SecurityGroup.php'; $security_group = new SecurityGroup(); $security_group->retrieve($distributionOptions[1]); $group_users = $security_group->get_linked_beans('users', 'User'); $users = array(); $r_users = array(); if ($distributionOptions[2] != '') { $r_users = $this->getRoleUsers($distributionOptions[2]); } foreach ($group_users as $group_user) { if ($distributionOptions[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': $users = $this->getRoleUsers($distributionOptions[2]); break; case 'all': default: $users = get_user_array(false); break; } return $users; }
function get_email_recipients() { $params = unserialize(base64_decode($this->email_recipients)); $emails = array(); if (isset($params['email_target_type'])) { foreach ($params['email_target_type'] as $key => $field) { switch ($field) { case 'Email Address': $emails[] = $params['email'][$key]; break; case 'Specify User': $user = new User(); $user->retrieve($params['email'][$key]); $emails[] = $user->emailAddress->getPrimaryAddress($user); break; case 'Users': $users = array(); switch ($params['email'][$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['email'][$key][1]); $users = $security_group->get_linked_beans('users', 'User'); $r_users = array(); if ($params['email'][$key][2] != '') { require_once 'modules/ACLRoles/ACLRole.php'; $role = new ACLRole(); $role->retrieve($params['email'][$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 ($users as $user_id => $user) { if ($params['email'][$key][2] != '' && !isset($r_users[$user->id])) { unset($users[$user_id]); } } 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['email'][$key][2]); $users = $role->get_linked_beans('users', 'User'); break; case 'all': default: global $db; $sql = "SELECT id from users WHERE status='Active' AND portal_only=0 "; $result = $db->query($sql); while ($row = $db->fetchByAssoc($result)) { $user = new User(); $user->retrieve($row['id']); $users[$user->id] = $user; } break; } foreach ($users as $user) { $emails[] = $user->emailAddress->getPrimaryAddress($user); } break; } } } return $emails; }
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; }
private function getEmailsFromParams(SugarBean $bean, $params) { $emails = array(); //backward compatible if (isset($params['email_target_type']) && !is_array($params['email_target_type'])) { $email = ''; switch ($params['email_target_type']) { case 'Email Address': $params['email'] = array($params['email']); break; case 'Specify User': $params['email'] = array($params['email_user_id']); break; case 'Related Field': $params['email'] = array($params['email_target']); break; } $params['email_target_type'] = array($params['email_target_type']); $params['email_to_type'] = array('to'); } //end backward compatible if (isset($params['email_target_type'])) { foreach ($params['email_target_type'] as $key => $field) { switch ($field) { case 'Email Address': if (trim($params['email'][$key]) != '') { $emails[$params['email_to_type'][$key]][] = $params['email'][$key]; } break; case 'Specify User': $user = new User(); $user->retrieve($params['email'][$key]); $user_email = $user->emailAddress->getPrimaryAddress($user); if (trim($user_email) != '') { $emails[$params['email_to_type'][$key]][] = $user_email; $emails['template_override'][$user_email] = array('Users' => $user->id); } break; case 'Users': $users = array(); switch ($params['email'][$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['email'][$key][1]); $users = $security_group->get_linked_beans('users', 'User'); $r_users = array(); if ($params['email'][$key][2] != '') { require_once 'modules/ACLRoles/ACLRole.php'; $role = new ACLRole(); $role->retrieve($params['email'][$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 ($users as $user_id => $user) { if ($params['email'][$key][2] != '' && !isset($r_users[$user->id])) { unset($users[$user_id]); } } 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['email'][$key][2]); $users = $role->get_linked_beans('users', 'User'); break; case 'all': default: global $db; $sql = "SELECT id from users WHERE status='Active' AND portal_only=0 "; $result = $db->query($sql); while ($row = $db->fetchByAssoc($result)) { $user = new User(); $user->retrieve($row['id']); $users[$user->id] = $user; } break; } foreach ($users as $user) { $user_email = $user->emailAddress->getPrimaryAddress($user); if (trim($user_email) != '') { $emails[$params['email_to_type'][$key]][] = $user_email; $emails['template_override'][$user_email] = array('Users' => $user->id); } } break; case 'Related Field': $emailTarget = $params['email'][$key]; $relatedFields = array_merge($bean->get_related_fields(), $bean->get_linked_fields()); $field = $relatedFields[$emailTarget]; if ($field['type'] == 'relate') { $linkedBeans = array(); $idName = $field['id_name']; $id = $bean->{$idName}; $linkedBeans[] = BeanFactory::getBean($field['module'], $id); } else { if ($field['type'] == 'link') { $relField = $field['name']; if (isset($field['module']) && $field['module'] != '') { $rel_module = $field['module']; } else { if ($bean->load_relationship($relField)) { $rel_module = $bean->{$relField}->getRelatedModuleName(); } } $linkedBeans = $bean->get_linked_beans($relField, $rel_module); } else { $linkedBeans = $bean->get_linked_beans($field['link'], $field['module']); } } if ($linkedBeans) { foreach ($linkedBeans as $linkedBean) { $rel_email = $linkedBean->emailAddress->getPrimaryAddress($linkedBean); if (trim($rel_email) != '') { $emails[$params['email_to_type'][$key]][] = $rel_email; $emails['template_override'][$rel_email] = array($linkedBean->module_dir => $linkedBean->id); } } } break; case 'Record Email': $recordEmail = $bean->emailAddress->getPrimaryAddress($bean); if ($recordEmail == '' && isset($bean->email1)) { $recordEmail = $bean->email1; } if (trim($recordEmail) != '') { $emails[$params['email_to_type'][$key]][] = $recordEmail; } break; } } } return $emails; }