· 5 years ago · Jun 11, 2020, 01:02 PM
1<?php
2
3namespace App\Imports;
4
5use App\Cms\Brands;
6use App\Cms\Categories;
7use App\Cms\Seo;
8use App\Cms\Social;
9use App\Cms\Products;
10use App\Cms\Subproducts;
11use Illuminate\Support\Str;
12use Illuminate\Http\Request;
13use Illuminate\Support\Collection;
14use Illuminate\Support\Facades\App;
15use Illuminate\Support\Facades\Auth;
16use Maatwebsite\Excel\Concerns\Importable;
17use Illuminate\Contracts\Queue\ShouldQueue;
18use Maatwebsite\Excel\Concerns\ToCollection;
19use Maatwebsite\Excel\Concerns\WithStartRow;
20use Maatwebsite\Excel\Concerns\WithBatchInserts;
21use Maatwebsite\Excel\Concerns\WithChunkReading;
22
23class ProductsImport implements ToCollection, WithChunkReading, ShouldQueue, WithBatchInserts
24{
25
26 use \App\Http\Traits\TraitSeo;
27 use \App\Http\Traits\TraitSocial;
28 use \App\Http\Traits\TraitCache;
29 use Importable;
30
31 public function collection(Collection $rows)
32 {
33 $counter = 0;
34 //XLS Importer
35 foreach ($rows as $key => $row) {
36
37 //If the key is bigger then 6 (6 = headers), you can go further.
38 if ($key > 6) {
39
40 //Here we take the first part of the title before the - (that's the product title)
41 $title = explode('-', $row[4]);
42 $productTitle = trim($title[0]);
43
44 //First we gonna check if the title doesn't exists in the products table.
45 //If the title does not exists, we gonna make a product for that.
46 if (!Products::where('title', $productTitle)->exists()) {
47
48
49 $arr_firstblock = [
50 'type' => 'text',
51 'id' => str_random(10),
52 ];
53
54 //CREATE A NEW PRODUCT
55 $product = new Products;
56 $product->active = 'Y';
57 $product->siteid = 1; //(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
58 //$product->place = 0;
59 $product->lang = 'nl'; //(App::getLocale()) ? App::getLocale() : 'nl';
60 $product->ctime = strtotime("now");
61 $product->online = "U";
62 $product->ontime = strtotime("now");
63 $product->offtime = 0;
64 $product->cuserid = 4; //(Auth::user()->id) ? Auth::user()->id : 4;
65 $product->mtime = strtotime("now");
66 $product->muserid = 4; //(Auth::user()->id) ? Auth::user()->id : 4;
67 $product->title = $productTitle;
68 $product->intro = '';
69 $product->content = '';
70 $product->crypt = str_slug(password_hash($productTitle.'[3*!-& test]', PASSWORD_DEFAULT));
71 $product->pagebuild = [$arr_firstblock];
72 $product->save();
73
74 $product->description = 'description';
75 $product->usages = 'usages';
76 $product->technical = 'technical';
77 $product->second_hand = 0;
78 $product->in_stock = 0;
79 $product->save();
80
81
82
83 $categorieCode = Categories::where('title', $row[72] )->get();
84 $brandCode = Brands::where('title', $row[36] )->get();
85
86 $product->categories()->sync($categorieCode[0]->id, []);
87 $product->brands()->sync($brandCode[0]->id, []);
88
89
90 // HERE WE GONNE MAKE A NEW SEO FOR THE PRODUCT THAT JUST HAS BEEN CREATED
91 $slug = str_slug($product->title.str_random(5));
92
93 // How many of the same slugs are in the database for this module?
94 $arr_count = Seo::where('module', Session('import_modulename'))
95 ->where('siteid', Session('ss_cms_domainid'))
96 ->where('lang', strtolower(Session('ss_cms_activelang')))
97 ->where('slug', $slug)
98 ->count();
99
100 $arr_seo_item = new Seo;
101 $arr_seo_item->manual = 'N';
102 $arr_seo_item->module = 'products';
103 $arr_seo_item->lang = 'nl';// (App::getLocale()) ? App::getLocale() : 'nl';
104 $arr_seo_item->itemid = $product->id;
105 $arr_seo_item->siteid = 1; //(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
106 $arr_seo_item->title = $productTitle;
107 $arr_seo_item->custom_title = null;
108 $arr_seo_item->slug = $arr_count == 0 ? $slug : $slug.'-'.($arr_count + 1);
109 $arr_seo_item->description = null;
110 $arr_seo_item->keywords = null;
111 $arr_seo_item->nofollow = 'N';
112 $arr_seo_item->noindex = 'N';
113 $arr_seo_item->save();
114
115 $arr_social_item = new Social;
116 $arr_social_item->manual = 'N';
117 $arr_social_item->module = 'products';
118 $arr_social_item->lang = 'nl'; //(App::getLocale()) ? App::getLocale() : 'nl';
119 $arr_social_item->itemid = $product->id;
120 $arr_social_item->siteid = 1; //(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
121 $arr_social_item->title = $productTitle;
122 $arr_social_item->link = $arr_seo_item->slug;
123 $arr_social_item->description = '';
124 $arr_social_item->save();
125
126
127 //For each product that has been created, check to create a subproduct of it.
128 // Here we check if there is a subproduct with that article_nr. if so, skip
129 // If there is not a subproduct with that articlenr, then create one.
130 if (!Subproducts::where('article_nr', $row[3])->exists()) {
131 //HERE WE CREATE A SUBPRODUCT FOR THE PRODUCT
132 //get the PRODUCT ID of the product that will have a subproduct
133
134 $product_id = Products::where('title', $productTitle)->get();
135 $product_id = $product_id[0]->id;
136
137 $arr_firstblock = [
138 'type' => 'text',
139 'id' => str_random(10),
140 ];
141
142 $arr_sub_item = new Subproducts;
143 $arr_sub_item->active = 'y';
144 $arr_sub_item->siteid = 1;//(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
145 $arr_sub_item->lang = 'nl';
146 $arr_sub_item->ctime = strtotime("now");
147 $arr_sub_item->online = "U";
148 $arr_sub_item->ontime = strtotime("now");
149 $arr_sub_item->offtime = 0;
150 $arr_sub_item->cuserid = 4;//(Auth::user()->id) ? Auth::user()->id : 4;
151 $arr_sub_item->mtime = 0;
152 $arr_sub_item->muserid = 0;
153 $arr_sub_item->title = $row[4] ?? 'titel'.$key;
154 $arr_sub_item->article_nr = isset($row[3]) ? $row[3] : '';
155 $arr_sub_item->packaging = isset($row[6]) ? $row[6] : '';
156 $arr_sub_item->product_id = $product_id;
157 $arr_sub_item->intro = '';
158 $arr_sub_item->content = '';
159 $arr_sub_item->crypt = str_slug(password_hash($arr_sub_item->title.'[4*!-& test]',
160 PASSWORD_DEFAULT));
161 $arr_sub_item->pagebuild = [$arr_firstblock];
162 $arr_sub_item->save();
163
164 $specs = json_encode($title[1]);
165 $arr_sub_item->specs = $specs;
166 $arr_sub_item->save();
167
168 // HERE WE GONNE MAKE A NEW SEO FOR THE PRODUCT THAT JUST HAS BEEN CREATED
169 $slug = str_slug($product->title.str_random(5));
170
171 // How many of the same slugs are in the database for this module?
172 $arr_count = Seo::where('module', Session('import_modulename'))
173 ->where('siteid', Session('ss_cms_domainid'))
174 ->where('lang', strtolower(Session('ss_cms_activelang')))
175 ->where('slug', $slug)
176 ->count();
177
178 $arr_seo_item = new Seo;
179 $arr_seo_item->manual = 'N';
180 $arr_seo_item->module = 'subproducts';
181 $arr_seo_item->lang = 'nl';// (App::getLocale()) ? App::getLocale() : 'nl';
182 $arr_seo_item->itemid = $arr_sub_item->id;
183 $arr_seo_item->siteid = 1; //(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
184 $arr_seo_item->title = $arr_sub_item->title;
185 $arr_seo_item->custom_title = null;
186 $arr_seo_item->slug = $arr_count == 0 ? $slug : $slug.'-'.($arr_count + 1);
187 $arr_seo_item->description = null;
188 $arr_seo_item->keywords = null;
189 $arr_seo_item->nofollow = 'N';
190 $arr_seo_item->noindex = 'N';
191 $arr_seo_item->save();
192
193 }
194
195 } elseif (Products::where('title', $productTitle)->exists()) {
196
197 if (!Subproducts::where('article_nr', $row[3])->exists()) {
198
199 //HERE WE CREATE A SUBPRODUCT FOR THE PRODUCT.
200
201 //Here we take the first part of the title before the - (that's the product title)
202 $title = explode('-', $row[4]);
203 $productTitle = trim($title[0]);
204
205 //get the PRODUCT ID of the product that will have a subproduct
206 $product_id = Products::where('title', $productTitle)->get();
207 $product_id = $product_id[0]->id;
208
209 $arr_firstblock = [
210 'type' => 'text',
211 'id' => str_random(10),
212 ];
213
214 $arr_sub_item = new Subproducts;
215 $arr_sub_item->active = 'y';
216 $arr_sub_item->siteid = 1;//(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
217 $arr_sub_item->lang = 'nl';
218 $arr_sub_item->ctime = strtotime("now");
219 $arr_sub_item->online = "U";
220 $arr_sub_item->ontime = strtotime("now");
221 $arr_sub_item->offtime = 0;
222 $arr_sub_item->cuserid = 4;//(Auth::user()->id) ? Auth::user()->id : 4;
223 $arr_sub_item->mtime = 0;
224 $arr_sub_item->muserid = 0;
225 $arr_sub_item->title = $row[4] ?? 'titel'.$key;
226 $arr_sub_item->article_nr = isset($row[3]) ? $row[3] : '';
227 $arr_sub_item->packaging = isset($row[6]) ? $row[6] : '';
228 $arr_sub_item->product_id = $product_id;
229 $arr_sub_item->intro = '';
230 $arr_sub_item->content = '';
231 $arr_sub_item->crypt = str_slug(password_hash($arr_sub_item->title.'[4*!-& test]',
232 PASSWORD_DEFAULT));
233 $arr_sub_item->pagebuild = [$arr_firstblock];
234 $arr_sub_item->save();
235
236 $specs = json_encode($title[1]);
237 $arr_sub_item->specs = $specs;
238 $arr_sub_item->save();
239
240
241 // HERE WE GONNE MAKE A NEW SEO FOR THE PRODUCT THAT JUST HAS BEEN CREATED
242 $slug = str_slug($product->title.str_random(5));
243
244 // How many of the same slugs are in the database for this module?
245 $arr_count = Seo::where('module', Session('import_modulename'))
246 ->where('siteid', Session('ss_cms_domainid'))
247 ->where('lang', strtolower(Session('ss_cms_activelang')))
248 ->where('slug', $slug)
249 ->count();
250
251 $arr_seo_item = new Seo;
252 $arr_seo_item->manual = 'N';
253 $arr_seo_item->module = 'subproducts';
254 $arr_seo_item->lang = 'nl';// (App::getLocale()) ? App::getLocale() : 'nl';
255 $arr_seo_item->itemid = $arr_sub_item->id;
256 $arr_seo_item->siteid = 1; //(session('ss_cms_domainid')) ? session('ss_cms_domainid') : 1;
257 $arr_seo_item->title = $arr_sub_item->title;
258 $arr_seo_item->custom_title = null;
259 $arr_seo_item->slug = $arr_count == 0 ? $slug : $slug.'-'.($arr_count + 1);
260 $arr_seo_item->description = null;
261 $arr_seo_item->keywords = null;
262 $arr_seo_item->nofollow = 'N';
263 $arr_seo_item->noindex = 'N';
264 $arr_seo_item->save();
265
266 }
267 }
268
269 }
270
271 }
272 }
273
274 public function batchSize(): int
275 {
276 return 10000;
277 }
278
279 public function chunkSize(): int
280 {
281 return 10000;
282 }
283
284
285}