Пример #1
0
 public function renderJson()
 {
     $results = array();
     foreach ($this->_params['tags'] as $tag) {
         $normalized = Tinhte_XenTag_Helper::getNormalizedTagText($tag['tag_text']);
         if (isset($results[$normalized])) {
             continue;
         }
         $results[$normalized] = $this->_getPresentationForResult($tag);
     }
     return array('results' => $results);
 }
Пример #2
0
 public function getGeneralConstraintsFromInput(array $input, &$errors = null)
 {
     if (isset($GLOBALS[Tinhte_XenTag_Constants::GLOBALS_CONTROLLERPUBLIC_SEARCH_SEARCH])) {
         $GLOBALS[Tinhte_XenTag_Constants::GLOBALS_CONTROLLERPUBLIC_SEARCH_SEARCH]->Tinhte_XenTag_actionSearch($this, $input);
     }
     $constraints = parent::getGeneralConstraintsFromInput($input, $errors);
     if (!empty($input[Tinhte_XenTag_Constants::SEARCH_INPUT_TAGS])) {
         $tagTexts = $input[Tinhte_XenTag_Constants::SEARCH_INPUT_TAGS];
         if (!is_array($tagTexts)) {
             $tagTexts = array($tagTexts);
         }
         /* @var $tagModel Tinhte_XenTag_Model_Tag */
         $tagModel = $this->getModelFromCache('Tinhte_XenTag_Model_Tag');
         // runs through basic validation first
         foreach ($tagTexts as &$tagText) {
             $tagText = Tinhte_XenTag_Helper::getNormalizedTagText($tagText);
         }
         $constraints[Tinhte_XenTag_Constants::SEARCH_CONSTRAINT_TAGS] = $tagTexts;
     }
     return $constraints;
 }
Пример #3
0
 public function render()
 {
     if ($this->_useGlobalTags) {
         $globalTagsOrTexts = $this->_getTagModel()->getTagsOrTextsFromCache();
         $tagsOrTexts = array();
         foreach ($this->_tagsOrTexts as $tagOrText) {
             $tagText = Tinhte_XenTag_Helper::getTextFromTagOrText($tagOrText);
             $tagsOrTexts[Tinhte_XenTag_Helper::getNormalizedTagText($tagText)] = $tagOrText;
         }
         foreach ($globalTagsOrTexts as $globalTagOrText) {
             $globalTagText = Tinhte_XenTag_Helper::getTextFromTagOrText($globalTagOrText);
             $tagsOrTexts[Tinhte_XenTag_Helper::getNormalizedTagText($globalTagText)] = $globalTagOrText;
         }
         $this->_tagsOrTexts = $tagsOrTexts;
     }
     if (!empty($this->_tagsOrTexts)) {
         $autoTagOptions = array('onceOnly' => Tinhte_XenTag_Option::get('autoTagOnceOnly'));
         return Tinhte_XenTag_Integration::autoTag($this->_html, $this->_tagsOrTexts, $autoTagOptions);
     } else {
         return $this->_html;
     }
 }
Пример #4
0
 protected function _verifyText(&$text)
 {
     $text = Tinhte_XenTag_Helper::getNormalizedTagText($text);
     $censored = XenForo_Helper_String::censorString($text);
     if ($censored !== $text) {
         $this->error(new XenForo_Phrase('tinhte_xentag_tag_no_contain_censored'), 'tag_text');
         return false;
     }
     if (Tinhte_XenTag_Helper::isTagContainingSeparator($text)) {
         $this->error(new XenForo_Phrase('tinhte_xentag_tag_can_not_contain_comma'), 'tag_text');
         return false;
     }
     $textLength = utf8_strlen($text);
     if ($textLength < Tinhte_XenTag_Option::get('tagMinLength')) {
         $this->error(new XenForo_Phrase('tinhte_xentag_tag_must_be_at_least_x_length', array('minLength' => Tinhte_XenTag_Option::get('tagMinLength'))), 'tag_text');
         return false;
     }
     if ($textLength > Tinhte_XenTag_Option::get('tagMaxLength')) {
         $this->error(new XenForo_Phrase('tinhte_xentag_tag_can_not_longer_than_x', array('maxLength' => Tinhte_XenTag_Option::get('tagMaxLength'))), 'tag_text');
         return false;
     }
     return true;
 }
Пример #5
0
 /**
  * Updates list of tags for specified piece of content. Any addition, removal
  * will be processed accordingly with record from database.
  *
  * @param string $contentType
  * @param unsigned int $contentId
  * @param unsigned int $contentUserId
  * @param array $tagTexts
  * @param XenForo_DataWriter $dw
  * @param array $options
  *
  * @return number of tags after update.
  *
  * @throws XenForo_Exception
  */
 public static function updateTags($contentType, $contentId, $contentUserId, array $tagTexts, XenForo_DataWriter $dw, array $options = array())
 {
     $options = array_merge(array('throwException' => true), $options);
     /* @var $tagModel Tinhte_XenTag_Model_Tag */
     $tagModel = $dw->getModelFromCache('Tinhte_XenTag_Model_Tag');
     /* @var $taggedModel Tinhte_XenTag_Model_TaggedContent */
     $taggedModel = $dw->getModelFromCache('Tinhte_XenTag_Model_TaggedContent');
     if ($dw->isInsert()) {
         // saves 1 query
         $existingTags = array();
     } else {
         $existingTags = $tagModel->getTagsOfContent($contentType, $contentId);
     }
     $changed = false;
     $newTagTexts = array();
     $removedTagTexts = array();
     $updatedTags = $existingTags;
     $tagModel->lookForNewAndRemovedTags($existingTags, $tagTexts, $newTagTexts, $removedTagTexts);
     $canCreateNew = XenForo_Visitor::getInstance()->hasPermission('general', Tinhte_XenTag_Constants::PERM_USER_CREATE_NEW);
     $errorHandler = XenForo_DataWriter::ERROR_SILENT;
     if (!empty($options['throwException'])) {
         $errorHandler = XenForo_DataWriter::ERROR_EXCEPTION;
     }
     if (!empty($newTagTexts)) {
         // sondh@2012-09-21
         // remove duplicate
         foreach (array_keys($newTagTexts) as $key) {
             $newTagTexts[$key] = Tinhte_XenTag_Helper::getNormalizedTagText($newTagTexts[$key]);
         }
         $newTagTexts = array_unique($newTagTexts);
         $newButExistingTags = $tagModel->getTagsByText($newTagTexts);
         foreach ($newTagTexts as $newTagText) {
             $newTag = $tagModel->getTagFromArrayByText($newButExistingTags, $newTagText);
             if (empty($newTag)) {
                 if ($canCreateNew) {
                     /* @var $dwTag Tinhte_XenTag_DataWriter_Tag */
                     $dwTag = XenForo_DataWriter::create('Tinhte_XenTag_DataWriter_Tag', $errorHandler);
                     $dwTag->set('tag_text', $newTagText);
                     $dwTag->set('created_user_id', $contentUserId);
                     if ($dwTag->save()) {
                         $newTag = $dwTag->getMergedData();
                     }
                 } else {
                     if (!empty($options['throwException'])) {
                         throw new XenForo_Exception(new XenForo_Phrase('tinhte_xentag_you_can_not_create_new_tag'), true);
                     }
                     continue;
                 }
             }
             if (empty($newTag)) {
                 // no tag to use, abort
                 continue;
             }
             if (!empty($newTag['is_staff']) and !XenForo_Visitor::getInstance()->hasPermission('general', Tinhte_XenTag_Constants::PERM_USER_IS_STAFF)) {
                 if (!empty($options['throwException'])) {
                     throw new XenForo_Exception(new XenForo_Phrase('tinhte_xentag_you_can_not_use_tag_x', array('tag_text' => $newTag['tag_text'])), true);
                 }
                 continue;
             }
             /* @var $dwTag Tinhte_XenTag_DataWriter_TaggedContent */
             $dwTagged = XenForo_DataWriter::create('Tinhte_XenTag_DataWriter_TaggedContent', $errorHandler);
             $dwTagged->set('tag_id', $newTag['tag_id']);
             $dwTagged->set('content_type', $contentType);
             $dwTagged->set('content_id', $contentId);
             $dwTagged->set('tagged_user_id', $contentUserId);
             if ($dwTagged->save()) {
                 $updatedTags[] = $newTag;
                 self::$_newTaggeds[] = array_merge($dwTagged->getMergedData(), $newTag);
                 $changed = true;
             }
         }
     }
     if (!empty($removedTagTexts)) {
         foreach ($removedTagTexts as $removedTagText) {
             $removedTag = $tagModel->getTagFromArrayByText($existingTags, $removedTagText);
             if (!empty($removedTag)) {
                 if (!empty($removedTag['is_staff']) and !XenForo_Visitor::getInstance()->hasPermission('general', Tinhte_XenTag_Constants::PERM_USER_IS_STAFF)) {
                     if (!empty($options['throwException'])) {
                         throw new XenForo_Exception(new XenForo_Phrase('tinhte_xentag_you_can_not_remove_tag_x', array('tag_text' => $removedTag['tag_text'])), true);
                     }
                     continue;
                 }
                 /* @var $dwTag Tinhte_XenTag_DataWriter_TaggedContent */
                 $dwTagged = XenForo_DataWriter::create('Tinhte_XenTag_DataWriter_TaggedContent', $errorHandler);
                 $data = array('tag_id' => $removedTag['tag_id'], 'content_type' => $contentType, 'content_id' => $contentId);
                 $dwTagged->setExistingData($data, true);
                 if ($dwTagged->delete()) {
                     // remove the removed tag from updated tags array
                     foreach (array_keys($updatedTags) as $key) {
                         if ($updatedTags[$key]['tag_id'] == $removedTag['tag_id']) {
                             unset($updatedTags[$key]);
                         }
                     }
                     $changed = true;
                 }
             }
         }
     }
     if ($changed) {
         $tagModel->rebuildTagsCache();
     }
     $packedTags = $tagModel->packTags($updatedTags);
     foreach ($packedTags as $packedTag) {
         if (!is_string($packedTag)) {
             // at least one of the packed tag is not tag text
             // we need to return the packed tags array
             return $packedTags;
         }
     }
     // simply return the counter
     return count($packedTags);
 }