ProductControllerCop.php 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318
  1. <?php
  2. namespace Packages\Product\Http\Controllers;
  3. use Illuminate\Support\Facades\Auth;
  4. use App\Http\Controllers\Controller;
  5. use Facade\Ignition\Support\Packagist\Package;
  6. use Illuminate\Database\Eloquent\Builder;
  7. use Illuminate\Support\Facades\Facade;
  8. use Illuminate\Support\Facades\File;
  9. use Illuminate\Support\Facades\Storage;
  10. use Illuminate\Support\Str;
  11. use Packages\Product\Models\Draft;
  12. use Packages\Product\Http\Requests\ProductRequest;
  13. use Packages\Product\Http\Requests\ProductRequestEdit;
  14. use Packages\Product\Models\Product;
  15. use Illuminate\Http\Request;
  16. use App\Models\Category;
  17. use App\Models\Upload;
  18. use phpDocumentor\Reflection\Types\Compound;
  19. class ProductController extends Controller
  20. {
  21. public function __construct()
  22. {
  23. }
  24. // *******************************************show page product ************
  25. public function index()
  26. {
  27. $categories = Category::orderBy('id', 'DESC')->get();
  28. $draft = Draft::orderBy('id', 'DESC')->first();
  29. $products = Product::query()->orderBy('id', 'DESC');
  30. if (request()->has('productCategory') && request('productCategory') != '')
  31. $products = $products->whereHas('categories', function (Builder $query) {
  32. $query->whereRaw('id = ?', [request('productCategory')]);
  33. });
  34. if (request()->has('productStatus') && request('productStatus') != '')
  35. $products = $products->whereRaw('status = ?', [request('productStatus')]);
  36. if (request()->has('productName') && request('productName') != '')
  37. $products = $products->whereRaw('title = ?', [request('productName')]);
  38. if (request()->has('creatorId') && request('creatorId') != '')
  39. $products = $products->whereHas('user', function (Builder $query) {
  40. $query->whereRaw('name= ?', [request('creatorId')]);
  41. });
  42. $products = $products->paginate();
  43. return view('product::index', compact('products', 'categories', 'draft'));
  44. }
  45. //**************************************************** page create products *****************
  46. public function create(Request $request)
  47. {
  48. $categories = Category::orderBy('parent_id')->where('parent_id', 0)->get();
  49. $uploads = Upload::orderBy('id')->get()->first();
  50. if (!$request->has('draft_id')) {
  51. $draft = new Draft();
  52. $draft->object_type = 'Packages\Product\Draft';
  53. $draft->delete();
  54. $draft->save();
  55. return redirect()->route('products.create', ['draft_id' => $draft->id]);
  56. } else {
  57. $draft = Draft::find($request->draft_id);
  58. if (is_null($draft)) {
  59. return redirect()->route('products.create');
  60. }
  61. }
  62. $args = [
  63. 'draft' => $draft
  64. ];
  65. $product= Product:: orderBy('id', 'DESC');
  66. return view('product::product.create', $args, compact('categories', 'product', 'uploads'));
  67. }
  68. // *********************************************************save products ******************
  69. public function store(ProductRequest $request)
  70. {
  71. // dd($request->all());
  72. $request->merge([
  73. 'price' => preg_replace('/[^0-9]+/', '', $request->price),
  74. 'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
  75. ]);
  76. if (!$request->has('draft_id')) {
  77. return abort(403, 'دسترسی به این صفحه ممکن نیست ؛ لطفا مجددا تلاش کنید.');
  78. }
  79. $draft = Draft::findOrFail($request->draft_id);
  80. // $info = $request->only(['featureDescription', 'featureCaption']);
  81. $data = [
  82. 'title' => $request->title,
  83. 'slug' => $request->slug,
  84. 'sku' => $request->sku,
  85. 'ReleaseStatus' => $request->ReleaseStatus,
  86. 'price' => $request->price,
  87. 'sale_price' => $request->sale_price,
  88. 'type' => $request->type,
  89. 'status' => $request->status,
  90. 'discription' => $request->discription,
  91. 'creator_id' => auth()->user()->id,
  92. // 'descriptionImg' => $request->featureDescription,
  93. // 'caption' => $request->featureCaption,
  94. ];
  95. // if( $draft->attachments->count() == 0 ){
  96. // return redirect()->back()
  97. // ->withErrors( ['error_message' => 'حتما باید یک فایل آپلود شود.'] )
  98. // ->withInput( $request->all() );
  99. // }
  100. // dd($draft->attachments->count() == 0);
  101. $product = Product::create($data);
  102. $product->categories()->sync($request->categories);
  103. Upload::where('parent_type', 'Packages\Product')->where('uploadable_id', $draft->id)->where('uploadable_id', $draft->id)
  104. ->update(['parent_type' => 'image\Product', 'uploadable_id' => $product->id]);
  105. $draft->delete();
  106. // $uploaded = Upload::create();
  107. $msg = 'ذخیره محصول با موفقیت انجام شد ';
  108. return redirect(route('products.edit', $product->id))->with('success', $msg);
  109. }
  110. // ******************************************************edit page product*********************************************
  111. public function edit(Product $product)
  112. {
  113. $categories = Category::where('parent_id', 0)->orderBy('title')->get();
  114. return view('product::product.edit', compact('product', 'categories'));
  115. }
  116. // *******************************************update page product*****************************************
  117. public function update(ProductRequestEdit $request, Product $product)
  118. {
  119. $request->merge([
  120. 'price' => preg_replace('/[^0-9]+/', '', $request->price),
  121. 'sale_price' => preg_replace('/[^0-9]+/', '', $request->sale_price),
  122. ]);
  123. $data = [
  124. 'title' => $request->title,
  125. 'slug' => $request->slug,
  126. 'sku' => $request->sku,
  127. 'price' => $request->price,
  128. 'sale_price' => $request->sale_price,
  129. 'type' => $request->type,
  130. 'ReleaseStatus' => $request->ReleaseStatus,
  131. 'status' => $request->status,
  132. 'discription' => $request->discription,
  133. 'creator_id' => auth()->user()->id,
  134. ];
  135. if ($request->has('gallery_image')) {
  136. $file = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
  137. $type = 'gallery_image';
  138. $diskName = 'product';
  139. $this->updateGallery($file, $diskName, $product, $type);
  140. }
  141. if ($request->has('featured_image')) {
  142. $info = $request->only(['featureDescription', 'featureCaption']);
  143. $file = $request->file('featured_image');
  144. $type = 'featured_image';
  145. $diskName = 'product';
  146. $this->updateFeature($file, $diskName, $product, $type, $info);
  147. }
  148. $product->update($data);
  149. $product->categories()->sync($request->categories);
  150. $msg = 'ویرایش محصول با موفقیت انجام شد ';
  151. return redirect(route('products.index'))->with('success', $msg);
  152. }
  153. //***************************** delete products*****************************************
  154. public function destroy(Product $product)
  155. {
  156. $product->delete();
  157. {
  158. return back();
  159. }
  160. }
  161. //****************upload images in the product************************
  162. public function uploadFeature(Request $request)
  163. {
  164. if ($request->has('featured_image')) {
  165. $info = $request->only(['featureDescription', 'featureCaption']);
  166. $file = $request->file('featured_image');
  167. $type = 'featured_image';
  168. $diskName = 'product';
  169. $fileExtension = $file->getClientOriginalExtension();
  170. $fileMimeType = $file->getMimeType();
  171. $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
  172. $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
  173. $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
  174. if (File::exists($filePath)) {
  175. $fileName = time() . '_' . $fileName;
  176. }
  177. $draft = Draft::orderBy('id', 'Desc')->get()->first();
  178. $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
  179. $uploadData = [
  180. 'name' => $fileName,
  181. 'path' => $upload,
  182. 'mime_type' => $fileMimeType,
  183. 'extension' => $fileExtension,
  184. 'parent_type' => 'packages\product',
  185. 'uploadable_type' => 'Packages\Product\Models\Product',
  186. 'uploadable_id' => $draft->id,
  187. 'type' => $type,
  188. 'descriptionImg' => $info['featureDescription'],
  189. 'caption' => $info['featureCaption'],
  190. ];
  191. $uploads = Upload::where('type', $type)->where('uploadable_id', $draft->id)->first();
  192. if (empty($uploads)) {
  193. $uploaded = Upload::create($uploadData);
  194. } else {
  195. $uploaded = $uploads->update($uploadData);
  196. }
  197. }
  198. }
  199. public function updateFeature($file, $diskName, $product, $type, $info)
  200. {
  201. $fileExtension = $file->getClientOriginalExtension();
  202. $fileMimeType = $file->getMimeType();
  203. $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
  204. $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
  205. $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
  206. if (File::exists($filePath)) {
  207. $fileName = time() . '_' . $fileName;
  208. }
  209. $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
  210. $uploadData = [
  211. 'name' => $fileName,
  212. 'path' => $upload,
  213. 'mime_type' => $fileMimeType,
  214. 'extension' => $fileExtension,
  215. 'type' => $type,
  216. 'descriptionImg' => $info['featureDescription'],
  217. 'caption' => $info['featureCaption'],
  218. ];
  219. $uploaded = $product->uploads()->update($uploadData);
  220. }
  221. //************************delete image in the product**********************
  222. public function destroyFile(Upload $upload)
  223. {
  224. $upload->delete();
  225. $msg = 'حذف عکس محصول با موفقیت انجام شد ';
  226. session()->flash('image_deleted', $msg);
  227. return redirect()->back();
  228. }
  229. // ****************************uploadGallery********************
  230. public function uploadGallery(Request $request)
  231. {
  232. if ($request->has('gallery_image')) {
  233. $files = $request->only('gallery_image', 'captionGallery', 'descriptionGallery');
  234. $type = 'gallery_image';
  235. $diskName = 'product';
  236. // dd($files);
  237. foreach ($files['gallery_image'] as $key => $file) {
  238. $fileExtension = $file->getClientOriginalExtension();
  239. $fileMimeType = $file->getMimeType();
  240. $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
  241. $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
  242. $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
  243. if (File::exists($filePath)) {
  244. $fileName = time() . '_' . $fileName;
  245. }
  246. $draft = Draft::orderBy('id', 'Desc')->get()->first();
  247. $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
  248. $uploadData = [
  249. 'name' => $fileName,
  250. 'path' => $upload,
  251. 'mime_type' => $fileMimeType,
  252. 'extension' => $fileExtension,
  253. 'parent_type' => 'packages\product',
  254. 'uploadable_type' => 'Packages\Product\Models\Product',
  255. 'uploadable_id' => $draft->id,
  256. 'type' => $type,
  257. 'descriptionImg' => $files['descriptionGallery'][$key],
  258. 'caption' => $files['captionGallery'][$key],
  259. ];
  260. // dd($uploadData);
  261. // $uploaded = Upload::where('uploadable_id', $draft->id)->update($uploadData);
  262. $uploaded = Upload::create($uploadData);
  263. dd($uploaded);
  264. }
  265. }
  266. }
  267. public function updateGallery($files, $diskName, $product, $type)
  268. {
  269. foreach ($files['gallery_image'] as $key => $file) {
  270. $fileExtension = $file->getClientOriginalExtension();
  271. $fileMimeType = $file->getMimeType();
  272. $afterDiskRoot = '/' . jdate()->format('Y') . '/' . jdate()->format('m');
  273. $fileName = jdate(time())->format('Ymd') . '_' . $file->getClientOriginalName();
  274. $filePath = storage_path('app/public/' . $diskName . $afterDiskRoot . '/' . $fileName);
  275. if (File::exists($filePath)) {
  276. $fileName = time() . '_' . $fileName;
  277. }
  278. $upload = $file->storeAs($afterDiskRoot, $fileName, $diskName);
  279. $uploadData = [
  280. 'name' => $fileName,
  281. 'path' => $upload,
  282. 'mime_type' => $fileMimeType,
  283. 'extension' => $fileExtension,
  284. 'type' => $type,
  285. 'descriptionImg' => $files['descriptionGallery'][$key],
  286. 'caption' => $files['captionGallery'][$key],
  287. ];
  288. $uploaded = $product->uploads()->update($uploadData);
  289. }
  290. }
  291. }