Exemple #1
0
 public function insertTags($inputTags, $postId, &$postTagModel)
 {
     $tagInputs = explode(',', $inputTags);
     foreach ($tagInputs as $tagInput) {
         $slug = Base_Helper_String::generateSlug($tagInput);
         // Check tag is exists
         $tag = $this->fetch('id', 'WHERE slug = :slug LIMIT 1', array(':slug' => $slug));
         if (isset($tag->id)) {
             // Check post_tag is exists
             if (!$postTagModel->fetch('*', 'WHERE id_post = :id_post AND id_tag = :id_tag LIMIT 1', array(':id_post' => $postId, ':id_tag' => $tag->id))) {
                 // Save post_tag
                 $postTagModel->id_post = $postId;
                 $postTagModel->id_tag = $tag->id;
                 $postTagModel->save();
             }
         } else {
             // Save tag
             $this->name = $tagInput;
             $this->slug = Base_Helper_String::generateSlug($tagInput);
             $this->save();
             // Save post_tag
             $postTagModel->id_post = $postId;
             $postTagModel->id_tag = $this->lastInsertId;
             $postTagModel->save();
         }
     }
 }
 public function suggestAction()
 {
     $params = $this->_request['params'];
     $tagModel = new Tag_Model_Tag();
     $tags = $tagModel->fetchAll('name', 'WHERE slug LIKE :slug', array(':slug' => '%' . Base_Helper_String::generateSlug($params['term']) . '%'));
     $data = array();
     foreach ($tags as $tag) {
         $data[]['label'] = $tag->name;
     }
     $this->_noRender = true;
     echo json_encode($data);
 }
 public function editAction()
 {
     $params = $this->_request['params'];
     $this->view->post = $post = $this->postModel->find_one($params['id']);
     if ($post) {
         $post->tags = $post->getTags();
         if ($this->isPost()) {
             if ($_FILES['thumbnail']) {
                 // Save thumbnail
                 $uploadFile = new Core_Helper_Upload();
                 $configs['uploadPath'] = BASE_PATH . '/public/upload/images/';
                 $configs['allowedTypes'] = 'gif|jpg|jpeg|png';
                 $config['maxSize'] = '2';
                 $configs['overwrite'] = false;
                 $configs['removeSpaces'] = true;
                 $uploadFile->initialize($configs);
                 $uploadFile->doUpload('thumbnail');
                 if ($err = $uploadFile->getErrors()) {
                     $this->view->errors = array('thumbnail' => array_shift($err));
                     $this->view->post = $this->_request['params'];
                     return;
                 }
             }
             // Update the post
             $categoryArr = array_filter($this->view->subcategories, create_function('$obj', 'return $obj->id == ' . $params['subcategory'] . ';'));
             $params['category'] = array_shift($categoryArr);
             $postModel = new Post_Model_Post();
             $postModel->initialize($params);
             $postModel->update();
             if (!$params['tags']) {
                 // Delete all tags of the post
                 if ($tagIds) {
                     $postTagModel->delete('WHERE id_post = :id_post AND id_tag IN (' . implode(',', $tagIds) . ')', array(':id_post' => $postId));
                 }
             } else {
                 foreach (explode(',', $params['tags']) as $tagInput) {
                     $slug = Base_Helper_String::generateSlug($tagInput);
                     $tagInputs[$slug] = $tagInput;
                 }
                 // If deleted tags
                 if ($deletedTagSlugs = array_diff(array_keys($tags), array_keys($tagInputs))) {
                     $deletedTagSlugs = array_map(array($postTagModel->db(), 'quote'), $deletedTagSlugs);
                     foreach ($tagModel->fetchAll('id', 'WHERE slug IN(' . implode(',', $deletedTagSlugs) . ')') as $tag) {
                         $deletedTagIds[] = $tag->id;
                     }
                     // Delete tags of the Post
                     $postTagModel->delete('WHERE id_post = :id_post AND id_tag IN (' . implode(',', $deletedTagIds) . ')', array(':id_post' => $postId));
                 }
                 // If inserted tags
                 if ($insertedTagSlugs = array_diff(array_keys($tagInputs), array_keys($tags))) {
                     foreach ($insertedTagSlugs as $insertedTagSlug) {
                         $insertedTagNames[] = $tagInputs[$insertedTagSlug];
                     }
                     $tagModel->insertTags(array_values($insertedTagNames), $postId, $postTagModel);
                 }
             }
             $this->redirect(array('route' => 'route_admin_post'));
         }
     }
 }