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()
 {
     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);
         }
     }
 }