Пример #1
0
 /** @noinspection PhpUnusedPrivateMethodInspection
  * @param Request $request
  *
  * @return bool|mixed
  * @throws PermissionRequiredException
  * @throws exceptions\LoginRequiredException
  */
 private function addAuthor(Request $request)
 {
     if (!$this->auth->checkPermission(Auth::EDIT_PUBLICATION)) {
         throw new PermissionRequiredException(Auth::EDIT_PUBLICATION);
     }
     $id = Validator::sanitizeNumber($request->get('id'));
     if (!$id) {
         throw new UnexpectedValueException();
     }
     $author_model = new AuthorModel($this->db);
     $validator = $author_model->getValidator();
     if ($validator->validate($request->post())) {
         $data = $validator->getSanitizedResult();
         $author = new Author($data);
         $author_id = $author_model->store($author);
         // TODO: priority?
         try {
             $this->model->addAuthor($id, $author_id, 0);
             return true;
         } catch (DBDuplicateEntryException $e) {
             $this->errors[] = 'This author is already assigned to this publication';
             return false;
         }
     } else {
         $this->errors = array_merge($this->errors, $validator->getErrors());
         return false;
     }
 }
Пример #2
0
 /**
  *
  * @param array $input
  *
  * @return boolean
  * @throws DBDuplicateEntryException
  * @throws Exception
  */
 private function store_publication($input)
 {
     if (empty($input['type'])) {
         $this->errors[] = 'Publication type required';
         return false;
     }
     $publication_model = new PublicationModel($this->db);
     $validator = $publication_model->getValidator($input['type']);
     if ($validator->validate($input)) {
         $data = $validator->getSanitizedResult();
         $publication = new Publication($data);
     } else {
         $this->errors = array_merge($this->errors, $validator->getErrors());
         return false;
     }
     $authors = array();
     $author_model = new AuthorModel($this->db);
     if (!empty($input['authors'])) {
         $validator = $author_model->getValidator();
         foreach ($input['authors'] as $input_author) {
             if ($validator->validate($input_author)) {
                 $data = $validator->getSanitizedResult();
                 $authors[] = new Author($data);
             } else {
                 $this->errors = array_merge($this->errors, $validator->getErrors());
             }
         }
     }
     if (empty($authors)) {
         $this->errors[] = 'At least one author is required';
     }
     $citations = array();
     if (!empty($input['citations'])) {
         $citations = $this->getPublicationIdsFromTitle($input['citations']);
     }
     $keywords = array();
     $keyword_model = new KeywordModel($this->db);
     if (!empty($input['keywords'])) {
         $validator = $keyword_model->getValidator();
         foreach ($input['keywords'] as $input_keyword) {
             if ($validator->validate(array('name' => $input_keyword))) {
                 $data = $validator->getSanitizedResult();
                 $keywords[] = new Keyword($data);
             } else {
                 $this->errors = array_merge($this->errors, $validator->getErrors());
             }
         }
     }
     $url_model = new UrlModel($this->db);
     if (!empty($input['url'])) {
         $validator = $url_model->getValidator();
         $url_array = array('name' => 'External', 'url' => $input['url']);
         if ($validator->validate($url_array)) {
             $url_data = $validator->getSanitizedResult();
             $url = new Url($url_data);
         } else {
             $this->errors = array_merge($this->errors, $validator->getErrors());
         }
     }
     if (empty($this->errors) && isset($publication)) {
         //$this->db->beginTransaction(); TODO there is a deadlock when enabling transactions
         try {
             $publication_id = $publication_model->store($publication);
             $priority = 1;
             foreach ($authors as $author) {
                 $author_id = $author_model->store($author);
                 $publication_model->addAuthor($publication_id, $author_id, $priority);
                 $priority++;
             }
             foreach ($citations as $citation_id) {
                 $publication_model->addCitation($publication_id, $citation_id);
             }
             foreach ($keywords as $keyword) {
                 $keyword_id = $keyword_model->store($keyword);
                 $publication_model->addKeyword($publication_id, $keyword_id);
             }
             if (!empty($url)) {
                 $url_model->store($url, $publication_id);
             }
             //$this->db->commitTransaction();
             return true;
         } catch (DBDuplicateEntryException $e) {
             throw $e;
         } catch (Exception $e) {
             //$this->db->cancelTransaction();
             throw $e;
         }
     }
     return false;
 }