/** * List all tags * * @return void */ public function displayTask() { // Incoming $filters = array('search' => urldecode(Request::getState($this->_option . '.' . $this->_controller . '.search', 'search', '')), 'by' => strtolower(Request::getState($this->_option . '.' . $this->_controller . '.by', 'filterby', 'all')), 'sort' => Request::getState($this->_option . '.' . $this->_controller . '.sort', 'filter_order', 'raw_tag'), 'sort_Dir' => Request::getState($this->_option . '.' . $this->_controller . '.sortdir', 'filter_order_Dir', 'ASC')); $model = Tag::all(); $t = $model->getTableName(); $s = Substitute::blank()->getTableName(); $model->select('DISTINCT ' . $t . '.*')->join($s, $s . '.tag_id', $t . '.id', 'left'); if ($filters['search']) { $filters['search'] = strtolower((string) $filters['search']); $model->whereLike($t . '.raw_tag', $filters['search'], 1)->orWhereLike($t . '.tag', $filters['search'], 1)->orWhereLike($s . '.raw_tag', $filters['search'], 1)->orWhereLike($s . '.tag', $filters['search'], 1)->resetDepth(); } if ($filters['by'] == 'admin') { $model->whereEquals($t . '.admin', 1); } else { if ($filters['by'] == 'user') { $model->whereEquals($t . '.admin', 0); } } // Get records $rows = $model->order($t . '.' . $filters['sort'], $filters['sort_Dir'])->paginated('limitstart', 'limit')->rows(); // Output the HTML $this->view->set('filters', $filters)->set('rows', $rows)->display(); }
/** * 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; }
/** * Get a list of tags * * @param string $rtrn Format of data to return * @param array $filters Filters to apply * @param boolean $clear Clear cached data? * @return mixed */ public function tags($rtrn = '', $filters = array(), $clear = false) { if (!isset($filters['scope']) && $this->get('scope') != 'site') { $filters['scope'] = (string) $this->get('scope'); } if (!isset($filters['scope_id']) && $this->get('scope_id') != 0) { $filters['scope_id'] = (int) $this->get('scope_id'); } $tbl = Object::blank()->getTableName(); $results = Tag::all(); if (isset($filters['sort']) && $filters['sort'] == 'taggedon') { $results->select($tbl . '.taggedon')->join($tbl, $tbl . '.tagid', $results->getTableName() . '.id'); } if (isset($filters['tagger_id']) || isset($filters['scope']) || isset($filters['scope_id']) || isset($filters['label'])) { $results->join($tbl, $tbl . '.tagid', $results->getTableName() . '.id'); if (isset($filters['tagger_id']) && $filters['tagger_id']) { $results->whereEquals($tbl . '.taggerid', (int) $filters['tagger_id']); } if (isset($filters['scope']) && $filters['scope']) { $results->whereEquals($tbl . '.tbl', (string) $filters['scope']); } if (isset($filters['scope_id']) && $filters['scope_id']) { $results->whereEquals($tbl . '.objectid', (int) $filters['scope_id']); } if (isset($filters['label']) && $filters['label']) { $results->whereEquals($tbl . '.label', (string) $filters['label']); // find labeled tags } } if (isset($filters['admin']) && $filters['admin'] !== null) { $results->whereEquals('admin', (int) $filters['admin']); } if (isset($filters['created_by']) && $filters['created_by'] > 0) { $results->whereEquals('created_by', (int) $filters['created_by']); } if (isset($filters['modified_by']) && $filters['modified_by'] > 0) { $results->whereEquals('modified_by', $filters['modified_by']); } if (isset($filters['search']) && $filters['search']) { $filters['search'] = strtolower((string) $filters['search']); $tbl = Substitute::blank()->getTableName(); $results->select($results->getTableName() . '.*')->join($tbl, $tbl . '.tag_id', $results->getTableName() . '.id', 'left'); $results->whereLike($results->getTableName() . '.raw_tag', $filters['search'], 1)->orWhereLike($results->getTableName() . '.tag', $filters['search'], 1)->orWhereLike($tbl . '.raw_tag', $filters['search'], 1)->resetDepth(); } switch (strtolower($rtrn)) { case 'count': if (!isset($this->_cache['tags.count']) || $clear) { $results->select($results->getTableName() . '.id', null, true); $this->_cache['tags.count'] = (int) $results->total(); } return $this->_cache['tags.count']; break; case 'top': break; case 'list': case 'results': default: $results->select($results->getTableName() . '.*')->group($results->getTableName() . '.id'); if (!$this->_cache['tags.list'] || $clear) { if (isset($filters['limit']) && $filters['limit'] != 0 && $filters['limit'] != 'all') { if (!isset($filters['start'])) { $filters['start'] = 0; } $results->limit($filters['limit']); $results->start($filters['start']); } if (isset($filters['sort']) && $filters['sort'] != '') { if ($filters['sort'] == 'total') { $filters['sort'] = 'objects'; } $filters['sort_Dir'] = isset($filters['sort_Dir']) && $filters['sort_Dir'] ? $filters['sort_Dir'] : "ASC"; $results->order($filters['sort'], $filters['sort_Dir']); } $this->_cache['tags.list'] = $results->rows(); } return $this->_cache['tags.list']; break; } }