コード例 #1
0
 public function actionUploaddata($id)
 {
     set_time_limit(0);
     $model = $this->loadModel($id);
     $catalog = null;
     //Для модуля Каталог, Двигатели, Трансмисия
     if ($model->module == 1) {
         $catalog = CatalogRubrics::model()->getRootIdAndName();
     }
     if ($model->module == 2) {
         $catalog = CatalogengineRubrics::model()->getRootIdAndName();
     }
     if ($model->module == 3) {
         $catalog = CatalogtransmissionRubrics::model()->getRootIdAndName();
     }
     if (isset($_POST['LoadxmlRubrics'])) {
         $err = 0;
         //$model->attributes = $_POST['LoadxmlRubrics'];
         $filepatch = YiiBase::getPathOfAlias('webroot') . '/../uploads/filestorage/loadxml/';
         $model->testedfile = CUploadedFile::getInstance($model, 'testedfile');
         if (!empty($model->testedfile)) {
             $ext = pathinfo($model->testedfile);
             $ext = $ext['extension'];
             if ($model->ext != $ext) {
                 if (($model->ext == 'xls' || $model->ext == 'xlsx') && ($ext == 'xls' || $ext == 'xlsx')) {
                     $err = 0;
                 } else {
                     $err = 1;
                     Yii::app()->user->setFlash('error', "Файл должен быть в формате: " . $model->ext);
                 }
             }
         } else {
             $err = 1;
             Yii::app()->user->setFlash('error', "Вы не выбрали файл");
         }
         if ($err == 0) {
             //Загружаем файл
             $filename = $model->testedfile->getName();
             $model->testedfile->saveAs($filepatch . $filename);
             //xls и xlsx
             if (preg_match("/.xls/i", $filename) || preg_match("/.xlsx/i", $filename)) {
                 $exel = new Parsfile();
                 $exel->pars_exel($filepatch . $filename);
                 $output = $exel->OutArr;
             }
             //xml
             if (preg_match("/.xml/i", $filename)) {
                 $xml = new Parsfile();
                 $xml->pars_xml($filepatch . $filename, $model->tag);
                 $output = $xml->XmlFieldsHash;
             }
             //csv
             if (preg_match("/.csv/i", $filename)) {
                 $csv = new Parsfile();
                 $splitter = LoadxmlRubrics::model()->getSplitter((int) $model->splitter);
                 $csv->pars_csv($filepatch . $filename, $splitter);
                 $output = $csv->OutArr;
             }
             //модуль каталог
             if ($model->module == 1 || $model->module == 2 || $model->module == 3) {
                 $table_pref = "tbl_";
                 if ($model->module == 1) {
                     $table = "catalog_elements";
                 }
                 //Для модуля Каталог
                 if ($model->module == 2) {
                     $table = "catalogengine_elements";
                 }
                 //Для модуля Двигатели
                 if ($model->module == 3) {
                     $table = "catalogtransmission_elements";
                 }
                 //Для модуля Трансмисия
                 // Вычисляем, какие столбцы нужно обходить циклом для группировки значений
                 $groups_need = array();
                 if (!empty($model->groups)) {
                     $list_groups = unserialize($model->groups);
                     $cnt_list = count($list_groups);
                     $group_i = 0;
                     for ($i = 1; $i <= $cnt_list; ++$i) {
                         if ($list_groups[$i]) {
                             $groups_need[$group_i] = $i;
                             // создаем массив с номерами нужных столбцов
                             ++$group_i;
                         }
                     }
                 }
                 $count_need = count($groups_need);
                 // кол-во нужных столбов
                 $count_table = count($output);
                 // кол-во строк в таблице
                 $val_arr_group = array();
                 // массив значений для группировки
                 $val_arr_cnt = array();
                 // массив для хранения кол-ва записей группировки
                 $val_arr_group_r = array();
                 // массив значений для группировки
                 if ($count_need > 0) {
                     for ($j = 1; $j <= $count_table; ++$j) {
                         for ($i = 0; $i <= $count_need; ++$i) {
                             $group_id = $groups_need[$i];
                             $group_cnt = intval($val_arr_cnt[$group_id]);
                             $read_vars = trim($output[$j][$group_id]);
                             $read_vars_alt = $this->totranslit($read_vars);
                             if (!in_array($read_vars_alt, $val_arr_group[$group_id]) and $read_vars_alt) {
                                 $val_arr_group[$group_id][$group_cnt] = $read_vars_alt;
                                 // записываем в массив значение
                                 $val_arr_group_r[$group_id][$group_cnt]['name'] = $read_vars;
                                 $val_arr_group_r[$group_id][$group_cnt]['alt_name'] = $read_vars_alt;
                                 ++$group_cnt;
                                 $val_arr_cnt[$group_id] = $group_cnt;
                                 // записываем в массив кол-во записей
                             }
                         }
                     }
                 }
                 //надо очищать каталог перед импортом - сдеать вызов тут
                 if (isset($_POST['LoadxmlRubrics']['clearsect']) && (int) $_POST['LoadxmlRubrics']['clearsect'] == 1 && (int) $_POST['LoadxmlRubrics']['parent_id'] > 0) {
                     if ($model->module == 1) {
                         CatalogElements::model()->deleteAll("`parent_id` = :parent_id", array(':parent_id' => (int) $_POST['LoadxmlRubrics']['parent_id']));
                     }
                     if ($model->module == 2) {
                         CatalogengineElements::model()->deleteAll("`parent_id` = :parent_id", array(':parent_id' => (int) $_POST['LoadxmlRubrics']['parent_id']));
                     }
                     if ($model->module == 3) {
                         CatalogtransmissionElements::model()->deleteAll("`parent_id` = :parent_id", array(':parent_id' => (int) $_POST['LoadxmlRubrics']['parent_id']));
                     }
                 }
                 /*
                 foreach ($val_arr_group_r as $keys_in ) {
                     foreach ( $keys_in as $test ) {
                         $nde = array();
                         $nde['name'] = trim($test['name']);
                         $nde['page_name'] = $test['name'];
                         $nde['url'] = strtolower($test['alt_name']);
                         if ((int)$_POST['LoadxmlRubrics']['parent_id']>0){
                             $category = new CatalogRubrics;
                             $category->name = $nde['name'];
                             $category->page_name = $nde['page_name'];
                             $category->url = $nde['url'];
                             $category->parent_id = (int)$_POST['LoadxmlRubrics']['parent_id'];
                             $root = CatalogRubrics::model()->findByPk($category->parent_id);
                             $category->appendTo($root);
                         }
                     }
                 }
                 */
                 $MainFields = null;
                 $fields = explode('|', $model->content2);
                 $FieldCount = 0;
                 foreach ($fields as $k => $v) {
                     if (isset($v[0]) && !empty($v[0])) {
                         $subfields = explode('%', $v);
                         $MainFields2[$k] = $subfields;
                         if ($model->ext == 'xml') {
                             $subfields[1] = $xml->XmlAntiMap[$subfields[1]];
                         }
                         $MainFields[$k] = $subfields;
                         $FieldCount++;
                     }
                 }
                 $uniqKey = '';
                 foreach ($MainFields as $mk => $mv) {
                     if ($mv[0] == $model->unique && $model->unique != '') {
                         $uniqKey = $model->unique;
                     }
                 }
                 //Пишем данные в таблицы
                 foreach ($output as $k => $v) {
                     $sett = '';
                     $setv = '';
                     $UniqField = '';
                     $WriteFlag = 1;
                     foreach ($MainFields as $K => $V) {
                         $val = '';
                         if ($model->ext == 'xml') {
                             $xml_field = $MainFields2[$K][1];
                             $val = $xml->XmlFieldsHash[$k][$xml_field];
                             if ($V[1] == 3) {
                                 $val = preg_replace("/[^1234567890.,]/", '', $val);
                                 $vall = preg_replace("/\\,]/", '.', $val);
                             }
                             if ($V[1] == 2) {
                                 if (trim($xml->XmlFieldsHash[$k][$xml_field]) == '') {
                                     $val = '';
                                 } else {
                                     $val = 1;
                                 }
                             }
                         } else {
                             $val = $v[$V[1]];
                             if ($V[2] == 3) {
                                 $val = preg_replace("/[^1234567890.,]/", '', $val);
                                 $vall = preg_replace("/\\,]/", '.', $val);
                             }
                             if ($V[2] == 2) {
                                 if (trim($v[$V[2] - 1]) == '') {
                                     $val = '';
                                 } else {
                                     $val = 1;
                                 }
                             }
                         }
                         //print $val."||||<br/>";
                         if ($sett == '') {
                             $sett .= " `{$V['0']}` ";
                         } else {
                             $sett .= ", `{$V['0']}` ";
                         }
                         if ($setv == '') {
                             $setv .= " '" . $val . "' ";
                         } else {
                             $setv .= ", '" . addslashes($val) . "' ";
                         }
                         if ($V[0] == $uniqKey && $V[0] != '') {
                             $UniqField = $uniqKey;
                             $resultU = Yii::app()->db->createCommand()->select('*')->from('{{' . $table . '}}')->where(" `" . $UniqField . "` = '" . $val . "'")->queryAll();
                             foreach ($resultU as $rowU) {
                                 if ($rowU['id'] != '') {
                                     Yii::app()->db->createCommand()->delete("`" . $table . "`", 'id=:id', array(':id' => $rowU['id']));
                                 }
                             }
                         }
                         if ($V[3] != '') {
                             //if($val == ''){$WriteFlag = 0;}
                         }
                     }
                     $sett .= ',`parent_id` ';
                     $setv .= ", '" . (int) $_POST['LoadxmlRubrics']['parent_id'] . "' ";
                     if ($WriteFlag != '0') {
                         $sql = "INSERT INTO " . $table_pref . $table . " ({$sett}) VALUES ({$setv})";
                         Yii::app()->db->createCommand($sql)->execute();
                     } else {
                         print "0 <br/>";
                     }
                 }
             }
             Yii::app()->user->setFlash('success', "Данные загружены");
         }
     }
     $this->render('uploaddata', array('model' => $model, 'catalog' => $catalog));
 }
コード例 #2
0
 public function getFeeldsContent2($module)
 {
     //$module: 1- каталог, 2-двигатели, 3-коробка передач
     $result = array();
     switch ($module) {
         case 1:
             $result = CatalogElements::model()->attributeLabels();
             break;
         case 2:
             $result = CatalogengineElements::model()->attributeLabels();
             break;
         case 3:
             $result = CatalogtransmissionElements::model()->attributeLabels();
             break;
     }
     if (isset($result['id'])) {
         unset($result['id']);
     }
     return $result;
 }