/**
  * Internal function to return a article purpose object from a row.
  * @param $row array
  * @return ArticlePurpose object
  */
 function &_returnArticlePurposeFromRow(&$row)
 {
     $articlePurpose = new ArticlePurpose();
     $articlePurpose->setId($row['article_purpose_id']);
     $articlePurpose->setArticleId($row['article_id']);
     $articlePurpose->setType($row['type']);
     $articlePurpose->setCTPhase($row['ct_phase']);
     $articlePurpose->setAllocation($row['allocation']);
     $articlePurpose->setMasking($row['masking']);
     $articlePurpose->setControl($row['control']);
     $articlePurpose->setAssignment($row['assignment']);
     $articlePurpose->setEndpoint($row['endpoint']);
     HookRegistry::call('ArticlePurposeDAO::_returnArticlePurposeFromRow', array(&$articlePurpose, &$row));
     return $articlePurpose;
 }
 /**
  * Save changes to article.
  * @param $request Request
  * @return int the article ID
  */
 function execute(&$request)
 {
     $articleDao =& DAORegistry::getDAO('ArticleDAO');
     $article =& $this->article;
     ///////////////////////
     ///// UPDATE TEXTS/////
     ///////////////////////
     $journal = Request::getJournal();
     $articleTexts = $this->getData('articleTexts');
     foreach ($journal->getSupportedLocaleNames() as $localeKey => $localeValue) {
         if ($articleTexts[$localeKey]['articleTextId'] > 0) {
             $articleText = $article->getArticleTextByLocale($localeKey);
             $isExistingArticleText = true;
         } else {
             $articleText = new ArticleText();
             $isExistingArticleText = false;
         }
         if ($articleText != null) {
             $articleText->setArticleId($article->getId());
             $articleText->setLocale($localeKey);
             $articleText->setScientificTitle($articleTexts[$localeKey]['scientificTitle']);
             $articleText->setPublicTitle($articleTexts[$localeKey]['publicTitle']);
             $articleText->setDescription($articleTexts[$localeKey]['description']);
             $articleText->setKeyInclusionCriteria($articleTexts[$localeKey]['keyInclusionCriteria']);
             $articleText->setKeyExclusionCriteria($articleTexts[$localeKey]['keyExclusionCriteria']);
             $articleText->setRecruitmentInfo($articleTexts[$localeKey]['recruitmentInfo']);
             if ($isExistingArticleText == false) {
                 $article->addArticleText($articleText);
             }
         }
         unset($articleText);
     }
     ///////////////////////////////////////////
     /////////// Update secondary IDs //////////
     ///////////////////////////////////////////
     $secIds = $article->getArticleSecIds();
     $secIdsData = $this->getData('articleSecIds');
     //Remove secondary IDs
     foreach ($secIds as $secId) {
         $isPresent = false;
         foreach ($secIdsData as $secIdData) {
             if (!empty($secIdData['articleSecIdId'])) {
                 if ($secId->getId() == $secIdData['articleSecIdId']) {
                     $isPresent = true;
                 }
             }
         }
         if (!$isPresent) {
             $article->removeArticleSecId($secId->getId());
         }
         unset($secId);
     }
     foreach ($secIdsData as $secIdData) {
         if (!empty($secIdData['type']) && !empty($secIdData['id'])) {
             if (!empty($secIdData['articleSecIdId'])) {
                 // Update an existing sec id
                 $secId =& $article->getArticleSecId($secIdData['articleSecIdId']);
             } else {
                 // Create a new sec id
                 $secId = new ArticleSecId();
             }
             if ($secId != null) {
                 $secId->setArticleId($article->getId());
                 $secId->setType($secIdData['type']);
                 $secId->setSecId($secIdData['id']);
                 $article->addArticleSecId($secId);
             }
             unset($secId);
         }
     }
     ///////////////////////////////////////////
     ////////// Update Article Details /////////
     ///////////////////////////////////////////
     $articlelDetailsData = $this->getData('articleDetails');
     if ($article->getArticleDetails()) {
         $articleDetails = $article->getArticleDetails();
     } else {
         $articleDetails = new ArticleDetails();
     }
     $articleDetails->setArticleId($article->getId());
     $articleDetails->setProtocolVersion($articlelDetailsData['protocolVersion']);
     $articleDetails->setTherapeuticArea($articlelDetailsData['therapeuticArea'], $articlelDetailsData['otherTherapeuticArea']);
     $articleDetails->setHealthCondDiseaseFromArray($articlelDetailsData['healthConds']);
     $articleDetails->setMinAgeNum($articlelDetailsData['minAgeNum']);
     $articleDetails->setMinAgeUnit($articlelDetailsData['minAgeUnit']);
     $articleDetails->setMaxAgeNum($articlelDetailsData['maxAgeNum']);
     $articleDetails->setMaxAgeUnit($articlelDetailsData['maxAgeUnit']);
     $articleDetails->setSex($articlelDetailsData['sex']);
     $articleDetails->setHealthy($articlelDetailsData['healthy']);
     $articleDetails->setLocaleSampleSize($articlelDetailsData['localeSampleSize']);
     $articleDetails->setMultinational($articlelDetailsData['multinational']);
     $articleDetails->setIntSampleSizeFromArray($articlelDetailsData['intSampleSize']);
     $articleDetails->setStartDate($articlelDetailsData['startDate']);
     $articleDetails->setEndDate($articlelDetailsData['endDate']);
     $articleDetails->setRecruitmentStatus($articlelDetailsData['recruitStatus']);
     $articleDetails->setAdvertisingScheme($articlelDetailsData['adScheme']);
     $articleDetails->setCompensationPolicy($articlelDetailsData['compensationPolicy']);
     $article->setArticleDetails($articleDetails);
     ///////////////////////////////////////////
     ////////// Update Article Purposes ////////
     ///////////////////////////////////////////
     $purposesData = $this->getData('purposes');
     $purposes = $article->getArticlePurposes();
     //Remove deleted article purposes
     foreach ($purposes as $purpose) {
         $isPresent = false;
         foreach ($purposesData as $purposeData) {
             if (!empty($purposeData['id'])) {
                 if ($purpose->getId() == $purposeData['id']) {
                     $isPresent = true;
                 }
             }
         }
         if (!$isPresent) {
             $article->removeArticlePurpose($purpose->getId());
         }
         unset($purpose);
     }
     foreach ($purposesData as $purposeData) {
         if (!empty($purposeData['interventional'])) {
             if (!empty($purposeData['id'])) {
                 // Update an existing purpose
                 $purpose =& $article->getArticlePurpose($purposeData['id']);
             } else {
                 // Create a new article purpose
                 $purpose = new ArticlePurpose();
             }
             if ($purpose != null) {
                 $purpose->setArticleId($article->getId());
                 if ($purposeData['interventional'] == ARTICLE_PURPOSE_TYPE_OBS) {
                     $purpose->setType(ARTICLE_PURPOSE_TYPE_OBS);
                 } else {
                     $purpose->setType($purposeData['type']);
                 }
                 $purpose->setCTPhase($purposeData['ctPhase']);
                 $purpose->setAllocation($purposeData['allocation']);
                 $purpose->setMasking($purposeData['masking']);
                 $purpose->setControl($purposeData['control']);
                 $purpose->setAssignment($purposeData['assignment']);
                 $purpose->setEndpoint($purposeData['endpoint']);
                 $article->addArticlePurpose($purpose);
             }
             unset($purpose);
         }
     }
     ///////////////////////////////////////////
     ////////// Update Article Outcomes ////////
     ///////////////////////////////////////////
     $primaryOutcomesData = $this->getData('primaryOutcomes');
     $secondaryOutcomesData = $this->getData('secondaryOutcomes');
     $articlelOutcomes = $article->getArticleOutcomes();
     //Remove outcomes
     foreach ($articlelOutcomes as $articlelOutcome) {
         $isPresent = false;
         foreach ($primaryOutcomesData as $primaryOutcomeData) {
             foreach ($journal->getSupportedLocaleNames() as $localeKey => $localeValue) {
                 if (!empty($primaryOutcomeData[$localeKey]['primaryOutcomeId'])) {
                     if ($articlelOutcome[$localeKey]->getId() == $primaryOutcomeData[$localeKey]['primaryOutcomeId']) {
                         $isPresent = true;
                     }
                 }
             }
         }
         foreach ($secondaryOutcomesData as $secondaryOutcomeData) {
             foreach ($journal->getSupportedLocaleNames() as $localeKey => $localeValue) {
                 if (!empty($secondaryOutcomeData[$localeKey]['secondaryOutcomeId'])) {
                     if ($articlelOutcome[$localeKey]->getId() == $secondaryOutcomeData[$localeKey]['secondaryOutcomeId']) {
                         $isPresent = true;
                     }
                 }
             }
         }
         if (!$isPresent) {
             foreach ($journal->getSupportedLocaleNames() as $localeKey => $localeValue) {
                 if (!empty($articlelOutcome[$localeKey])) {
                     $article->removeArticleOutcome($articlelOutcome[$localeKey]->getId());
                 }
             }
         }
         unset($articlelOutcome);
     }
     foreach ($primaryOutcomesData as $poKey => $primaryOutcomeData) {
         foreach ($journal->getSupportedLocaleNames() as $localeKey => $localeValue) {
             if (!empty($primaryOutcomeData[$localeKey]['name']) && !empty($primaryOutcomeData[$localeKey]['measurement']) && !empty($primaryOutcomeData[$localeKey]['timepoint'])) {
                 if (!empty($primaryOutcomeData[$localeKey]['primaryOutcomeId'])) {
                     $primaryOutcome = $article->getArticleOutcome($primaryOutcomeData[$localeKey]['primaryOutcomeId']);
                 } else {
                     $primaryOutcome = new ArticleOutcome();
                 }
                 if ($primaryOutcome != null) {
                     $primaryOutcome->setArticleId($article->getId());
                     $primaryOutcome->setName($primaryOutcomeData[$localeKey]['name']);
                     $primaryOutcome->setLocale($localeKey);
                     $primaryOutcome->setType(ARTICLE_OUTCOME_PRIMARY);
                     $primaryOutcome->setMeasurement($primaryOutcomeData[$localeKey]['measurement']);
                     $primaryOutcome->setTimepoint($primaryOutcomeData[$localeKey]['timepoint']);
                     $article->addArticleOutcome($primaryOutcome);
                 }
                 unset($primaryOutcome);
             }
         }
     }
     foreach ($secondaryOutcomesData as $poKey => $secondaryOutcomeData) {
         foreach ($journal->getSupportedLocaleNames() as $localeKey => $localeValue) {
             if (!empty($secondaryOutcomeData[$localeKey]['name']) && !empty($secondaryOutcomeData[$localeKey]['measurement']) && !empty($secondaryOutcomeData[$localeKey]['timepoint'])) {
                 if (!empty($secondaryOutcomeData[$localeKey]['secondaryOutcomeId'])) {
                     $secondaryOutcome = $article->getArticleOutcome($secondaryOutcomeData[$localeKey]['secondaryOutcomeId']);
                 } else {
                     $secondaryOutcome = new ArticleOutcome();
                 }
                 if ($secondaryOutcome != null) {
                     $secondaryOutcome->setArticleId($article->getId());
                     $secondaryOutcome->setName($secondaryOutcomeData[$localeKey]['name']);
                     $secondaryOutcome->setLocale($localeKey);
                     $secondaryOutcome->setType(ARTICLE_OUTCOME_SECONDARY);
                     $secondaryOutcome->setMeasurement($secondaryOutcomeData[$localeKey]['measurement']);
                     $secondaryOutcome->setTimepoint($secondaryOutcomeData[$localeKey]['timepoint']);
                     $article->addArticleOutcome($secondaryOutcome);
                 }
                 unset($secondaryOutcome);
             }
         }
     }
     //update step
     if ($article->getSubmissionProgress() <= $this->step) {
         $article->stampStatusModified();
         $article->setSubmissionProgress($this->step + 1);
     } elseif ($article->getSubmissionProgress() == 9) {
         $article->setSubmissionProgress(8);
     }
     parent::execute();
     // Save the article
     $articleDao->updateArticle($article);
     return $this->articleId;
 }