protected function _postDelete() { parent::_postDelete(); $tagTbl = new Book_Model_DbTable_Tags(); $tagTbl->delete(array('post_id = ?' => $this->getIdentity())); $signatureTbl = new Book_Model_DbTable_Signatures(); $signatureTbl->delete(array('parent_object_type = ?' => $this->getType(), 'parent_object_id = ?' => $this->getIdentity())); }
public function editAction() { $this->_initActions(); $subject = $this->_getSubject(); $this->_checkSubject(); $this->_checkAuthorization('view'); $this->view->form = $form = new Book_Form_Post(array('postName' => 'Edit the post')); $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer(); if (!$this->getRequest()->isPost()) { $this->view->toTaggedUsers = $taggedUsers = $subject->getTaggedUsers(); $toValues = array(); foreach ($taggedUsers as $taggedUser) { array_push($toValues, $taggedUser->getIdentity()); } $this->view->toTaggedBooks = $taggedBooks = $subject->getTaggedBooks(); $toBookValues = array(); foreach ($taggedBooks as $taggedBook) { array_push($toBookValues, $taggedBook->getIdentity()); } // prepare tags $postTags = $subject->tags()->getTagMaps(); $tagString = ''; foreach ($postTags as $tagmap) { if ($tagString !== '') { $tagString .= ', '; } $tagString .= $tagmap->getTag()->getTitle(); } $data = array('toValues' => implode($toValues, ','), 'toBookValues' => implode($toBookValues, ','), 'tags' => $tagString); $parentBook = $subject->getParentObject(); if (!empty($parentBook)) { $this->view->parentBook = $parentBook; $data['parentBookValue'] = $parentBook->getIdentity(); $form->getElement('hasParent')->setChecked(true); } else { $form->getElement('hasParent')->setChecked(false); } $form->populate(array_merge($subject->toArray(), $data)); $this->view->isPopulated = true; return; } else { if ($form->isValid($this->getRequest()->getPost())) { $values = $form->getValues(); $values['parent_id'] = null; $values['parent_type'] = null; if ($values['hasParent'] === '1') { if (!empty($values['parentBookValue'])) { $values['parent_id'] = $values['parentBookValue']; $values['parent_type'] = 'book'; } } $newTaggedUsers = array(); $bookAuthorTbl = new Book_Model_DbTable_BookAuthor(); $notificationTbl = Engine_Api::_()->getDbtable('notifications', 'activity'); $tagTbl = new Book_Model_DbTable_Tags(); $db = Engine_Db_Table::getDefaultAdapter(); $db->beginTransaction(); try { $subject->setFromArray($values); $subject->save(); if (!empty($values['photo'])) { $subject->setPhoto($form->photo); } $tags = array(); foreach (preg_split('/[,]+/', $values['tags']) as $tag) { $t = trim($tag); if (!empty($t)) { array_push($tags, $t); } } $subject->tags()->setTagMaps($viewer, $tags); $bookIds = explode(',', $values['toBookValues']); $taggedBooks = $subject->getTaggedBooks(); $taggedBookIds = array(); if (empty($bookIds)) { $tagTbl->delete(array('object_type = ?' => 'book', 'post_id = ?' => $subject->getIdentity())); } else { foreach ($taggedBooks as $taggedBook) { array_push($taggedBookIds, $taggedBook->getIdentity()); if (!in_array($taggedBook->getIdentity(), $bookIds)) { $tagTbl->delete(array('object_type = ?' => 'book', 'object_id = ?' => $taggedBook->getIdentity(), 'post_id = ?' => $subject->getIdentity())); } } foreach ($bookIds as $bookId) { if (!in_array($bookId, $taggedBookIds)) { $newBook = Engine_Api::_()->getItem('book', $bookId); if ($newBook) { $newTaggedBook = $tagTbl->createRow(array('post_id' => $subject->getIdentity(), 'object_type' => 'book', 'object_id' => $bookId)); $newTaggedBook->save(); } } } } $userIds = explode(',', $values['toValues']); $taggedUsers = $subject->getTaggedUsers(); $taggedUserIds = array(); if (empty($userIds)) { $tagTbl->delete(array('object_type = ?' => 'user', 'post_id = ?' => $subject->getIdentity())); } else { foreach ($taggedUsers as $taggedUser) { array_push($taggedUserIds, $taggedUser->getIdentity()); if (!in_array($taggedUser->getIdentity(), $userIds)) { $tagTbl->delete(array('object_type = ?' => 'user', 'object_id = ?' => $taggedUser->getIdentity(), 'post_id = ?' => $subject->getIdentity())); } } foreach ($userIds as $userId) { if (!in_array($userId, $taggedUserIds)) { $newUser = Engine_Api::_()->user()->getUser($userId); if ($newUser) { $newTaggedUser = $tagTbl->createRow(array('post_id' => $subject->getIdentity(), 'object_type' => 'user', 'object_id' => $userId)); $newTaggedUser->save(); array_push($newTaggedUsers, $newUser); } } } } $db->commit(); } catch (Engine_Image_Adapter_Exception $e) { Zend_Registry::get('Zend_Log')->log($e->__toString(), Zend_Log::WARN); } catch (Exception $e) { $db->rollBack(); throw $e; } foreach ($newTaggedUsers as $newTaggedUser) { $notificationTbl->addNotification($newTaggedUser, $viewer, $subject, 'post_tagged'); } $this->_redirectCustom($subject); } } }