Esempio n. 1
0
    /**
     * @param $attribute
     * @param null $default
     * @return bool|float|int|null|string
     */
    public function attribute($attribute, $default = null)
    {
        if ($this->getIsNewRecord()) {
            return null;
        }

        //@TODO переделать на получение в 1 запрос
        $models = EavProductVariant::model()->with('attribute')->findAll(
            'product_id = :product AND attribute_id = :attribute',
            array(
                ':product' => $this->id,
                ':attribute' => $attribute->id,
            )
        );

        if (null === $models || empty($models)) {
            return null;
        }
        $ret = array();
        if(count($models)==1){
        	return $models[0]->value($default);
        }
        foreach ($models as $model) {
        	$ret[] = $model->value($default);
        }
        return $ret;
    }
    /**
     * Updates a particular model.
     * If update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id the ID of the model to be updated
     */
    public function actionUpdate($new = false)
    {
        if ($new === true){
            $model = new Objects;
        } else {
            $id = (int)$_GET['id'];
            $model=$this->loadModel($id);
            if($model->verified == true){
                $this->pageTitle = 'Объекты';
                $this->active_link = 'objects';
            } else if($model->verified == false){
                $this->pageTitle = 'Новые объекты';
                $this->active_link = 'new_objects';
            } 
            $old_mesto = $model->address;
        }

        // Uncomment the following line if AJAX validation is needed
         $this->performAjaxValidation($model, 'objects-form');

        if(isset($_POST['Objects']))
        {
            $typeAttributes = Yii::app()->getRequest()->getPost('EavOptions', array());
            if(!empty($typeAttributes)){
                $er = $ers = null;
                foreach ($typeAttributes as $attribute => $value) {

                    if (null == $value || (is_array($value) && empty($value))) {
                        continue;
                    }

                    //сохраняем значения
                    if(!is_array($value)){
                        $modelE = new EavProductVariant();
                        $er = $modelE->store($attribute, $value, $model, $forcheck = true);
                        if($er == false){
                            $ers = $modelE->errors;
                            break;
                        }
                    } else {
                        foreach ($value as $val) {
                            $modelE = new EavProductVariant();
                            $er = $modelE->store($attribute, $val, $model, $forcheck = true);
                            if($er == false){
                                $ers = $modelE->errors;
                                break 2;
                            }
                        }
                    }

                }
                 if(!empty($ers)){
                    $this->addFlashMessage($ers,'error');
                    $this->refresh();
                 }
            }

            $model->attributes=$_POST['Objects'];
            if(!$model->categories_ar || !is_array($model->categories_ar)){
                $model->categories_ar = array();
                $model->categorie = null;
              } else {
                $model->categorie = $model->categories_ar[0];
              }

            if($new ||  (!empty($old_mesto) && $old_mesto != $model->address)){
                $words = explode(',',$model->address,2);

                if(!empty($words)){
                  $city = trim($words[0]);
                  $trueCity = City::model()->find('LOWER(title)=:title or LOWER(alternative_title)=:title',array(':title'=>MHelper::String()->toLower($city)));
                  if(!$trueCity)
                    $trueCity = City::addNewCity($city);
                  if($trueCity)
                    $model->city_id = $trueCity->id;
                }
                if(isset($words[1])){
                    $words = explode(',',$words[1],2);
                    if(isset($words[0]) && !empty($words[0])){
                        $model->street = trim($words[0]);
                    }
                    if(isset($words[1]) && !empty($words[1])){
                        $model->dom = trim($words[1]);
                    }
                }
                
              }


            if($model->saveData(array(), $typeAttributes, array(), array(), $model->video)){
                
                

                if(isset(Yii::app()->session['deleteObjectsFiles']))
                {
                    $sessAr = unserialize(Yii::app()->session['deleteObjectsFiles']);
                    if(isset($sessAr['id']) && $sessAr['id'] == $model->id && isset($sessAr['files']) && is_array($sessAr['files']))
                  {
                     $files = $model->images;
                     if($files)
                     {
                      foreach ($files as $file) {
                        if(in_array($file->id,$sessAr['files'])){
                          $file->delete();
                        }
                      }
                     }
                  }
                }
              $model->addDropboxFiles($this->uploadsession);
              Yii::app()->session->remove($this->uploadsession);
              if(isset(Yii::app()->session['deleteObjectsFiles']))
            unset(Yii::app()->session['deleteObjectsFiles']);
                if(Yii::app()->request->isAjaxRequest){
                    
                } else {
                    $text = $new? "Объект {$model->title} добавлен" : "Объект {$model->title} отредактирован";
                    $this->addFlashMessage($text,'success');
                    if($model->verified == true) {
                        $this->redirect(Yii::app()->createAbsoluteUrl('catalog/admin/objects'));
                    } else if($model->verified == false){
                        $this->redirect(Yii::app()->createAbsoluteUrl('catalog/admin/objects/new_objects'));
                    } 

                }

            } else {
                $this->addFlashMessage($model->errors,'error');
                $this->refresh();
            }
        }

        $categories_ar[] = $model->categorie;
        $video = $model->objectsVideo;

        $this->render('update',array(
            'model'=>$model,
            'categories_ar'=>$categories_ar,
            'video'=>$video,
        ));
    }