/** * This sets up the search parameters, gets the query results, * and renders them * * @param array $config * @return string */ private function getResults($config) { include_once DIR . '/includes/functions_misc.php'; $search_core = vB_Search_Core::get_instance(); //first see if we can get cached results $hashkey = $this->getHash(); $cache_data = vB_Cache::instance()->read($hashkey, false, false); if ( $cache_data) { //If there are no id's, we're done. if (empty($cache_data['ids'])) { return false; } //We have a content type and an array of id's. We need to build the result //objects. $controller = vB_Search_Core::get_instance()->get_search_type_from_id($cache_data['contenttypeid']); if (method_exists($controller, 'create_array')) { $results = $controller->create_array($cache_data['ids']); } else if(method_exists($controller, 'create_item')) { $results = array(); foreach ($cache_data['ids'] as $resultid) { $result = $controller->create_item($resultid); if ($result) { $results[] = $result; } } } else { return false; } return array('results' => $results, 'criteria' => $cache_data['criteria']); } //First set the contenttype, if appropriate. if (! intval($config['days'])) { $config['days'] = 7; } if (! intval($config['count'])) { $config['count'] = 10; } if (isset($config['contenttypeid']) AND $config['contenttypeid']) { $contenttypeid = $config['contenttypeid']; $criteria = $search_core->create_criteria(vB_Search_Core::SEARCH_ADVANCED); //It's important to figure whether we need to group or not. We group for blogentries and posts $criteria->set_grouped( vB_Search_Core::GROUP_NO); $search_type = vB_Search_Core::get_instance()->get_search_type_from_id($contenttypeid); $criteria->set_advanced_typeid($contenttypeid); $criteria->add_contenttype_filter($contenttypeid); //Ugly hack, but we need to do this if the content type is blogentry. if (vB_Types::instance()->getContentTypeID('vBBlog_BlogEntry') == $contenttypeid ) { vB::$vbulletin->GPC['ignorecomments'] = true; } } else if ((isset($config['forumchoice']) AND count($config['forumchoice']) AND $config['forumchoice'][0]) OR (isset($config['prefixchoice']) AND count($config['prefixchoice']) AND $config['prefixchoice'][0])) { $contenttypeid = vB_Types::instance()->getContentTypeID('vBForum_Post'); $search_type = vB_Search_Core::get_instance()->get_search_type_from_id($contenttypeid); $criteria = $search_core->create_criteria($contenttypeid); $criteria->set_advanced_typeid($contenttypeid); $criteria->add_contenttype_filter($contenttypeid); $criteria->set_grouped(vB_Search_Core::GROUP_NO); } else if ((isset($config['group']) AND $config['group'] != '') OR (isset($config['cat']) AND count($config['cat']) AND $config['cat'][0])) { //We haven't gotten a specific content type, and we won't work without one, // so assume we're showing visitor messages. $contenttypeid = vB_Types::instance()->getContentTypeID('vBForum_SocialGroupMessage'); $search_type = vB_Search_Core::get_instance()->get_search_type_from_id($contenttypeid); $criteria = $search_core->create_criteria($contenttypeid); $criteria->set_advanced_typeid($contenttypeid); $criteria->add_contenttype_filter($contenttypeid); } else { return $vbphrase['widget_needs_configuration']; } //tag applies to several types. Let's do that next. if (isset($config['tag']) AND ($config['tag'] != '') ) { $criteria->add_tag_filter($config['tag']); } //now set the content-type specific items. if ((isset($config['forumchoice']) AND count($config['forumchoice']) AND $config['forumchoice'][0]) AND $contenttypeid == vB_Types::instance()->getContentTypeID('vBForum_Post')) { $criteria->add_forumid_filter($config['forumchoice'], $config['childforums']); } else { $criteria->add_excludeforumid_filter(vB::$vbulletin->options['vbcmsforumid']); } if ((isset($config['prefixchoice']) AND count($config['prefixchoice']) AND $config['prefixchoice'][0]) AND $contenttypeid == vB_Types::instance()->getContentTypeID('vBForum_Post')) { $criteria->add_filter('prefix', vB_Search_Core::OP_EQ, $config['prefixchoice'], true); } if ((isset($config['cat']) AND count($config['cat']) AND $config['cat'][0]) AND $contenttypeid == vB_Types::instance()->getContentTypeID('vBForum_SocialGroup')) { $criteria->add_filter('sgcategory', vB_Search_Core::OP_EQ, $config['cat'], true); } if (isset($config['group']) AND ($config['group'] != '') AND $contenttypeid == vB_Types::instance()->getContentTypeID('vBForum_SocialGroupMessage')) { $criteria->add_filter('socialgroup', vB_Search_Core::OP_EQ, $config['group'], true); } else if ((isset($config['cat']) AND count($config['cat']) AND $config['cat'][0]) AND $contenttypeid == vB_Types::instance()->getContentTypeID('vBForum_SocialGroupMessage')) { $criteria->add_filter('sgcategoryid', vB_Search_Core::OP_EQ, $config['cat'], true); } $search_type->add_advanced_search_filters($criteria, vB::$vbulletin); $current_user = new vB_Legacy_CurrentUser(); $timelimit = TIMENOW - (86400 * $config['days']); $criteria->add_date_filter(vB_Search_Core::OP_GT, $timelimit); if ($config['username'] AND $config['username'] != '') { $criteria->add_user_filter($config['username'], true, true); } else if ($config['friends']) { $friends = $this->getFriends($current_user->getField('userid')); if (count($friends)) { $criteria->add_userid_filter($friends, false); } else { return ''; } } if ($config['keywords'] AND $config['keywords'] != '') { $criteria->add_keyword_filter($config['keywords'], false); } $criteria->set_sort('dateline', 'desc'); $results = vB_Search_Results::create_from_cache($current_user, $criteria); if (!$results) { $results = vB_Search_Results::create_from_criteria($current_user, $criteria); } $page_results = $results->get_page(1, $config['count'], 1); //prepare types for render $items_by_type = array(); foreach ($page_results as $item) { $typeid = $item->get_contenttype(); if ($typeid) { $items_by_type[$typeid][] = $item; $ids[] = $item->get_id(); } } foreach ($items_by_type as $contenttype => $items) { $type = vB_Search_Core::get_instance()->get_search_type_from_id($contenttype); $type->prepare_render($results->get_user(), $items); } vB_Cache::instance()->write($hashkey,array('contenttypeid' => $contenttypeid, 'ids' => $ids, 'criteria' =>$criteria), $this->cache_ttl, $this->getCacheEvent()); return array('results' => $page_results, 'criteria' => $criteria); }
public static function create_array($ids) { global $vbulletin; $current_user = new vB_Legacy_CurrentUser(); $select = array(); $joins = array(); $where = array(); $threadids = implode(',', array_map('intval', $ids)); $select[] = "thread.*"; $where[] = "thread.threadid IN ({$threadids})"; // always add the thread preview field to the results, unless it is disabled. if ($vbulletin->options['threadpreview'] > 0) { $select[] = "post.pagetext AS preview"; $joins['threadpreview'] = "LEFT JOIN " . TABLE_PREFIX . "post AS post ON(post.postid = thread.firstpostid)"; } else { //being a chicken here and making sure we don't remove the preview item //from the array in case it matters $select[] = "'' as preview"; } if (!$current_user->isGuest()) { //join in thread subscriptions if enabled if ($vbulletin->options['threadsubscribed']) { $select[] = "subscribethread.subscribethreadid"; $joins['subscribethread'] = "LEFT JOIN " . TABLE_PREFIX . "subscribethread AS subscribethread ON\n\t\t\t\t\tsubscribethread.threadid = thread.threadid AND\n\t\t\t\t\tsubscribethread.userid = " . intval($current_user->getField('userid')); } if ($vbulletin->options['threadmarking']) { $select[] = "threadread.readtime"; $joins['threadread'] = "LEFT JOIN " . TABLE_PREFIX . "threadread AS threadread ON\n\t\t\t\t\tthreadread.threadid = thread.threadid AND\n\t\t\t\t\tthreadread.userid = " . intval($current_user->getField('userid')); } } if ($vbulletin->options['avatarenabled']) { $select[] = 'avatar.avatarpath, NOT ISNULL(customavatar.userid) AS hascustomavatar, user.avatarrevision, customavatar.dateline AS avatardateline, customavatar.width AS width, customavatar.height AS height, customavatar.height_thumb AS height_thumb, customavatar.width_thumb AS width_thumb, customavatar.filedata_thumb, first_user.avatarrevision AS first_avatarrevision, first_avatar.avatarpath AS first_avatarpath, NOT ISNULL(first_customavatar.userid) AS first_hascustomavatar, first_customavatar.dateline AS first_avatardateline, first_customavatar.width AS first_width, first_customavatar.height AS first_height, first_customavatar.height_thumb AS first_height_thumb, first_customavatar.width_thumb AS first_width_thumb, first_customavatar.filedata_thumb AS first_filedata_thumb'; $joins[] = 'LEFT JOIN ' . TABLE_PREFIX . 'user AS user ON (user.userid = thread.lastposterid)'; $joins[] = 'LEFT JOIN ' . TABLE_PREFIX . 'avatar AS avatar ON (avatar.avatarid = user.avatarid)'; $joins[] = 'LEFT JOIN ' . TABLE_PREFIX . 'customavatar AS customavatar ON (customavatar.userid = user.userid)'; $joins[] = 'LEFT JOIN ' . TABLE_PREFIX . 'user AS first_user ON (first_user.userid = thread.postuserid)'; $joins[] = 'LEFT JOIN ' . TABLE_PREFIX . 'avatar AS first_avatar ON (first_avatar.avatarid = first_user.avatarid)'; $joins[] = 'LEFT JOIN ' . TABLE_PREFIX . 'customavatar AS first_customavatar ON (first_customavatar.userid = first_user.userid)'; } if ($vbulletin->options['showdots']) { $select[] = "lastpost.lastposttime"; $joins['lastpost'] = "LEFT JOIN (\n\t\t\t\tSELECT threadid, MAX(dateline) AS lastposttime\n\t\t\t\tFROM " . TABLE_PREFIX . "post\n\t\t\t\tWHERE threadid IN ({$threadids})\n\t\t\t\t\tAND userid = " . intval($current_user->getField('userid')) . "\n\t\t\t\tGROUP BY threadid\n\t\t\t) AS lastpost ON (lastpost.threadid = thread.threadid)"; } $set = $vbulletin->db->query_read_slave("\n\t\t\tSELECT " . implode(",", $select) . "\n\t\t\tFROM " . TABLE_PREFIX . "thread AS thread\n\t\t\t\t" . implode("\n", $joins) . "\n\t\t\tWHERE " . implode(' AND ', $where) . "\n\t\t"); $threads = array(); while ($threadinfo = $vbulletin->db->fetch_array($set)) { $threads[$threadinfo['threadid']] = self::create_from_record($threadinfo); } return $threads; }
public static function create_array($ids) { global $vbulletin; $current_user = new vB_Legacy_CurrentUser(); $select = array(); $joins = array(); $where = array(); $select[] = "thread.*"; $where[] = "thread.threadid IN (" . implode(',', array_map('intval', $ids)) . ")"; // always add the thread preview field to the results, unless it is disabled. if ($vbulletin->options['threadpreview'] > 0) { $select[] = "post.pagetext AS preview"; $joins['threadpreview'] = "LEFT JOIN " . TABLE_PREFIX . "post AS post ON(post.postid = thread.firstpostid)"; } else { //being a chicken here and making sure we don't remove the preview item //from the array in case it matters $select[] = "'' as preview"; } if (!$current_user->isGuest()) { //join in thread subscriptions if enabled if ($vbulletin->options['threadsubscribed']) { $select[] = "subscribethread.subscribethreadid"; $joins['subscribethread'] = "LEFT JOIN " . TABLE_PREFIX . "subscribethread AS subscribethread ON\r\n\t\t\t\t\tsubscribethread.threadid = thread.threadid AND\r\n\t\t\t\t\tsubscribethread.userid = " . intval($current_user->getField('userid')); } if ($vbulletin->options['threadmarking']) { $select[] = "threadread.readtime"; $joins['threadread'] = "LEFT JOIN " . TABLE_PREFIX . "threadread AS threadread ON\r\n\t\t\t\t\tthreadread.threadid = thread.threadid AND\r\n\t\t\t\t\tthreadread.userid = " . intval($current_user->getField('userid')); } } global $vbulletin; $set = $vbulletin->db->query($q = "\r\n\t\t\tSELECT " . implode(",", $select) . "\r\n\t\t\tFROM " . TABLE_PREFIX . "thread AS thread\r\n\t\t\t\t" . implode("\n", $joins) . "\r\n\t\t\tWHERE " . implode(' AND ', $where) . "\r\n\t\t"); $threads = array(); while ($threadinfo = $vbulletin->db->fetch_array($set)) { $threads[$threadinfo['threadid']] = self::create_from_record($threadinfo); } return $threads; }