コード例 #1
0
 /**
  * Import a language
  * 
  */
 public static function importLanguage($filename, $showMessage = true, $fastImport = false)
 {
     $localErrors = array();
     $allAccounts = array();
     $dom = new domDocument();
     if (!$dom->load($filename)) {
         if ($showMessage) {
             throw new CException(Yii::t('lazy8', 'input file could not be xml parsed'));
         } else {
             throw new CException('input file could not be xml parsed');
         }
     }
     $root = $dom->documentElement;
     if ($root->nodeName != "lazy8webportlang") {
         if ($showMessage) {
             $localErrors = array(array(Yii::t('lazy8', 'Upload failed.  This is not a valid file.'), Yii::t('lazy8', 'Select a file and try again')));
         }
         $this->render('importlang');
         return $localErrors;
     }
     if ($root->getAttribute('version') > 1.0) {
         if ($showMessage) {
             $localErrors = array(array(Yii::t('lazy8', 'There maybe problems because this is a file version greater then this programs version'), Yii::t('lazy8', 'Select a file and try again')));
         }
     }
     $nodeLanguages = $root->getElementsByTagName('language');
     unset($root);
     unset($dom);
     foreach ($nodeLanguages as $nodeLanguage) {
         //make sure the company code is unique
         //Message::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='".$nodeLanguage->getAttribute('langcode')."'")->execute();
         //make sure that all the source messages exist for this language
         if (!$fastImport) {
             $sources = SourceMessage::model()->findAll();
             foreach ($sources as $source) {
                 $foundMessage = Message::model()->find(array('condition' => "language='" . $nodeLanguage->getAttribute('langcode') . "' AND id=" . $source->id));
                 if ($foundMessage == null) {
                     $trans = new Message();
                     $trans->id = $source->id;
                     $trans->translation = $source->message;
                     $trans->language = $nodeLanguage->getAttribute('langcode');
                     $trans->save();
                 }
             }
         } else {
             //quickly delete all occurances of this language
             SourceMessage::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='{$nodeLanguage->getAttribute('langcode')}'")->execute();
         }
         //update the version information
         if ($nodeLanguage->getAttribute('version') != null) {
             $foundOption = Options::model()->find('name=:name AND userId=:id AND companyId=:compid', array(':name' => "Language.Version." . $nodeLanguage->getAttribute('langcode'), ':id' => 0, ':compid' => 0));
             if ($foundOption !== null) {
                 $foundOption->delete();
             }
             $createOption = new Options();
             $createOption->name = "Language.Version." . $nodeLanguage->getAttribute('langcode');
             $createOption->userId = 0;
             $createOption->companyId = 0;
             $createOption->datavalue = $nodeLanguage->getAttribute('version');
             $createOption->save();
         }
         //get all the messages
         $nodeMessages = $nodeLanguage->getElementsByTagName('message');
         foreach ($nodeMessages as $nodeMessage) {
             $sources = SourceMessage::model()->find(array('condition' => "category='" . $nodeMessage->getAttribute('category') . "' AND message='" . SourceMessageController::sqlCleanString(CHtml::encode($nodeMessage->getAttribute('key'))) . "'"));
             if ($sources == null) {
                 $newSource = new SourceMessage();
                 $newSource->message = CHtml::encode($nodeMessage->getAttribute('key'));
                 $newSource->category = $nodeMessage->getAttribute('category');
                 $maxId = 0;
                 $command = SourceMessage::model()->dbConnection->createCommand("SELECT MAX(id) as maxid FROM SourceMessage");
                 try {
                     $reader = $command->query();
                 } catch (Exception $e) {
                     echo '<h2>Died on Sql Maxid</h2>';
                     throw $e;
                 }
                 if ($reader !== null && count($reader) > 0) {
                     foreach ($reader as $row) {
                         $maxId = $row['maxid'] + 1;
                         break;
                         //there is only one row here anyway.
                     }
                 }
                 $newSource->id = $maxId;
                 if ($newSource->save()) {
                     $modelMessage = new Message();
                     $modelMessage->language = $nodeLanguage->getAttribute('langcode');
                     $modelMessage->id = $newSource->id;
                     $modelMessage->translation = SourceMessageController::getNodeText($nodeMessage, "translation");
                     $modelMessage->save();
                     if (!$fastImport) {
                         //now we must add to all of the other languages.
                         $langs = Message::model()->findAll(array('select' => 'distinct language'));
                         if (isset($langs) && count($langs) > 0) {
                             foreach ($langs as $lang) {
                                 if ($nodeLanguage->getAttribute('langcode') != $lang->language) {
                                     $newLangItem = new Message();
                                     $newLangItem->id = $newSource->id;
                                     $newLangItem->language = $lang->language;
                                     $newLangItem->translation = CHtml::encode($nodeMessage->getAttribute('key'));
                                     $newLangItem->save();
                                 }
                             }
                         }
                     }
                 }
             } else {
                 //make sure the key is unique
                 if (!$fastImport) {
                     Message::model()->dbConnection->createCommand("DELETE FROM Message WHERE language='" . $nodeLanguage->getAttribute('langcode') . "' AND id=" . $sources->id)->execute();
                 }
                 $modelMessage = new Message();
                 $modelMessage->language = $nodeLanguage->getAttribute('langcode');
                 $modelMessage->id = $sources->id;
                 $modelMessage->translation = SourceMessageController::getNodeText($nodeMessage, "translation");
                 //try{
                 if (!$modelMessage->save()) {
                     if ($showMessage) {
                         $localErrors[] = array(Yii::t('lazy8', 'Failed import of translation.') . ' = ' . $nodeLanguage->getAttribute('langcode') . ' ; ' . $nodeMessage->getAttribute('key'), Yii::t('lazy8', 'Select a file and try again'));
                     }
                 }
                 //}catch(Exception $e){
                 //	echo "error on langcode=" . $nodeLanguage->getAttribute('langcode') . " key=" . $nodeMessage->getAttribute('key');
                 //	die();
                 //}
             }
         }
     }
     return $localErrors;
 }
コード例 #2
0
 /**
  * Initializes the application.
  * This method overrides the parent implementation by preloading the 'request' component.
  */
 protected function init()
 {
     $isBadDatabase = false;
     if (isset($_GET) && isset($_GET['r']) && $_GET['r'] == 'site/baddatabase') {
         $isBadDatabase = true;
     }
     if (!$isBadDatabase && count($_GET) == 0 || isset($_GET['r']) && $_GET['r'] == 'site/index') {
         //just some tests to see if the database is working
         $connection = null;
         try {
             $connection = yii::app()->getDb();
             //new CDbConnection($dsn,$username,$password);
             // establish connection. You may try...catch possible exceptions
             $connection->active = true;
         } catch (Exception $zz) {
             $isBadDatabase = true;
         }
         if (!$isBadDatabase) {
             try {
                 $command = $connection->createCommand('select * from Company LIMIT 1');
                 $reader = $command->query();
             } catch (Exception $zz) {
                 //create the database
                 $fileread = fopen(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../controllers/lazy8web.sql', "r");
                 while (!feof($fileread)) {
                     $sqlCommand = "";
                     $line = trim(fgets($fileread));
                     $sqlCommand .= $line;
                     while (!feof($fileread) && substr($line, strlen($line) - 1, 1) != ";") {
                         $line = fgets($fileread);
                         $sqlCommand .= $line;
                     }
                     $command = $connection->createCommand($sqlCommand);
                     $command->execute();
                 }
                 fclose($fileread);
                 //need to give the database time to reset itself. Otherwise it cant find the tables.
                 yii::app()->getDb()->__sleep(false);
                 sleep(3);
                 yii::app()->getDb()->setActive(true);
             }
             $criteria = new CDbCriteria();
             $pages = new CPagination(10);
             $pages->pageSize = 2;
             $pages->applyLimit($criteria);
             $models = Message::model()->findAll($criteria);
             if (!isset($models) || count($models) == 0) {
                 include 'SourceMessageController.php';
                 include 'ReportController.php';
                 //the languages are not initialized yet
                 SourceMessageController::importLanguage(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../controllers/lazy8weblanguage.xml', false, true);
                 //load the reports as well
                 ReportController::importFile(dirname(__FILE__) . DIRECTORY_SEPARATOR . '../controllers/lazy8webreport.xml', false);
             }
             $models = User::model()->findAll();
             if (!isset($models) || count($models) == 0) {
                 //load an admin person.
                 $model = new User();
                 $model->username = '******';
                 $model->displayname = 'Admin!ChangeName!';
                 $model->salt = hash('sha1', uniqid(rand(), true));
                 $model->password = hash('sha1', 'admin' . $model->salt);
                 $model->confirmPassword = hash('sha1', 'admin' . $model->salt);
                 $model->save();
                 $optionsUser = User::optionsUserTemplate();
                 //preset with the default value for the user options
                 foreach ($optionsUser as $key => $option) {
                     $_POST['option_' . $key] = $option[1];
                 }
                 foreach ($optionsUser as $key => $option) {
                     if ($option[6] == 'true') {
                         $_POST['option_' . $key] = 1;
                     }
                 }
                 User::updateOptionTemplate(User::optionsUserTemplate(), $model->id, 0);
             }
         }
     }
     if (!$isBadDatabase) {
         $this->mainMenu = array('Data Entry' => array("label" => Yii::t("lazy8", "Data Entry"), "visible" => Yii::app()->user->getState('allowTrans') || Yii::app()->user->getState('allowAccount') || Yii::app()->user->getState('allowCustomer'), array("url" => array("route" => "trans"), "label" => Yii::t("lazy8", "Transactions"), "visible" => Yii::app()->user->getState('allowTrans')), array("url" => array("route" => "account"), "label" => Yii::t("lazy8", "Accounts"), "visible" => Yii::app()->user->getState('allowAccount')), array("url" => array("route" => "customer"), "label" => Yii::t("lazy8", "Tags"), "visible" => Yii::app()->user->getState('allowCustomer')), array("url" => array("route" => "donor"), "label" => Yii::t("lazy8", "Donors"), "visible" => Yii::app()->user->getState('allowDonor'))), 'Reports' => array("label" => Yii::t("lazy8", "Reports"), "visible" => Yii::app()->user->getState('allowReports'), "url" => array("route" => "report/report")), 'Company' => array("label" => Yii::t("lazy8", "Company"), "visible" => Yii::app()->user->getState('allowCompanyCreation') || Yii::app()->user->getState('allowPeriod') || Yii::app()->user->getState('allowAccountTypes') || Yii::app()->user->getState('allowExport') || Yii::app()->user->getState('allowImport'), array("url" => array("route" => "company"), "label" => Yii::t("lazy8", "Company"), "visible" => Yii::app()->user->getState('allowCompanyCreation')), array("url" => array("route" => "period"), "label" => Yii::t("lazy8", "Period"), "visible" => Yii::app()->user->getState('allowPeriod') && Yii::app()->user->getState('selectedCompanyId') != 0), array("url" => array("route" => "accountType"), "label" => Yii::t("lazy8", "Account types"), "visible" => Yii::app()->user->getState('allowAccountTypes') && Yii::app()->user->getState('selectedCompanyId') != 0), array("url" => array("route" => "company/export&id=" . Yii::app()->user->getState('selectedCompanyId')), "label" => Yii::t("lazy8", "Export this company"), "visible" => !Yii::app()->user->getState('allowCompanyCreation') && Yii::app()->user->getState('allowCompanyExport') != 0)), 'Setup' => array("label" => Yii::t("lazy8", "Setup"), "visible" => Yii::app()->user->getState('allowSelf') || Yii::app()->user->getState('allowAdmin'), array("url" => array("route" => "user/update&id=" . Yii::app()->user->id), "label" => Yii::t("lazy8", "Your profile"), "visible" => Yii::app()->user->getState('allowSelf') || Yii::app()->user->getState('allowAdmin')), array("url" => array("route" => "options"), "label" => Yii::t("lazy8", "Website"), "visible" => Yii::app()->user->getState('allowAdmin')), array("url" => array("route" => "user"), "label" => Yii::t("lazy8", "Users"), "visible" => Yii::app()->user->getState('allowAdmin')), array("url" => array("route" => "report"), "label" => Yii::t("lazy8", "Reports"), "visible" => Yii::app()->user->getState('allowAdmin')), array("url" => array("route" => "sourceMessage"), "label" => Yii::t("lazy8", "Translations"), "visible" => Yii::app()->user->getState('allowAdmin')), array("url" => array("route" => "changeLog"), "label" => Yii::t("lazy8", "Logs"), "visible" => Yii::app()->user->getState('allowChangeLog')), 'Reports Cron' => array("label" => Yii::t("lazy8", "Run Reports"), "visible" => Yii::app()->user->getState('allowAdmin'), 'disabled' => false, 'icon' => 'protected/images/skull.png', 'url' => array('route' => '', 'link' => '#', 'htmlOptions' => array('class' => 'menulink')), array('url' => array('route' => 'account/cronbalance', 'htmlOptions' => array('target' => '_BLANK', 'id' => 'balanceCheck', 'onClick' => 'return confirm(\'WARNING: Please Read Carefully. You are about to tun the daily Balance Check. This will automatically run once a day even if you run it now. Are you sure you would like to continue?\');')), "label" => Yii::t("lazy8", "Balance Check"), "visible" => Yii::app()->user->getState('allowAdmin')), array('url' => array('route' => 'report/cronreports', 'htmlOptions' => array('target' => '_BLANK', 'id' => 'weekltReport', 'onClick' => 'return confirm(\'WARNING: Please Read Carefully. You are about to run the Weekly Report. This will generate reports for all accounts and send emails to anyone who might be associatted with each account. Are you sure you would like to continue?\');')), "label" => Yii::t("lazy8", "Weekly Report"), "visible" => Yii::app()->user->getState('allowAdmin')))), 'Logout' => array("url" => array("route" => "/site/logout"), "label" => Yii::t("lazy8", "Logout")));
         //Load all the modules
         parent::init();
         $mods = yii::app()->modules;
         foreach ($mods as $key => $mod) {
             if (is_file(YiiBase::getPathOfAlias($mod['class']) . '.php')) {
                 $type = Yii::import($mod['class'], true);
                 $cc = new $type($key, $this);
                 $cc->RegisterEvents($this, $this->mainMenu);
             }
         }
     }
 }