Esempio n. 1
0
 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);
 }
Esempio n. 2
0
 /**
  * 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;
 }
Esempio n. 3
0
 protected static function addTrackerFilter(SugarQuery $q, SugarQuery_Builder_Where $where, $interval)
 {
     global $db;
     $td = new SugarDateTime();
     $td->modify($interval);
     $min_date = $td->asDb();
     // Have to do a subselect because MAX() and GROUP BY don't get along with
     // databases other than MySQL
     $q->joinRaw(" INNER JOIN ( SELECT t.item_id item_id, MAX(t.date_modified) track_max " . " FROM tracker t " . " WHERE t.module_name = '" . $db->quote($q->from->module_name) . "' " . " AND t.user_id = '" . $db->quote($GLOBALS['current_user']->id) . "' " . " AND t.date_modified >= " . $db->convert("'" . $min_date . "'", 'datetime') . " " . " GROUP BY t.item_id " . " ) tracker ON tracker.item_id = " . $q->from->getTableName() . ".id ", array('alias' => 'tracker'));
     // Now, if they want tracker records, so let's order it by the tracker date_modified
     $q->order_by = array();
     $q->orderByRaw('tracker.track_max', 'DESC');
     $q->distinct(false);
 }