public function save()
 {
     /*saves the data in the db*/
     $newMainAccount = new MainAccount();
     $newMainAccount->attributes = $this->attributes;
     if (!$newMainAccount->save()) {
         throw new Exception("Sorry cant save new Main account . Reason : " . CHtml::errorSummary($newMainAccount));
     }
     return $newMainAccount;
 }
 public function actionCheckMainAccounts()
 {
     header("Content-Type: application/json");
     $criteria = new CDbCriteria();
     $criteria->compare("status", MainAccount::MAIN_ACCT_STATUS_INACTIVE);
     $allMainAccounts = MainAccount::model()->findAll($criteria);
     $checker = new MainAccountChecker();
     foreach ($allMainAccounts as $key => $currentMainAccount) {
         if ($checker->isActive($currentMainAccount)) {
             $currentMainAccount->status = MainAccount::MAIN_ACCT_STATUS_ACTIVE;
             $currentMainAccount->save();
             //update status
         }
     }
     echo json_encode(array("status" => "ok", "message" => "ok"));
 }
 public function actionExportAll()
 {
     $fileName = 'All Account credentias-' . date("Y-m-d");
     header("Pragma: public");
     header("Expires: 0");
     header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
     header("Cache-Control: private", false);
     header("Content-Type: application/octet-stream");
     header("Content-Disposition: attachment; filename=\"{$fileName}.csv\";");
     header("Content-Transfer-Encoding: binary");
     $csvFile = sys_get_temp_dir() . '/' . uniqid() . '.csv';
     $csvFileObj = fopen($csvFile, "w+");
     /*prepare CSV File*/
     /*find all main accounts*/
     $allMainAccount = MainAccount::model()->findAll();
     fputcsv($csvFileObj, array("Main Account", "Sub Account", "Password"));
     foreach ($allMainAccount as $key => $currentMainAccount) {
         /*find all sub accounts*/
         if (count($currentMainAccount->subAccounts) === 0) {
             fputcsv($csvFileObj, array($currentMainAccount->username, '', $currentMainAccount->password));
         }
         foreach ($currentMainAccount->subAccounts as $key => $currentSubAccountModel) {
             /*write to csv*/
             fputcsv($csvFileObj, array($currentMainAccount->username, $currentSubAccountModel->username, $currentSubAccountModel->password));
         }
     }
     fclose($csvFileObj);
     echo file_get_contents($csvFile);
     /*download file*/
 }