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());
 }
Beispiel #2
0
 public function importRawBooks()
 {
     try {
         $bookTbl = new Book_Model_DbTable_Books();
         $select = $bookTbl->select();
         $select->from($bookTbl->info('name'), new Zend_Db_Expr('MAX(`rawbook_id`) as max_rawbook_id'));
         $data = $select->query()->fetch();
         $maxRawbookId = (int) $data['max_rawbook_id'];
         $userTbl = new User_Model_DbTable_Users();
         $rawBookTbl = new Book_Model_DbTable_Rawbooks();
         $rawBookSelect = $rawBookTbl->select();
         $rawBookSelect->where('rawbook_id > ?', $maxRawbookId);
         $rawBookSelect->order('rawbook_id ASC');
         $rawBookSelect->limit(self::DEFAULT_LIMIT);
         $rawBooks = $rawBookTbl->fetchAll($rawBookSelect);
         foreach ($rawBooks as $rawBook) {
             if (!empty($rawBook['publisher'])) {
                 $publisherSelect = $userTbl->select()->where('displayname LIKE ?', $rawBook['publisher']);
                 $publisher = $userTbl->fetchRow($publisherSelect);
             }
             if (!empty($rawBook['book_company'])) {
                 $bookCompanySelect = $userTbl->select()->where('displayname LIKE ?', $rawBook['book_company']);
                 $bookCompany = $userTbl->fetchRow($bookCompanySelect);
             }
             $data = array('book_name' => $rawBook->book_name, 'published_date' => date('Y-m-d H:i:s', $rawBook->published_date), 'price' => $rawBook->price, 'num_page' => $rawBook->num_page, 'description' => $rawBook->description, 'rawbook_id' => $rawBook->getIdentity(), 'user_id' => 1);
             if (isset($publisher) && !empty($publisher)) {
                 $data['publisher_id'] = $publisher->getIdentity();
             }
             if (isset($bookCompany) && !empty($bookCompany)) {
                 $data['book_company_id'] = $bookCompany->getIdentity();
             }
             $book = $bookTbl->createRow($data);
             $book->save();
             if (!empty($rawBook['photo'])) {
                 $image = Engine_Image::factory();
                 $name = basename($rawBook['photo']);
                 $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
                 $params = array('parent_id' => $book->getIdentity(), 'parent_type' => $book->getType(), 'user_id' => 1);
                 // Save
                 $storage = Engine_Api::_()->storage();
                 $image->open($rawBook['photo'])->write($path . '/m_' . $name)->destroy();
                 // Store
                 $iMain = $storage->create($path . '/m_' . $name, $params);
                 // Remove temp files
                 @unlink($path . '/m_' . $name);
                 $book->photo_id = $iMain->getIdentity();
                 $book->save();
                 $photoTbl = new Book_Model_DbTable_Photos();
                 $photo = $photoTbl->createRow(array('parent_object_type' => $book->getType(), 'parent_object_id' => $book->getIdentity(), 'file_id' => $iMain->getIdentity(), 'user_id' => 1, 'approved' => 1, 'default' => 1));
                 $photo->save();
             }
         }
         return true;
     } catch (Exception $e) {
         throw $e;
     }
 }
Beispiel #3
0
 public function addPhoto($photo, $viewer)
 {
     $file = $photo->getFileName();
     $name = basename($file);
     $path = APPLICATION_PATH . DIRECTORY_SEPARATOR . 'temporary';
     $params = array('parent_id' => $viewer->getIdentity(), 'parent_type' => $viewer->getType());
     // Save
     $storage = Engine_Api::_()->storage();
     // Resize image (main)
     $image = Engine_Image::factory();
     $image->open($file)->write($path . '/m_' . $name)->destroy();
     // Store
     $iMain = $storage->create($path . '/m_' . $name, $params);
     // Remove temp files
     @unlink($path . '/m_' . $name);
     $photos = new Book_Model_DbTable_Photos();
     $photo = $photos->createRow(array('parent_object_type' => $this->getType(), 'parent_object_id' => $this->getIdentity(), 'file_id' => $iMain->getIdentity(), 'user_id' => $viewer->getIdentity()));
     $photo->save();
     return $photo;
 }
 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());
 }