Пример #1
0
 public function Search($Search, $Offset = 0, $Limit = 20)
 {
     $BaseUrl = C('Plugins.Solr.SearchUrl', 'http://localhost:8983/solr/select/?');
     if (!$BaseUrl) {
         throw new Gdn_UserException("The search url has not been configured.");
     }
     if (!$Search) {
         return array();
     }
     // Escepe the search.
     $Search = preg_replace('`([][+&|!(){}^"~*?:\\\\-])`', "\\\\\$1", $Search);
     // Add the category watch.
     $Categories = CategoryModel::CategoryWatch();
     if ($Categories === FALSE) {
         return array();
     } elseif ($Categories !== TRUE) {
         $Search = 'CategoryID:(' . implode(' ', $Categories) . ') AND ' . $Search;
     }
     // Build the search url.
     $BaseUrl .= strpos($BaseUrl, '?') === FALSE ? '?' : '&';
     $Query = array('q' => $Search, 'start' => $Offset, 'rows' => $Limit);
     $Url = $BaseUrl . http_build_query($Query);
     // Grab the data.
     $Curl = curl_init($Url);
     curl_setopt($Curl, CURLOPT_RETURNTRANSFER, 1);
     $CurlResult = curl_exec($Curl);
     curl_close($Curl);
     // Parse the result into the form that the search controller expects.
     $Xml = new SimpleXMLElement($CurlResult);
     $Result = array();
     if (!isset($Xml->result)) {
         return array();
     }
     foreach ($Xml->result->children() as $Doc) {
         $Row = array();
         foreach ($Doc->children() as $Field) {
             $Name = (string) $Field['name'];
             $Row[$Name] = (string) $Field;
         }
         // Add the url.
         switch ($Row['DocType']) {
             case 'Discussion':
                 $Row['Url'] = '/discussion/' . $Row['PrimaryID'] . '/' . Gdn_Format::Url($Row['Title']);
                 break;
             case 'Comment':
                 $Row['Url'] = "/discussion/comment/{$Row['PrimaryID']}/#Comment_{$Row['PrimaryID']}";
                 break;
         }
         // Fix the time.
         $Row['DateInserted'] = strtotime($Row['DateInserted']);
         $Result[] = $Row;
     }
     // Join the users into the result.
     Gdn_DataSet::Join($Result, array('table' => 'User', 'parent' => 'UserID', 'prefix' => '', 'Name', 'Photo'));
     return $Result;
 }
Пример #2
0
 public function JoinParticipants(&$Data)
 {
     $this->SQL->From('UserConversation uc')->Join('User u', 'u.UserID = uc.UserID');
     Gdn_DataSet::Join($Data, array('alias' => 'uc', 'parent' => 'ConversationID', 'column' => 'Participants', 'UserID', 'u.Name', 'u.Photo'), array('sql' => $this->SQL));
 }
 /**
  * Get a list of conversaitons for a user's inbox. This is an optimized version of ConversationModel::Get().
  *
  * @param int $UserID
  * @param int $Offset Number to skip.
  * @param int $Limit Maximum to return.
  */
 public function Get2($UserID, $Offset = 0, $Limit = FALSE)
 {
     if (!$Limit) {
         $Limit = C('Conversations.Conversations.PerPage', 30);
     }
     // The self join is intentional in order to force the query to us an index-scan instead of a table-scan.
     $Data = $this->SQL->Select('c.*')->Select('uc2.DateLastViewed')->Select('uc2.CountReadMessages')->Select('uc2.LastMessageID', '', 'UserLastMessageID')->From('UserConversation uc')->Join('UserConversation uc2', 'uc.ConversationID = uc2.ConversationID and uc.UserID = uc2.UserID')->Join('Conversation c', 'c.ConversationID = uc2.ConversationID')->Where('uc.UserID', $UserID)->Where('uc.Deleted', 0)->OrderBy('uc.DateConversationUpdated', 'desc')->Limit($Limit, $Offset)->Get();
     $Data->DatasetType(DATASET_TYPE_ARRAY);
     $Result =& $Data->Result();
     // Add some calculated fields.
     foreach ($Result as &$Row) {
         if ($Row['UserLastMessageID']) {
             $Row['LastMessageID'] = $Row['UserLastMessageID'];
         }
         $Row['CountNewMessages'] = $Row['CountMessages'] - $Row['CountReadMessages'];
         unset($Row['UserLastMessageID']);
     }
     // Join the participants.
     $this->JoinParticipants($Result);
     // Join in the last message.
     Gdn_DataSet::Join($Result, array('table' => 'ConversationMessage', 'prefix' => 'Last', 'parent' => 'LastMessageID', 'child' => 'MessageID', 'InsertUserID', 'DateInserted', 'Body', 'Format'));
     return $Data;
 }