PageRenderTime 41ms CodeModel.GetById 14ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Http/Controllers/BrandController.php

https://bitbucket.org/hhhunan/smart_make_up_mirror
PHP | 280 lines | 175 code | 35 blank | 70 comment | 19 complexity | 33cafb1db2adb398f999ae9464d5df3e MD5 | raw file
  1. <?php
  2. namespace App\Http\Controllers;
  3. use App\Models\Brand;
  4. use App\Models\BrandTranslation;
  5. use App\Models\Image;
  6. use App\Models\Language;
  7. use App\Models\Product;
  8. use App\Models\ProductBrand;
  9. use Illuminate\Http\Request;
  10. use Illuminate\Support\Facades\Validator;
  11. use Illuminate\Validation\Rule;
  12. use App\AddonLib;
  13. use DB;
  14. class BrandController extends Controller
  15. {
  16. public $lang_code;
  17. public $addon;
  18. public function __construct(Request $request)
  19. {
  20. $this->lang_code = $request->header("language");
  21. $this->addon = new AddonLib();
  22. }
  23. /**
  24. * Display a listing of the resource.
  25. *
  26. * @return \Illuminate\Http\Response
  27. */
  28. public function index(Request $request)
  29. {
  30. // Get Language id by LangCode (en/es/ru..)
  31. $lang_id = Language::where('code', $this->lang_code ? $this->lang_code : "en")->value('id');
  32. // Get all brands
  33. $brand = Brand::with(['images', "translation" => function ($query) use ($lang_id) {
  34. $query->with('language')->where('language_id', $lang_id);
  35. }]);
  36. // Brands translation options
  37. if ($this->addon->checkUriParameter($request, 'search') || $this->addon->checkUriParameter($request, 'sort')) {
  38. $this->addon->customSortSearch($brand, [
  39. "join" => [
  40. "select" => "brands.*",
  41. "table" => "brand_translations",
  42. "first" => "brands.id",
  43. "second" => "brand_translations.brand_id",
  44. "type" => "inner"
  45. ],
  46. "search" => $this->addon->checkUriParameter($request, 'search') ? [
  47. "column" => "brand_translations.name",
  48. "pattern" => $request->get('search')
  49. ] : false,
  50. "sorting" => $this->addon->checkUriParameter($request, 'sort') ? [
  51. "column" => "brand_translations.name",
  52. "flag" => $request->get('sort')
  53. ] : false,
  54. "lang_id" => $lang_id
  55. ]);
  56. }
  57. else
  58. {
  59. $brand->orderBy('id', 'desc');
  60. }
  61. // Paginations
  62. $page = $brand->paginate(10)->toArray();
  63. if (sizeof($page['data'])) {
  64. $page['sort_type'] = $this->addon->checkUriParameter($request, 'sort') ? $request->get('sort') : "NORMAL";
  65. return $page;
  66. }
  67. // Returned, not found items messages.
  68. return response()->json(['message' => \Lang::get('custom.no_item')], 200);
  69. }
  70. /**
  71. * Show the form for creating a new resource.
  72. *
  73. * @return \Illuminate\Http\Response
  74. */
  75. public function create()
  76. {
  77. }
  78. /**
  79. * Store a newly created resource in storage.
  80. *
  81. * @param \Illuminate\Http\Request $request
  82. * @return \Illuminate\Http\Response
  83. */
  84. public function store(Request $request)
  85. {
  86. // Create new Object Brand.
  87. $brand = new Brand();
  88. // Starting validator.
  89. $validation = Validator::make($request->all(), array_add($brand->ruleForCreate, 'status', Rule::in([0, 1])));
  90. if ($validation->fails()) {
  91. return response()->json(['message' => $validation->errors()->getMessages()], 400);
  92. }
  93. $brand->status = $request->has('status') ? $request->get('status') : 1;
  94. if($request->has('image')){
  95. $image = $this->addon->fileUploader($request->get('image'), 'brand');
  96. if (isset($image['message']))
  97. {
  98. return response()->json($image['message'], 400);
  99. }
  100. $brand->image_id = $image;
  101. }
  102. // Saved Brand object
  103. if (!$brand->save()) {
  104. // Returned created error messages
  105. return response()->json(['message' => \Lang::get('custom.error_save')], 400);
  106. }
  107. // Starting transaction
  108. DB::transaction(function () use ($brand, $request) {
  109. // Brand transaction
  110. $languages = Language::pluck('code', 'id');
  111. $translations = [];
  112. foreach ($languages as $lngId => $lngCode) {
  113. if (array_key_exists($lngCode, $request->get('name'))) {
  114. $translations[$lngId]['name'] = $request->get('name')[$lngCode];
  115. $translations[$lngId]['brand_id'] = $brand->id;
  116. $translations[$lngId]['language_id'] = $lngId;
  117. }
  118. }
  119. $brand->translationForSync()->sync($translations);
  120. }, 3);
  121. // Returned created success messages
  122. return response()->json(["messages" => \Lang::get('custom.success_creat')], 200);
  123. }
  124. /**
  125. * Display the specified resource.
  126. *
  127. * @param \App\Models\Brand $brand
  128. * @return \Illuminate\Http\Response
  129. */
  130. public function show($id)
  131. {
  132. // Brand db joins
  133. $brand = Brand::with(['images', 'products' => function ($query) {
  134. $this->products_counts = $query->count('id');
  135. $query->with('images');
  136. }, "translation" => function ($query) {
  137. if ($this->lang_code)
  138. $query->with('language')->whereHas('language', function ($query) {
  139. $query->where('code', $this->lang_code);
  140. });
  141. else
  142. $query->with('language');
  143. }])->findOrFail($id);
  144. // Marge product counts
  145. $brand["products_counts"] = $this->products_counts;
  146. return $brand;
  147. }
  148. /**
  149. * Show the form for editing the specified resource.
  150. *
  151. * @param \App\Models\Brand $brand
  152. * @return \Illuminate\Http\Response
  153. */
  154. public function edit(Brand $brand)
  155. {
  156. //
  157. }
  158. /**
  159. * Update the specified resource in storage.
  160. *
  161. * @param \Illuminate\Http\Request $request
  162. * @param \App\Models\Brand $brand
  163. * @return \Illuminate\Http\Response
  164. */
  165. public function update(Request $request, $id)
  166. {
  167. // Starting validator
  168. $validation = Validator::make($request->all(), array_add((new Brand)->ruleForEdit, 'status', Rule::in([0, 1])));
  169. if ($validation->fails()) {
  170. return response()->json(['message' => $validation->errors()->getMessages()], 400);
  171. }
  172. $brand = Brand::where("id", $id)->first();
  173. $brand->status = $request->has('status') ? $request->get('status') : $brand->status;
  174. if($request->has('image')) {
  175. $image = $this->addon->fileUploader($request->get("image"), 'brand', true, $brand->image_id);
  176. if (isset($image['message']))
  177. {
  178. return response()->json($image['message'], 400);
  179. }
  180. $brand->image_id = $image;
  181. }
  182. // Saved brand object.
  183. if (!$brand->save()) {
  184. return response()->json(['message' => \Lang::get('custom.error_save')], 400);
  185. }
  186. // Starting transaction
  187. if ($request->get('name')) {
  188. DB::transaction(function () use ($brand, $request) {
  189. $languages = Language::pluck('code', 'id');
  190. $translations = [];
  191. foreach ($languages as $lngId => $lngCode) {
  192. if (array_key_exists($lngCode, $request->get('name'))) {
  193. $translations[$lngId]['name'] = $request->get('name')[$lngCode];
  194. $translations[$lngId]['brand_id'] = $brand->id;
  195. $translations[$lngId]['language_id'] = $lngId;
  196. }
  197. }
  198. $brand->translationForSync()->sync($translations);
  199. }, 3);
  200. }
  201. // Returned success updated messages.
  202. return response()->json(["messages" => \Lang::get('custom.success_updated')], 200);
  203. }
  204. /**
  205. * Remove the specified resource from storage.
  206. *
  207. * @param \App\Models\Brand $brand
  208. * @return \Illuminate\Http\Response
  209. */
  210. public function destroy($id)
  211. {
  212. $brand = Brand::findOrFail($id);
  213. // remove files
  214. if (file_exists(public_path($brand->images->uri))) {
  215. unlink(public_path($brand->images->uri));
  216. }
  217. if ($brand->exists()) {
  218. $product_id = ProductBrand::where('brand_id', $id)->value('product_id');
  219. // Destroyed brand in products.
  220. ProductBrand::where('brand_id', $id)->delete();
  221. if ($brand->delete()) {
  222. if (!ProductBrand::where('product_id', $product_id)->count())
  223. Product::where("id", $product_id)->update(['status' => '0']);
  224. // Returning success destroy messages.
  225. return response()->json(['message' => \Lang::get('custom.destroy')], 200);
  226. }
  227. }
  228. // Returned error updated messages.
  229. return response()->json(['message' => \Lang::get('custom.error_destroy')], 400);
  230. }
  231. /**
  232. * getBrand
  233. * @param mixed $request
  234. * @return mixed
  235. */
  236. public function getBrand()
  237. {
  238. // Get all Brands
  239. return Brand::select('brands.id')->with(["translation" => function ($query) {
  240. $query->with('language')->whereHas('language', function ($query) {
  241. $query->where('code', "en");
  242. });
  243. }])->get();
  244. }
  245. }