public function editAction() { if (!$this->_getParam('id')) { throw new Exception('No entry id given or url is wrong'); } $this->view->heading = 'Edited: '; $form = new Admin_Form_EntryEdit(); if (!$this->getRequest()->isPost()) { $this->view->heading = 'Edit: '; $entry_mapper = new Application_Model_EntryMapper(); $this->view->entry = $entry_mapper->find($this->_getParam('id')); $this->view->entry->getTags(); if (!$this->view->entry) { $this->view->failedFind = true; return; } $form->setElementFilters(array()); // disable all element filters $this->_repopulateForm($form, $this->view->entry); $this->view->entryForm = $form; return; } elseif (!$form->isValid($_POST)) { $this->view->failedValidation = true; $this->view->heading = 'Edit entry failed'; $this->view->entryForm = $form; return; } $values = $form->getValues(); $data = array('id' => $values['id'], 'title' => $values['title'], 'last_modified' => strtotime($values['date']), 'content' => $values['body'], 'extended_content' => $values['extended_body'], 'hide' => $values['hide'], 'description' => $values['description']); $entry = new Application_Model_Entry($data); $entry_mapper = new Application_Model_EntryMapper(); $entry_mapper->save($entry); $this->view->entrySaved = true; $this->view->entry = $entry; $tag_string = strtolower($values['tags']); $tag_array = explode(',', $tag_string); array_walk($tag_array, 'trim'); $tag_collection = $entry->getTags(); $tag_array = array_unique($tag_array); $tag_mapper = new Application_Model_TagMapper(); // add new tags foreach ($tag_array as $tag) { $add = true; // if the tag does not already exist foreach ($tag_collection as $obj) { if ($obj->tag == $tag) { $add = false; } } if ($add) { $tag_obj = new Application_Model_Tag(array('tag' => $tag, 'entry' => $data['id'])); $tag_mapper->save($tag_obj); } } // delete removed tags foreach ($tag_collection as $obj) { $delete = true; // if the tag does exist but is not in the array from the form foreach ($tag_array as $tag) { if ($obj->tag == $tag) { $delete = false; } } if ($delete) { $tag_mapper->delete($obj); } } }
public function editAction() { $request = $this->getRequest(); $post_id = $request->getParam("id"); if (empty($post_id)) { throw new Exception("empty post_id"); } $post_mapper = new Application_Model_PostMapper(); $post = $post_mapper->find($post_id); if (empty($post)) { throw new Exception("empty post"); } $this->view->post = $post; $user_id = get_user_id(); if ($post['user_id'] != $user_id) { throw new Exception("Not Authorized"); } $tag_mapper = new Application_Model_TagMapper(); $tags1 = $tag_mapper->findAllByColumn('post_id', $post_id); $tag_array = array(); foreach ($tags1 as $tag) { $tag_content = "#" . $tag['content']; $tag_array[] = $tag_content; } $this->view->tags = $tag_array; if ($request->isPost()) { $content = $request->getParam("content"); $pieces = explode("#", $content); $post_content = $pieces[0]; $tags = array(); for ($i = 1; $i < sizeof($pieces); $i++) { $tags[] = $pieces[$i]; } $post_model = new Application_Model_Post(); $post_model->_fields['id'] = $post_id; $post_model->_fields['updated_at'] = time(); $post_model->_fields['created_at'] = $post['created_at']; $post_model->_fields['content'] = $post_content; $post_model->_fields['user_id'] = $user_id; $post_model->_fields['is_reported'] = 0; $post_mapper = new Application_Model_PostMapper(); $post_id = $post_mapper->save($post_model); $tag_mapper = new Application_Model_TagMapper(); foreach ($tags1 as $tag1) { $tag_mapper->delete($tag1['id']); } foreach ($tags as $tag) { $comment_model = new Application_Model_Tag(); $comment_model->_fields['content'] = $tag; $comment_model->_fields['post_id'] = $post_id; $comment_model->_fields['type'] = TYPE_TAG; $tag_mapper->save($comment_model); } if (isset($_FILES['post_pic'])) { if (is_uploaded_file($_FILES['post_pic']['tmp_name'])) { if (!move_uploaded_file($_FILES['post_pic']['tmp_name'], APPLICATION_PATH . "/../public/post_pic/" . $post_id . '.png')) { throw new Exception("error in moving file"); } } } $this->_redirect("/post/home"); } }
public function editAction() { $request = $this->getRequest(); $id = $request->getParam("id"); // Get the idea information from the database $form = new Application_Form_Idea(); $mapper = new Application_Model_IdeaMapper(); $idea = new Application_Model_Idea(); $mapper->find($id, $idea); // Get the tag information from the database $mapper = new Application_Model_TagMapper(); $tags = $mapper->fetchByIdea($idea); // formulate the tag string for the idea $tagString = ""; foreach ($tags as $tag) { $tagString = $tagString . $tag->getTag() . ";"; } $idea->setTagString($tagString); //handle changes to travel idea if ($this->getRequest()->isPost()) { if ($form->isValid($this->getRequest()->getPost())) { //get the newly updated form value $idea = new Application_Model_Idea($form->getValues()); //set the id of the idea in order to make changes to the same idea in database $idea->setId($id); // Get idea author information $author = new Application_Model_Member(); $authns = new Zend_Session_Namespace('AuthNameSpace'); $mapper = new Application_Model_MemberMapper(); $mapper->find($authns->userId, $author); $idea->setAuthor($author); // save the idea in the database $mapper = new Application_Model_IdeaMapper(); $mapper->save($idea); // Delete all the tag related to this idea $mapper = new Application_Model_TagMapper(); $mapper->deleteByIdeaId($id); $this->_logger->log('after update idea, tag String =' . $idea->tagString, Zend_Log::INFO); // Extract the updated tag $tags = explode(";", $idea->tagString); foreach ($tags as $temp) { if (strlen($temp) > 0) { $tag = new Application_Model_Tag(); $tag->setTag($temp); $tag->setIdeaId($id); $this->_logger->log('tag = ' . $temp, Zend_Log::INFO); // Save the tag in the database $mapper->save($tag); } } $this->_helper->FlashMessenger()->setNamespace('success')->addMessage('Update Successful!'); $this->_logger->log('Update Idea successfully! ID =' . $id, Zend_Log::INFO); $this->_redirector->gotoSimple('detail', 'ideas', null, array('id' => $id)); return; } } //form data array to populate idea creation form with existing data $data = array('title' => $idea->getTitle(), 'destination' => $idea->getDestination(), 'startDate' => $idea->getStartDate(), 'endDate' => $idea->getEndDate(), 'tagString' => $idea->getTagString()); $form->populate($data); $this->view->form = $form; }