/**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'show' page.
  */
 public function actionWeboptions()
 {
     if (isset($_POST['options_posted'])) {
         User::updateOptionTemplate(User::optionsWebTemplate(), 0, 0);
         $this->redirect(Yii::app()->request->baseUrl);
     } else {
         $this->render('weboptions', array('weboptions' => Options::model()->findAll('companyId=0 AND userId=0', array())));
     }
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'show' page.
  */
 public function actionUpdate()
 {
     $model = $this->loadUser();
     if (isset($_POST['User'])) {
         $oldPassword = $model->password;
         $modelBeforeChange = $model->toString();
         $model->attributes = $_POST['User'];
         //salt must contain something
         if (!isset($model->salt) || strlen($model->salt) == 0) {
             $model->salt = hash('sha1', uniqid(rand(), true));
             //since the salt has changed, We must have a new password.  Force it if it is not changed
             if (strlen($model->password) == 0 && strlen($model->confirmPassword == 0)) {
                 //this forces the entry of a new password
                 unset($oldPassword);
             }
         }
         //only check the password if they have entered something. Blank password is ignored.
         if (strlen($model->password) == 0 && strlen($model->confirmPassword == 0)) {
             $model->password = $oldPassword;
             $model->confirmPassword = $oldPassword;
         } else {
             $model->password = hash('sha1', $model->password . $model->salt);
             $model->confirmPassword = hash('sha1', $model->confirmPassword . $model->salt);
         }
         if ($model->save()) {
             if (Yii::app()->user->getState('allowAdmin')) {
                 //remove all companies for this user
                 User::model()->dbConnection->createCommand("DELETE FROM CompanyUser WHERE userId={$model->id}")->execute();
                 $companies = array();
                 if (isset($_POST['companies'])) {
                     $companies = $_POST['companies'];
                 }
                 //must remove all options for unchosen companies.
                 $beforeCompanies = $model->companies;
                 foreach ($beforeCompanies as $beforeComp) {
                     $foundCompany = false;
                     foreach ($companies as $comp) {
                         if ($comp == $beforeComp->id) {
                             $foundCompany = true;
                             break;
                         }
                     }
                     if (!$foundCompany) {
                         //company no longer selected. Delete all options
                         User::model()->dbConnection->createCommand("INSERT INTO CompanyUser (userId, companyId) VALUES ({$model->id},{$beforeComp->id})")->execute();
                     }
                 }
                 $foundCurrentCompany = false;
                 $foundSelectedCompany = false;
                 //re-add the selected companies
                 foreach ($companies as $comp) {
                     User::model()->dbConnection->createCommand("INSERT INTO CompanyUser (userId, companyId) VALUES ({$model->id},{$comp})")->execute();
                     if ($model->selectedCompanyId == $comp) {
                         $foundCurrentCompany = true;
                     }
                     if ($_POST['companyForOptionsBeforeChange'] == $comp) {
                         $foundSelectedCompany = true;
                     }
                 }
                 if (!$foundCurrentCompany) {
                     //they have remove the currently selected company.  We must remove it as being selected
                     $model->selectedCompanyId = 0;
                     $model->selectedPeriodId = 0;
                     $model->confirmPassword = $model->password;
                     $model->save();
                     if (Yii::app()->user->id == $_GET['id']) {
                         $model->setStates();
                     }
                 }
             }
             //do the options
             $this->setOptionsByTemplate(false);
             //user options first
             User::updateOptionTemplate(User::optionsUserTemplate(), $model->id, 0, Yii::app()->user->getState('allowAdmin'));
             //company options, only if admin
             if (!isset($_POST['save']) && Yii::app()->user->getState('allowAdmin')) {
                 //we must set all companies to the template chosen
                 foreach ($companies as $comp) {
                     User::updateOptionTemplate(User::optionsCompanyUserTemplate(), $model->id, $comp);
                 }
             } elseif (isset($foundSelectedCompany) && $foundSelectedCompany && Yii::app()->user->getState('allowAdmin')) {
                 //no template chosen.  Just update the visible options for the selected company
                 $model->updateOptionTemplate($model->optionsCompanyUserTemplate(), $model->id, $_POST['companyForOptionsBeforeChange']);
             }
             $stringModel = $model->toString();
             if ($modelBeforeChange != $stringModel) {
                 ChangeLog::addLog('UPDATE', 'Account', 'BEFORE<br />' . $modelBeforeChange . '<br />AFTER<br />' . $stringModel);
             }
             //we need to reload this user to get all the updates in the companies.
             $this->_model = User::model()->findbyPk(isset($id) && $id !== null ? $id : $_GET['id']);
             $model = $this->_model;
             //				$this->redirect(array('update','id'=>$model->id));
         } else {
             unset($this->_model->password);
             unset($this->_model->confirmPassword);
         }
     } else {
         unset($this->_model->password);
         unset($this->_model->confirmPassword);
         //make sure the options are all created
         $this->_model->setStates();
     }
     $companyForOptions = null;
     $companyList = null;
     $options = null;
     $allCompanies = null;
     $selectedCompanies = null;
     if (Yii::app()->user->getState('allowAdmin') == 1 || Yii::app()->user->getState('allowAdmin') == 1) {
         $companiesUsers = $model->companies;
         $companyForOptions = null;
         if (isset($companiesUsers) && isset($companiesUsers[0])) {
             $companyForOptions = $companiesUsers[0]->id;
         }
         if (isset($_POST['companyForOptions'])) {
             $companyForOptions = $_POST['companyForOptions'];
         }
         $options = Options::model()->findAll('companyId=:comp AND userId=:id', array(':comp' => $companyForOptions, ':id' => $model->id));
         $companyList = CHtml::listData($companiesUsers, 'id', 'name');
         $usersModel = User::model()->findbyPk($_GET['id']);
         //put  all the user companies in an array
         $companiesUsers = $usersModel->companies;
         $selectedCompanies = array();
         if (isset($companiesUsers)) {
             foreach ($companiesUsers as $companyUser) {
                 $selectedCompanies[] = $companyUser->id;
             }
         }
         $allCompanies = Company::model()->findAll();
     }
     $model->password = "";
     $model->confirmPassword = "";
     $this->render('update', array('model' => $model, 'companyForOptions' => $companyForOptions, 'companies' => $companyList, 'weboptions' => $options, 'allCompanies' => $allCompanies, 'selectedCompanies' => $selectedCompanies));
 }
 /**
  * Updates a particular model.
  * If update is successful, the browser will be redirected to the 'show' page.
  */
 public function actionUpdate()
 {
     $model = $this->loadCompany();
     if (isset($_POST['Company'])) {
         $modelBeforeChange = $model->toString();
         $model->attributes = $_POST['Company'];
         if ($model->save()) {
             User::updateOptionTemplate(User::optionsCompanyTemplate(), 0, $model->id);
             $stringModel = $model->toString();
             if ($modelBeforeChange != $stringModel) {
                 ChangeLog::addLog('UPDATE', 'Company', 'BEFORE<br />' . $modelBeforeChange . '<br />AFTER<br />' . $stringModel);
             }
             $this->redirect(array('admin', 'id' => $model->id));
         }
     }
     User::setOptionStatesAndControlTable(false, false, Yii::app()->user, User::optionsCompanyTemplate(), $model->id, 0);
     $criteria = new CDbCriteria();
     $criteria->compare('companyId', $model->id);
     $criteria->compare('userId', 0);
     $options = Options::model()->findAll($criteria);
     $this->render('update', array('model' => $model, 'options' => $options));
 }
 /**
  * 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);
             }
         }
     }
 }