public function editAction() { $this->_initActions(); $subject = $this->_getSubject(); $this->_checkSubject(); $this->_checkAuthorization('view'); $this->view->form = $form = new Book_Form_Work(array('workTitle' => 'Edit the work')); $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer(); if (!$this->getRequest()->isPost()) { $values = $subject->toArray(); $form->populate($values); return; } if ($form->isValid($this->getRequest()->getPost())) { $values = $form->getValues(); $db = Engine_Db_Table::getDefaultAdapter(); $db->beginTransaction(); try { $preIsLong = $subject->is_long; $subject->setFromArray($values); $subject->save(); if (!$subject->is_long && !$preIsLong) { $chapter = $subject->getChapters()->current(); $chapter->content = $values['content']; $chapter->save(); } else { $chapterTbl = new Book_Model_DbTable_Chapters(); $chapterTbl->delete(array('work_id = ?' => $subject->getIdentity())); $chapter = $chapterTbl->createRow(array('title' => null, 'content' => trim($values['content']), 'work_id' => $subject->getIdentity(), 'published' => $subject->published)); $chapter->save(); } if (!empty($values['photo'])) { $subject->setPhoto($form->photo); } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } if ($subject->published) { $actionTbl = Engine_Api::_()->getDbTable('actions', 'activity'); $actions = $actionTbl->getActionsByObjectAndType($subject, 'work_new'); if (empty($actions)) { $action = $actionTbl->addActivity($viewer, $work, 'work_new'); } } $this->_redirectCustom($subject); } }