Ejemplo n.º 1
0
 public function field($name)
 {
     if (!$this->id) {
         return new ListsFields();
     }
     $model = ListsFields::model()->findByAttributes(array("list_id" => $this->list_id, "name" => $name));
     if (is_object($model)) {
         $model->object = $this;
     }
     return is_object($model) ? $model : new ListsFields();
 }
Ejemplo n.º 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));
 }
Ejemplo n.º 3
0
 public function actionDelete()
 {
     $success = false;
     $id = $_REQUEST['id'];
     if (is_numeric($id)) {
         $model = ListsFields::model()->findByPk($id);
         if (is_object($model)) {
             if ($model->delete()) {
                 $values = ListsFieldsValues::model()->findAllByAttributes(array("field_id" => $model->id));
                 if (is_array($values) && count($values)) {
                     foreach ($values as $v) {
                         $v->delete();
                     }
                 }
                 $success = true;
             }
         }
     }
     echo json_encode(array('status' => $success ? 'success' : 'fail'));
 }