Example #1
0
 public function setValue($val)
 {
     if (!is_object($this->object)) {
         return false;
     }
     switch ($this->data_type) {
         case 'multiple':
             $fv = ListsFieldsValues::model()->findAllByAttributes(array("field_id" => $this->id, "object_id" => $this->object->id));
             $exists = array();
             $hasErrors = false;
             if (is_array($fv) && count($fv)) {
                 foreach ($fv as $item) {
                     if (!in_array($item->int_val, $val)) {
                         $item->delete();
                     } else {
                         $exists[] = $item->int_val;
                     }
                 }
             }
             if (is_array($val) && count($val)) {
                 foreach ($val as $v) {
                     if (!in_array($v, $exists)) {
                         $listItem = ListsItems::model()->findByPk($v);
                         if (is_object($listItem)) {
                             $newFieldValue = new ListsFieldsValues();
                             $newFieldValue->setAttributes(array("field_id" => $this->id, "object_id" => $this->object->id, "int_val" => $v));
                             if (!$newFieldValue->save()) {
                                 $hasErrors = true;
                             }
                         }
                     }
                 }
             }
             return !$hasErrors;
         case 'list':
             $hasErrors = true;
             $fv = ListsFieldsValues::model()->findByAttributes(array("field_id" => $this->id, "object_id" => is_object($this->object) ? $this->object->id : 0));
             if (!is_object($fv)) {
                 $fv = new ListsFieldsValues();
             }
             $v = ListsItems::model()->findByPk($val);
             if (is_object($v)) {
                 $fv->setAttributes(array("field_id" => $this->id, "object_id" => $this->object->id, "int_val" => $val));
                 if ($fv->save()) {
                     $hasErrors = false;
                 }
             }
             return !$hasErrors;
         case 'int':
         case 'bool':
         case 'date':
             $v = ListsFieldsValues::model()->findByAttributes(array("field_id" => $this->id, "object_id" => $this->object->id));
             if (!is_object($v)) {
                 $v = new ListsFieldsValues();
             }
             $v->setAttributes(array("field_id" => $this->id, "object_id" => $this->object->id, "int_val" => $val));
             return $v->save();
         default:
             $v = ListsFieldsValues::model()->findByAttributes(array("field_id" => $this->id, "object_id" => $this->object->id));
             if (!is_object($v)) {
                 $v = new ListsFieldsValues();
             }
             $v->setAttributes(array("field_id" => $this->id, "object_id" => $this->object->id, "text_val" => $val));
             return $v->save();
     }
 }
Example #2
0
 public function actionEdit($id)
 {
     $this->layout = '//layouts/admin';
     $success = false;
     $model = ListsItems::model()->findByPk($id);
     $list = Lists::model()->findByPk($model->list_id);
     $this->pageTitle = is_object($model) ? $model->name : 'Новый элемент справочника';
     $this->breadcrumbs = array('Справочники' => array('/admin/lists'), $list->name => array('/admin/lists/edit/' . $list->id), is_object($model) ? $model->name : 'Новый элемент справочника');
     if (isset($_POST['data']) && is_object($model)) {
         $dataArray = $_POST['data'];
         $additional = isset($dataArray['additional']) ? $dataArray['additional'] : array();
         unset($dataArray['additional']);
         $model->setAttributes($dataArray);
         if ($model->save()) {
             $success = true;
             if (is_array($additional) && count($additional)) {
                 foreach ($additional as $fieldName => $data) {
                     $fieldModel = ListsFields::model()->findByAttributes(array("name" => $fieldName, "list_id" => $model->list_id));
                     if (is_object($fieldModel)) {
                         switch ($fieldModel->data_type) {
                             case 'varchar':
                             case 'text':
                             case 'html':
                                 $newVal = ListsFieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id));
                                 if (!is_object($newVal)) {
                                     $newVal = new ListsFieldsValues();
                                 }
                                 $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "text_val" => $data));
                                 $newVal->save();
                                 break;
                             case 'int':
                             case 'list':
                                 $newVal = ListsFieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id));
                                 if (!is_object($newVal)) {
                                     $newVal = new ListsFieldsValues();
                                 }
                                 $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => $data));
                                 $newVal->save();
                                 break;
                             case 'bool':
                                 $data = $data ? 1 : 0;
                                 $newVal = ListsFieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id));
                                 if (!is_object($newVal)) {
                                     $newVal = new ListsFieldsValues();
                                 }
                                 $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => $data));
                                 $newVal->save();
                                 break;
                             case 'date':
                                 $newVal = ListsFieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id));
                                 if (!is_object($newVal)) {
                                     $newVal = new ListsFieldsValues();
                                 }
                                 $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => strtotime($data)));
                                 $newVal->save();
                                 break;
                             case 'multiple':
                                 /* 09.04.2015
                                  * TODO: придумать что то лучше, чем удаление записей перед созданием новых...
                                  */
                                 $oldValues = ListsFieldsValues::model()->findAllByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id));
                                 if (is_array($oldValues) && count($oldValues)) {
                                     foreach ($oldValues as $ov) {
                                         $ov->delete();
                                     }
                                 }
                                 if (is_array($data) && count($data)) {
                                     foreach ($data as $v) {
                                         $newVal = new ListsFieldsValues();
                                         $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "int_val" => $v));
                                         $newVal->save();
                                     }
                                 }
                                 break;
                         }
                     }
                 }
             }
             if (isset($_FILES) && is_array($_FILES) && count($_FILES)) {
                 foreach ($_FILES as $fieldName => $data) {
                     $fieldModel = ListsFields::model()->findByAttributes(array("name" => $fieldName, "list_id" => $model->list_id));
                     $newVal = ListsFieldsValues::model()->findByAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id));
                     if (!is_object($newVal)) {
                         $newVal = new ListsFieldsValues();
                     }
                     $uploaddir = 'images/upload/' . date("d.m.Y", time());
                     if (!is_dir($uploaddir)) {
                         mkdir($uploaddir);
                     }
                     $tmp_name = $data["tmp_name"];
                     $name = $data["name"];
                     if ($name) {
                         if ($newVal->text_val) {
                             $pic = Yii::app()->basePath . "/.." . $newVal->text_val;
                             if (is_file($pic)) {
                                 unlink($pic);
                             }
                         }
                         $ext = explode(".", $name);
                         $uploadfile = $uploaddir . '/nc_' . $model->id . '_' . md5(basename($name) . time()) . "." . end($ext);
                         if (move_uploaded_file($tmp_name, $uploadfile)) {
                             $newVal->setAttributes(array("field_id" => $fieldModel->id, "object_id" => $model->id, "text_val" => "/" . $uploadfile));
                             $newVal->save();
                         }
                     }
                 }
             }
         }
     }
     $this->render('edit', array('model' => $model, 'list' => $list, 'success' => $success, 'errors' => $model->errors));
 }