public function filterModuleList(ServiceBase $api, array $args, $acl = 'list') { if (!empty($args['module_list'])) { $module_list = explode(',', $args['module_list']); foreach ($this->moduleList as $link_name => $module) { if (!in_array($module, $module_list)) { unset($this->moduleList[$link_name]); } } } // if the module list is empty then someone passed in bad modules for the history if (empty($this->moduleList)) { throw new SugarApiExceptionInvalidParameter("Module List is empty, must contain: Meetings, Calls, Notes, Tasks, or Emails"); } $query = new SugarQuery(); $api->action = 'list'; $orderBy = array(); // modules is a char field used for sorting on module name // it is added to the select below, it can be sorted on but needs to be removed from // the arguments to allow it to be maintained throughout the code $removedModuleDirection = false; if (!empty($args['order_by'])) { $orderBy = explode(',', $args['order_by']); foreach ($orderBy as $key => $list) { list($field, $direction) = explode(':', $list); // `picture` is considered the same field as `module` because it // corresponds to the module icon. if ($field === 'module' || $field === 'picture') { unset($orderBy[$key]); $removedModuleDirection = !empty($direction) ? $direction : 'DESC'; } } $args['order_by'] = implode(',', $orderBy); $orderBy[] = "module:{$removedModuleDirection}"; } if (!empty($args['fields'])) { $args['fields'] .= "," . implode(',', $this->validFields); } else { $args['fields'] = implode(',', $this->validFields); } if (!empty($args['order_by']) || !empty($args['fields'])) { $args = $this->scrubFields($args); } unset($args['order_by']); foreach ($this->moduleList as $link_name => $module) { $args['filter'] = array(); $savedFields = $args['fields']; $args['link_name'] = $link_name; $fields = explode(',', $args['fields']); foreach ($fields as $k => $field) { if (isset($args['placeholder_fields'][$module][$field])) { unset($fields[$k]); } } $args['fields'] = implode(',', $fields); if (!empty($this->moduleFilters[$module])) { $args['filter'] = $this->moduleFilters[$module]; } list($args, $q, $options, $linkSeed) = $this->filterRelatedSetup($api, $args); $q->select()->selectReset(); $q->orderByReset(); // ORACLE doesn't allow order by in UNION queries if (!empty($args['placeholder_fields'])) { $newFields = array_merge($args['placeholder_fields'][$module], $fields); } else { $newFields = $fields; } sort($newFields); foreach ($newFields as $field) { if ($field == 'module') { continue; } // special case for description on emails if ($module == 'Emails' && $field == 'description') { // ORACLE requires EMPTY_CLOB() for union queries if CLOB fields were used before $q->select()->fieldRaw(DBManagerFactory::getInstance()->emptyValue('text') . " email_description"); } else { if (isset($args['placeholder_fields'][$module][$field])) { $q->select()->fieldRaw("'' {$args['placeholder_fields'][$module][$field]}"); } else { $q->select()->field($field); } } } $q->select()->field('id'); $q->select()->field('assigned_user_id'); $q->limit = $q->offset = null; $q->select()->fieldRaw("'{$module}'", 'module'); $query->union($q); $query->limit($options['limit'] + 1); $query->offset($options['offset']); $args['fields'] = $savedFields; } if (!empty($orderBy)) { if ($removedModuleDirection !== false) { $orderBy[] = "module:{$removedModuleDirection}"; } foreach ($orderBy as $order) { $ordering = explode(':', $order); if (count($ordering) > 1) { $query->orderByRaw("{$ordering[0]}", "{$ordering[1]}"); } else { $query->orderByRaw("{$ordering[0]}"); } } } else { $query->orderByRaw('date_modified'); } return $this->runQuery($api, $args, $query, $options); }
/** * Gets recently viewed records. * * @param ServiceBase $api Current api. * @param array $args Arguments from request. * @param string $acl (optional) ACL action to check, default is `list`. * @return array List of recently viewed records. */ public function getRecentlyViewed($api, $args, $acl = 'list') { $this->requireArgs($args, array('module_list')); $options = $this->parseArguments($args); $moduleList = $this->filterModules($options['moduleList'], $acl); if (empty($moduleList)) { return array('next_offset' => -1, 'records' => array()); } if (count($moduleList) === 1) { $moduleName = $moduleList[0]; $seed = BeanFactory::newBean($moduleName); $mainQuery = $this->getRecentlyViewedQueryObject($seed, $options); $mainQuery->orderByRaw('MAX(tracker.date_modified)', 'DESC'); } else { $mainQuery = new SugarQuery(); foreach ($moduleList as $moduleName) { $seed = BeanFactory::newBean($moduleName); $mainQuery->union($this->getRecentlyViewedQueryObject($seed, $options), true); } $mainQuery->orderByRaw('last_viewed_date', 'DESC'); } // Add an extra record to the limit so we can detect if there are more records to be found. $mainQuery->limit($options['limit'] + 1); $mainQuery->offset($options['offset']); $data = $beans = array(); $data['next_offset'] = -1; // 'Cause last_viewed_date is an alias (not a real field), we need to // temporarily store its values and append it later to each recently // viewed record $lastViewedDates = array(); $results = $mainQuery->execute(); $db = DBManagerFactory::getInstance(); foreach ($results as $idx => $recent) { if ($idx == $options['limit']) { $data['next_offset'] = (int) ($options['limit'] + $options['offset']); break; } $seed = BeanFactory::getBean($recent['module_name'], $recent['id']); $lastViewedDates[$seed->id] = $db->fromConvert($recent['last_viewed_date'], 'datetime'); $beans[$seed->id] = $seed; } $data['records'] = $this->formatBeans($api, $args, $beans); global $timedate; // Append last_viewed_date to each recently viewed record foreach ($data['records'] as &$record) { $record['_last_viewed_date'] = $timedate->asIso($timedate->fromDb($lastViewedDates[$record['id']])); } return $data; }
/** * Assign the Quota out to the reporting users for the given user if they are a manager * * @param string $user_id The User for which we want to look for reportee's for * @param string $timeperiod_id Which timeperiod * @return bool */ public function assignQuota($user_id, $timeperiod_id) { if (User::isManager($user_id) === false) { return false; } // get everyone but the current user $sq = new SugarQuery(); $sq->select(array('id', 'quota', 'user_id')); $sq->from($this); $sq->where()->equals('assigned_user_id', $user_id)->notEquals('user_id', $user_id)->equals('timeperiod_id', $timeperiod_id)->equals('draft', 1); // now just get the current user $sq2 = new SugarQuery(); $sq2->select(array('id', 'quota', 'user_id')); $sq2->from($this); $sq2->where()->equals('assigned_user_id', $user_id)->Equals('user_id', $user_id)->equals('timeperiod_id', $timeperiod_id)->equals('draft', 1); // create the union query now $sqU = new SugarQuery(); $sqU->union($sq)->addQuery($sq2); // run the query $worksheets = $sqU->execute(); $this->fixTopLevelManagerQuotaRollup($user_id, $timeperiod_id); foreach ($worksheets as $worksheet) { $type = $worksheet['user_id'] == $user_id ? 'Direct' : 'Rollup'; $disableAS = $worksheet['user_id'] == $user_id; // now lets actually assign the quota $this->_assignQuota($worksheet['quota'], $type, $worksheet['user_id'], $timeperiod_id, $disableAS); /* @var $worksheet_obj ForecastManagerWorksheet */ $worksheet_obj = BeanFactory::getBean($this->module_name, $worksheet['id']); $this->rollupDraftToCommittedWorksheet($worksheet_obj, array('quota')); } return true; }