/** * Set the field values * * @param array $values * @return Category */ public function setFieldValues(array $values = null) { parent::setFieldValues($values); if ($_POST && null !== $this->title) { // Check for dupe name $tag = Table\Tags::findBy(['title' => $this->title]); if (isset($tag->id) && $this->id != $tag->id) { $this->getElement('title')->addValidator(new Validator\NotEqual($this->title, 'That tag already exists.')); } } return $this; }
/** * Get tag content * * @param mixed $id * @param boolean $fields * @return array */ public function getTagContent($id, $fields = false) { if (!is_numeric($id)) { $tag = Table\Tags::findBy(['title' => $id]); if (isset($tag->id)) { $id = $tag->id; } } $items = []; $c2t = Table\TagItems::findBy(['tag_id' => $id]); if ($c2t->hasRows()) { foreach ($c2t->rows() as $c) { if ($fields) { $item = \Phire\Fields\Model\FieldValue::getModelObject('Phire\\Content\\Model\\Content', [$c->content_id], 'getById', $this->filters); } else { $class = 'Phire\\Content\\Model\\Content'; $model = new $class(); call_user_func_array([$model, 'getById'], [$c->content_id]); $item = $model; } if ($item->status == 1 && count($item->roles) == 0) { $items[$item->id] = new \ArrayObject($item->toArray(), \ArrayObject::ARRAY_AS_PROPS); } } } return $items; }
/** * Save tag relationships * * @param AbstractController $controller * @param Application $application * @return void */ public static function save(AbstractController $controller, Application $application) { $contentId = null; if ($_POST && $controller->hasView() && null !== $controller->view()->id && null !== $controller->view()->form && $controller->view()->form !== false && $controller->view()->form instanceof \Pop\Form\Form) { $tags = $controller->view()->form->content_tags; $contentId = $controller->view()->id; if (null !== $contentId) { $c2t = new Table\TagItems(); $c2t->delete(['content_id' => $contentId]); } if (!empty($tags)) { $tags = explode(',', $tags); foreach ($tags as $tag) { $tag = trim($tag); $t = Table\Tags::findBy(['title' => $tag]); if (!isset($t->id)) { $t = new Table\Tags(['title' => $tag, 'slug' => Slug::filter($tag)]); $t->save(); } $c2t = new Table\TagItems(['content_id' => $contentId, 'tag_id' => $t->id]); $c2t->save(); } } } }