/**
  * 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();
     }
 }
 /**
  * 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;
     }
 }