Exemplo n.º 1
0
 /**
  * Основной метод сохранения сообщения в базу
  * 
  * @return DiscussionMsg
  */
 public function saveMsg($threadId, $text, $theme, $templateId, $templateData, PsUser $author, PsUser $userTo = null, DiscussionMsg $parent = null)
 {
     //Строим запрос
     $insert = array();
     $params = array();
     //Код родителя
     if ($parent) {
         $insert['id_parent'] = '?';
         $params[] = $parent->getId();
     }
     //Пользователь, написавший сообщение
     $insert['id_user'] = '******';
     $params[] = $author->getId();
     //Пользователь, которому предназначено это сообщение
     $insert['id_user_to'] = '?';
     $params[] = $userTo ? $userTo->getId() : null;
     //Глубина сообщения
     $insert['n_deep'] = '?';
     $params[] = $parent ? $parent->getDeep() + 1 : 1;
     //Известен ли комментарий пользователю
     $insert['b_known'] = '?';
     $params[] = $userTo && !$userTo->isIt($author) ? 0 : 1;
     //Подтверждён ли комментарий админом (только если сообщение пишет админ или сообщение написано от имени дефолтного администратора)
     $insert['b_confirmed'] = '?';
     $params[] = $author->isAuthorisedAsAdmin() || PsUser::defaultAdmin()->isIt($author) ? 1 : 0;
     if (is_integer($templateId)) {
         $insert['id_template'] = '?';
         $params[] = $templateId;
         $insert['v_template'] = '?';
         $params[] = $templateData;
     } else {
         //Тема. Если есть предок - тема не нужна
         if (!$parent && $this->themeColumn) {
             $insert[$this->themeColumn] = '?';
             $params[] = $theme;
         }
         //Содержимое
         $insert['content'] = '?';
         $params[] = $text;
     }
     //entityId
     if ($this->threadIdColumn) {
         $insert[$this->threadIdColumn] = '?';
         $params[] = $threadId;
     }
     //date
     $insert['dt_event'] = 'UNIX_TIMESTAMP()';
     //Ссылка на root
     $msgId = null;
     if ($parent) {
         $insert['id_root'] = '?';
         $params[] = $parent->getRootId();
     } else {
         $msgId = $this->getNextRootId();
         $insert['id_root'] = '?';
         $params[] = $msgId;
         $insert[$this->idColumn] = '?';
         $params[] = $msgId;
     }
     $query = 'insert into ' . $this->table . ' (' . implode(', ', array_keys($insert)) . ') values (' . implode(', ', $insert) . ')';
     if ($msgId) {
         $this->update($query, $params);
     } else {
         $msgId = $this->insert($query, $params);
     }
     //Если мы писали ответ на сообщение, написанное нам - отметим, что мы знаем об этом сообщении
     if ($parent && !$parent->isKnown() && $parent->isToUser($author)) {
         $this->update("update {$this->table} set b_known=1 where {$this->idColumn}=? and id_user_to=?", array($parent->getId(), $author->getId()));
     }
     //Если админ отвечает на какое-то сообщение, то оно считается подтверждённым
     if ($parent && !$parent->isConfirmed() && $author->isAuthorisedAsAdmin()) {
         $this->update("update {$this->table} set b_confirmed=1 where {$this->idColumn}=?", $parent->getId());
     }
     return $this->getMsgById($msgId);
 }