Commit 63b9774b83c7646c0e3ce98fbcad091dcbe07ea9
1 parent
492fcbc5
+ Импорт структуры с файла.csv вида Категория-1;Категория-2;Категория-3
Showing
5 changed files
with
610 additions
and
0 deletions
Show diff stats
1 | +<?php | |
2 | + | |
3 | +namespace backend\controllers; | |
4 | + | |
5 | +use Yii; | |
6 | +use backend\models\Import; | |
7 | +use yii\web\UploadedFile; | |
8 | + | |
9 | +class ImportController extends \yii\web\Controller | |
10 | +{ | |
11 | + public function actionIndex() | |
12 | + { | |
13 | + $model = new Import(); | |
14 | + return $this->render('index', [ | |
15 | + 'model' => $model | |
16 | + ]); | |
17 | + } | |
18 | + | |
19 | + public function actionUpload() | |
20 | + { | |
21 | + $model = new Import(); | |
22 | + | |
23 | + if ($model->load(Yii::$app->request->post())) | |
24 | + { | |
25 | + $model->file = UploadedFile::getInstances($model, 'file'); | |
26 | + | |
27 | + // Копируем файл в директорию | |
28 | + $path = $_SERVER['DOCUMENT_ROOT'].'/import/'; | |
29 | + | |
30 | + foreach ($model->file as $file) | |
31 | + { | |
32 | + //var_dump(substr ($path.$file->name, 0, -10)); die; | |
33 | + Import::importFile ($file); | |
34 | + } | |
35 | + } | |
36 | + } | |
37 | + | |
38 | +} | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace backend\models; | |
4 | + | |
5 | +use Yii; | |
6 | +use yii\base\Model; | |
7 | +use common\models\Tools; | |
8 | +use common\models\Termin; | |
9 | +use yii\web\UploadedFile; | |
10 | + | |
11 | +/** | |
12 | + * Import товаров | |
13 | + */ | |
14 | +class Import extends \yii\db\ActiveRecord | |
15 | +{ | |
16 | + public $file; | |
17 | + | |
18 | + /** | |
19 | + * @inheritdoc | |
20 | + */ | |
21 | + public function rules() | |
22 | + { | |
23 | + return [ | |
24 | + [['file'], 'file', 'skipOnEmpty' => false, 'extensions' => 'csv', 'maxFiles' => 2], | |
25 | + ]; | |
26 | + } | |
27 | + | |
28 | + static function importFile ($filename) | |
29 | + { | |
30 | + // создаем папку | |
31 | + if (! is_dir ($path = $_SERVER['DOCUMENT_ROOT'].'/import/')) | |
32 | + { | |
33 | + mkdir ($path, 0777, true); | |
34 | + } | |
35 | + | |
36 | + // копируем файл | |
37 | + copy ($filename->tempName, $path.$filename->name); | |
38 | + | |
39 | + // по умолчанию | |
40 | + // $termin_pid MEGA КАСТЫЛЬ!!! Это категория "Каталог товаров", | |
41 | + // под которую подтягуються термины, которые на нашли себе parent | |
42 | + $termin_pid = 8; | |
43 | + // $template_id шаблон каьегорий | |
44 | + $template_id = 3; | |
45 | + $lang_id = 2; | |
46 | + $type = 'H'; | |
47 | + | |
48 | + // массив для импортп товаров | |
49 | + $MASS = []; | |
50 | + | |
51 | + // открываем файл и перебираем | |
52 | + $fp = fopen ($path.$filename->name, 'r'); | |
53 | + while ($ROW = fgetcsv ($fp, 10000, ';')) | |
54 | + { | |
55 | + // чистим | |
56 | + foreach ($ROW as $key => &$value) | |
57 | + { | |
58 | + $value = trim ($value) == 'NULL' ? NULL : trim ($value); | |
59 | + } | |
60 | + | |
61 | + $ROW['category_title'] = $ROW[0]; | |
62 | + $ROW['group_title'] = $ROW[1]; | |
63 | + $ROW['subgroup_title'] = $ROW[2]; | |
64 | + | |
65 | + // проверяем если через "," | |
66 | + | |
67 | + // var_dump($array[1]); die; | |
68 | + | |
69 | + // массив для поиска/добавления термина | |
70 | + $basic = [ | |
71 | + 'type' => $type, | |
72 | + 'lang_id' => $lang_id, | |
73 | + 'template_id' => $template_id, | |
74 | + ]; | |
75 | + | |
76 | + // категория | |
77 | + if ($ROW['category_title'] == NULL) | |
78 | + { | |
79 | + CONTINUE; | |
80 | + } | |
81 | + | |
82 | + $termin_id = Termin::addIfNotExists ($basic + [ | |
83 | + 'termin_title' => $ROW['category_title'], | |
84 | + 'termin_pid' => $termin_pid, // MEGA КАСТЫЛЬ!!! | |
85 | + ]); | |
86 | + | |
87 | + // подгруппа | |
88 | + if ($ROW['group_title'] != NULL) | |
89 | + { | |
90 | + $termin_id = Termin::addIfNotExists ($basic + [ | |
91 | + 'termin_title' => $ROW['group_title'], | |
92 | + 'termin_pid' => $termin_id, | |
93 | + ]); | |
94 | + } | |
95 | + | |
96 | + // группа | |
97 | + if ($ROW['subgroup_title'] != NULL) | |
98 | + { | |
99 | + $termin_id = Termin::addIfNotExists ($basic + [ | |
100 | + 'termin_title' => $ROW['subgroup_title'], | |
101 | + 'termin_pid' => $termin_id, | |
102 | + ]); | |
103 | + } | |
104 | + | |
105 | + } | |
106 | + | |
107 | + // удаляем файл | |
108 | + chmod($path.$filename->name, 0777); | |
109 | + unlink ($path.$filename->name); | |
110 | + | |
111 | +/* | |
112 | + echo '<pre>'; | |
113 | + | |
114 | + var_dump($category); | |
115 | + var_dump($group); | |
116 | + var_dump($subgroup); | |
117 | + | |
118 | + echo '</pre>'; | |
119 | + | |
120 | + // ОБЩЕЕ | |
121 | + // PRODUCT | |
122 | + Артикул | |
123 | + Категория | |
124 | + Группа | |
125 | + Подгруппа | |
126 | + Описание | |
127 | + Штрих-код | |
128 | + | |
129 | + // СПРАВОЧНИК ИЛИ ДОП. ПОЛЯ | |
130 | + // ??? | |
131 | + Торговая марка | |
132 | + Производитель | |
133 | + ID | |
134 | + Наименование | |
135 | + кол-во в пакете | |
136 | + Ед. Изм | |
137 | + опт / розница | |
138 | + Диаметр шляпки min | |
139 | + Диаметр шляпки max | |
140 | + Единица измерения | |
141 | + Цвет шляпки | |
142 | + Цвет шляпки (фильтр) | |
143 | + Цвет мякоти цвет мякоти (фильтр) | |
144 | + Длина ножки min | |
145 | + Длина ножки max | |
146 | + Единица измерения | |
147 | + примечание | |
148 | +*/ | |
149 | + } | |
150 | + | |
151 | + public function findTerminOneQuery ($type, $name, $parent_name) | |
152 | + { | |
153 | +/* | |
154 | + return yii::$app->db->createCommand(' | |
155 | + SELECT | |
156 | + `termin`.termin_id, `parent`.termin_id as termin_pid | |
157 | + FROM `termin` | |
158 | + INNER JOIN `termin_lang` ON `termin_lang`.termin_alias = "'.$name.'" | |
159 | + INNER JOIN `termin_relation` ON `termin_relation`.termin_id = `termin`.termin_id | |
160 | + LEFT JOIN ( | |
161 | + IF NOT EXISTS (SELECT * | |
162 | + FROM `termin_lang` | |
163 | + INNER JOIN `termin` ON `termin`.termin_id = `termin_lang`.termin_id | |
164 | + AND `termin`.type = "'.$type.'" | |
165 | + WHERE `termin_lang`.termin_alias = "'.$parent_name.'" | |
166 | + ) | |
167 | + THEN (SELECT * | |
168 | + FROM `termin_lang` | |
169 | + INNER JOIN `termin` ON `termin`.termin_id = `termin_lang`.termin_id | |
170 | + AND `termin`.type = "'.$type.'" | |
171 | + WHERE `termin_lang`.termin_alias = "'.$parent_name.'" | |
172 | + ) | |
173 | + ELSE (SELECT * | |
174 | + FROM `termin_lang` | |
175 | + INNER JOIN `termin` ON `termin`.termin_id = `termin_lang`.termin_id | |
176 | + AND `termin`.type = "'.$type.'" | |
177 | + ) | |
178 | + ) as `parent` ON `parent`.termin_id = `termin_relation`.termin_pid | |
179 | + WHERE `termin`.type = "'.$type.'" | |
180 | + ')->queryOne(); | |
181 | +*/ | |
182 | + } | |
183 | + | |
184 | +} | ... | ... |
1 | +<?php | |
2 | + | |
3 | +use yii\helpers\Html; | |
4 | +use yii\widgets\ActiveForm; | |
5 | +use yii\helpers\ArrayHelper; | |
6 | +use kartik\select2\Select2; | |
7 | + | |
8 | +/* @var $this yii\web\View */ | |
9 | +/* @var $model common\models\Menu */ | |
10 | +/* @var $form yii\widgets\ActiveForm */ | |
11 | +?> | |
12 | + | |
13 | +<div class="menu-form"> | |
14 | + | |
15 | + <?php | |
16 | + $form = ActiveForm::begin([ | |
17 | + 'action' => '/backend/web/index.php?r=import/upload', | |
18 | + 'options' => [ | |
19 | + 'enctype' => 'multipart/form-data', | |
20 | + 'class' => 'import', | |
21 | + ] | |
22 | + ]); | |
23 | + ?> | |
24 | + | |
25 | + <?= $form->field($model, 'file[]')->fileInput(['multiple' => true ]) ?> | |
26 | + | |
27 | + <div class="form-group"> | |
28 | + <?= Html::submitButton('button', ['class' => 'btn btn-primary']) ?> | |
29 | + </div> | |
30 | + | |
31 | + <?php ActiveForm::end(); ?> | |
32 | + | |
33 | +</div> | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace common\models; | |
4 | + | |
5 | +class SqlQueryBuilder | |
6 | +{ | |
7 | + /** | |
8 | + * @var array list of data to build the query | |
9 | + */ | |
10 | + protected $query = array( | |
11 | + 'select' => array(), | |
12 | + 'from' => '', | |
13 | + 'join' => array(), | |
14 | + 'where' => array(), | |
15 | + 'group' => array(), | |
16 | + 'having' => array(), | |
17 | + 'order' => array(), | |
18 | + 'limit' => array('offset' => 0, 'limit' => 0), | |
19 | + ); | |
20 | + | |
21 | + /** | |
22 | + * Add fields in query selection | |
23 | + * | |
24 | + * @param string $fields List of fields to concat to other fields | |
25 | + * @return DbQuery | |
26 | + */ | |
27 | + public function select ($fields) | |
28 | + { | |
29 | + if (! empty ($fields)) | |
30 | + { | |
31 | + $this->query['select'][] = $fields; | |
32 | + } | |
33 | + return $this; | |
34 | + } | |
35 | + | |
36 | + /** | |
37 | + * Set table for FROM clause | |
38 | + * | |
39 | + * @param string $table Table name | |
40 | + * @return DbQuery | |
41 | + */ | |
42 | + public function from ($table, $alias = null) | |
43 | + { | |
44 | + if (! empty ($table)) | |
45 | + { | |
46 | + $this->query['from'][] = '`'.$table.'`'.($alias ? ' '.$alias : ''); | |
47 | + } | |
48 | + return $this; | |
49 | + } | |
50 | + | |
51 | + /** | |
52 | + * Add JOIN clause | |
53 | + * E.g. $this->join('RIGHT JOIN '._DB_PREFIX_.'product p ON ...'); | |
54 | + * | |
55 | + * @param string $join Complete string | |
56 | + * @return DbQuery | |
57 | + */ | |
58 | + public function join ($join) | |
59 | + { | |
60 | + if (! empty ($join)) | |
61 | + { | |
62 | + $this->query['join'][] = $join; | |
63 | + } | |
64 | + return $this; | |
65 | + } | |
66 | + | |
67 | + /** | |
68 | + * Add LEFT JOIN clause | |
69 | + * | |
70 | + * @param string $table Table name (without prefix) | |
71 | + * @param string $alias Table alias | |
72 | + * @param string $on ON clause | |
73 | + */ | |
74 | + public function leftJoin ($table, $alias = null, $on = null) | |
75 | + { | |
76 | + return $this->join('LEFT JOIN `'.$table.'`'.($alias ? ' `'.$alias.'`' : '').($on ? ' ON '.$on : '')); | |
77 | + } | |
78 | + | |
79 | + /** | |
80 | + * Add INNER JOIN clause | |
81 | + * E.g. $this->innerJoin('product p ON ...') | |
82 | + * | |
83 | + * @param string $table Table name (without prefix) | |
84 | + * @param string $alias Table alias | |
85 | + * @param string $on ON clause | |
86 | + */ | |
87 | + public function innerJoin ($table, $alias = null, $on = null) | |
88 | + { | |
89 | + return $this->join('INNER JOIN `'.$table.'`'.($alias ? ' '.$alias : '').($on ? ' ON '.$on : '')); | |
90 | + } | |
91 | + | |
92 | + /** | |
93 | + * Add LEFT OUTER JOIN clause | |
94 | + * | |
95 | + * @param string $table Table name (without prefix) | |
96 | + * @param string $alias Table alias | |
97 | + * @param string $on ON clause | |
98 | + */ | |
99 | + public function leftOuterJoin ($table, $alias = null, $on = null) | |
100 | + { | |
101 | + return $this->join('LEFT OUTER JOIN `'.$table.'`'.($alias ? ' '.$alias : '').($on ? ' ON '.$on : '')); | |
102 | + } | |
103 | + | |
104 | + /** | |
105 | + * Add NATURAL JOIN clause | |
106 | + * | |
107 | + * @param string $table Table name (without prefix) | |
108 | + * @param string $alias Table alias | |
109 | + */ | |
110 | + public function naturalJoin ($table, $alias = null) | |
111 | + { | |
112 | + return $this->join('NATURAL JOIN `'.$table.'`'.($alias ? ' '.$alias : '')); | |
113 | + } | |
114 | + | |
115 | + /** | |
116 | + * Add a restriction in WHERE clause (each restriction will be separated by AND statement) | |
117 | + * | |
118 | + * @param string $restriction | |
119 | + * @return DbQuery | |
120 | + */ | |
121 | + public function where ($restriction) | |
122 | + { | |
123 | + if (! empty ($restriction)) | |
124 | + { | |
125 | + $this->query['where'][] = $restriction; | |
126 | + } | |
127 | + return $this; | |
128 | + } | |
129 | + | |
130 | + /** | |
131 | + * Add a restriction in HAVING clause (each restriction will be separated by AND statement) | |
132 | + * | |
133 | + * @param string $restriction | |
134 | + * @return DbQuery | |
135 | + */ | |
136 | + public function having ($restriction) | |
137 | + { | |
138 | + if (! empty ($restriction)) | |
139 | + { | |
140 | + $this->query['having'][] = $restriction; | |
141 | + } | |
142 | + return $this; | |
143 | + } | |
144 | + | |
145 | + /** | |
146 | + * Add an ORDER B restriction | |
147 | + * | |
148 | + * @param string $fields List of fields to sort. E.g. $this->order('myField, b.mySecondField DESC') | |
149 | + * @return DbQuery | |
150 | + */ | |
151 | + public function orderBy ($fields) | |
152 | + { | |
153 | + if (! empty ($fields)) | |
154 | + { | |
155 | + $this->query['order'][] = $fields; | |
156 | + } | |
157 | + return $this; | |
158 | + } | |
159 | + | |
160 | + /** | |
161 | + * Add a GROUP BY restriction | |
162 | + * | |
163 | + * @param string $fields List of fields to sort. E.g. $this->group('myField, b.mySecondField DESC') | |
164 | + * @return DbQuery | |
165 | + */ | |
166 | + public function groupBy ($fields) | |
167 | + { | |
168 | + if (! empty ($fields)) | |
169 | + { | |
170 | + $this->query['group'][] = $fields; | |
171 | + } | |
172 | + return $this; | |
173 | + } | |
174 | + | |
175 | + /** | |
176 | + * Limit results in query | |
177 | + * | |
178 | + * @param string $fields List of fields to sort. E.g. $this->order('myField, b.mySecondField DESC') | |
179 | + * @return DbQuery | |
180 | + */ | |
181 | + public function limit ($limit, $offset = 0) | |
182 | + { | |
183 | + $offset = (int)$offset; | |
184 | + if ($offset < 0) | |
185 | + { | |
186 | + $offset = 0; | |
187 | + } | |
188 | + | |
189 | + $this->query['limit'] = array( | |
190 | + 'offset' => $offset, | |
191 | + 'limit' => (int)$limit, | |
192 | + ); | |
193 | + return $this; | |
194 | + } | |
195 | + | |
196 | + /** | |
197 | + * Generate and get the query | |
198 | + * | |
199 | + * @return string | |
200 | + */ | |
201 | + public function build () | |
202 | + { | |
203 | + $sql = 'SELECT '.((($this->query['select'])) ? implode (",\n", $this->query['select']) : '*')."\n"; | |
204 | + | |
205 | + if (! $this->query['from']) | |
206 | + { | |
207 | + die('DbQuery->build() missing from clause'); | |
208 | + } | |
209 | + $sql .= 'FROM '.implode (', ', $this->query['from'])."\n"; | |
210 | + | |
211 | + if ($this->query['join']) | |
212 | + { | |
213 | + $sql .= implode ("\n", $this->query['join'])."\n"; | |
214 | + } | |
215 | + | |
216 | + if ($this->query['where']) | |
217 | + { | |
218 | + $sql .= 'WHERE ('.implode (') AND (', $this->query['where']).")\n"; | |
219 | + } | |
220 | + | |
221 | + if ($this->query['group']) | |
222 | + { | |
223 | + $sql .= 'GROUP BY '.implode(', ', $this->query['group'])."\n"; | |
224 | + } | |
225 | + | |
226 | + if ($this->query['having']) | |
227 | + { | |
228 | + $sql .= 'HAVING ('.implode (') AND (', $this->query['having']).")\n"; | |
229 | + } | |
230 | + | |
231 | + if ($this->query['order']) | |
232 | + { | |
233 | + $sql .= 'ORDER BY '.implode (', ', $this->query['order'])."\n"; | |
234 | + } | |
235 | + | |
236 | + if ($this->query['limit']['limit']) | |
237 | + { | |
238 | + $limit = $this->query['limit']; | |
239 | + $sql .= 'LIMIT '.(($limit['offset']) ? $limit['offset'].', '.$limit['limit'] : $limit['limit']); | |
240 | + } | |
241 | +/* | |
242 | + ob_start(); | |
243 | + var_dump($sql); | |
244 | + echo ob_get_clean(); | |
245 | +*/ | |
246 | + return $sql; | |
247 | + } | |
248 | + | |
249 | + public function __toString () | |
250 | + { | |
251 | + return $this->build(); | |
252 | + } | |
253 | +} | |
254 | + | ... | ... |
1 | +<?php | |
2 | + | |
3 | +namespace common\models; | |
4 | + | |
5 | +class Tools | |
6 | +{ | |
7 | + /** | |
8 | + * Проверяет на наличие элементов в массиве, если нет - создает с значением или без | |
9 | + * @param array $mass | |
10 | + * @param array $exist | |
11 | + */ | |
12 | + static function ifNotExist (&$mass, $exist) | |
13 | + { | |
14 | + if (! is_array ($mass)) | |
15 | + { | |
16 | + $mass = array(); | |
17 | + } | |
18 | + | |
19 | + foreach ($exist as $key => $value) | |
20 | + { | |
21 | + if (is_int ($key)) | |
22 | + { | |
23 | + $exist[$value] = ''; | |
24 | + unset ($exist[$key]); | |
25 | + } | |
26 | + } | |
27 | + | |
28 | + foreach ($exist as $key => $value) | |
29 | + { | |
30 | + if (! isset ($mass[$key]) | |
31 | + || (isset ($mass[$key]) && $mass[$key] === '')) | |
32 | + { | |
33 | + $mass[$key] = $value; | |
34 | + } | |
35 | + } | |
36 | + } | |
37 | + | |
38 | + static function translit ($string, $setting = 'all') | |
39 | + { | |
40 | + $letter = array ( | |
41 | + | |
42 | + 'а' => 'a', 'б' => 'b', 'в' => 'v', | |
43 | + 'г' => 'g', 'д' => 'd', 'е' => 'e', | |
44 | + 'ё' => 'e', 'ж' => 'zh', 'з' => 'z', | |
45 | + 'и' => 'i', 'й' => 'y', 'к' => 'k', | |
46 | + 'л' => 'l', 'м' => 'm', 'н' => 'n', | |
47 | + 'о' => 'o', 'п' => 'p', 'р' => 'r', | |
48 | + 'с' => 's', 'т' => 't', 'у' => 'u', | |
49 | + 'ф' => 'f', 'х' => 'h', 'ц' => 'c', | |
50 | + 'ч' => 'ch', 'ш' => 'sh', 'щ' => 'sch', | |
51 | + 'ь' => "", 'ы' => 'y', 'ъ' => "", | |
52 | + 'э' => 'e', 'ю' => 'yu', 'я' => 'ya', | |
53 | + 'ї' => 'yi', 'є' => 'ye', 'і' => 'ee', | |
54 | + | |
55 | + 'А' => 'A', 'Б' => 'B', 'В' => 'V', | |
56 | + 'Г' => 'G', 'Д' => 'D', 'Е' => 'E', | |
57 | + 'Ё' => 'E', 'Ж' => 'Zh', 'З' => 'Z', | |
58 | + 'И' => 'I', 'Й' => 'Y', 'К' => 'K', | |
59 | + 'Л' => 'L', 'М' => 'M', 'Н' => 'N', | |
60 | + 'О' => 'O', 'П' => 'P', 'Р' => 'R', | |
61 | + 'С' => 'S', 'Т' => 'T', 'У' => 'U', | |
62 | + 'Ф' => 'F', 'Х' => 'H', 'Ц' => 'C', | |
63 | + 'Ч' => 'Ch', 'Ш' => 'Sh', 'Щ' => 'Sch', | |
64 | + 'Ь' => "", 'Ы' => 'Y', 'Ъ' => "", | |
65 | + 'Э' => 'E', 'Ю' => 'Yu', 'Я' => 'Ya', | |
66 | + 'Ї' => 'Yi', 'Є' => 'Ye', 'І' => 'Ee' | |
67 | + ); | |
68 | + | |
69 | + $symbol = array ( | |
70 | + ' ' => '-', "'" => '', '"' => '', | |
71 | + '!' => '', "@" => '', '#' => '', | |
72 | + '$' => '', "%" => '', '^' => '', | |
73 | + ';' => '', "*" => '', '(' => '', | |
74 | + ')' => '', "+" => '', '~' => '', | |
75 | + '.' => '', ',' => '-', '?' => '', | |
76 | + '…' => '', '№' => 'N', '°' => '', | |
77 | + '`' => '', '|' => '', '&' => '-and-', | |
78 | + '<' => '', '>' => '' | |
79 | + ); | |
80 | + | |
81 | + if ($setting == 'all') | |
82 | + { | |
83 | + $converter = $letter + $symbol; | |
84 | + } | |
85 | + else if ($setting == 'letter') | |
86 | + { | |
87 | + $converter = $letter; | |
88 | + } | |
89 | + else if ($setting == 'symbol') | |
90 | + { | |
91 | + $converter = $symbol; | |
92 | + } | |
93 | + | |
94 | + $url = strtr ($string, $converter); | |
95 | + | |
96 | + $url = str_replace ("---", '-', $url); | |
97 | + $url = str_replace ("--", '-', $url); | |
98 | + | |
99 | + return $url; | |
100 | + } | |
101 | +} | |
0 | 102 | \ No newline at end of file | ... | ... |