/** * (non-PHPdoc) * * @see Lampcms\Interfaces.Search::getResults() * * @todo if request is ajax we may return result * via Respoder::sendAjax() - just return html block * it includes pagination is necessary * or we may return array or results if it's feasable * * @throws \Lampcms\DevException * @return string html of search results * with pagination */ protected function getResults() { if (!isset($this->countResults)) { throw new \Lampcms\DevException('Count not available. You must run search() before running getCount()'); } /** * If we already know that there are no results * then no need to run the same query * as we did for getCount() */ if (0 === $this->countResults) { d('count is 0, no need to run search query'); return $this; } $offset = 0; $sql = "SELECT\r\n\t\t\t\t\tqid as _id, \r\n\t\t\t\t\ttitle, \r\n\t\t\t\t\turl, \r\n\t\t\t\t\tintro, \r\n\t\t\t\t\tDATE_FORMAT(ts, '%%M %%e, %%Y %%l:%%i %%p') as hts,\r\n\t\t\t\t\tusername,\r\n\t\t\t\t\tavtr,\r\n\t\t\t\t\ttags_html\r\n\t\t\t\t\tFROM question_title\r\n\t\t\t\t\tWHERE %s\r\n\t\t\t\t\t%s\r\n\t\t\t\t\tLIMIT %d\r\n\t\t\t\t\tOFFSET :offset"; /** * Now need to paginate and * get value of offset * and pagination links * IF pagination is necessary */ if ($this->countResults > $this->perPage) { d('cp'); $Paginator = \Lampcms\Paginator::factory($this->Registry); $Paginator->paginate($this->countResults, $this->perPage, array('path' => $this->getPagerPath(), 'currentPage' => $this->pageID)); $offset = ($Paginator->getPager()->getCurrentPageID() - 1) * $this->perPage; d('$offset: ' . $offset); $this->pagerLinks = $Paginator->getLinks(); } $sql = \sprintf($sql, $this->condition, $this->order, $this->perPage); d('sql: ' . $sql); $sth = $this->Registry->Db->makePrepared($sql); $sth->bindParam(':subj', $this->term, \PDO::PARAM_STR); $sth->bindParam(':offset', $offset, \PDO::PARAM_INT); $sth->execute(); $this->aRes = $sth->fetchAll(); return $this; }
/** * Get html div with answers for this one question, * sorted according to param passed in request * * Enclose result in <div> and add pagination to bottom * of div if there is any pagination necessary! * * @todo add skip() and limit() to cursor * in order to use pagination. * * @param Question $Question * * @param string $result desired format of result. Possible * options are: html, array or json object * * * * @throws Exception * @return string html block */ public function getAnswers(Question $Question, $result = 'html') { $qid = $Question[Schema::PRIMARY]; $url = $Question['url']; $pageID = $this->Registry->Router->getPageID(); d('url: ' . $url . ' $pageID: ' . $pageID); $this->pagetPath = $Question->getUrl() . '/'; $urlParts = $this->Registry->Ini->getSection('URI_PARTS'); $cond = $this->Registry->Router->getSegment(3, 's', $urlParts['SORT_RECENT']); d('cond: ' . $cond); $noComments = false === (bool) $this->Registry->Ini->MAX_COMMENTS; d('no comments: ' . $noComments); $aFields = $noComments || false === (bool) $this->Registry->Ini->SHOW_COMMENTS ? array('comments' => 0) : array(); /** * Extra security validation, * IMPORTANT because we should never use * anything in Mongo methods directly from * user input */ if (!in_array($cond, array($urlParts['SORT_RECENT'], $urlParts['SORT_BEST'], $urlParts['SORT_OLDEST']))) { throw new Exception('Invalid value of param "cond" was: ' . $cond); } $where = array(Schema::QUESTION_ID => $qid); /** * Important as of version 0.2 no longer using i_del_ts * as indicator of deleted status - instead using i_status */ if (!$this->Registry->Viewer->isModerator()) { d('not moderator. Get only questions with status 1'); $where[Schema::RESOURCE_STATUS_ID] = Schema::POSTED; } else { $where[Schema::RESOURCE_STATUS_ID] < Schema::DELETED; } switch ($cond) { case $urlParts['SORT_RECENT']: /** * Accepted answer will always be the first one, * then most recently modified */ $sort = array(Schema::IS_ACCEPTED => -1, Schema::LAST_MODIFIED_TIMESTAMP => -1); break; case $urlParts['SORT_OLDEST']: $sort = array(Schema::CREATED_TIMESTAMP => 1); break; case $urlParts['SORT_BEST']: default: /** * Accepted answer will be first * then most highly voted */ $sort = array(Schema::IS_ACCEPTED => -1, Schema::VOTES_SCORE => -1); } $cursor = $this->Registry->Mongo->ANSWERS->find($where, $aFields); d('$cursor: ' . gettype($cursor)); $cursor->sort($sort); $oPager = Paginator::factory($this->Registry); $oPager->paginate($cursor, $this->Registry->Ini->PER_PAGE_ANSWERS, array('path' => $this->pagetPath . $cond, 'append' => false, 'currentPage' => $pageID)); $pagerLinks = $oPager->getLinks(); $ownerId = $Question[Schema::POSTER_ID]; $showLink = $ownerId > 0 && ($this->Registry->Viewer->isModerator() || $ownerId == $this->Registry->Viewer->getUid()); $noComments = $noComments ? ' nocomments' : ''; $func = function (&$a) use($showLink, $noComments) { /** * Don't show Accept link for * already accepted answer */ if (!$a['accepted']) { if ($showLink) { $a['accept_link'] = '<a class="accept ttt" title="@@Click to accept this as best answer@@" href="{_WEB_ROOT_}/{_accept_}/' . $a['_id'] . '">@@Accept@@</a>'; } } else { $a['accepted'] = '<img src="{_IMAGE_SITE_}{_DIR_}/images/accepted.png" alt="@@Best answer@@" class="ttt" title="@@Owner of the question accepted this as best answer@@">'; } $a['nocomments'] = $noComments; $a['edited'] = '@@Edited@@'; }; /** * Create div with answers, append pagination links * to bottom and return the whole div block */ $answers = \tplAnswer::loop($cursor, true, $func) . $pagerLinks; return $answers; }
/** * Paginate the results of cursor * * @return object $this */ protected function paginate() { d('paginating'); $oPaginator = Paginator::factory($this->Registry); $oPaginator->paginate($this->Cursor, $this->perPage, array('path' => '{_WEB_ROOT_}/' . $this->pagerPath, 'currentPage' => $this->pageID)); $this->pagerLinks = $oPaginator->getLinks(); return $this; }
/** * Create a paginator object * and paginate results of select * * @return object $this */ protected function paginate() { d('paginating with $this->pagerPath: ' . $this->pagerPath); $Paginator = Paginator::factory($this->Registry); $Paginator->paginate($this->Cursor, $this->PER_PAGE, array('path' => $this->pagerPath)); $this->pagerLinks = $Paginator->getLinks(); d('$this->pagerLinks: ' . $this->pagerLinks); return $this; }
/** * Create a paginator object * and paginate results of select * * @return object $this */ protected function paginate() { d('paginating with $this->pagerPath: ' . $this->pagerPath); $Paginator = Paginator::factory($this->Registry); $Paginator->paginate($this->Cursor, $this->PER_PAGE, array('currentPage' => $this->pageID, 'path' => '{_WEB_ROOT_}/' . $this->pagerPath)); $this->pagerLinks = $Paginator->getLinks(); return $this; }