/** * Set event tags. * * @param array $tags * @return Model_Event */ public function set_tags(array $tags = null) { $old_tags = $this->tags(); $new_tags = (array) $tags; // Delete removed tags foreach (array_diff(array_keys($old_tags), $new_tags) as $tag_id) { $this->remove('tag', (int) $tag_id); } // Add new tags $add = array(); foreach (array_diff($new_tags, array_keys($old_tags)) as $tag_id) { $tag = Model_Tag::factory((int) $tag_id); if ($tag && $tag->loaded()) { $add[] = (int) $tag->id; } } if ($add) { $this->relate('tags', $add); } // Normalized tags for old version, to be deprecated if ($this->music != ($music = implode(', ', $this->tags()))) { $this->music = $music; $this->save(); } return $this; }
/** * Action: tag * * @param integer $group_id */ public function action_tag($group_id = null) { $this->history = false; if ($group_id && $this->request->action() !== 'tag') { // Add new tag $group = Model_Tag_Group::factory($group_id); if (!$group->loaded()) { throw new Model_Exception($group, $group_id); } $tag = Model_Tag::factory(); $tag->tag_group_id = $group_id; $tag->author_id = Visitor::$user->id; $tag->created = time(); $this->view = View_Page::factory($group->name); $this->view->subtitle = HTML::chars($group->description); } else { if ($tag_id = (int) $this->request->param('id')) { // Edit old tag $tag = Model_Tag::factory($tag_id); if (!$tag->loaded()) { throw new Model_Exception($tag, $tag_id); } $this->view = View_Page::factory($tag->name); $this->view->subtitle = HTML::chars($tag->description); $this->page_actions[] = array('link' => Route::model($tag, 'delete') . '?' . Security::csrf_query(), 'text' => '<i class="icon-trash icon-white"></i> ' . __('Delete tag'), 'class' => 'btn btn-danger tag-delete'); } else { Request::back(Route::url('tags')); } } $errors = array(); if ($_POST) { $tag->name = Arr::get($_POST, 'name'); $tag->description = Arr::get($_POST, 'description'); try { $tag->save(); $this->request->redirect(Route::model($tag)); } catch (Validation_Exception $e) { $errors = $e->array->errors('validate'); } } $this->view->add(View_Page::COLUMN_CENTER, $this->section_tag($tag, $errors)); }