public function createPostAction() { if ($this->request->isPost()) { $post = new Post(); $post->topic = $this->request->getPost('title'); $post->content = str_replace('</div>', '\\n', str_replace('<div>', '', $this->request->getPost('post'))); $post->is_service = $this->request->getPost('is_service'); $post->created_at = date('d.m.Y, h:i'); if ($post->save()) { $tt = explode(',', $this->request->getPost('tags')); foreach ($tt as $t) { $tag = Tag::findFirst(array('conditions' => 'name = ?1', 'bind' => array(1 => $t))); if ($tag) { $tag_post = new PostTag(); $tag_post->post_id = $post->id; $tag_post->tag_id = $tag->id; if (!$tag_post->save()) { message($this, 'd', 'Ошибка привязки категории к посту'); return $this->response->redirect(); } } else { $tag = new Tag(); $tag->name = $t; if ($tag->save()) { $tag_post = new PostTag(); $tag_post->post_id = $post->id; $tag_post->tag_id = $tag->id; if (!$tag_post->save()) { message($this, 'd', 'Ошибка привязки категории к посту'); return $this->response->redirect(); } } else { message($this, 'd', 'Ошибка сохранения новой категории'); return $this->response->redirect(); } } } message($this, 's', 'Новость успешно добавлена'); return $this->response->redirect(); } else { foreach ($post->getMessages() as $message) { message($this, "d", "Ошибка: " . $message->getMessage() . " в поле " . $message->getField() . ". Тип: " . $message->getType()); } return $this->response->redirect(); } } else { $this->assets->collection('headerJs')->addJs("js/jquery-ui.min.js"); $this->assets->collection('headerCss')->addCss("css/jquery-ui.min.css")->addCss("css/jquery-ui.structure.min.css"); $tt = Tag::find(); $tags = ""; foreach ($tt as $tag) { $tags .= '"' . $tag->name . '", '; } $tags = substr($tags, 0, -2); $this->view->setParamToView('tags', $tags); } }
public function addTag($tag) { if ($tag) { $pt = new PostTag(); $pt->setTag($tag); $this->addPostTag($pt); return true; } return false; }
public function getDashletFields() { $fields = parent::getDashletFields(); $all = PostTag::get()->map('Title', 'Title')->toArray(); $fields->insertAfter(MultiValueTextField::create('FilterTags', 'Tags to filter by', $all), 'Title'); return $fields; }
// Get all the Form Data $myPost->SetValues($_POST); switch ($_POST['submit_button']) { case 'Save Draft and Continue Editing': $myPost->SetValue('status', 'Draft'); $redirect = false; break; case 'Save and Publish': $myPost->SetValue('status', 'Published'); default: break; } // Save the info to the DB if there is no errors if ($myPost->Save()) { // Sync in the Tags $myPostTag = new PostTag(); $myPostTag->SetValue('post_id', $myPost->GetPrimary()); $myPostTag->Sync($myPost->GetValue('tags')); SetAlert('Post Information Saved.', 'success'); // Redirect if needed if ($redirect) { header('location:posts.php'); die; } } } // If Deleting the Page if ($_POST['submit_button'] == 'Delete') { // Get all the form data $myPost->SetValues($_POST); // Remove the info from the DB
/** * Method called to associate a PostTag object to this object * through the PostTag foreign key attribute * * @param PostTag $l PostTag * @return void * @throws PropelException */ public function addPostTag(PostTag $l) { $this->collPostTags[] = $l; $l->setTag($this); }
public function servicesAction() { $posts = Post::find(array('conditions' => 'is_service = 1', 'limit' => 5)); $tags = array(); foreach ($posts as $index => $post) { $pts = PostTag::find(array('conditions' => 'post_id = ?1', 'bind' => array(1 => $post->id))); $tags[$index] = array(); foreach ($pts as $pt) { $tags[$index][] = Tag::findFirst(array('conditions' => 'id = ?1', 'bind' => array(1 => $pt->tag_id))); } } $this->view->setParamToView('posts', $posts); $this->view->setParamToView('tags', $tags); }
public function run() { //DB::table('post_tag')->delete(); // pivot tag PostTag::create(array('tag_id' => 1, 'post_id' => 1)); }
/** * Tag this post with a particular tag * * @param string $tag */ public function tag($tags, $clearExisting = false) { if (!is_array($tags)) { $tags = array($tags); } if ($clearExisting) { $this->Tags()->removeAll(); } $created = array(); foreach ($tags as $tag) { if (!preg_match('/[a-z0-9_-]/i', $tag)) { continue; } $existing = PostTag::get()->filter(array('Title' => $tag))->first(); if (!$existing) { $existing = PostTag::create(); $existing->Title = $tag; $existing->write(); } $this->Tags()->add($existing, array('Tagged' => date('Y-m-d H:i:s'))); $created[] = $existing; } return $created; }
public function Sync($tag_list) { // Requere a valid post_id $myPost = new Post(); $myPost->SetPrimary($this->GetValue('post_id')); if (!$myPost->GetInfo()) { return false; } // Get a list of all the tags for this post $tmpPostTag = new PostTag(); $tmpPostTag->SetValue('post_id', $myPost->GetPrimary()); $tmpTag = new Tag(); $tmpPostTag->Join($tmpTag, 'tag_id', 'LEFT'); $tmpPostTag->GetList(); // Create an array worth using $old_tags = array(); foreach ($tmpPostTag->results as $tag) { $old_tags[$tag['post_tag_id']] = $tag['tag']; } // Split up the categories and make sure there is tags $new_tags = explode(',', strtolower($tag_list)); foreach ($new_tags as $key => $data) { $new_tags[$key] = trim($data); } // Get the difference $diff_tags = array_diff($new_tags, $old_tags); // Add the different tags to the database foreach ($diff_tags as $tag) { // Save the Tag $tmpTag->ResetValues(); $tmpTag->SetValue('tag', $tag); $item = $tmpTag->GetAssoc('tag', 'tag', 'ASC', 0, 1); if (count($item) == 1) { $tmpTag->SetPrimary(key($item)); } else { $tmpTag->Save(); } $tmpPostTag->ResetValues(); $tmpPostTag->SetValue('post_id', $myPost->GetPrimary()); $tmpPostTag->SetValue('tag_id', $tmpTag->GetPrimary()); $tmpPostTag->Save(); } // Get a list of the tags to remove $rem_tags = array_diff($old_tags, $new_tags); // Remove the non-needed tags foreach ($rem_tags as $id => $tag) { $tmpPostTag->ResetValues(); $tmpPostTag->SetPrimary($id); $tmpPostTag->Delete(); } }
/** * Save changes made in Post editing */ function savePostChanges() { Doo::loadHelper('DooValidator'); $_POST['content'] = trim($_POST['content']); //get defined rules and add show some error messages $validator = new DooValidator(); $validator->checkMode = DooValidator::CHECK_SKIP; if ($error = $validator->validate($_POST, 'post_edit.rules')) { $data['rootUrl'] = Doo::conf()->APP_URL; $data['title'] = 'Error Occured!'; $data['content'] = '<p style="color:#ff0000;">' . $error . '</p>'; $data['content'] .= '<p>Go <a href="javascript:history.back();">back</a> to edit.</p>'; $this->render('admin_msg', $data); } else { Doo::loadModel('Post'); Doo::loadModel('Tag'); $p = new Post($_POST); //delete the previous linked tags first Doo::loadModel('PostTag'); $pt = new PostTag(); $pt->post_id = $p->id; $pt->delete(); //update the post along with the tags if (self::$tags != Null) { $tags = array(); foreach (self::$tags as $t) { $tg = new Tag(); $tg->name = $t; $tags[] = $tg; } $p->relatedUpdate($tags); } else { $p->update(); } //clear the sidebar cache Doo::cache('front')->flushAllParts(); $data['rootUrl'] = Doo::conf()->APP_URL; $data['title'] = 'Post Updated!'; $data['content'] = '<p>Your changes is saved successfully.</p>'; $data['content'] .= '<p>Click <a href="' . $data['rootUrl'] . 'article/' . $p->id . '">here</a> to view the post.</p>'; $this->render('admin_msg', $data); } }