public function getSelect($selectedColumns = null) { $select = parent::getSelect($selectedColumns); $tableName = $this->info(Zend_Db_Table_Abstract::NAME); $bookAuthorTable = new Book_Model_DbTable_BookAuthor(); $bookAuthorTableName = $bookAuthorTable->info(Zend_Db_Table_Abstract::NAME); // $select->joinLeft( // $bookAuthorTableName, // "$tableName.book_id = $bookAuthorTableName.book_id", // array('GROUP_CONCAT(author_id) As author_ids', 'GROUP_CONCAT(author_name) As author_names') // ); // $select->where("$bookAuthorTableName.type = ?", Book_Plugin_Constants::AUTHOR); $bookAuthorSelect = $bookAuthorTable->select()->from($bookAuthorTableName, array(new Zend_Db_Expr('GROUP_CONCAT(author_id) As author_ids', 'GROUP_CONCAT(author_name) As author_names')))->where("{$bookAuthorTableName}.type = ?", Book_Plugin_Constants::AUTHOR); $select->columns(new Zend_Db_Expr('(' . $bookAuthorSelect->assemble() . ')')); return $select; }
public function isBookAuthor($user) { $bookAuthorTable = new Book_Model_DbTable_BookAuthor(); $select = $bookAuthorTable->select()->where('book_id = ?', $this->getIdentity())->where('author_id = ?', $user->getIdentity()); return $bookAuthorTable->fetchRow($select) != NULL; }
public function editAction() { $this->_initActions(); $subject = $this->_getSubject(); $this->_checkSubject(); $this->_checkAuthorization('edit'); $this->view->form = $form = new Book_Form_Book(array('bookTitle' => 'Edit the book')); $this->view->viewer = $viewer = Engine_Api::_()->user()->getViewer(); if (!$this->getRequest()->isPost()) { // TODO [DangTH] : check again the translator and authors $authors = $subject->getAuthors(Book_Plugin_Constants::AUTHOR); $this->view->toObjects = $toObjects = array(); if ($subject->is_foreign) { $this->view->toTranslatros = $translators = $subject->getAuthors(Book_Plugin_Constants::TRANSLATOR); } $toValues = array(); $authorNames = array(); foreach ($authors as $author) { if ($author instanceof User_Model_User) { array_push($toValues, $author->getIdentity()); array_push($toObjects, $author); } else { array_push($authorNames, $author); } } $arrData = $subject->toArray(); if (!empty($toValues)) { $arrData = array_merge($arrData, array('toValues' => implode($toValues, ','))); } if (!empty($authorNames)) { $arrData = array_merge($arrData, array('authors' => implode($authorNames, ','))); } if ($subject->is_foreign) { $translators = $subject->getAuthors(Book_Plugin_Constants::TRANSLATOR, TRUE); $toTranslatorValues = array(); $translatorNames = array(); $this->view->toTranslators = $toTranslators = array(); foreach ($translators as $translator) { if ($translator instanceof User_Model_User) { array_push($toTranslatorValues, $translator->getIdentity()); array_push($toTranslators); } else { array_push($translatorNames, $translator); } } if (!empty($toTranslatorValues)) { $arrData = array_merge($arrData, array('toTranslatorValues' => implode($toTranslatorValues, ','))); } if (!empty($translatorNames)) { $arrData = array_merge($arrData, array('translators' => implode($translatorNames, ','))); } // TODO [DangTH] : check again, it is not convienient using this $arrData['is_foreign'] = '1'; } $form->populate($arrData); $this->view->isPopulated = true; return; } $form->getElement('photo')->setRequired(false); if ($form->isValid($this->getRequest()->getPost())) { $bookAuthorTbl = new Book_Model_DbTable_BookAuthor(); $notificationTbl = Engine_Api::_()->getDbtable('notifications', 'activity'); $db = Engine_Db_Table::getDefaultAdapter(); $db->beginTransaction(); try { $values = $form->getValues(); $subject->setFromArray($values); $subject->save(); if (!empty($values['photo'])) { try { $subject = $subject->setPhoto($form->photo); $photoTable = new Book_Model_DbTable_Photos(); $photoTableName = $photoTable->info('name'); $photoTable->update(array('file_id' => $subject->photo_id), array("{$photoTableName}.default = ?" => '1', "{$photoTableName}.parent_object_id = ?" => $subject->getIdentity(), "{$photoTableName}.parent_object_type = ?" => $subject->getType())); // $photoTbl->update(array('default' => 0), array( // 'parent_object_id = ?' => $book->getIdentity(), // 'parent_object_type = ?' => $book->getType() // )); } catch (Engine_Image_Adapter_Exception $e) { Zend_Registry::get('Zend_Log')->log($e->__toString(), Zend_Log::WARN); } } $authorIds = explode(',', $values['toValues']); $authors = $subject->getAuthors(); $listAuthorIds = array(); foreach ($authors as $idx => $author) { if (!empty($author) && is_object($author)) { array_push($listAuthorIds, $author->getIdentity()); if (!in_array($author->getIdentity(), $authorIds)) { $bookAuthorTbl->delete(array('book_id = ?' => $subject->getIdentity(), 'author_id = ?' => $author->getIdentity())); if (!$author->isSelf($viewer)) { $notificationTbl->addNotification($author, $viewer, $subject, 'book_remove_author_from_book'); } unset($authors[$idx]); } } } $select = $bookAuthorTbl->select()->where('book_id = ?', $subject->getIdentity()); foreach ($authorIds as $authorId) { if (!empty($authorId)) { if (!in_array($authorId, $listAuthorIds)) { $row = $bookAuthorTbl->createRow(array('author_id' => $authorId, 'book_id' => $subject->getIdentity())); $row->save(); $author = Engine_Api::_()->user()->getUser($authorId); $notificationTbl->addNotification($author, $viewer, $subject, 'book_add_author_to_book'); } } } $db->commit(); } catch (Exception $e) { $db->rollBack(); throw $e; } $this->_redirectCustom($subject); } }