Example #1
0
 public function createAction()
 {
     if (!$this->_helper->requireUser()->isValid()) {
         return;
     }
     $viewer = Engine_Api::_()->user()->getViewer();
     $this->view->form = $form = new Book_Form_Book(array('bookTitle' => 'Post a book'));
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     $values = $form->getValues();
     $bookTable = new Book_Model_DbTable_Books();
     $bookAuthorTable = new Book_Model_DbTable_BookAuthor();
     $db = Engine_Db_Table::getDefaultAdapter();
     $db->beginTransaction();
     try {
         $book = $bookTable->createRow($values);
         $book->creation_date = date('Y-m-d H:i:s');
         $book->modified_date = $book->creation_date;
         $book->category_id = $this->_getParam('category_id_0', 0);
         $book->user_id = $viewer->getIdentity();
         $book->save();
         if (!empty($values['photo'])) {
             try {
                 $book = $book->setPhoto($form->photo);
                 $photoTable = new Book_Model_DbTable_Photos();
                 $photo = $photoTable->createRow(array('parent_object_id' => $book->getIdentity(), 'parent_object_type' => $book->getType(), 'file_id' => $book->photo_id, 'user_id' => $viewer->getIdentity(), 'approved' => 1, 'default' => 1));
                 $photo->save();
             } catch (Engine_Image_Adapter_Exception $e) {
                 Zend_Registry::get('Zend_Log')->log($e->__toString(), Zend_Log::WARN);
             }
         }
         $authorIds = explode(',', $values['toValues']);
         if (!empty($authorIds)) {
             foreach ($authorIds as $authorId) {
                 if (!empty($authorId)) {
                     $bookAuthor = $bookAuthorTable->createRow();
                     $bookAuthor->book_id = $book->getIdentity();
                     $bookAuthor->author_id = $authorId;
                     $bookAuthor->save();
                 }
             }
         }
         $authorsName = explode(',', $values['authors']);
         foreach ($authorsName as $name) {
             if (!empty($name)) {
                 $bookAuthor = $bookAuthorTable->createRow();
                 $bookAuthor->book_id = $book->getIdentity();
                 $bookAuthor->author_id = 0;
                 $bookAuthor->author_name = $name;
                 $bookAuthor->save();
             }
         }
         if ($book->is_foreign && !empty($values['toTranslatorsValues'])) {
             $translatorIds = explode(',', $values['toTranslatorsValues']);
             if (!empty($translatorIds)) {
                 foreach ($translatorIds as $translatorId) {
                     if (!empty($translatorId)) {
                         $bookAuthor = $bookAuthorTable->createRow();
                         $bookAuthor->book_id = $book->getIdentity();
                         $bookAuthor->author_id = $translatorId;
                         $bookAuthor->type = Book_Plugin_Constants::TRANSLATOR;
                         $bookAuthor->save();
                     }
                 }
             }
             $translatorsName = explode(',', $values['translators']);
             foreach ($translatorsName as $name) {
                 if (!empty($name)) {
                     $bookAuthor = $bookAuthorTable->createRow();
                     $bookAuthor->book_id = $book->getIdentity();
                     $bookAuthor->author_id = 0;
                     $bookAuthor->author_name = $name;
                     $bookAuthor->type = Book_Plugin_Constants::TRANSLATOR;
                     $bookAuthor->save();
                 }
             }
         }
         // CREATE AUTH STUFF HERE
         $auth = Engine_Api::_()->authorization()->context;
         $roles = array('owner', 'parent_member', 'registered', 'everyone');
         foreach ($roles as $i => $role) {
             $auth->setAllowed($book, $role, 'view', true);
         }
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     $actionTbl = Engine_Api::_()->getDbTable('actions', 'activity');
     $action = $actionTbl->addActivity($viewer, $book, 'book_new');
     if ($action != null) {
         $actionTbl->attachActivity($action, $book);
     }
     $this->_redirectCustom($book->getHref());
 }
Example #2
0
 public function createBookAction()
 {
     if (!$this->_helper->requireUser()->isValid()) {
         return;
     }
     $viewer = Engine_Api::_()->user()->getViewer();
     $this->view->form = $form = new Book_Form_Book_Create();
     if (!$this->getRequest()->isPost()) {
         return;
     }
     if (!$form->isValid($this->getRequest()->getPost())) {
         return;
     }
     $values = $form->getValues();
     $bookTable = new Book_Model_DbTable_Books();
     $bookAuthorTable = new Book_Model_DbTable_BookAuthor();
     $db = Engine_Db_Table::getDefaultAdapter();
     $db->beginTransaction();
     try {
         $book = $bookTable->createRow($values);
         $book->creation_date = date('Y-m-d H:i:s');
         $book->modified_date = $book->creation_date;
         $book->category_id = $this->_getParam('category_id_0', 0);
         $book->user_id = $viewer->getIdentity();
         $book->save();
         if (!empty($values['photo'])) {
             try {
                 $book->setPhoto($form->photo);
                 $bookPhotoTable = new Book_Model_DbTable_Photos();
                 $bookPhoto = $bookPhotoTable->createRow(array('parent_type' => $book->getType(), 'parent_id' => $book->getIdentity(), 'file_id' => $book->photo_id, 'approved' => 1, 'default' => 1));
                 $bookPhoto->save();
             } catch (Engine_Image_Adapter_Exception $e) {
                 Zend_Registry::get('Zend_Log')->log($e->__toString(), Zend_Log::WARN);
             }
         }
         $authorIds = explode(',', $values['toValues']);
         foreach ($authorIds as $authorId) {
             $bookAuthor = $bookAuthorTable->createRow();
             $bookAuthor->book_id = $book->getIdentity();
             $bookAuthor->author_id = $authorId;
             $bookAuthor->save();
         }
         $photoTbl = new Book_Model_DbTable_Photos();
         $photo = $photoTbl->createRow(array('parent_object_type' => $book->getType(), 'parent_object_id' => $book->getIdentity(), 'file_id' => $book->photo_id, 'user_id' => $viewer->getIdentity(), 'default' => 1, 'approved' => 1));
         $photo->save();
         $db->commit();
     } catch (Exception $e) {
         $db->rollBack();
         throw $e;
     }
     $this->_redirectCustom($book->getHref());
 }