Beispiel #1
0
 public function actionAdd($id)
 {
     $this->layout = '//layouts/admin';
     $list = Lists::model()->findByPk($id);
     $this->pageTitle = $list->name . ' :: Новый элемент справочника';
     $this->breadcrumbs = array('Справочники' => array('/admin/lists'), $list->name => array('/admin/lists/edit/' . $list->id), 'Новый элемент');
     $success = false;
     if (isset($_POST['data'])) {
         $model = new ListsItems();
         $dataArray = $_POST['data'];
         $model->setAttributes($dataArray);
         if ($model->save()) {
             $success = true;
         }
     }
     if (!isset($model) || !is_object($model)) {
         $model = new ListsItems();
     }
     if ($success) {
         $this->redirect("/admin/lists/edit/" . $list->id);
     } else {
         $this->render('add', array('model' => $list, 'errors' => $model->errors));
     }
 }
Beispiel #2
0
 public function createObject($name, $langId, $dataArray)
 {
     $model = new CatalogObjects();
     //prepare data
     $uploaddir = 'images/upload/' . date("d.m.Y", time());
     $attributes = array();
     switch ($langId) {
         case 19:
             $attributes['category_id'] = 1;
             //@todo Выбор категории для импорта
             break;
         case 20:
             $attributes['category_id'] = 5;
             break;
         case 21:
             $attributes['category_id'] = 9;
             break;
     }
     $attributes['name'] = $name;
     $attributes['h1'] = $name;
     $attributes['title'] = $name;
     $toUrl = $langId == 21 ? $name . "-" . $dataArray['artikul'] . "-fr" : $name . "-" . $dataArray['artikul'];
     $attributes['url'] = translit::convert($toUrl, "-");
     $attributes['price'] = isset($dataArray['price']) ? str_replace(",", ".", $dataArray['price']) : 0;
     $attributes['is_active'] = 0;
     $attributes['is_deleted'] = 0;
     $attributes['on_main'] = 0;
     $attributes['lang_id'] = $langId;
     $attributes['views_count'] = 0;
     $artikul = isset($dataArray['artikul']) ? $dataArray['artikul'] : '';
     if ($langId == 19) {
         $attributes['anons'] = $dataArray['name_ru'];
     } else {
         $attributes['anons'] = '';
     }
     if (is_dir('import_images/' . $artikul)) {
         if (is_file('import_images/' . $artikul . '/1.jpg')) {
             $anonsPicPath = $uploaddir . "/a_" . md5(time() . $artikul . "a1") . ".jpg";
             $contentPicPath = $uploaddir . "/c_" . md5(time() . $artikul . "c1") . ".jpg";
             if (copy('import_images/' . $artikul . '/1.jpg', $anonsPicPath)) {
                 $attributes['anons_pic'] = "/" . $anonsPicPath;
             }
             if (copy('import_images/' . $artikul . '/1.jpg', $contentPicPath)) {
                 $attributes['content_pic'] = "/" . $contentPicPath;
             }
         }
         if (is_file('import_images/' . $artikul . '/2.jpg')) {
             $extraPicPath = $uploaddir . "/e_" . md5(time() . $artikul . "2") . ".jpg";
             if (copy('import_images/' . $artikul . '/2.jpg', $extraPicPath)) {
                 $attributes['extra_img'] = "/" . $extraPicPath;
             }
         }
     }
     //push data
     $model->setAttributes($attributes);
     if ($model->save()) {
         //prepare additional data
         if (isset($dataArray['ratings']['RP'])) {
             $model->field('rp')->setValue($dataArray['ratings']['RP']);
         }
         if (isset($dataArray['ratings']['WS'])) {
             $model->field('ws')->setValue($dataArray['ratings']['WS']);
         }
         $additionalFields = CatalogFilteredFields::model()->findAllByAttributes(array("category_id" => $model->category_id));
         if (is_array($additionalFields) && count($additionalFields)) {
             foreach ($additionalFields as $f) {
                 $fieldData = isset($dataArray[$f->name]) ? $dataArray[$f->name] : null;
                 $langPrefix = 'ru';
                 switch ($langId) {
                     case 19:
                         $langPrefix = 'ru';
                         break;
                     case 20:
                         $langPrefix = 'en';
                         break;
                     case 21:
                         $langPrefix = 'fr';
                         break;
                 }
                 if (is_array($fieldData) || $fieldData && !is_array($fieldData)) {
                     switch ($f->data_type) {
                         case 'int':
                             if (is_numeric($dataArray[$f->name])) {
                                 $model->field($f->name)->setValue($dataArray[$f->name]);
                             }
                             break;
                         case 'date':
                             if ($dataArray[$f->name]) {
                                 $model->field($f->name)->setValue(strtotime($dataArray[$f->name]));
                             }
                             break;
                         case 'bool':
                             $model->field($f->name)->setValue($fieldData ? 1 : 0);
                             break;
                         case 'multiple':
                             if (is_array($fieldData) && isset($fieldData[$langPrefix])) {
                                 $values = $fieldData[$langPrefix];
                                 if (is_array($values) && count($values)) {
                                     $setArr = array();
                                     $num = 0;
                                     foreach ($values as $value) {
                                         $listItem = ListsItems::model()->findByAttributes(array("list_id" => $f->rel, "name" => $value));
                                         if (!is_object($listItem)) {
                                             $listItem = new ListsItems();
                                             $listItem->setAttributes(array("list_id" => $f->rel, "name" => $value));
                                             $listItem->save();
                                             if (isset($fieldData['en'][$num]) && $fieldData['en'][$num]) {
                                                 $listItem->field("en_name")->setValue($fieldData['en'][$num]);
                                             }
                                             if (isset($fieldData['fr'][$num]) && $fieldData['fr'][$num]) {
                                                 $listItem->field("fr_name")->setValue($fieldData['fr'][$num]);
                                             }
                                         }
                                         $setArr[] = $listItem->id;
                                         $num++;
                                     }
                                     $model->field($f->name)->setValue($setArr);
                                 }
                             } else {
                                 if (is_array($fieldData)) {
                                     $values = $fieldData;
                                     if (is_array($values) && count($values)) {
                                         $setArr = array();
                                         foreach ($values as $value) {
                                             $listItem = ListsItems::model()->findByAttributes(array("list_id" => $f->rel, "name" => $value));
                                             if (!is_object($listItem)) {
                                                 $listItem = new ListsItems();
                                                 $listItem->setAttributes(array("list_id" => $f->rel, "name" => $value));
                                                 $listItem->save();
                                             }
                                             $setArr[] = $listItem->id;
                                         }
                                         $model->field($f->name)->setValue($setArr);
                                     }
                                 }
                             }
                             break;
                         case 'list':
                             if (is_array($fieldData) && isset($fieldData[$langPrefix])) {
                                 $value = $fieldData[$langPrefix];
                                 $listItem = ListsItems::model()->findByAttributes(array("list_id" => $f->rel, "name" => $value));
                                 if (!is_object($listItem)) {
                                     $listItem = new ListsItems();
                                     $listItem->setAttributes(array("list_id" => $f->rel, "name" => $value));
                                     $listItem->save();
                                     if (isset($fieldData['en']) && $fieldData['en']) {
                                         $listItem->field("en_name")->setValue($fieldData['en']);
                                     }
                                     if (isset($fieldData['fr']) && $fieldData['fr']) {
                                         $listItem->field("fr_name")->setValue($fieldData['fr']);
                                     }
                                 }
                                 $model->field($f->name)->setValue($listItem->id);
                             } else {
                                 if ($f->name == 'brand') {
                                     $brandName = $fieldData['name'];
                                     $value = $brandName;
                                     $listItem = ListsItems::model()->findByAttributes(array("list_id" => $f->rel, "name" => $value));
                                     if (!is_object($listItem)) {
                                         $listItem = new ListsItems();
                                         $listItem->setAttributes(array("list_id" => $f->rel, "name" => $value));
                                         $listItem->save();
                                         if (isset($fieldData['en']) && $fieldData['en']) {
                                             $listItem->field("en_name")->setValue($fieldData['en']);
                                         }
                                         if (isset($fieldData['fr']) && $fieldData['fr']) {
                                             $listItem->field("fr_name")->setValue($fieldData['fr']);
                                         }
                                         if (isset($fieldData['site'])) {
                                             $listItem->field('ru_site')->setValue($fieldData['site']);
                                         }
                                     } else {
                                         if (isset($fieldData['en']) && $fieldData['en']) {
                                             $listItem->field("en_name")->setValue($fieldData['en']);
                                         }
                                         if (isset($fieldData['fr']) && $fieldData['fr']) {
                                             $listItem->field("fr_name")->setValue($fieldData['fr']);
                                         }
                                         if (isset($fieldData['site'])) {
                                             $listItem->field('ru_site')->setValue($fieldData['site']);
                                         }
                                     }
                                     $model->field($f->name)->setValue($listItem->id);
                                 } else {
                                     if (!is_array($fieldData)) {
                                         //$value = $fieldData;
                                         $listItem = ListsItems::model()->findByAttributes(array("list_id" => $f->rel, "name" => $fieldData));
                                         if (!is_object($listItem)) {
                                             $listItem = new ListsItems();
                                             $listItem->setAttributes(array("list_id" => $f->rel, "name" => $fieldData));
                                             $listItem->save();
                                         }
                                         $model->field($f->name)->setValue($listItem->id);
                                     }
                                 }
                             }
                             break;
                         default:
                             $value = is_array($fieldData) && isset($fieldData[$langPrefix]) ? $fieldData[$langPrefix] : $fieldData;
                             $model->field($f->name)->setValue(trim($value));
                             break;
                     }
                 }
             }
         }
     } else {
         print_r($model->errors);
         die;
     }
 }