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 actionFilterRegions()
 {
     $regions = array();
     $requestData = array();
     if (isset($_REQUEST['data']['country'])) {
         $selectedCountry = $_REQUEST['data']['country'];
         $requestData = $selectedCountry;
         if (is_array($selectedCountry) && count($selectedCountry)) {
             $criteria = new CDbCriteria();
             $criteria->condition = "field_id = 3";
             $criteria->addInCondition("int_val", $selectedCountry);
             $criteria->group = "object_id";
             $listsFieldsValues = ListsFieldsValues::model()->findAll($criteria);
             if (is_array($listsFieldsValues) && count($listsFieldsValues)) {
                 foreach ($listsFieldsValues as $lfv) {
                     $o = ListsItems::model()->findByPk($lfv->object_id);
                     if (is_object($o)) {
                         $regions[] = $o;
                     }
                 }
             }
         }
     }
     $this->renderPartial("filter_regions", array("regions" => $regions, "requestData" => $requestData));
 }
Example #3
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));
 }
Example #4
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'));
 }
Example #5
0
            } else {
                ?>
style="display: none;"<?php 
            }
            ?>
>
                        <?php 
            if ($fieldName == 'region') {
                $selectedCountry = isset($_REQUEST['data']['country']) ? $_REQUEST['data']['country'] : array();
                $regions = array();
                if (is_array($selectedCountry) && count($selectedCountry)) {
                    $criteria = new CDbCriteria();
                    $criteria->condition = "field_id = 3";
                    $criteria->addInCondition("int_val", $selectedCountry);
                    $criteria->group = "object_id";
                    $listsFieldsValues = ListsFieldsValues::model()->findAll($criteria);
                    if (is_array($listsFieldsValues) && count($listsFieldsValues)) {
                        foreach ($listsFieldsValues as $lfv) {
                            $regions[] = ListsItems::model()->findByPk($lfv->object_id);
                        }
                    }
                }
                if (is_array($regions) && count($regions)) {
                    ?>
                                <ul class="region f_<?php 
                    echo $fieldName;
                    ?>
">
                                <?php 
                    foreach ($regions as $region) {
                        $regionField = CatalogFilteredFields::model()->findByAttributes(array("category_id" => $this->currentCategory->id, "name" => "region"));
Example #6
0
                ?>
                                                            <input type="checkbox" name="data[additional][<?php 
                echo $field->name;
                ?>
]" value="1"<?php 
                echo is_object($fValue) && $fValue->int_val ? ' checked="checked"' : '';
                ?>
 />
                                                            <label><?php 
                echo $field->title;
                ?>
</label>
                                                            <?php 
                break;
            case 'image':
                $fValue = ListsFieldsValues::model()->findByAttributes(array("field_id" => $field->id, "object_id" => $model->id));
                ?>
                                                            <label><?php 
                echo $field->title;
                ?>
</label>
                                                            <input type="file" name="<?php 
                echo $field->name;
                ?>
" class="form-control" />
                                                            <?php 
                if (is_object($fValue) && $fValue->text_val) {
                    ?>
                                                                <img src="<?php 
                    echo $fValue->text_val;
                    ?>