From: <gem...@li...> - 2013-03-25 11:53:16
|
Revision: 1205 http://sourceforge.net/p/gemstracker/code/1205 Author: mennodekker Date: 2013-03-25 11:53:12 +0000 (Mon, 25 Mar 2013) Log Message: ----------- OpenRosa: Moved scan forms from controller to Source Modified Paths: -------------- trunk/library/classes/Gems/Default/DatabaseAction.php trunk/library/classes/Gems/Default/OpenrosaAction.php trunk/library/classes/Gems/Menu.php trunk/library/classes/OpenRosa/Tracker/Source/OpenRosa.php Modified: trunk/library/classes/Gems/Default/DatabaseAction.php =================================================================== --- trunk/library/classes/Gems/Default/DatabaseAction.php 2013-03-22 14:27:33 UTC (rev 1204) +++ trunk/library/classes/Gems/Default/DatabaseAction.php 2013-03-25 11:53:12 UTC (rev 1205) @@ -401,7 +401,7 @@ if ($translations) { $filedir = APPLICATION_PATH . '/languages'; if (! file_exists($filedir)) { - mkdir($filedir, 0777, true); + @mkdir($filedir, 0777, true); } $filename = $filedir . '/' . GEMS_PROJECT_NAME . 'DatabaseTranslations.php'; Modified: trunk/library/classes/Gems/Default/OpenrosaAction.php =================================================================== --- trunk/library/classes/Gems/Default/OpenrosaAction.php 2013-03-22 14:27:33 UTC (rev 1204) +++ trunk/library/classes/Gems/Default/OpenrosaAction.php 2013-03-25 11:53:12 UTC (rev 1205) @@ -269,43 +269,6 @@ } } } - - public function scanAction() - { - $model = $this->getModel(); - - //Perform a scan of the form directory, to update the database of forms - $eDir = dir($this->formDir); - - $formCnt = 0; - $addCnt = 0; - while (false !== ($filename = $eDir->read())) { - if (substr($filename, -4) == '.xml') { - $formCnt++; - $form = new OpenRosa_Tracker_Source_OpenRosa_Form($this->formDir . $filename); - $filter['gof_form_id'] = $form->getFormID(); - $filter['gof_form_version'] = $form->getFormVersion(); - $forms = $model->load($filter); - - if (!$forms) { - $newValues = array(); - $newValues['gof_id'] = null; - $newValues['gof_form_id'] = $form->getFormID(); - $newValues['gof_form_version'] = $form->getFormVersion(); - $newValues['gof_form_title'] = $form->getTitle(); - $newValues['gof_form_xml'] = $filename; - $newValues = $model->save($newValues); - MUtil_Echo::r($newValues, 'added form'); - $addCnt++; - } - } - } - - $cache = GemsEscort::getInstance()->cache; - $cache->clean(); - - $this->html[] = sprintf('Checked %s forms and added %s forms', $formCnt, $addCnt); - } public function scanresponsesAction() { Modified: trunk/library/classes/Gems/Menu.php =================================================================== --- trunk/library/classes/Gems/Menu.php 2013-03-22 14:27:33 UTC (rev 1204) +++ trunk/library/classes/Gems/Menu.php 2013-03-25 11:53:12 UTC (rev 1205) @@ -292,7 +292,6 @@ $parent = $this; } $page = $parent->addBrowsePage($label, 'pr.openrosa','openrosa'); - $page->addButtonOnly($this->_('Scan FormDefs'), 'pr.openrosa.scan', 'openrosa', 'scan'); $page->addButtonOnly($this->_('Scan Responses'), 'pr.openrosa.scan', 'openrosa', 'scanresponses'); $this->addPage(null, null, 'openrosa', 'submission'); $this->addPage(null, null, 'openrosa', 'formList'); //mind the capital L here Modified: trunk/library/classes/OpenRosa/Tracker/Source/OpenRosa.php =================================================================== --- trunk/library/classes/OpenRosa/Tracker/Source/OpenRosa.php 2013-03-22 14:27:33 UTC (rev 1204) +++ trunk/library/classes/OpenRosa/Tracker/Source/OpenRosa.php 2013-03-25 11:53:12 UTC (rev 1205) @@ -51,19 +51,64 @@ { /** * This holds the path to the location where the form definitions will be stored. - * Will be set on init to: GEMS_ROOT_DIR . '/var/uploads/openrosa/forms/'; * * @var string */ - public $formDir; + protected $formDir; + + /** + * This holds the path to the location where OpenRosa will store it's files. + * Will be set on init to: GEMS_ROOT_DIR . '/var/uploads/openrosa/'; + * + * @var string + */ + protected $baseDir; + + /** + * + * @var Gems_Loader + */ + protected $loader; + + /** + * + * @var Zend_Translate + */ + protected $translate; + public function __construct(array $sourceData, Zend_Db_Adapter_Abstract $gemsDb) { parent::__construct($sourceData, $gemsDb); - $this->formDir = GEMS_ROOT_DIR . '/var/uploads/openrosa/forms/'; + $this->baseDir = GEMS_ROOT_DIR . '/var/uploads/openrosa/'; + $this->formDir = $this->baseDir . 'forms/'; } - + /** + * Open the dir, suppressing possible errors and try to + * create when it does not exist + * + * @param type $directory + * @return Directory + */ + protected function _checkDir($directory) + { + $eDir = @dir($directory); + if (false == $eDir) { + // Dir does probably not exist + if (!is_dir($directory)) { + if (false === @mkdir($directory, 0777, true)) { + MUtil_Echo::pre(sprintf($this->translate->_('Directory %s not found and unable to create'), $directory), 'OpenRosa ERROR'); + } else { + $eDir = @dir($directory); + } + } + } + + return $eDir; + } + + /** * Returns the source surveyId for a given Gems survey Id * * @param type $surveyId @@ -73,7 +118,47 @@ { return $this->tracker->getSurvey($surveyId)->getSourceSurveyId(); } + + protected function _scanForms() + { + $messages = array(); + $formCnt = 0; + $addCnt = 0; + $eDir = $this->_checkDir($this->formDir); + + if ($eDir !== false) { + $model = $this->loader->getModels()->getOpenRosaFormModel(); + while (false !== ($filename = $eDir->read())) { + if (substr($filename, -4) == '.xml') { + $formCnt++; + $form = new OpenRosa_Tracker_Source_OpenRosa_Form($this->formDir . $filename); + $filter['gof_form_id'] = $form->getFormID(); + $filter['gof_form_version'] = $form->getFormVersion(); + $forms = $model->load($filter); + if (!$forms) { + $newValues = array(); + $newValues['gof_id'] = null; + $newValues['gof_form_id'] = $form->getFormID(); + $newValues['gof_form_version'] = $form->getFormVersion(); + $newValues['gof_form_title'] = $form->getTitle(); + $newValues['gof_form_xml'] = $filename; + $newValues = $model->save($newValues); + if (Gems_Tracker::$verbose) { + MUtil_Echo::r($newValues, 'added form'); + } + $addCnt++; + } + } + } + } + + $cache = GemsEscort::getInstance()->cache; + $cache->clean(); + + $messages[] = sprintf('Checked %s forms and added %s forms', $formCnt, $addCnt); + } + //put your code here public function checkSourceActive($userId) { @@ -247,6 +332,8 @@ public function synchronizeSurveys($userId) { + $messages = $this->_scanForms(); + // Surveys in LS $db = $this->getSourceDatabase(); @@ -258,8 +345,9 @@ if (!$openRosaSurveys) { //If no surveys present, just use an empty array as array_combine fails $openRosaSurveys = array(); + $openRosaSurveyIds = array(); } else { - $openRosaSurveyIds = array_combine(array_keys($openRosaSurveys), array_keys($openRosaSurveys)); + $openRosaSurveyIds = array_combine(array_keys($openRosaSurveys), array_keys($openRosaSurveys)); } // Surveys in Gems @@ -293,6 +381,8 @@ $survey->exists = false; $survey->saveSurvey($values, $userId); } + + return $messages; } public function updateConsent(Gems_Tracker_Token $token, $surveyId, $sourceSurveyId = null, $consentCode = null) This was sent by the SourceForge.net collaborative development platform, the world's largest Open Source development site. |