예제 #1
0
 public function parseArguments($api, $args, $seed)
 {
     $parsed = parent::parseArguments($api, $args, $seed);
     $deleted = false;
     if (isset($args['deleted']) && (strtolower($args['deleted']) == 'true' || $args['deleted'] == '1')) {
         $deleted = true;
     }
     $whereParts = array();
     // TODO: Upgrade this to use the full-text search for basic searches
     if (isset($args['q'])) {
         $args['q'] = trim($args['q']);
         $tableName = $seed->table_name;
         $basicSearch = $GLOBALS['db']->quote($args['q']);
         if (is_a($seed, 'Person')) {
             // Search by first_name, last_name
             if (strpos($args['q'], ' ') !== false) {
                 // There is a space in there, search by first name and last name
                 list($leftPart, $rightPart) = explode(' ', $args['q']);
                 $leftPart = $GLOBALS['db']->quote($leftPart);
                 $rightPart = $GLOBALS['db']->quote($rightPart);
                 $whereParts[] = "( {$tableName}.first_name LIKE '{$leftPart}%' AND {$tableName}.last_name LIKE '{$rightPart}%' ) OR ( {$tableName}.last_name LIKE '{$leftPart}%' AND {$tableName}.first_name LIKE '{$right_part}%' )";
             } else {
                 // No space, search by first name or last name
                 $whereParts[] = "{$tableName}.first_name LIKE '{$basicSearch}%' OR {$tableName}.last_name LIKE '{$basicSearch}%' ";
             }
         } else {
             // Search by name
             $whereParts[] = "{$tableName}.name LIKE '{$basicSearch}%' ";
         }
     }
     $params = array();
     if (isset($args['favorites']) && $args['favorites']) {
         $params['favorites'] = true;
     }
     if (count($whereParts) > 0) {
         $where = '(' . implode(") AND (", $whereParts) . ')';
     } else {
         $where = '';
     }
     return array('deleted' => $deleted, 'limit' => $parsed['limit'], 'offset' => $parsed['offset'], 'userFields' => $parsed['fields'], 'orderBy' => $this->convertOrderByToSql($parsed['orderBy']), 'params' => $params, 'whereParts' => $whereParts, 'where' => $where);
 }
예제 #2
0
 /**
  * This function pulls all of the search-related options out of the $args array and returns a fully-populated array with either the defaults or the provided settings
  * @param $api ServiceBase The API class of the request
  * @param $args array The arguments array passed in from the API
  * @return array Many elements containing each setting the search engine uses
  */
 public function parseSearchOptions(ServiceBase $api, array $args)
 {
     $options = array();
     if (isset($args['module_list']) && count($args['module_list']) == 1) {
         // We can create a bean of this type
         $seed = BeanFactory::newBean($args['module_list']);
     } else {
         $seed = null;
     }
     $options = parent::parseArguments($api, $args, $seed);
     // We need to support 'deleted' same as ListApi
     $options['deleted'] = false;
     if (isset($args['deleted']) && (strtolower($args['deleted']) == 'true' || $args['deleted'] == '1')) {
         $options['deleted'] = true;
     }
     $options['query'] = '';
     if (isset($args['q'])) {
         $options['query'] = trim($args['q']);
     }
     $options['limitPerModule'] = $this->defaultModuleLimit;
     if (!empty($args['max_num_module'])) {
         $options['limitPerModule'] = (int) $args['max_num_module'];
     }
     $options['searchFields'] = array();
     if (!empty($args['search_fields'])) {
         $options['searchFields'] = explode(',', $args['search_fields']);
     }
     $options['selectFields'] = array('id');
     if (!empty($args['order_by'])) {
         if (strpos($args['order_by'], ',') !== 0) {
             // There is a comma, we are ordering by more than one thing
             $orderBys = explode(',', $args['order_by']);
         } else {
             $orderBys = array($args['order_by']);
         }
         $orderByArray = array();
         foreach ($orderBys as $order) {
             if (strpos($order, ':')) {
                 // It has a :, it's specifying ASC / DESC
                 list($column, $direction) = explode(':', $order);
                 if (strtolower($direction) == 'desc') {
                     $direction = 'DESC';
                 } else {
                     $direction = 'ASC';
                 }
             } else {
                 // No direction specified, let's let it fly free
                 $column = $order;
                 $direction = 'ASC';
             }
             /*
               // Need to extend this to do field security on all modules that we are searching by.
                             if ( !$api->security->canAccessField($seed,$column,'list') || !isset($seed->field_defs[$column]) ) {
                                 throw new SugarApiExceptionNotAuthorized('No access to view field: '.$column.' in module: '.$args['module']);
                             }
             */
             // If this field has already been added, don't do it again
             // Common cause of this was the id field, since we always add it
             // by default.
             if (in_array($column, $options['selectFields'])) {
                 // Before busting out of this, ensure we have what we need
                 if (empty($orderByData[$column])) {
                     $orderByData[$column] = $direction == 'ASC' ? true : false;
                     if (!in_array("{$column} {$direction}", $orderByArray)) {
                         $orderByArray[] = $column . ' ' . $direction;
                     }
                 }
                 continue;
             }
             $options['selectFields'][] = $column;
             $orderByData[$column] = $direction == 'ASC' ? true : false;
             $orderByArray[] = $column . ' ' . $direction;
         }
         $options['orderBySetByApi'] = true;
         $orderBy = implode(',', $orderByArray);
     } else {
         /*
          * Adding id to the default sort by.  When data has the same date_modified the sort could change with the
          * offset showing the same record on multiple pages
          */
         $orderBy = 'date_modified DESC, id DESC';
         $orderByData['date_modified'] = false;
         $orderByData['id'] = false;
         $options['orderBySetByApi'] = false;
         $options['selectFields'][] = 'date_modified';
     }
     $options['orderByArray'] = $orderByData;
     $options['orderBy'] = $orderBy;
     $options['moduleList'] = array();
     if (!empty($args['module_list'])) {
         $options['moduleList'] = explode(',', $args['module_list']);
         // remove any empty moduleList array entries..if someone were to do Contacts, it would not hit elastic because '' is not an elastic module.
         $options['moduleList'] = array_filter($options['moduleList']);
     }
     $options['primaryModule'] = 'Home';
     if (!empty($args['primary_module'])) {
         $options['primaryModule'] = $args['primary_module'];
     } else {
         if (isset($options['moduleList'][0])) {
             $options['primaryModule'] = $options['moduleList'][0];
         }
     }
     // we want favorites info with records, so that we can flag a favorite out of a recordset
     $options['favorites'] = false;
     if (!empty($args['favorites']) && $args['favorites'] == true) {
         // Setting favorites to 1 includes favorites information,
         // setting it to 2 searches for favorite records.
         $options['favorites'] = 2;
     }
     $options['my_items'] = false;
     if (!empty($args['my_items'])) {
         // TODO: When the real filters get in, change it so that this is just described as an additional filter.
         $options['my_items'] = $args['my_items'];
     }
     $fieldFilters = array();
     // Sort out the multi-module field filter
     if (!empty($args['fields'])) {
         if (is_array($args['fields'])) {
             // This one has multiple modules in it we need to split it up among all of the modules
             $fieldFilters = $args['fields'];
         } else {
             // They want one filter across all modules
             $fieldFilters['_default'] = explode(',', $args['fields']);
         }
     } else {
         $fieldFilters['_default'] = '';
     }
     // Ensure date_modified and id are in the list of fields
     foreach ($fieldFilters as $key => $fieldArray) {
         if (empty($fieldArray)) {
             // Just allow the defaults to take over
             continue;
         }
         foreach (array('id', 'date_modified') as $requiredField) {
             if (!in_array($requiredField, $fieldArray)) {
                 $fieldFilters[$key][] = $requiredField;
             }
         }
     }
     $options['fieldFilters'] = $fieldFilters;
     return $options;
 }