コード例 #1
0
 public function actionAdd($id = 0)
 {
     //	 !\Yii::app()->user->getProfile()->company->validate
     if (\Yii::app()->user->getProfile()->company->validate && !\ext\services\AccountingPayed::getInstance()->checkPayed()) {
         $this->redirect('/cabinet/tarif');
     }
     $AddressesForm = new AddressesForm();
     if (!empty($id)) {
         $Reis = Reis::model()->findByPk($id);
         if (!Yii::app()->user->checkAccess('admin') && ((Yii::app()->getUser()->getProfile()->modules->head != UserModules::DIRECTOR_COMPANY || Yii::app()->user->getProfile()->company_id != $Reis->user->company_id) && (Yii::app()->getUser()->getProfile()->modules->transport != '1' || $Reis->user_id != Yii::app()->user->id) || Yii::app()->getUser()->getProfile()->company->active == '0')) {
             throw new CHttpException(403);
         }
         $i = 1;
         $AddressesForm = $Reis->getAddressForm();
         $CostForm = $Reis->getCostForm();
         $ContentManager = null;
     } else {
         if (Yii::app()->user->getProfile()->content_manager == '0') {
             if (!Yii::app()->user->checkAccess('admin') && (Yii::app()->getUser()->getProfile()->modules->head != UserModules::DIRECTOR_COMPANY && Yii::app()->getUser()->getProfile()->modules->transport != '1' || Yii::app()->getUser()->getProfile()->company->active == '0')) {
                 throw new CHttpException(403);
             } elseif (!Yii::app()->getUser()->getProfile()->company->validate) {
                 $this->redirect('/complete');
             }
             $ContentManager = null;
         } else {
             $ContentManager = new ContentManager();
         }
         $Reis = new Reis();
         $Reis->when_type_id = 40;
         $Reis->when_from = date("Y-m-d");
         $Reis->when_to = date("Y-m-d", time() + 60 * 60 * 24 * 15);
         $CostForm = new CostForm();
     }
     $company_id = Yii::app()->getUser()->getProfile()->company_id;
     $Contacts = User::model()->findAllByAttributes(['company_id' => $company_id]);
     $contacts = [];
     foreach ($Contacts as $Contact) {
         $contacts[$Contact->id] = $Contact->first_name . ' ' . $Contact->last_name . ' (' . $Contact->contact_phone . ')';
     }
     $AddressesForm->setScenario('reis');
     // alternative validation
     if (!empty($_POST)) {
         if (!empty($_POST['Reis']['reis_id'])) {
             $Reis = Reis::model()->findByPk($_POST['Reis']['reis_id']);
         } else {
             $Reis = new Reis();
         }
         $postReis = $Reis->processComplexAttributes($_POST);
         $Reis->setAttributes($postReis['Reis'], false);
         $AddressesForm->setAttributes($_POST['AddressesForm'], false);
         if ($Reis->price_asc == '0') {
             $CostForm->setAttributes($_POST['CostForm'], false);
         }
         if (Yii::app()->user->getProfile()->content_manager == '1' && $id == 0) {
             $ContentManager->setAttributes($_POST['ContentManager'], false);
             $contentValid = $ContentManager->validate();
         } else {
             $contentValid = true;
         }
         $valid = $AddressesForm->validate();
         $valid = $Reis->validate() && $CostForm->validate() && $valid && $contentValid;
         if (!$valid) {
             Yii::import('bootstrap.widgets.TbActiveForm');
             if ($ContentManager) {
                 echo TbActiveForm::validate(array($Reis, $AddressesForm, $CostForm, $ContentManager));
             } else {
                 echo TbActiveForm::validate(array($Reis, $AddressesForm, $CostForm));
             }
             Yii::app()->end();
         } else {
             if (Yii::app()->user->getProfile()->content_manager == '1' && $id == 0) {
                 $Reis->user_id = User::createFakeUser($ContentManager);
                 $Reis->contact_id = $Reis->user_id;
             }
             $Reis->save();
             $Reis->setRelations($postReis['Reis']);
             $Reis->setAddress($AddressesForm);
             $Reis->setCost($CostForm);
             $Reis->autosearch();
             echo CJSON::encode(array('status' => '500', 'redirect' => '/transportaddsuccess/' . $Reis->reis_id));
             Yii::app()->end();
         }
     }
     // список своего транспорта
     $listAvtopark = null;
     if (Yii::app()->user->checkAccess('avtopark')) {
         $criteria = new CDbCriteria();
         $criteria->with = ['user' => ['joinType' => 'INNER JOIN', 'condition' => 'user.company_id = :company_id']];
         $criteria->params = [':company_id' => $company_id];
         $listAvtoparkTmp = Transport::model()->findAll($criteria);
         $arrAttr = ['transport_id', 'name'];
         // только 2 этих аттрибута, остальное подгрузить через ajax
         foreach ($listAvtoparkTmp as $vlist) {
             $arrTmp = [];
             foreach ($arrAttr as $vattr) {
                 $arrTmp[$vattr] = $vlist->attributes[$vattr];
             }
             $listAvtopark[] = $arrTmp;
         }
     }
     $this->render('add', ['Reis' => $Reis, 'addreses' => $AddressesForm, 'CostForm' => $CostForm, 'contacts' => $contacts, 'listAvtopark' => $listAvtopark, 'ContentManager' => $ContentManager]);
 }