Exemple #1
0
 /**
  * @param object $oObject
  * @param object $oHelper
  * @return array
  */
 public static function DbInsertArrays($oObject, $oHelper)
 {
     $aResult = array(false, false);
     $sQueryParams = '';
     $bUseLogQueryParams = (bool) CApi::GetConf('labs.db.log-query-params', false);
     $oObject->initBeforeChange();
     $aStaticMap = $oObject->getMap();
     $aMap = api_AContainer::DbWriteKeys($aStaticMap, true);
     $aDbKeys = array_keys($aMap);
     $aResult[0] = array_map(array(&$oHelper, 'EscapeColumn'), $aDbKeys);
     $aDbValues = array_values($aMap);
     foreach ($aDbValues as $iIndex => $sKey) {
         $mValue = $oObject->{$sKey};
         if (isset($aStaticMap[$sKey][0])) {
             if ('password' === $aStaticMap[$sKey][0]) {
                 $mValue = api_Utils::EncodePassword($mValue);
             } else {
                 if ('datetime' === $aStaticMap[$sKey][0]) {
                     $mValue = $oHelper->TimeStampToDateFormat($mValue);
                 } else {
                     if ('serialize' === $aStaticMap[$sKey][0]) {
                         $mValue = '' === $mValue ? '' : serialize($mValue);
                     }
                 }
             }
         }
         $aDbValues[$iIndex] = is_string($mValue) ? $oHelper->EscapeString($mValue) : (int) $mValue;
         if ($bUseLogQueryParams) {
             $sDbKey = isset($aDbKeys[$iIndex]) ? $aDbKeys[$iIndex] : '!unknown!';
             $sQueryParams .= API_CRLF . API_TAB . $sDbKey . ' = ' . $aDbValues[$iIndex];
         }
     }
     $aResult[1] = $aDbValues;
     if ($bUseLogQueryParams) {
         CApi::Log($sQueryParams);
     }
     return $aResult;
 }
 /**
  * Perform the search
  * @param array $tags
  * @param array $options
  */
 public function run(array $tags, array $options)
 {
     $order = !empty($options['sortKey']) ? $options['sortKey'] : 'search_id';
     $dir = !empty($options['sortOrder']) ? $options['sortOrder'] : 'desc';
     $return = array();
     $query = '';
     $searchIds = array();
     /* Format query */
     if (!empty($options['meta_parent_id'])) {
         $this->sphinxClient->SetFilter('tag_meta_parent_id', is_array($options['meta_parent_id']) ? IPSLib::cleanIntArray($options['meta_parent_id']) : $options['meta_parent_id']);
     }
     if (!empty($options['meta_id'])) {
         $this->sphinxClient->SetFilter('tag_meta_id', is_array($options['meta_id']) ? IPSLib::cleanIntArray($options['meta_id']) : $options['meta_id']);
     }
     if (isset($options['meta_app'])) {
         $query .= ' @tag_meta_app ' . (is_array($options['meta_app']) ? implode("|", $options['meta_app']) : $options['meta_app']) . '';
     }
     if (isset($options['meta_area'])) {
         if (is_array($options['meta_area'])) {
             $_areas = array();
             foreach ($options['meta_area'] as $v) {
                 $_areas[] = str_replace('-', '_', $v);
             }
             $options['meta_area'] = $_areas;
         }
         $query .= ' @tag_meta_area ' . (is_array($options['meta_area']) ? implode("|", $options['meta_area']) : str_replace('-', '_', $options['meta_area'])) . '';
     }
     if (!empty($options['not_meta_id'])) {
         $this->sphinxClient->SetFilter('tag_meta_id', is_array($options['not_meta_id']) ? IPSLib::cleanIntArray($options['not_meta_id']) : array($options['not_meta_id']), true);
     }
     if (isset($tags)) {
         if (is_array($tags)) {
             foreach ($tags as $key => $tag) {
                 $tags[$key] = $this->sphinxClient->EscapeString($tag);
             }
         } else {
             $tags = $this->sphinxClient->EscapeString($tags);
         }
         if (isset($options['match']) and $options['match'] == 'loose') {
             $query .= ' @tag_text (' . (is_array($tags) ? implode(" | ", $tags) : $tags) . ')';
         } else {
             $query .= ' @tag_text "^' . (is_array($tags) ? implode('$" | "^', $tags) : $tags) . '$"';
         }
     }
     /* Did we add in perm check? */
     if (!empty($options['isViewable'])) {
         $query .= ' @tag_perm_text ",' . implode('," | ",', $this->member->perm_id_array) . ',"';
         $this->sphinxClient->SetFilter('tag_perm_visible', array(1));
     }
     /* Sort */
     if ($dir == 'asc') {
         $this->sphinxClient->SetSortMode(SPH_SORT_ATTR_ASC, str_replace('tg.', '', $order));
     } else {
         $this->sphinxClient->SetSortMode(SPH_SORT_ATTR_DESC, str_replace('tg.', '', $order));
     }
     /* Limit Results */
     if (!empty($options['limit']) || !empty($options['offset'])) {
         $this->sphinxClient->SetLimits(intval($options['offset']), intval($options['limit']));
     }
     /* run it */
     $result = $this->sphinxClient->Query($query, $this->settings['sphinx_prefix'] . 'core_tags_search_main,' . $this->settings['sphinx_prefix'] . 'core_tags_search_delta');
     $this->logSphinxWarnings();
     /* Check matches and fetch data */
     if (is_array($result['matches']) && count($result['matches'])) {
         foreach ($result['matches'] as $res) {
             $searchIds[] = $res['attrs']['search_id'];
         }
     }
     if (count($searchIds)) {
         /* Fetch */
         if (count($options['joins'])) {
             $this->DB->build(array('select' => 'tg.*', 'from' => array('core_tags' => 'tg'), 'where' => 'tg.tag_id IN(' . implode(",", $searchIds) . ')', 'add_join' => $options['joins'], 'order' => str_replace('search_id', 'tag_id', $order) . ' ' . $dir));
         } else {
             $this->DB->build(array('select' => '*', 'from' => 'core_tags', 'where' => 'tag_id IN(' . implode(",", $searchIds) . ')', 'add_join' => $options['joins'], 'order' => str_replace('search_id', 'tag_id', $order) . ' ' . $dir));
         }
         $this->DB->execute();
         while ($row = $this->DB->fetch()) {
             $return[$row['tag_id']] = $row;
         }
     }
     return $return;
 }