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