Example #1
0
 /**
  * Lists all models.
  */
 public function actionIndex()
 {
     /*
     $model=new Option('search');
     $model->unsetAttributes();  // clear any default values
     if(isset($_GET['Option']))
     	$model->attributes=$_GET['Option'];
     */
     $model = new OptionForm();
     if (isset($_POST['OptionForm'])) {
         //$model->attributes=$_POST['OptionForm'];
         if ($model->save($_POST['OptionForm'])) {
             Yii::app()->user->setFlash('success', '更新成功');
             $this->redirect(array('index'));
         }
     }
     //var_dump($model->options);
     $this->render('index', array('model' => $model));
 }
 /**
  *  Добавление товара
  * @param int $id
  * @throws CHttpException
  * @throws \CException
  */
 public function actionStoreadd($id = 0)
 {
     \Yii::import('application.modules.store.models.*');
     \Yii::import('application.modules.cabinet.*');
     $photos = array();
     $values = array();
     $request = Yii::app()->request->pathInfo;
     $type = preg_match('/(service)/', $request);
     $StoreCategory = new \StoreCategory();
     $ProductaForm = new \ProductaForm();
     $Address = new \Address();
     if ($id != 0) {
         $Product = \Product::model()->findByPk($id);
         if (!Yii::app()->user->checkAccess('admin') && ((Yii::app()->getUser()->getProfile()->modules->head != \UserModules::DIRECTOR_COMPANY || Yii::app()->user->getProfile()->company_id != $Product->user->company_id) && (Yii::app()->getUser()->getProfile()->modules->product != '1' || $Product->user_id != Yii::app()->user->id) || Yii::app()->getUser()->getProfile()->company->active == '0')) {
             throw new CHttpException(403);
         }
         $StoreCategory = \StoreCategory::model()->findByPk($Product->category_id);
         $Address->name = $Product->address_name;
         $Address->lat = $Product->address_lat;
         $Address->long = $Product->address_long;
         $OptionForm = $Product->getOptionForm();
         $ProductaForm->SetCategoryId($Product->category_id);
         $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->product != '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();
         }
         $Product = new \Product();
         $Product->payment_id = 2;
         $Product->price = '';
         $StoreCategory = new \StoreCategory();
         $StoreCategory->slug = 'tmp';
         $OptionForm = new \OptionForm();
     }
     $Product->type_id = 1;
     $Product->tiu_id = 0;
     if (\Yii::app()->getRequest()->getPost('call')) {
         $Product->scenario = 'call';
     }
     if (\Yii::app()->getRequest()->getPost('Product')) {
         $photos = \Yii::app()->getRequest()->getPost('photos');
         $attributes = \Yii::app()->getRequest()->getPost('Product');
         if (Yii::app()->user->getProfile()->content_manager == '1' && $id == 0) {
             $ContentManager->setAttributes($_POST['ContentManager'], false);
             $contentValid = $ContentManager->validate();
         } else {
             $contentValid = true;
         }
         $OptionForm->setAttributes($_POST['OptionForm'], false);
         $valid = $OptionForm->validate();
         $Address->setAttributes($_POST['Address'], false);
         $Product->address_name = $Address->name;
         $Product->address_lat = $Address->lat;
         $Product->address_long = $Address->long;
         if (isset($attributes['category_id'])) {
             $StoreCategory = \StoreCategory::model()->findByPk($attributes['category_id']);
             $ProductaForm->SetCategoryId($attributes['category_id']);
             if (isset($StoreCategory)) {
                 $type = \Type::model()->findByAttributes(['name' => $StoreCategory->slug]);
                 $attributes['type_id'] = $type->id;
             } else {
                 $StoreCategory = new \StoreCategory();
                 $StoreCategory->addErrors(['name' => Yii::t('default', "Необходимо заполнить поле «{title}».", array('{title}' => $StoreCategory->attributeLabels()['name']))]);
             }
         }
         if ($ProductaForm->validate()) {
             $Product = $Product->saveData($attributes, array(), array(), $photos);
         } else {
             $contentValid = false;
         }
         if (empty($_POST['Address']['name']) || empty($_POST['Address']['lat']) && empty($_POST['Address']['long'])) {
             if ($type) {
                 $Address->addError('name', Yii::t('default', "Необходимо заполнить поле «Адрес оказания услуги»."));
             } else {
                 $Address->addError('name', Yii::t('default', "Необходимо заполнить поле «Адрес товара»."));
             }
         }
         if (!$Product->hasErrors() && $contentValid == true) {
             if (Yii::app()->user->getProfile()->content_manager == '1' && $id == 0) {
                 $user_id = \User::createFakeUser($ContentManager);
                 Yii::app()->db->createCommand()->update('tbl_user', array('user_id' => $user_id), 'id=:id', array(':id' => $Product->id));
             }
             $Product->setOption($OptionForm);
             $Product->autosearch();
             $this->render('storeaddsuccess', ['Product' => $Product, 'StoreCategory' => $StoreCategory, 'Address' => $Address, 'type' => $type]);
             exit;
         }
     }
     $this->render('productadd', ['Product' => $Product, 'StoreCategory' => $StoreCategory, 'Address' => $Address, 'OptionForm' => $OptionForm, 'ContentManager' => $ContentManager, 'photos' => $photos, 'values' => $values, 'ProductaForm' => $ProductaForm]);
 }