PageRenderTime 25ms CodeModel.GetById 20ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/backend/ProductController.php

https://gitlab.com/techniconline/kmc
PHP | 341 lines | 244 code | 49 blank | 48 comment | 16 complexity | f73d697c7e208ee9dba6c2fcd7c653ed MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers\backend;
  3. use App\Feature;
  4. use App\FeatureGroup;
  5. use App\FeatureValue;
  6. use App\Product;
  7. use App\Category;
  8. use App\Providers\Helpers\Category\CategoryHelper;
  9. use App\Providers\Helpers\PersianDate\PersianDate;
  10. use Illuminate\Support\Facades\Input;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\Redirect;
  13. use App\Media;
  14. use Illuminate\Support\Facades\Lang;
  15. use App\BaseModel;
  16. use App\Language;
  17. use Illuminate\Support\Facades\Config;
  18. use Intervention\Image\Facades\Image;
  19. use Illuminate\Support\Facades\File;
  20. use Illuminate\Support\Facades\Request;
  21. use Illuminate\Support\Facades\Session;
  22. use Illuminate\Support\Facades\DB;
  23. class ProductController extends BackendController
  24. {
  25. public $product;
  26. public $featureGroup;
  27. public $feature;
  28. public $featureValue;
  29. public $category;
  30. public $locale;
  31. public $langActive;
  32. public $language;
  33. public $listLang;
  34. public function __construct(Category $category, Product $product, Language $language, Feature $feature, FeatureGroup $featureGroup, FeatureValue $featureValue)
  35. {
  36. $this->products = $product;
  37. $this->category = $category;
  38. $this->featureGroup = $featureGroup;
  39. $this->feature = $feature;
  40. $this->featureValue = $featureValue;
  41. $this->listLang = Config::get('app.locales');
  42. $this->langActive = app('getLocale');
  43. $this->language = $language;
  44. $this->locale = app('locale') == '' ? '' : app('locale') . '.';
  45. }
  46. /**
  47. * Display a listing of the resource.
  48. *
  49. * @return Response
  50. */
  51. public function index()
  52. {
  53. $lang = $this->language->getLanguage($this->langActive);
  54. $data = $this->products
  55. ->join('products_lang', 'products_lang.id', '=', 'products.id')
  56. ->where('products.status', '=', 1)
  57. ->where('products_lang.lang_id', '=', $lang['lang_id'])
  58. ->select(['products_lang.title', 'products.*'])
  59. ->get();
  60. return view('backend.product.index')->withData($data)->with('locale', $this->locale);
  61. }
  62. /**
  63. * Show the form for creating a new resource.
  64. *
  65. * @return Response
  66. */
  67. public function create()
  68. {
  69. $lang = $this->language->getLanguage($this->langActive);
  70. $results = $this->category->getDataCategory(1);
  71. $roots = $results['roots'];
  72. $translatedName = $results['translatedLang'];
  73. $resFeature = $this->products->getDataFeatures($lang['lang_id'], []);
  74. return view('backend.product.create', ['btnTitle' => Lang::get('backend/Product.controller.InsertProduct')
  75. , 'error' => ''
  76. , 'dataFG' => $resFeature['dataFG']
  77. , 'allDataFG' => $resFeature['allDataFG']
  78. , 'color_selected' => $resFeature['color_selected']
  79. , 'title' => Lang::get('backend/Product.controller.CreateProduct')])
  80. ->with('roots', $roots)
  81. ->with('translatedName', $translatedName)
  82. ->with('locale', $this->locale)
  83. ->with('listLang', $this->listLang)
  84. ->with('setLang', app('getLocaleDefault'));
  85. }
  86. /**
  87. * Store a newly created resource in storage.
  88. *
  89. * @return Response
  90. */
  91. public function store()
  92. {
  93. $input = Input::all();
  94. $input['user_id'] = Auth::employee()->get()->id;
  95. $input['status'] = 1;
  96. $input['address'] = isset($input['formatted_address']) ? $input['formatted_address'] : null;
  97. $input['category_id'] = isset($input['categories']) ? $input['categories'][0] : null;
  98. $alias = isset($input['alias']) && $input['alias'] ? str_replace(" ", "-", $input['alias']) : CategoryHelper::createAlias($input['title']);
  99. $input['alias'] = $alias;
  100. $input['active_date'] = isset($input['active_date']) && $input['active_date'] ? $input['active_date'] : date('Y-m-d H:i:s');
  101. $activeDateArr = explode(" ", $input['active_date']);
  102. $input['active_date'] = PersianDate::convert_date_H_to_M($activeDateArr[0]) . ' ' . $activeDateArr[1];
  103. $fill = $this->products->fill($input);
  104. $isValid = $fill->validationData($input, $this->products->rules);
  105. if (!$isValid) {
  106. $return = Redirect::back()->withInput()->withErrors($this->products->errors);
  107. return $return;
  108. }
  109. $this->products->save();
  110. if ($id = $this->products->id) {
  111. if (Request::hasFile('image_product')) {
  112. $extension = Input::file('image_product')->getClientOriginalExtension();
  113. $createFolder = base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/product/' . $id . '/';
  114. if (!file_exists($createFolder)) {
  115. mkdir($createFolder, 0755);
  116. }
  117. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/product/' . $id . '/' . $id . '-1920x600.' . $extension)) {
  118. File::delete($id . '-1920x600.' . $extension);
  119. }
  120. Image::make(Input::file('image_product'))->fit(1920, 600)->save('uploads/images/product/' . $id . '/' . $id . '-1920x600.' . $extension);
  121. $updateProduct = $this->products->find($id);
  122. $updateProduct->image = '/uploads/images/product/' . $id . '/' . $id . '-1920x600.' . $extension;
  123. $updateProduct->save();
  124. $messages[] = ['level' => BaseModel::level_success, 'message' => Lang::get('backend/Content.messages.uploadFileSuccess')];
  125. Session::flash('messages', $messages);
  126. }
  127. $input['id'] = $id;
  128. foreach ($this->listLang as $key => $value) {
  129. $lang = $this->language->getLanguage($key);
  130. $input['lang_id'] = $lang['lang_id'];
  131. $alias = isset($input['alias']) && $input['alias'] ? str_replace(" ", "-", $input['alias']) : CategoryHelper::createAlias($input['title']);
  132. $input['alias'] = $alias;
  133. $result = $this->products->saveProductLang($input);
  134. }
  135. $resCategories = $this->products->saveProductCategories($id, $input['categories']);
  136. return Redirect::route($this->locale . 'backend.product.edit', array('product_id' => $id))->with('locale', $this->locale);
  137. }
  138. return Redirect::back()->withErrors(Lang::get('backend/Product.messages.errCreate'));
  139. }
  140. /**
  141. * Display the specified resource.
  142. *
  143. * @param int $product_id
  144. * @return Response
  145. */
  146. public function show($product_id)
  147. {
  148. // $data=$this->products->where('status',1)->find($product_id);
  149. // return view('backend.product.show',['data'=>$data])->with('locale',$this->locale);
  150. }
  151. /**
  152. * Show the form for editing the specified resource.
  153. *
  154. * @param int $product_id
  155. * @return Response
  156. */
  157. public function edit($product_id)
  158. {
  159. $results = $this->category->getDataCategory(1);
  160. $roots = $results['roots'];
  161. $translatedName = $results['translatedLang'];
  162. $lang = $this->language->getLanguage($this->langActive);
  163. // $data=$this->products->where('status',1)->find($product_id);
  164. $data = $this->products
  165. ->join('products_lang', 'products_lang.id', '=', 'products.id')
  166. ->where('products.id', '=', $product_id)
  167. ->where('products.status', '=', 1)
  168. ->where('products_lang.lang_id', '=', $lang['lang_id'])
  169. ->select(['products_lang.title', 'products_lang.short_description', 'products_lang.description', 'products_lang.alias', 'products.*'])
  170. ->get()->first();
  171. $errors = '';
  172. if (!$data)
  173. $errors[] = Lang::get('backend/Product.messages.errValid');
  174. $mediaModel = new Media();
  175. $images = $mediaModel->getMediaProduct('image', $product_id);
  176. $videos = $mediaModel->getMediaProduct('video', $product_id);
  177. $categoriesProduct = $this->products->getProductCategories($product_id, true);
  178. $featuresProduct = $this->featureValue
  179. ->join('feature_value_lang', 'feature_value.id', '=', 'feature_value_lang.feature_value_id')
  180. ->join('feature_product', 'feature_product.feature_value_id', '=', 'feature_value.id')
  181. ->where('feature_product.product_id', $product_id)
  182. ->where('feature_value_lang.lang_id', '=', $lang['lang_id'])
  183. ->select(['feature_value.id', 'feature_value_lang.value'])
  184. ->get();
  185. // ->lists('feature_value.id' , 'feature_value_lang.value');
  186. // dd($featuresProduct);
  187. $featuresProduct_NEW = [];
  188. foreach ($featuresProduct as $item) {
  189. $featuresProduct_NEW[$item->value][] = $item->id;
  190. }
  191. //
  192. // dd($featuresProduct_NEW);
  193. $resFeature = $this->products->getDataFeatures($lang['lang_id'], $featuresProduct_NEW);
  194. // dd($resFeature);
  195. return view('backend.product.create', [
  196. 'data' => $data
  197. , 'dataFG' => $resFeature['dataFG']
  198. , 'allDataFG' => $resFeature['allDataFG']
  199. , 'color_selected' => $resFeature['color_selected']
  200. , 'featuresProduct' => $featuresProduct
  201. , 'images' => $images
  202. , 'videos' => $videos
  203. , 'categoriesProduct' => $categoriesProduct
  204. , 'title' => Lang::get('backend/Product.controller.ProductEdit')
  205. , 'btnTitle' => Lang::get('backend/Product.controller.UpdateProduct')
  206. , 'error' => $errors
  207. ])
  208. ->with('roots', $roots)
  209. ->with('translatedName', $translatedName)
  210. ->with('locale', $this->locale)->with('listLang', $this->listLang)->with('setLang', $this->langActive);
  211. }
  212. /**
  213. * Update the specified resource in storage.
  214. *
  215. * @param int $product_id
  216. * @return Response
  217. */
  218. public function update($product_id)
  219. {
  220. $input = Input::all();
  221. $lang = $this->language->getLanguage($input['lang']);
  222. $input['lang_id'] = $lang['lang_id'];
  223. $inputImageClass = isset($input['class']) ? $input['class'] : null;
  224. $inputImageSort = isset($input['sort']) ? $input['sort'] : null;
  225. $inputImageFeatureSelect = isset($input['feature_value_selected']) ? $input['feature_value_selected'] : null;
  226. $inputImage = Input::get('image');
  227. $inputImageTitle = Input::get('image_title');
  228. $inputVideo = Input::get('video');
  229. $inputVideoTitle = Input::get('video_title');
  230. $inputMediaDel = Input::get('media_del');
  231. $input['user_id'] = Auth::employee()->get()->id;
  232. $input['active_date'] = isset($input['active_date']) && $input['active_date'] ? $input['active_date'] : date('Y-m-d H:i:s');
  233. $activeDateArr = explode(" ", $input['active_date']);
  234. $input['active_date'] = PersianDate::convert_date_H_to_M($activeDateArr[0]) . ' ' . $activeDateArr[1];
  235. $input['category_id'] = isset($input['categories']) ? $input['categories'][0] : null;
  236. $mediaArr = array(
  237. 'images' => $inputImage,
  238. 'images_title' => $inputImageTitle,
  239. 'images_class' => $inputImageClass,
  240. 'images_sort' => $inputImageSort,
  241. 'images_feature_select' => $inputImageFeatureSelect,
  242. 'videos' => $inputVideo,
  243. 'videos_title' => $inputVideoTitle
  244. );
  245. $alias = isset($input['alias']) && $input['alias'] ? str_replace(" ", "-", $input['alias']) : CategoryHelper::createAlias($input['title']);
  246. $input['alias'] = $alias;
  247. $result = $this->products->updateProduct($product_id, $input, $mediaArr, $inputMediaDel);
  248. $resCategories = $this->products->saveProductCategories($product_id, $input['categories']);
  249. $req = Request::hasFile('image_product');
  250. if (Request::hasFile('image_product')) {
  251. $extension = Input::file('image_product')->getClientOriginalExtension();
  252. $createFolder = base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/product/' . $product_id . '/';
  253. if (!file_exists($createFolder)) {
  254. mkdir($createFolder, 0755);
  255. }
  256. if (File::exists(base_path() . env('PUBLIC_PATH_URL', 'public') . '/uploads/images/product/' . $product_id . '/' . $product_id . '-1920x600.' . $extension)) {
  257. File::delete($product_id . '-1920x600.' . $extension);
  258. }
  259. Image::make(Input::file('image_product'))->fit(1920, 600)->save('uploads/images/product/' . $product_id . '/' . $product_id . '-1920x600.' . $extension);
  260. $updateProduct = $this->products->find($product_id);
  261. $updateProduct->image = '/uploads/images/product/' . $product_id . '/' . $product_id . '-1920x600.' . $extension;
  262. $updateProduct->save();
  263. $messages[] = ['level' => BaseModel::level_success, 'message' => Lang::get('backend/Content.messages.uploadFileSuccess')];
  264. Session::flash('messages', $messages);
  265. }
  266. if (!$result['action']) {
  267. $return = Redirect::back()->withInput()->withErrors($result['message']);
  268. return $return;
  269. }
  270. return Redirect::back()->with('message', $result['message']);
  271. }
  272. /**
  273. * Remove the specified resource from storage.
  274. *
  275. * @param int $product_id
  276. * @return Response
  277. */
  278. public function destroy($product_id)
  279. {
  280. $result = $this->products->del($product_id);
  281. // $result = DB::table('products_lang')->where('id', '=', $product_id)->delete();
  282. return ($result);
  283. }
  284. public function isDefault()
  285. {
  286. $input = Input::all();
  287. $product_id = $input['product_id'];
  288. $media_id = $input['media_id'];
  289. $result = $this->products->isDefault($product_id, $media_id);
  290. return $result;
  291. }
  292. }