/** * Get tags for a list of IDs * * @param array $ids Bulletin ids * @param integer $admin Admin flag * @return array */ public function getTagsForIds($ids = array(), $admin = 0) { $tt = Tag::blank(); $tj = Object::blank(); if (!is_array($ids) || empty($ids)) { return false; } $ids = array_map('intval', $ids); $sql = "SELECT t.tag, t.raw_tag, t.admin, rt.objectid\n\t\t\t\tFROM " . $tt->getTableName() . " AS t \n\t\t\t\tINNER JOIN " . $tj->getTableName() . " AS rt ON (rt.tagid = t.id) AND rt.tbl='" . $this->_scope . "' \n\t\t\t\tWHERE rt.objectid IN (" . implode(',', $ids) . ") "; switch ($admin) { case 1: $sql .= ""; break; case 0: default: $sql .= "AND t.admin=0 "; break; } $sql .= "ORDER BY raw_tag ASC"; $db = \App::get('db'); $db->setQuery($sql); $tags = array(); if ($items = $db->loadObjectList()) { foreach ($items as $item) { if (!isset($tags[$item->objectid])) { $tags[$item->objectid] = array(); } $tags[$item->objectid][] = $item; } } return $tags; }
/** * Get top publication tags * * @param integer $limit * @return mixed Return description (if any) ... */ public function getTopTags($limit) { $tj = \Components\Tags\Models\Object::blank(); $sql = "SELECT t.tag, t.raw_tag, t.admin, tj.tagid, tj.objectid, COUNT(tj.tagid) AS tcount "; $sql .= "FROM #__tags AS t "; $sql .= "JOIN " . $tj->getTableName() . " AS tj ON t.id=tj.tagid "; $sql .= "LEFT JOIN #__publication_versions AS V ON V.publication_id=tj.objectid AND tj.tbl='publications' "; $sql .= "WHERE t.id=tj.tagid AND t.admin=0 "; $sql .= "AND tj.tbl=" . $this->_db->quote($this->_tbl) . " "; $sql .= "AND V.state=1 AND V.main=1 AND V.access!=4 "; $sql .= "AND V.published_up < " . $this->_db->quote(Date::toSql()) . " "; $sql .= "AND (V.published_down = '0000-00-00 00:00:00' OR V.published_down > " . $this->_db->quote(Date::toSql()) . ") "; $sql .= "GROUP BY tagid "; $sql .= "ORDER BY tcount DESC "; $sql .= "LIMIT {$limit}"; $this->_db->setQuery($sql); return $this->_db->loadObjectList(); }
/** * Save tag substitutions * * @param string $tag_string * @return boolean */ public function saveSubstitutions($tag_string = '') { // Get the old list of substitutions $subs = array(); foreach ($this->substitutes()->rows() as $sub) { $subs[$sub->get('tag')] = $sub; } // Add the specified tags as substitutes if not // already a substitute $raw_tags = trim($tag_string); $raw_tags = preg_split("/(,|;)/", $raw_tags); $tags = array(); foreach ($raw_tags as $raw_tag) { $nrm = $this->normalize($raw_tag); $tags[] = $nrm; if (isset($subs[$nrm])) { continue; // Substitution already exists } $sub = Substitute::blank(); $sub->set('raw_tag', trim($raw_tag)); $sub->set('tag', trim($nrm)); $sub->set('tag_id', $this->get('id')); if (!$sub->save()) { $this->addError($sub->getError()); } } // Run through the old list of substitutions, finding any // not in the new list and delete them foreach ($subs as $key => $sub) { if (!in_array($key, $tags)) { if (!$sub->destroy()) { $this->addError($sub->getError()); return false; } } } // Get all possibly existing tags that are now aliases $ids = self::all()->whereIn('tag', $tags)->rows(); // Move associations on tag and delete tag foreach ($ids as $tag) { if ($tag->get('id') != $this->get('id')) { // Get all the associations to this tag // Loop through the associations and link them to a different tag Object::moveTo($tag->get('id'), $this->get('id')); // Get all the substitutions to this tag // Loop through the records and link them to a different tag Substitute::moveTo($tag->get('id'), $this->get('id')); // Delete the tag $tag->destroy(); } } $this->set('objects', $this->objects()->total()); $this->set('substitutes', $this->substitutes()->total()); $this->save(); return true; }
/** * Save an entry * * @return void */ public function saveTask() { // Check for request forgeries Request::checkToken(); $fields = Request::getVar('fields', array(), 'post'); $row = new Object(); if (!$row->bind($fields)) { Notify::error($row->getError()); return $this->editTask($row); } // Check content if (!$row->check()) { Notify::error($row->getError()); return $this->editTask($row); } // Store content if (!$row->store()) { Notify::error($row->getError()); return $this->editTask($row); } Notify::success(Lang::txt('COM_TAGS_OBJECT_SAVED')); // Redirect to main listing if ($this->_task == 'apply') { return $this->editTask($row); } App::redirect(Route::url('index.php?option=' . $this->_option . '&controller=' . $this->_controller, false)); }
/** * Remove all tags from an item * Option User ID to remove tags added by just that user. * * @param string $tagger User ID to remove tags for * @return mixed False if errors, integer on success */ public function removeAll($tagger = 0) { if (!$this->_scope_id) { $this->setError('Unable to remove tags: No objct ID provided.'); return false; } $to = Object::all()->whereEquals('tbl', $this->_scope)->whereEquals('objectid', $this->_scope_id); if ($tagger) { $to->whereEquals('taggerid', $tagger); } $tags = array(); foreach ($to->rows() as $row) { $tags[] = $row->get('tagid'); if (!$row->destroy()) { $this->setError($row->getError()); return false; } } foreach ($tags as $tag_id) { $tag = Tag::oneOrFail($tag_id); $tag->set('objects', $tag->objects()->total())->save(); } return true; }
/** * Remove one or more entries * * @return void */ public function removeTask() { // Check for request forgeries Request::checkToken(); if (!User::authorise('core.delete', $this->_option)) { App::abort(403, Lang::txt('JERROR_ALERTNOAUTHOR')); } $ids = Request::getVar('id', array()); $ids = !is_array($ids) ? array($ids) : $ids; // Make sure we have an ID if (empty($ids)) { Notify::warning(Lang::txt('COM_TAGS_ERROR_NO_ITEMS_SELECTED')); return $this->cancelTask(); } $i = 0; foreach ($ids as $id) { // Remove entry $row = Object::oneOrFail(intval($id)); if (!$row->destroy()) { Notify::error($row->getError()); continue; } $i++; } if ($i) { Notify::success(Lang::txt('COM_TAGS_OBJECT_REMOVED')); } $this->cancelTask(); }