/**
  * Check (parse and lookup) a citation
  * @param $args array
  * @param $request PKPRequest
  */
 function checkCitation(&$args, &$request)
 {
     if ($request->isPost()) {
         // We update the citation with the user's manual settings
         $originalCitation =& $this->_saveCitation($args, $request);
         if (is_null($originalCitation)) {
             // Return an error
             $json = new JSON('false', '');
             return $json->getString();
         }
     } else {
         // We retrieve the citation unchanged from the database.
         $originalCitation =& $this->_getCitationFromArgs($args, true);
     }
     // Only parse the citation if it's not been parsed before.
     // Otherwise we risk to overwrite user changes.
     $filteredCitation =& $originalCitation;
     if (!is_null($filteredCitation) && $filteredCitation->getCitationState() < CITATION_PARSED) {
         // Parse the requested citation
         $filterCallback = array(&$this, '_instantiateParserFilters');
         $filteredCitation =& $this->_filterCitation($filteredCitation, $filterCallback, CITATION_PARSED, $citationForm);
     }
     // Always re-lookup the citation even if it's been looked-up
     // before. The user asked us to re-check so there's probably
     // additional manual information in the citation fields.
     if (!is_null($filteredCitation)) {
         // Lookup the requested citation
         $filterCallback = array(&$this, '_instantiateLookupFilters');
         $filteredCitation =& $this->_filterCitation($filteredCitation, $filterCallback, CITATION_LOOKED_UP, $citationForm);
     }
     $filterErrors = array();
     if (is_null($filteredCitation)) {
         // Re-display the original citation unchanged with an error message
         $filterErrors[] = array('editedCitation' => Locale::translate('submission.citations.form.filterError'));
         $filteredCitation =& $originalCitation;
         $unsavedChanges = false;
     } else {
         $unsavedChanges = true;
     }
     // Display the citation editor with the new (but yet unsaved) citation data
     import('controllers.grid.citation.form.CitationForm');
     $citationForm = new CitationForm($filteredCitation, $unsavedChanges);
     // Add errors (if any)
     foreach ($filterErrors as $errorField => $errorMessage) {
         $citationForm->addError($errorField, $errorMessage);
     }
     // FIXME: modal() and ajaxAction() currently handle responses differently.
     // modal() should expect JSON messages also.
     $citationForm->initData();
     $renderedForm = $citationForm->fetch($request);
     if ($request->isPost()) {
         // This is a request initiated by ajaxAction()
         $json = new JSON('true', $renderedForm);
         return $json->getString();
     } else {
         // This is a request initiated by modal()
         return $renderedForm;
     }
 }
 /**
  * Internal method that re-checks the given citation and
  * returns a rendered citation editing form with the changes.
  * @param $request PKPRequest
  * @param $originalCitation Citation
  * @param $persist boolean whether to save (true) or render (false)
  * @return string|Citation a serialized JSON message with the citation
  *  form when $persist is false, else the persisted citation object.
  */
 function _recheckCitation(&$request, &$originalCitation, $persist = true)
 {
     // Extract filters to be applied from request
     $requestedFilters = $request->getUserVar('citationFilters');
     $filterIds = array();
     if (is_array($requestedFilters)) {
         foreach ($requestedFilters as $filterId => $value) {
             $filterIds[] = (int) $filterId;
         }
     }
     // Do the actual filtering of the citation.
     $citationDao =& DAORegistry::getDAO('CitationDAO');
     $filteredCitation =& $citationDao->checkCitation($request, $originalCitation, $filterIds);
     // Crate a new form for the filtered (but yet unsaved) citation data
     import('lib.pkp.classes.controllers.grid.citation.form.CitationForm');
     $citationForm = new CitationForm($request, $filteredCitation, $this->getAssocObject());
     // Transport filtering errors to form (if any).
     foreach ($filteredCitation->getErrors() as $index => $errorMessage) {
         $citationForm->addError('rawCitation[' . $index . ']', $errorMessage);
     }
     if ($persist) {
         // Persist the checked citation.
         $citationDao->updateObject($filteredCitation);
         // Return the persisted citation.
         return $filteredCitation;
     } else {
         // Only persist intermediate results.
         $citationDao->updateCitationSourceDescriptions($filteredCitation);
         // Mark the citation form "dirty".
         $citationForm->setUnsavedChanges(true);
         // Return the rendered form.
         $citationForm->initData();
         $json = new JSONMessage(true, $citationForm->fetch($request));
         return $json->getString();
     }
 }
 /**
  * Identify the requested citation, call the given callback to filter
  * it, persist it and re-display it in the citation form.
  * @param $request PKPRequest
  * @param $args array
  * @param $filterCallback callable
  * @param $citationStateAfterFiltering integer the state the citation will
  *  be set to after the filter was executed.
  * @return string
  */
 function &_filterCitation(&$request, $args, &$filterCallback, $citationStateAfterFiltering)
 {
     // Identify the citation to be filtered
     $citation =& $this->_getCitationFromArgs($args);
     // Make sure that the citation implements the
     // meta-data schema. (We currently only support
     // NLM citation.)
     $supportedMetadataSchemas =& $citation->getSupportedMetadataSchemas();
     assert(count($supportedMetadataSchemas) == 1);
     $metadataSchema =& $supportedMetadataSchemas[0];
     assert(is_a($metadataSchema, 'NlmCitationSchema'));
     // Extract the meta-data description from the citation
     $metadataDescription =& $citation->extractMetadata($metadataSchema);
     // Let the callback build the filter network
     $filterList = call_user_func($filterCallback, $citation, $metadataDescription);
     // The last entry in the filter list is the
     // input data for the returned filters.
     $muxInputData =& array_pop($filterList);
     // Initialize the sample demux input data array.
     $sampleDemuxInputData = array();
     // Instantiate the citation multiplexer filter
     import('filter.GenericMultiplexerFilter');
     $citationMultiplexer = new GenericMultiplexerFilter();
     $nullVar = null;
     foreach ($filterList as $citationFilter) {
         if ($citationFilter->supports($muxInputData, $nullVar)) {
             $citationMultiplexer->addFilter($citationFilter);
             unset($citationFilter);
             // We expect one citation description per filter
             // in the multiplexer result.
             $sampleDemuxInputData[] =& $metadataDescription;
         }
     }
     // Instantiate the citation de-multiplexer filter
     import('citation.NlmCitationDemultiplexerFilter');
     $citationDemultiplexer = new NlmCitationDemultiplexerFilter();
     $citationDemultiplexer->setOriginalCitation($citation);
     // Combine multiplexer and de-multiplexer to form the
     // final citation filter network.
     import('filter.GenericSequencerFilter');
     $citationFilterNet = new GenericSequencerFilter();
     $citationFilterNet->addFilter($citationMultiplexer, $muxInputData);
     $citationFilterNet->addFilter($citationDemultiplexer, $sampleDemuxInputData);
     // Send the input through the citation filter network.
     $filterErrors = array();
     $filteredCitation =& $citationFilterNet->execute($muxInputData);
     if (is_null($filteredCitation)) {
         // Re-display the current citation unchanged with an error message
         $filterErrors = array('editedCitation' => Locale::translate('submission.citations.form.filterError'));
         $filteredCitation =& $citation;
     } else {
         // Copy unfiltered data from the original citation to the filtered citation
         $article =& $this->getArticle();
         $filteredCitation->setId($citation->getId());
         $filteredCitation->setAssocId($article->getId());
         $filteredCitation->setAssocType(ASSOC_TYPE_ARTICLE);
         $filteredCitation->setRawCitation($citation->getRawCitation());
         $filteredCitation->setEditedCitation($citation->getEditedCitation());
         // Set the citation state
         $filteredCitation->setCitationState($citationStateAfterFiltering);
         // Persist the filtered citation
         $citationDAO =& DAORegistry::getDAO('CitationDAO');
         $citationDAO->updateCitation($filteredCitation);
     }
     // Re-display the form
     import('controllers.grid.citation.form.CitationForm');
     $citationForm = new CitationForm($filteredCitation);
     foreach ($filterErrors as $field => $message) {
         $citationForm->addError($field, $message);
     }
     $citationForm->initData();
     $citationForm->display($request);
     // The form has already been displayed.
     $emptyString = '';
     return $emptyString;
 }