/**
  * Update object for review.
  * @param $args array
  * @param $request PKPRequest
  */
 function updateObjectForReview($args, &$request)
 {
     $objectId = (int) $request->getUserVar('objectId');
     $reviewObjectTypeId = (int) $request->getUserVar('reviewObjectTypeId');
     $journal =& $request->getJournal();
     $journalId = $journal->getId();
     if ($objectId && !$this->_ensureObjectExists($objectId, $journalId, $reviewObjectTypeId)) {
         $request->redirect(null, 'editor', 'objectsForReview');
     }
     $ofrPlugin =& $this->_getObjectsForReviewPlugin();
     $ofrPlugin->import('classes.form.ObjectForReviewForm');
     $ofrForm = new ObjectForReviewForm($ofrPlugin->getName(), $objectId, $reviewObjectTypeId);
     $ofrForm->readInputData();
     // Add a role block
     if ($request->getUserVar('addPerson')) {
         $editData = true;
         $persons = $ofrForm->getData('persons');
         array_push($persons, array());
         $ofrForm->setData('persons', $persons);
         // Delete persons
     } else {
         if (($delPerson = $request->getUserVar('delPerson')) && count($delPerson) == 1) {
             $editData = true;
             list($delPerson) = array_keys($delPerson);
             $delPerson = (int) $delPerson;
             $persons = $ofrForm->getData('persons');
             if (isset($persons[$delPerson]['personId']) && !empty($persons[$delPerson]['personId'])) {
                 $deletedPersons = explode(':', $ofrForm->getData('deletedPersons'));
                 array_push($deletedPersons, $persons[$delPerson]['personId']);
                 $ofrForm->setData('deletedPersons', join(':', $deletedPersons));
             }
             array_splice($persons, $delPerson, 1);
             $ofrForm->setData('persons', $persons);
             // Change person order
         } else {
             if ($request->getUserVar('movePerson')) {
                 $editData = true;
                 $movePersonDir = $request->getUserVar('movePersonDir');
                 $movePersonDir = $movePersonDir == 'u' ? 'u' : 'd';
                 $movePersonIndex = (int) $request->getUserVar('movePersonIndex');
                 $persons = $ofrForm->getData('persons');
                 if (!($movePersonDir == 'u' && $movePersonIndex <= 0 || $movePersonDir == 'd' && $movePersonIndex >= count($persons) - 1)) {
                     $tmpPerson = $persons[$movePersonIndex];
                     if ($movePersonDir == 'u') {
                         $persons[$movePersonIndex] = $persons[$movePersonIndex - 1];
                         $persons[$movePersonIndex - 1] = $tmpPerson;
                     } else {
                         $persons[$movePersonIndex] = $persons[$movePersonIndex + 1];
                         $persons[$movePersonIndex + 1] = $tmpPerson;
                     }
                 }
                 $ofrForm->setData('persons', $persons);
             }
         }
     }
     if (!isset($editData) && $ofrForm->validate()) {
         $ofrForm->execute();
         // Notification
         if ($objectId) {
             $notificationType = NOTIFICATION_TYPE_OFR_UPDATED;
         } else {
             $notificationType = NOTIFICATION_TYPE_OFR_CREATED;
         }
         $this->_createTrivialNotification($notificationType, $request);
         if ($request->getUserVar('createAnother')) {
             $request->redirect(null, 'editor', 'createObjectForReview');
         } elseif ($request->getUserVar('addPerson') || $request->getUserVar('delPerson') || $request->getUserVar('movePerson')) {
             $request->redirect(null, 'editor', 'editObjectForReview', $objectId, array('reviewObjectTypeId' => $reviewObjectTypeId));
         } else {
             $request->redirect(null, 'editor', 'objectsForReview');
         }
     } else {
         $this->setupTemplate($request, true);
         $templateMgr =& TemplateManager::getManager($request);
         if ($objectId) {
             $templateMgr->assign('pageTitle', 'plugins.generic.objectsForReview.editor.edit');
         } else {
             $templateMgr->assign('pageTitle', 'plugins.generic.objectsForReview.editor.create');
         }
         $ofrForm->display($request);
     }
 }
 /**
  * Batch import from an ONIX XML export.
  * @param array $args
  * @param PKPRequest $request
  */
 function uploadONIXObjectForReview($args, &$request)
 {
     $user = $request->getUser();
     $journal =& $request->getJournal();
     $ofrOrgDao =& DAORegistry::getDAO('ObjectForReviewOrganizationDAO');
     $ofrPlugin =& $this->_getObjectsForReviewPlugin();
     $ofrPlugin->import('classes.form.ObjectForReviewForm');
     $reviewObjectTypeId = (int) $request->getUserVar('reviewObjectTypeId');
     import('classes.file.TemporaryFileManager');
     $temporaryFileManager = new TemporaryFileManager();
     $temporaryFile = $temporaryFileManager->handleUpload('onixFile', $user->getId());
     $filePath = $temporaryFile->getFilePath();
     $parser = new XMLParser();
     $doc =& $parser->parse($filePath);
     $multiple = $request->getUserVar('multiple');
     if ($doc) {
         // Determine if we have short or long tags.
         $productNodes = $doc->getChildByName('product');
         $shortTags = $productNodes ? true : false;
         for ($index = 0; $productNode = $doc->getChildByName($this->_getOnixTag('Product', $shortTags), $index); $index++) {
             $importData = array();
             if ($productNode) {
                 $publisherNode = $productNode->getChildByName($this->_getOnixTag('Publisher', $shortTags));
                 if ($publisherNode) {
                     $publisherNameNode = $publisherNode->getChildByName($this->_getOnixTag('PublisherName', $shortTags));
                     if ($publisherNameNode) {
                         $publisher = $publisherNameNode->getValue();
                         $organization =& $ofrOrgDao->getOrganizationByName(trim($publisher));
                         if ($organization) {
                             $importData['publisherId'] = $organization->getId();
                         }
                     }
                 }
                 $websiteNode = $publisherNode->getChildByName($this->_getOnixTag('Website', $shortTags));
                 if ($websiteNode) {
                     $websiteLinkNode = $websiteNode->getChildByName($this->_getOnixTag('WebsiteLink', $shortTags));
                     $websiteLink = $websiteLinkNode->getValue();
                     $importData['book_publisher_url'] = $websiteLink;
                 }
                 $titleNode = $productNode->getChildByName($this->_getOnixTag('Title', $shortTags));
                 if ($titleNode) {
                     $titleTextNode = $titleNode->getChildByName($this->_getOnixTag('TitleText', $shortTags));
                     $title = $titleTextNode->getValue();
                     $importData['title'] = $title;
                 }
                 $subTitleNode = $titleNode->getChildByName($this->_getOnixTag('Subtitle', $shortTags));
                 if ($subTitleNode) {
                     $subTitle = $subTitleNode->getValue();
                     $importData['shortTitle'] = $subTitle;
                 }
                 $seriesNode = $productNode->getChildByName($this->_getOnixTag('Series', $shortTags));
                 if ($seriesNode) {
                     $seriesTextNode = $seriesNode->getChildByName($this->_getOnixTag('TitleOfSeries', $shortTags));
                     $series = $seriesTextNode->getValue();
                     $importData['book_series'] = $series;
                 }
                 $languageNode = $productNode->getChildByName($this->_getOnixTag('Language', $shortTags));
                 if ($languageNode) {
                     $languageCodeNode = $languageNode->getChildByName($this->_getOnixTag('LanguageCode', $shortTags));
                     $language = $languageCodeNode->getValue();
                     $importData['language'] = substr($language, 0, 2);
                 } else {
                     $importData['language'] = 'en';
                 }
                 $pageNode = $productNode->getChildByName($this->_getOnixTag('NumberOfPages', $shortTags));
                 if ($pageNode) {
                     $pages = $pageNode->getValue();
                     $importData['book_pages_no'] = $pages;
                 }
                 // Abstract. Look for OtherText with
                 // sub element of TextTypeCode of '01' (main description)
                 $abstract = '';
                 for ($authorIndex = 0; $node = $productNode->getChildByName($this->_getOnixTag('OtherText', $shortTags), $authorIndex); $authorIndex++) {
                     $typeNode = $node->getChildByName($this->_getOnixTag('TextTypeCode', $shortTags));
                     if ($typeNode && $typeNode->getValue() == '01') {
                         $textNode = $node->getChildByName($this->_getOnixTag('Text', $shortTags));
                         if ($textNode) {
                             $abstract = strip_tags($textNode->getValue());
                         }
                         break;
                     }
                 }
                 $importData['abstract'] = $abstract;
                 // ISBN-13
                 for ($productIdentifierIndex = 0; $node = $productNode->getChildByName($this->_getOnixTag('ProductIdentifier', $shortTags), $productIdentifierIndex); $productIdentifierIndex++) {
                     $idTypeNode = $node->getChildByName($this->_getOnixTag('ProductIDType', $shortTags));
                     if ($idTypeNode && $idTypeNode->getValue() == '15') {
                         // ISBN-13
                         $textNode = $node->getChildByName($this->_getOnixTag('IDValue', $shortTags));
                         if ($textNode) {
                             $importData['book_isbn'] = $textNode->getValue();
                         }
                         break;
                     }
                 }
                 // Subjects
                 $importData['subjectKeywords'] = '';
                 $subjects = array();
                 for ($subjectIndex = 0; $node = $productNode->getChildByName($this->_getOnixTag('Subject', $shortTags), $subjectIndex); $subjectIndex++) {
                     $textNode = $node->getChildByName($this->_getOnixTag('SubjectHeadingText', $shortTags));
                     if ($textNode) {
                         $subjects[] = $textNode->getValue();
                     }
                 }
                 $importData['subjectKeywords'] = join(', ', $subjects);
                 $publicationDateNode = $productNode->getChildByName($this->_getOnixTag('PublicationDate', $shortTags));
                 if ($publicationDateNode) {
                     $publicationDate = $publicationDateNode->getValue();
                     $importData['date'] = $publicationDate;
                 }
                 // Contributors.
                 $persons = array();
                 for ($authorIndex = 0; $node = $productNode->getChildByName($this->_getOnixTag('Contributor', $shortTags), $authorIndex); $authorIndex++) {
                     $firstNameNode = $node->getChildByName($this->_getOnixTag('NamesBeforeKey', $shortTags));
                     if ($firstNameNode) {
                         $firstName = $firstNameNode->getValue();
                     }
                     $lastNameNode = $node->getChildByName($this->_getOnixTag('KeyNames', $shortTags));
                     if ($lastNameNode) {
                         $lastName = $lastNameNode->getValue();
                     }
                     $seqNode = $node->getChildByName($this->_getOnixTag('SequenceNumber', $shortTags));
                     if ($seqNode) {
                         $seq = $seqNode->getValue();
                     }
                     $contributorRoleNode = $node->getChildByName($this->_getOnixTag('ContributorRole', $shortTags));
                     $contributorRole = '';
                     if ($contributorRoleNode) {
                         switch ($contributorRoleNode->getValue()) {
                             case 'A01':
                                 $contributorRole = '1';
                                 break;
                             case 'B01':
                                 $contributorRole = '3';
                                 break;
                             case 'B09':
                                 $contributorRole = '4';
                                 break;
                             case 'B06':
                                 $contributorRole = '5';
                                 break;
                             default:
                                 $contributorRole = '2';
                                 // Contributor
                                 break;
                         }
                     }
                     $persons[] = array('personId' => '', 'role' => $contributorRole, 'firstName' => $firstName, 'middleName' => '', 'lastName' => $lastName, 'seq' => (int) $seq);
                     unset($node);
                 }
                 $importData['persons'] = $persons;
                 if (!$multiple) {
                     $temporaryFileManager->deleteFile($temporaryFile->getId(), $user->getId());
                     $this->editObjectForReview($args, &$request, $importData);
                     break;
                 } else {
                     // we are processing more than one Product.  Instaniate the form and let it
                     // handle the object creation.
                     $ofrForm = new ObjectForReviewForm($ofrPlugin->getName(), null, $reviewObjectTypeId, $importData);
                     $ofrForm->initData();
                     $ofrForm->execute();
                 }
             } else {
                 $request->redirect(null, 'editor', 'objectsForReview', 'onixError');
             }
         }
         $request->redirect(null, 'editor', 'objectsForReview');
     } else {
         // this deleteFile is only called if the document does not parse.
         $temporaryFileManager->deleteFile($temporaryFile->getId(), $user->getId());
         $request->redirect(null, 'editor', 'objectsForReview');
     }
 }