Example #1
0
 /**
  * Get array of up to 30
  * similar questions, create html block from
  * these questions and save in Question
  * under the sim_q key
  *
  * @param \Lampcms\Question $Question
  *
  * @throws \Exception
  * @internal param bool $ret indicates that this is a retry
  *           and prevents against retrying calling itself
  *           more than once
  *
  * @return object $this
  */
 public function getSimilarQuestions(\Lampcms\Question $Question)
 {
     if (!extension_loaded('pdo_mysql')) {
         d('pdo or pdo_mysql not loaded skipping parsing of similar items');
         return $this;
     }
     $qid = (int) $this->Question['_id'];
     $term = $Question['title'];
     $html = '';
     $aRes = array();
     $sql = "SELECT\r\n\t\t\t\tqid, \r\n\t\t\t\ttitle, \r\n\t\t\t\turl, \r\n\t\t\t\tintro\r\n\t\t\t\tFROM question_title\r\n\t\t\t\tWHERE \r\n\t\t\t\tqid != :qid\r\n \t\t\t\tAND " . self::BY_TITLE . "\r\n\t\t\t\tLIMIT 30";
     d('$sql: ' . $sql);
     try {
         $sth = $this->Registry->Db->makePrepared($sql);
         $sth->bindParam(':qid', $qid, \PDO::PARAM_INT);
         $sth->bindParam(':subj', $term, \PDO::PARAM_STR);
         $sth->execute();
         $aRes = $sth->fetchAll();
         d('found ' . count($aRes) . ' similar questions ' . print_r($aRes, 1));
         if (!empty($aRes)) {
             $html = \tplSimquestions::loop($aRes);
             $s = '<div id="sim_questions" class="similars">' . $html . '</div>';
             d('html: ' . $s);
             $Question->offsetSet('sim_q', $s);
             $Question->save();
         }
     } catch (\Exception $e) {
         $err = 'Exception: ' . get_class($e) . ' Unable to select mysql because: ' . $e->getMessage() . ' Err Code: ' . $e->getCode() . ' trace: ' . $e->getTraceAsString();
         d('mysql error: ' . $err);
         if ('42S02' === $e->getCode()) {
             if (true === TitleTagsTable::create($this->Registry)) {
                 return $this;
             } else {
                 throw $e;
             }
         } else {
             throw $e;
         }
     }
     return $this;
 }
Example #2
0
    /**
     * When question is edited in any way we will
     * run this method to also update
     * the index.
     *
     * @param \Lampcms\Question $Question $Question
     *
     * @throws \Exception
     * @return \Lampcms\Modules\Search\IndexerMySQL
     */
    public function updateQuestion(\Lampcms\Question $Question)
    {
        if (!extension_loaded('pdo_mysql')) {
            d('pdo_mysql not loaded ');
            return $this;
        }
        $res = false;
        $qid = $Question->offsetGet('_id');
        $title = $Question->offsetGet('title');
        $url = $Question->offsetGet('url');
        $intro = $Question->offsetGet('intro');
        $username = $Question['username'];
        $ulink = $Question['ulink'];
        $avatar = $Question['avtr'];
        $tags_html = $Question['tags_html'];
        $body = $Question['body'];
        d($qid . ' title: ' . $title . ' url: ' . $url . ' intro: ' . $intro);
        $sql = 'UPDATE question_title
		SET 
		title = :qtitle,
		q_body = :qbody,
		url = :qurl, 
		intro = :qintro, 
		username = :username,
		userlink = :userlink,
		avtr = :avatar,
		tags_html = :tags_html
		WHERE qid = :qid';
        try {
            $sth = $this->Registry->Db->makePrepared($sql);
            $sth->bindParam(':qid', $qid, \PDO::PARAM_INT);
            $sth->bindParam(':qtitle', $title, \PDO::PARAM_STR);
            $sth->bindParam(':qbody', $body, \PDO::PARAM_STR);
            $sth->bindParam(':qurl', $url, \PDO::PARAM_STR);
            $sth->bindParam(':qintro', $intro, \PDO::PARAM_STR);
            $sth->bindParam(':tags_html', $tags_html, \PDO::PARAM_STR);
            $sth->bindParam(':username', $username, \PDO::PARAM_STR);
            $sth->bindParam(':userlink', $ulink, \PDO::PARAM_STR);
            $sth->bindParam(':avatar', $avatar, \PDO::PARAM_STR);
            $res = $sth->execute();
        } catch (\Exception $e) {
            $err = 'Exception: ' . get_class($e) . ' Unable to insert into mysql because: ' . $e->getMessage() . ' Err Code: ' . $e->getCode() . ' trace: ' . $e->getTraceAsString();
            d('mysql error: ' . $err);
            if ('42S02' === $e->getCode()) {
                if (true === TitleTagsTable::create($this->Registry)) {
                    $this->indexTitle($Question);
                }
            } else {
                throw $e;
            }
        }
        d('res: ' . $res);
        return $this;
    }