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')); } } }