10
10
use Illuminate \Database \Eloquent \Model ;
11
11
use Illuminate \Database \Eloquent \Collection ;
12
12
use Illuminate \Pagination \LengthAwarePaginator ;
13
+ use Illuminate \Support \Facades \App ;
14
+ use AvoRed \Framework \Models \Contracts \ProductDownloadableUrlInterface ;
15
+ use AvoRed \Framework \Models \Repository \ProductDownloadableUrlRepository ;
13
16
14
17
class Product extends Model
15
18
{
@@ -170,6 +173,7 @@ public function saveProduct($data)
170
173
$ this ->saveProductCategories ($ data );
171
174
$ this ->saveProductProperties ($ data );
172
175
$ this ->saveProductAttributes ($ data );
176
+ $ this ->saveProductDownloadable ($ data );
173
177
174
178
Event::fire (new ProductAfterSave ($ this , $ data ));
175
179
@@ -187,6 +191,58 @@ protected function saveProductCategories($data)
187
191
$ this ->categories ()->sync ($ data ['category_id ' ]);
188
192
}
189
193
}
194
+
195
+ /**
196
+ * Save Product Downloadable Information
197
+ *
198
+ * @param array $data
199
+ * @return void
200
+ */
201
+ protected function saveProductDownloadable ($ data )
202
+ {
203
+
204
+ if (isset ($ data ['downloadable ' ]) && count ($ data ['downloadable ' ]) > 0 ) {
205
+
206
+ $ repository = App::get (ProductDownloadableUrlInterface::class);
207
+
208
+
209
+ $ mainDownloadableMedia = ($ data ['downloadable ' ]['main_product ' ]) ?? null ;
210
+
211
+ if (null === $ mainDownloadableMedia ) {
212
+ throw new \Exception ('Invalid Downloadable Media Given or Nothing Given ' );
213
+ }
214
+
215
+ $ tmpPath = str_split (strtolower (str_random (3 )));
216
+ $ path = 'uploads/downloadables/ ' . implode ('/ ' , $ tmpPath );
217
+ $ dbPath = $ mainDownloadableMedia ->store ($ path ,'avored ' );
218
+ $ token = str_random (32 );
219
+
220
+
221
+ $ downModel = $ repository ->query ()->whereProductId ($ this ->id )->first ();
222
+
223
+ if (null === $ downModel ) {
224
+ $ downModel = ProductDownloadableUrl::create ([
225
+ 'token ' => $ token ,
226
+ 'product_id ' => $ this ->id ,
227
+ 'main_path ' => $ dbPath
228
+ ]);
229
+ } else {
230
+ $ downModel ->update (['main_path ' => $ dbPath ]);
231
+ }
232
+
233
+
234
+
235
+ $ demoDownloadableMedia = ($ data ['downloadable ' ]['demo_product ' ]) ?? null ;
236
+
237
+ if (null !== $ demoDownloadableMedia ) {
238
+ $ tmpPath = str_split (strtolower (str_random (3 )));
239
+ $ path = 'uploads/downloadables/ ' . implode ('/ ' , $ tmpPath );
240
+ $ demoDbPath = $ demoDownloadableMedia ->store ($ path ,'avored ' );
241
+
242
+ $ downModel ->update (['demo_path ' => $ demoDbPath ]);
243
+ }
244
+ }
245
+ }
190
246
/**
191
247
* Save Product Attributes
192
248
*
@@ -624,4 +680,14 @@ public function orders()
624
680
{
625
681
return $ this ->hasMany (Order::class);
626
682
}
683
+
684
+ /**
685
+ * Product has downladable Url.
686
+ *
687
+ * @return \AvoRed\Framework\Models\Database\ProductDownloadableUrl
688
+ */
689
+ public function downloadable ()
690
+ {
691
+ return $ this ->hasOne (ProductDownloadableUrl::class);
692
+ }
627
693
}
0 commit comments