/** * search - search * * @param string[] $queryArray search terms * @param string $andor and/or how to treat search terms * @param integer $limit max number to return * @param integer $offset offset of first row to return * @param integer $userid a specific user id to limit the query * * @return array of result items * 'title' => the item title * 'content' => brief content or summary * 'link' => link to visit item * 'time' => time modified (unix timestamp) * 'uid' => author uid * 'image' => icon for search display * */ public function search($queryArray, $andor, $limit, $offset, $userid) { $andor = strtolower($andor) == 'and' ? 'and' : 'or'; $qb = \Xoops::getInstance()->db()->createXoopsQueryBuilder(); $eb = $qb->expr(); $qb->select('DISTINCT *')->fromPrefix('page_content')->where($eb->neq('content_status', '0'))->orderBy('content_create', 'DESC')->setFirstResult($offset)->setMaxResults($limit); if (is_array($queryArray) && !empty($queryArray)) { $queryParts = array(); foreach ($queryArray as $i => $q) { $qterm = ':qterm' . $i; $qb->setParameter($qterm, '%' . $q . '%', \PDO::PARAM_STR); $queryParts[] = $eb->orX($eb->like('content_title', $qterm), $eb->like('content_text', $qterm), $eb->like('content_shorttext', $qterm)); } if ($andor == 'and') { $qb->andWhere(call_user_func_array(array($eb, "andX"), $queryParts)); } else { $qb->andWhere(call_user_func_array(array($eb, "orX"), $queryParts)); } } else { $qb->setParameter(':uid', (int) $userid, \PDO::PARAM_INT); $qb->andWhere($eb->eq('content_author', ':uid')); } $myts = MyTextSanitizer::getInstance(); $items = array(); $result = $qb->execute(); while ($myrow = $result->fetch(\PDO::FETCH_ASSOC)) { $content = $myrow["content_shorttext"] . "<br /><br />" . $myrow["content_text"]; $content = $myts->xoopsCodeDecode($content); $items[] = array('title' => $myrow['content_title'], 'content' => Metagen::getSearchSummary($content, $queryArray), 'link' => "viewpage.php?id=" . $myrow["content_id"], 'time' => $myrow['content_create'], 'uid' => $myrow['content_author'], 'image' => 'images/logo_small.png'); } return $items; }
public function searchAdvanced($queryarray, $andor, $limit, $offset, $userid, $categories = array(), $sortby = 0, $searchin = "", $extra = "") { $publisher = Publisher::getInstance(); $ret = array(); if ($queryarray == '' || count($queryarray) == 0) { $hightlight_key = ''; } else { $keywords = implode('+', $queryarray); $hightlight_key = "&keywords=" . $keywords; } $itemsObjs = $publisher->getItemHandler()->getItemsFromSearch($queryarray, $andor, $limit, $offset, $userid, $categories, $sortby, $searchin, $extra); $withCategoryPath = $publisher->getConfig('search_cat_path'); $usersIds = array(); /* @var $obj PublisherItem */ foreach ($itemsObjs as $obj) { $item['image'] = "images/item_icon.gif"; $item['link'] = $obj->getItemUrl(); $item['link'] .= !empty($hightlight_key) && strpos($item['link'], '.php?') === false ? "?" . ltrim($hightlight_key, '&') : $hightlight_key; if ($withCategoryPath) { $item['title'] = $obj->getCategoryPath(false) . " > " . $obj->title(); } else { $item['title'] = $obj->title(); } $item['time'] = $obj->getVar('datesub'); //must go has unix timestamp $item['uid'] = $obj->getVar('uid'); $item['content'] = Metagen::getSearchSummary($obj->body(), $queryarray); $item['author'] = $obj->getVar('author_alias'); $item['datesub'] = $obj->datesub($publisher->getConfig('format_date')); $usersIds[$obj->getVar('uid')] = $obj->getVar('uid'); $ret[] = $item; unset($item, $sanitized_text); } $usersNames = XoopsUserUtility::getUnameFromIds($usersIds, $publisher->getConfig('format_realname'), true); foreach ($ret as $key => $item) { if ($item["author"] == '') { $ret[$key]["author"] = @$usersNames[$item["uid"]]; } } unset($usersNames, $usersIds); return $ret; }