/** * search * * @param array $pSearchHash basically the same parameters as a regular list * @access public * @return TRUE on success, FALSE on failure - mErrors will contain reason for failure */ function search(&$pSearchHash) { // PHP compatability issues include_once UTIL_PKG_PATH . 'PHP_Compat/Compat/Function/stripos.php'; global $gLibertySystem, $gBitSystem, $gBitUser, $gBitDbType; // initiate stuff BitBase::prepGetList($pSearchHash); $ret = $bindVars = array(); $selectSql = $whereSql = $orderSql = $joinSql = ''; // if all content has been selected, there is an empty value in the array if (isset($pSearchHash['content_type_guid']) && !is_array($pSearchHash['content_type_guid'])) { $pSearchHash['content_type_guid'] = array($pSearchHash['content_type_guid']); } if (!empty($pSearchHash['content_type_guid']) && in_array('', $pSearchHash['content_type_guid'])) { $pSearchHash['content_type_guid'] = array(); } // check if the user has the required permissions to view the requested content type foreach ($gLibertySystem->mContentTypes as $contentType) { if ((empty($pSearchHash['content_type_guid']) || in_array($contentType["content_type_guid"], $pSearchHash['content_type_guid'])) && $this->hasViewPermission($contentType["content_type_guid"])) { $allowed[] = $contentType["content_type_guid"]; } } if (in_array('bitcomment', $allowed)) { $pSearchHash['include_comments'] = TRUE; } if (!empty($allowed)) { $whereSql .= empty($whereSql) ? ' WHERE ' : ' AND '; $whereSql .= " lc.`content_type_guid` IN( " . implode(',', array_fill(0, count($allowed), '?')) . " ) "; $bindVars = array_merge($bindVars, $allowed); } else { $this->mErrors['permission'] = tra("You don't have the required permissions to search the requested content types."); } // create valid search SQL if ($errors = $this->prepareSearchSql($pSearchHash, $whereSql, $bindVars)) { $this->mErrors = $errors; } // get service SQL LibertyContent::getServicesSql('content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars, NULL, $pSearchHash); if (!empty($pSearchHash['sort_mode'])) { $orderSql = " ORDER BY lc." . $this->mDb->convertSortmode($pSearchHash['sort_mode']); } // only continue if we haven't choked so far if (empty($this->mErrors)) { $query = "\n\t\t\t\tSELECT \n\t\t\t\tuue.`login` AS `modifier_user`,\n\t\t\t\tuue.`real_name` AS `modifier_real_name`,\n\t\t\t\tuue.`user_id` AS `modifier_user_id`,\n\t\t\t\tuuc.`login` AS `creator_user`,\n\t\t\t\tuuc.`real_name` AS `creator_real_name`,\n\t\t\t\tuuc.`user_id` AS `creator_user_id`,\n\t\t\t\tlc.`data`, \n\t\t\t\tlc.`content_id`, \n\t\t\t\tlc.`title`, \n\t\t\t\tlcds.`data` AS `summary`, \n\t\t\t\tlct.`content_name`, \n\t\t\t\tlct.`content_name_plural`, \n\t\t\t\tlch.`hits`, \n\t\t\t\tlc.`last_modified`,\n\t\t\t\tlc.`created`,\n\t\t\t\tlc.`content_type_guid`\n\t\t\t\t{$selectSql}\n\t\t\t\tFROM `" . BIT_DB_PREFIX . "liberty_content` lc\n\t\t\t\t\tINNER JOIN `" . BIT_DB_PREFIX . "users_users` uuc ON (lc.`user_id`=uuc.`user_id`)\n\t\t\t\t\tINNER JOIN `" . BIT_DB_PREFIX . "users_users` uue ON (lc.`modifier_user_id`=uue.`user_id`)\n\t\t\t\t\tLEFT OUTER JOIN `" . BIT_DB_PREFIX . "liberty_content_data` lcds ON ( lc.`content_id` = lcds.`content_id` AND lcds.`data_type` = 'summary' )\n\t\t\t\t\tLEFT OUTER JOIN `" . BIT_DB_PREFIX . "liberty_content_types` lct ON ( lc.`content_type_guid` = lct.`content_type_guid` )\n\t\t\t\t\tLEFT OUTER JOIN `" . BIT_DB_PREFIX . "liberty_content_hits` lch ON ( lc.`content_id` = lch.`content_id` )\n\t\t\t\t{$joinSql} {$whereSql} {$orderSql}"; $result = $this->mDb->query($query, $bindVars, $pSearchHash['max_records'], $pSearchHash['offset']); while ($aux = $result->fetchRow()) { $data = $aux['summary'] . "\n" . $aux['data']; $aux['len'] = strlen($data); $lines = explode("\n", strip_tags($data)); foreach ($pSearchHash['findHash'] as $val) { $val = trim($val, "%"); $i = 0; foreach ($lines as $number => $line) { if ($i < 3 && !empty($line) && stripos($line, $val) !== FALSE) { $aux['display_lines'][$number + 1] = encode_email_addresses($line); $i++; } } if (!empty($aux['display_lines'])) { ksort($aux['display_lines']); } } $aux['display_url'] = BIT_ROOT_URL . "index.php?content_id=" . $aux['content_id']; $ret[] = $aux; } // do some custom sorting usort($ret, 'ilike_relevance_sort'); $query = "\n\t\t\t\tSELECT COUNT( lc.`content_id` )\n\t\t\t\tFROM `" . BIT_DB_PREFIX . "liberty_content` lc\n\t\t\t\t\tLEFT OUTER JOIN `" . BIT_DB_PREFIX . "liberty_content_data` lcds ON ( lc.`content_id` = lcds.`content_id` )\n\t\t\t\t\tLEFT OUTER JOIN `" . BIT_DB_PREFIX . "liberty_content_types` lct ON ( lc.`content_type_guid` = lct.`content_type_guid` )\n\t\t\t\t{$joinSql} {$whereSql}"; $pSearchHash['cant'] = $this->mDb->getOne($query, $bindVars); BitBase::postGetList($pSearchHash); return $ret; } else { return FALSE; } }
function find_with_and($allowed, $selectSql, $joinSql, $whereSql, $bindVars, &$pParamHash) { // Make a slot for the search word. $bindVars[0] = NULL; $bindVars = array_merge($bindVars, $allowed); LibertyContent::getServicesSql('content_list_sql_function', $selectSql, $joinSql, $whereSql, $bindVars); $ret = array(); $first = true; foreach ($pParamHash['words'] as $word) { $query = "SELECT lc.`content_id` AS hash_key,\n\t\t\t\t\t\tlc.`content_id`,\n\t\t\t\t\t\tlc.`title`,\n\t\t\t\t\t\tlc.`format_guid`,\n\t\t\t\t\t\tlc.`content_type_guid`,\n\t\t\t\t\t\tCOALESCE(lch.`hits`,0) AS hits,\n\t\t\t\t\t\tlc.`created`,\n\t\t\t\t\t\tlc.`last_modified`,\n\t\t\t\t\t\tlc.`data`,\n\t\t\t\t\t\tsi.`i_count` AS relevancy\n\t\t\t\t\t\t{$selectSql}\n\t\t\t\t\tFROM `" . BIT_DB_PREFIX . "liberty_content` lc\n\t\t\t\t\tLEFT OUTER JOIN `" . BIT_DB_PREFIX . "liberty_content_hits` lch ON (lc.`content_id` = lch.`content_id`)\n\t\t\t\t\t{$joinSql}\n\t\t\t\t\tINNER JOIN `" . BIT_DB_PREFIX . "search_index` si ON (si.`content_id`=lc.`content_id` AND si.`searchword` = ? )\n\t\t\t\t\tWHERE `i_count` > 0 {$whereSql}\n\t\t\t\t\tORDER BY 9 DESC, 5 DESC\n\t\t\t\t\t"; $bindVars[0] = $word; $result = $this->mDb->getAssoc($query, $bindVars); if ($first) { $ret = $result; $first = false; } else { $this->mergeResults($ret, $result); } } /* count it */ $pParamHash['cant'] = count($ret); /* Sort it */ uasort($ret, 'search_relevance_sort'); /* slice it */ $ret = array_slice($ret, $pParamHash['offset'], $pParamHash['offset'] + $pParamHash['max_records']); /* Set the hrefs. */ foreach ($ret as $content_id => $data) { $ret[$content_id]['href'] = BIT_ROOT_URL . "index.php?content_id=" . $data['content_id']; } return $ret; }