diff --git a/backend/controllers/ImportController.php b/backend/controllers/ImportController.php new file mode 100644 index 0000000..f37e584 --- /dev/null +++ b/backend/controllers/ImportController.php @@ -0,0 +1,38 @@ +render('index', [ + 'model' => $model + ]); + } + + public function actionUpload() + { + $model = new Import(); + + if ($model->load(Yii::$app->request->post())) + { + $model->file = UploadedFile::getInstances($model, 'file'); + + // Копируем файл в директорию + $path = $_SERVER['DOCUMENT_ROOT'].'/import/'; + + foreach ($model->file as $file) + { + //var_dump(substr ($path.$file->name, 0, -10)); die; + Import::importFile ($file); + } + } + } + +} diff --git a/backend/models/Import.php b/backend/models/Import.php new file mode 100644 index 0000000..b7feaf8 --- /dev/null +++ b/backend/models/Import.php @@ -0,0 +1,184 @@ + false, 'extensions' => 'csv', 'maxFiles' => 2], + ]; + } + + static function importFile ($filename) + { + // создаем папку + if (! is_dir ($path = $_SERVER['DOCUMENT_ROOT'].'/import/')) + { + mkdir ($path, 0777, true); + } + + // копируем файл + copy ($filename->tempName, $path.$filename->name); + + // по умолчанию + // $termin_pid MEGA КАСТЫЛЬ!!! Это категория "Каталог товаров", + // под которую подтягуються термины, которые на нашли себе parent + $termin_pid = 8; + // $template_id шаблон каьегорий + $template_id = 3; + $lang_id = 2; + $type = 'H'; + + // массив для импортп товаров + $MASS = []; + + // открываем файл и перебираем + $fp = fopen ($path.$filename->name, 'r'); + while ($ROW = fgetcsv ($fp, 10000, ';')) + { + // чистим + foreach ($ROW as $key => &$value) + { + $value = trim ($value) == 'NULL' ? NULL : trim ($value); + } + + $ROW['category_title'] = $ROW[0]; + $ROW['group_title'] = $ROW[1]; + $ROW['subgroup_title'] = $ROW[2]; + + // проверяем если через "," + + // var_dump($array[1]); die; + + // массив для поиска/добавления термина + $basic = [ + 'type' => $type, + 'lang_id' => $lang_id, + 'template_id' => $template_id, + ]; + + // категория + if ($ROW['category_title'] == NULL) + { + CONTINUE; + } + + $termin_id = Termin::addIfNotExists ($basic + [ + 'termin_title' => $ROW['category_title'], + 'termin_pid' => $termin_pid, // MEGA КАСТЫЛЬ!!! + ]); + + // подгруппа + if ($ROW['group_title'] != NULL) + { + $termin_id = Termin::addIfNotExists ($basic + [ + 'termin_title' => $ROW['group_title'], + 'termin_pid' => $termin_id, + ]); + } + + // группа + if ($ROW['subgroup_title'] != NULL) + { + $termin_id = Termin::addIfNotExists ($basic + [ + 'termin_title' => $ROW['subgroup_title'], + 'termin_pid' => $termin_id, + ]); + } + + } + + // удаляем файл + chmod($path.$filename->name, 0777); + unlink ($path.$filename->name); + +/* + echo '
';
+        
+            var_dump($category);
+            var_dump($group);
+            var_dump($subgroup);
+        
+        echo '
'; + + // ОБЩЕЕ + // PRODUCT + Артикул + Категория + Группа + Подгруппа + Описание + Штрих-код + + // СПРАВОЧНИК ИЛИ ДОП. ПОЛЯ + // ??? + Торговая марка + Производитель + ID + Наименование + кол-во в пакете + Ед. Изм + опт / розница + Диаметр шляпки min + Диаметр шляпки max + Единица измерения + Цвет шляпки + Цвет шляпки (фильтр) + Цвет мякоти цвет мякоти (фильтр) + Длина ножки min + Длина ножки max + Единица измерения + примечание +*/ + } + + public function findTerminOneQuery ($type, $name, $parent_name) + { +/* + return yii::$app->db->createCommand(' + SELECT + `termin`.termin_id, `parent`.termin_id as termin_pid + FROM `termin` + INNER JOIN `termin_lang` ON `termin_lang`.termin_alias = "'.$name.'" + INNER JOIN `termin_relation` ON `termin_relation`.termin_id = `termin`.termin_id + LEFT JOIN ( + IF NOT EXISTS (SELECT * + FROM `termin_lang` + INNER JOIN `termin` ON `termin`.termin_id = `termin_lang`.termin_id + AND `termin`.type = "'.$type.'" + WHERE `termin_lang`.termin_alias = "'.$parent_name.'" + ) + THEN (SELECT * + FROM `termin_lang` + INNER JOIN `termin` ON `termin`.termin_id = `termin_lang`.termin_id + AND `termin`.type = "'.$type.'" + WHERE `termin_lang`.termin_alias = "'.$parent_name.'" + ) + ELSE (SELECT * + FROM `termin_lang` + INNER JOIN `termin` ON `termin`.termin_id = `termin_lang`.termin_id + AND `termin`.type = "'.$type.'" + ) + ) as `parent` ON `parent`.termin_id = `termin_relation`.termin_pid + WHERE `termin`.type = "'.$type.'" + ')->queryOne(); +*/ + } + +} diff --git a/backend/views/import/index.php b/backend/views/import/index.php new file mode 100644 index 0000000..8862200 --- /dev/null +++ b/backend/views/import/index.php @@ -0,0 +1,33 @@ + + + diff --git a/common/models/SqlQueryBuilder.php b/common/models/SqlQueryBuilder.php new file mode 100644 index 0000000..116ef54 --- /dev/null +++ b/common/models/SqlQueryBuilder.php @@ -0,0 +1,254 @@ + array(), + 'from' => '', + 'join' => array(), + 'where' => array(), + 'group' => array(), + 'having' => array(), + 'order' => array(), + 'limit' => array('offset' => 0, 'limit' => 0), + ); + + /** + * Add fields in query selection + * + * @param string $fields List of fields to concat to other fields + * @return DbQuery + */ + public function select ($fields) + { + if (! empty ($fields)) + { + $this->query['select'][] = $fields; + } + return $this; + } + + /** + * Set table for FROM clause + * + * @param string $table Table name + * @return DbQuery + */ + public function from ($table, $alias = null) + { + if (! empty ($table)) + { + $this->query['from'][] = '`'.$table.'`'.($alias ? ' '.$alias : ''); + } + return $this; + } + + /** + * Add JOIN clause + * E.g. $this->join('RIGHT JOIN '._DB_PREFIX_.'product p ON ...'); + * + * @param string $join Complete string + * @return DbQuery + */ + public function join ($join) + { + if (! empty ($join)) + { + $this->query['join'][] = $join; + } + return $this; + } + + /** + * Add LEFT JOIN clause + * + * @param string $table Table name (without prefix) + * @param string $alias Table alias + * @param string $on ON clause + */ + public function leftJoin ($table, $alias = null, $on = null) + { + return $this->join('LEFT JOIN `'.$table.'`'.($alias ? ' `'.$alias.'`' : '').($on ? ' ON '.$on : '')); + } + + /** + * Add INNER JOIN clause + * E.g. $this->innerJoin('product p ON ...') + * + * @param string $table Table name (without prefix) + * @param string $alias Table alias + * @param string $on ON clause + */ + public function innerJoin ($table, $alias = null, $on = null) + { + return $this->join('INNER JOIN `'.$table.'`'.($alias ? ' '.$alias : '').($on ? ' ON '.$on : '')); + } + + /** + * Add LEFT OUTER JOIN clause + * + * @param string $table Table name (without prefix) + * @param string $alias Table alias + * @param string $on ON clause + */ + public function leftOuterJoin ($table, $alias = null, $on = null) + { + return $this->join('LEFT OUTER JOIN `'.$table.'`'.($alias ? ' '.$alias : '').($on ? ' ON '.$on : '')); + } + + /** + * Add NATURAL JOIN clause + * + * @param string $table Table name (without prefix) + * @param string $alias Table alias + */ + public function naturalJoin ($table, $alias = null) + { + return $this->join('NATURAL JOIN `'.$table.'`'.($alias ? ' '.$alias : '')); + } + + /** + * Add a restriction in WHERE clause (each restriction will be separated by AND statement) + * + * @param string $restriction + * @return DbQuery + */ + public function where ($restriction) + { + if (! empty ($restriction)) + { + $this->query['where'][] = $restriction; + } + return $this; + } + + /** + * Add a restriction in HAVING clause (each restriction will be separated by AND statement) + * + * @param string $restriction + * @return DbQuery + */ + public function having ($restriction) + { + if (! empty ($restriction)) + { + $this->query['having'][] = $restriction; + } + return $this; + } + + /** + * Add an ORDER B restriction + * + * @param string $fields List of fields to sort. E.g. $this->order('myField, b.mySecondField DESC') + * @return DbQuery + */ + public function orderBy ($fields) + { + if (! empty ($fields)) + { + $this->query['order'][] = $fields; + } + return $this; + } + + /** + * Add a GROUP BY restriction + * + * @param string $fields List of fields to sort. E.g. $this->group('myField, b.mySecondField DESC') + * @return DbQuery + */ + public function groupBy ($fields) + { + if (! empty ($fields)) + { + $this->query['group'][] = $fields; + } + return $this; + } + + /** + * Limit results in query + * + * @param string $fields List of fields to sort. E.g. $this->order('myField, b.mySecondField DESC') + * @return DbQuery + */ + public function limit ($limit, $offset = 0) + { + $offset = (int)$offset; + if ($offset < 0) + { + $offset = 0; + } + + $this->query['limit'] = array( + 'offset' => $offset, + 'limit' => (int)$limit, + ); + return $this; + } + + /** + * Generate and get the query + * + * @return string + */ + public function build () + { + $sql = 'SELECT '.((($this->query['select'])) ? implode (",\n", $this->query['select']) : '*')."\n"; + + if (! $this->query['from']) + { + die('DbQuery->build() missing from clause'); + } + $sql .= 'FROM '.implode (', ', $this->query['from'])."\n"; + + if ($this->query['join']) + { + $sql .= implode ("\n", $this->query['join'])."\n"; + } + + if ($this->query['where']) + { + $sql .= 'WHERE ('.implode (') AND (', $this->query['where']).")\n"; + } + + if ($this->query['group']) + { + $sql .= 'GROUP BY '.implode(', ', $this->query['group'])."\n"; + } + + if ($this->query['having']) + { + $sql .= 'HAVING ('.implode (') AND (', $this->query['having']).")\n"; + } + + if ($this->query['order']) + { + $sql .= 'ORDER BY '.implode (', ', $this->query['order'])."\n"; + } + + if ($this->query['limit']['limit']) + { + $limit = $this->query['limit']; + $sql .= 'LIMIT '.(($limit['offset']) ? $limit['offset'].', '.$limit['limit'] : $limit['limit']); + } +/* + ob_start(); + var_dump($sql); + echo ob_get_clean(); +*/ + return $sql; + } + + public function __toString () + { + return $this->build(); + } +} + diff --git a/common/models/Tools.php b/common/models/Tools.php new file mode 100644 index 0000000..2a1bac2 --- /dev/null +++ b/common/models/Tools.php @@ -0,0 +1,101 @@ + $value) + { + if (is_int ($key)) + { + $exist[$value] = ''; + unset ($exist[$key]); + } + } + + foreach ($exist as $key => $value) + { + if (! isset ($mass[$key]) + || (isset ($mass[$key]) && $mass[$key] === '')) + { + $mass[$key] = $value; + } + } + } + + static function translit ($string, $setting = 'all') + { + $letter = array ( + + 'а' => 'a', 'б' => 'b', 'в' => 'v', + 'г' => 'g', 'д' => 'd', 'е' => 'e', + 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', + 'и' => 'i', 'й' => 'y', 'к' => 'k', + 'л' => 'l', 'м' => 'm', 'н' => 'n', + 'о' => 'o', 'п' => 'p', 'р' => 'r', + 'с' => 's', 'т' => 't', 'у' => 'u', + 'ф' => 'f', 'х' => 'h', 'ц' => 'c', + 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', + 'ь' => "", 'ы' => 'y', 'ъ' => "", + 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', + 'ї' => 'yi', 'є' => 'ye', 'і' => 'ee', + + 'А' => 'A', 'Б' => 'B', 'В' => 'V', + 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', + 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', + 'И' => 'I', 'Й' => 'Y', 'К' => 'K', + 'Л' => 'L', 'М' => 'M', 'Н' => 'N', + 'О' => 'O', 'П' => 'P', 'Р' => 'R', + 'С' => 'S', 'Т' => 'T', 'У' => 'U', + 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', + 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch', + 'Ь' => "", 'Ы' => 'Y', 'Ъ' => "", + 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', + 'Ї' => 'Yi', 'Є' => 'Ye', 'І' => 'Ee' + ); + + $symbol = array ( + ' ' => '-', "'" => '', '"' => '', + '!' => '', "@" => '', '#' => '', + '$' => '', "%" => '', '^' => '', + ';' => '', "*" => '', '(' => '', + ')' => '', "+" => '', '~' => '', + '.' => '', ',' => '-', '?' => '', + '…' => '', '№' => 'N', '°' => '', + '`' => '', '|' => '', '&' => '-and-', + '<' => '', '>' => '' + ); + + if ($setting == 'all') + { + $converter = $letter + $symbol; + } + else if ($setting == 'letter') + { + $converter = $letter; + } + else if ($setting == 'symbol') + { + $converter = $symbol; + } + + $url = strtr ($string, $converter); + + $url = str_replace ("---", '-', $url); + $url = str_replace ("--", '-', $url); + + return $url; + } +} \ No newline at end of file -- libgit2 0.21.4