PageRenderTime 28ms CodeModel.GetById 23ms RepoModel.GetById 0ms app.codeStats 0ms

/app/Product.php

https://gitlab.com/techniconline/kmc
PHP | 430 lines | 329 code | 88 blank | 13 comment | 35 complexity | bbba89635ebb2a2758728309e550fe93 MD5 | raw file
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: Hamid
  5. * Date: 13/01/2015
  6. * Time: 09:03 AM
  7. */
  8. namespace App;
  9. use App\Http\Controllers\backend\FeatureController;
  10. use Illuminate\Support\Facades\Validator;
  11. use Illuminate\Support\Facades\Auth;
  12. use Illuminate\Support\Facades\DB;
  13. use Illuminate\Support\Facades\Lang;
  14. class Product extends BaseModel
  15. {
  16. protected $table = 'products';
  17. protected $primaryKey = 'id';
  18. protected $fillable = ['is_active', 'active_date', 'category_id'
  19. , 'price', 'quantity', 'condition', 'image'
  20. , 'lat', 'lng'
  21. , 'user_id', 'status'];
  22. protected $hidden = ['remember_token'];
  23. public $errors;
  24. public $rules = [
  25. 'category_id' => 'required|numeric',
  26. 'title' => 'required',
  27. 'alias' => 'required|unique:products_lang,alias',
  28. ];
  29. public $rules_upd = [
  30. 'category_id' => 'required|numeric',
  31. 'title' => 'required',
  32. ];
  33. public function getDataFeatures($lang_id, $featuresProduct , $search=false , $public_add=false, $search_advance=false)
  34. {
  35. $dataF = DB::table('features')
  36. ->join('features_lang', 'features_lang.feature_id', '=', 'features.id')
  37. ->where('features.status', '=', 1)
  38. ->where(function($query)use($search,$public_add,$search_advance){
  39. if($search){
  40. $query->where('search',1);
  41. }
  42. if($public_add){
  43. $query->where('public_add',1);
  44. }
  45. if($search_advance){
  46. $query->where('search_advance',1);
  47. }
  48. })
  49. ->where('features_lang.lang_id', '=', $lang_id)
  50. ->select([
  51. 'features_lang.name'
  52. , 'features.*'
  53. // , DB::raw('
  54. // (SELECT GROUP_CONCAT(CONCAT_WS("|",id,title,value)) FROM feature_value AS fv
  55. // INNER JOIN feature_value_lang AS fvl ON fv.id=fvl.feature_value_id AND lang_id=' . $lang_id . '
  56. // WHERE fv.feature_id=features.id) AS feature_value_list
  57. // ')
  58. ])
  59. ->get();
  60. $dataFV = DB::table('feature_value')
  61. ->join('feature_value_lang','feature_value.id','=','feature_value_lang.feature_value_id')
  62. ->where('lang_id',$lang_id)->get();
  63. $dataFV_new = [];
  64. foreach($dataFV as $item){
  65. $dataFV_new[$item->feature_id][] = ['id'=>$item->id, 'title'=>$item->title, 'value'=>$item->value];
  66. }
  67. $newDataFeature = [];
  68. $color_selected = [];
  69. foreach ($dataF as &$itemF) {
  70. if (isset($dataFV_new[$itemF->id]) && $dataFV_new[$itemF->id]) {
  71. $fvArr = $dataFV_new[$itemF->id];
  72. $cnt = 0;
  73. $feature_values = [];
  74. $feature_values_selected = '';
  75. $is_color = false;
  76. if ($itemF->type == 'color') {
  77. $is_color = true;
  78. }
  79. foreach ($fvArr as $itemFV) {
  80. $feature_values[$itemFV['id']] = $itemFV['title'] ? $itemFV['title'] : $itemFV['value'];
  81. $fp = isset($featuresProduct[$itemFV['value']]) ? $featuresProduct[$itemFV['value']] : [];
  82. if (in_array($itemFV['id'], $fp)) {
  83. $feature_values_selected[] = !$itemF->custom ?$itemFV['id'] : $itemFV['value'];
  84. if ($is_color) {
  85. $color_selected[$itemFV['value']] = $itemFV['title'];
  86. }
  87. }
  88. $cnt++;
  89. }
  90. if (isset($feature_values_selected) && count($feature_values_selected) > 1) {
  91. $itemF->feature_value_selected = $feature_values_selected;
  92. } elseif (isset($feature_values_selected)) {
  93. $itemF->feature_value_selected = isset($feature_values_selected[0]) ? $feature_values_selected[0] : 0;
  94. }
  95. $itemF->feature_values = $feature_values;
  96. }
  97. $newDataFeature[$itemF->id] = $itemF;
  98. }
  99. $fgIds=null;
  100. if($search){
  101. $featureIds = DB::table('features')->where('status',1)->where('search',$search)->lists('id');
  102. if($featureIds){
  103. $fgIds = DB::table('feature_group_relation')
  104. ->whereIn('feature_id',$featureIds)->lists('feature_group_id');
  105. $fgIds = array_unique($fgIds);
  106. }
  107. }
  108. $dataFG = DB::table('feature_group')
  109. ->join('feature_group_lang', 'feature_group_lang.feature_group_id', '=', 'feature_group.id')
  110. ->where('feature_group.status', '=', 1)
  111. ->where(function($queryf)use($fgIds,$search){
  112. if($fgIds){
  113. $queryf->whereIn('feature_group.id',$fgIds);
  114. }
  115. })
  116. ->where('feature_group_lang.lang_id', '=', $lang_id)
  117. ->select([
  118. 'feature_group_lang.name'
  119. , 'feature_group.*'
  120. , DB::raw('(SELECT GROUP_CONCAT(feature_id) FROM feature_group_relation WHERE feature_group_id=feature_group.id) as list_features')
  121. ])
  122. ->get();
  123. $allDataFG = [];
  124. foreach ($dataFG as &$itemFG) {
  125. if ($itemFG->list_features) {
  126. $listArr = explode(",", $itemFG->list_features);
  127. $listFeature = [];
  128. foreach ($listArr as $itemLi) {
  129. $listFeature[] = isset($newDataFeature[$itemLi]) ? $newDataFeature[$itemLi] : null;
  130. }
  131. $itemFG->features_array = $listFeature;
  132. }
  133. $allDataFG[] = $itemFG;
  134. }
  135. return [
  136. 'dataFG' => $dataFG
  137. , 'allDataFG' => $allDataFG
  138. , 'color_selected' => $color_selected
  139. ];
  140. }
  141. public function getFeaturesProduct($product_id, $lang_id = 3)
  142. {
  143. $result = DB::table('feature_product AS fp')
  144. ->join('feature_value_lang AS fvl', 'fp.feature_value_id', '=', 'fvl.feature_value_id')
  145. ->join('features_lang AS fl', 'fp.feature_id', '=', 'fl.feature_id')
  146. ->join('features AS f', 'fl.feature_id', '=', 'f.id')
  147. ->join('feature_group_relation AS fgr', 'fp.feature_id', '=', 'fgr.feature_id')
  148. ->join('feature_group_lang AS fgl', 'fgr.feature_group_id', '=', 'fgl.feature_group_id')
  149. ->join('feature_group AS fg', 'fg.id', '=', 'fgl.feature_group_id')
  150. ->select(['fvl.value', 'fvl.title', 'fl.name', 'f.type', 'fl.feature_id', 'fgl.name AS name_group', 'fg.id'])
  151. ->where('fvl.lang_id', $lang_id)
  152. ->where('fgl.lang_id', $lang_id)
  153. ->where('fl.lang_id', $lang_id)
  154. ->where('fp.product_id', $product_id)
  155. ->orderBy('fg.position')
  156. ->orderBy('fg.id')
  157. ->orderBy('fl.feature_id')
  158. ->get();
  159. return $result;
  160. }
  161. public function saveProductCategories($product_id, $categories = [])
  162. {
  163. if (!count($categories))
  164. return false;
  165. $resDel = DB::table('product_categories')->where('product_id', $product_id)->delete();
  166. $insertItems = [];
  167. foreach ($categories as $item) {
  168. $insertItems[] = ['product_id' => $product_id, 'categories_id' => $item, 'created_at' => date('Y-m-d H:i:s'), 'updated_at' => date('Y-m-d H:i:s')];
  169. }
  170. $result = DB::table('product_categories')->insert(
  171. $insertItems
  172. );
  173. return $result;
  174. }
  175. public function saveProductLang($params)
  176. {
  177. $lang_id = $params['lang_id'];
  178. $id = $params['id'];
  179. $title = $params['title'];
  180. $alias = $params['alias'];
  181. $resAlias = DB::table('products_lang')->where('alias', $alias)->where('id', '!=', $id)->first();
  182. if ($resAlias) {
  183. $alias .= $id;
  184. }
  185. $description = $params['description'];
  186. $short_description = $params['short_description'];
  187. $result = DB::table('products_lang')->insert(
  188. [
  189. 'id' => $id
  190. , 'title' => $title
  191. , 'alias' => $alias
  192. , 'description' => $description
  193. , 'short_description' => $short_description
  194. , 'lang_id' => $lang_id
  195. ]
  196. );
  197. return $result;
  198. }
  199. public function updateProductLang($params)
  200. {
  201. $lang_id = $params['lang_id'];
  202. $id = $params['id'];
  203. $title = $params['title'];
  204. $description = $params['description'];
  205. $alias = $params['alias'];
  206. $resAlias = DB::table('products_lang')->where('alias', $alias)->where('id', '!=', $id)->first();
  207. if ($resAlias) {
  208. $alias .= $id;
  209. }
  210. $short_description = $params['short_description'];
  211. $result = DB::table('products_lang')
  212. ->where('id', '=', $id)
  213. ->where('lang_id', '=', $lang_id)
  214. ->update(
  215. [
  216. 'title' => $title
  217. , 'alias' => $alias
  218. , 'description' => $description
  219. , 'short_description' => $short_description
  220. ]
  221. );
  222. return $result;
  223. }
  224. public function updateProduct($product_id, $data, $mediaArr = 0, $mediaDelArr = 0)
  225. {
  226. unset($data['image']);
  227. $product = $this->find($product_id);
  228. $fill = $product->fill($data);
  229. $isValid = $product->validationData($data, $this->rules_upd);
  230. if (!$isValid) {
  231. return array('action' => false, 'message' => $this->errors);
  232. }
  233. $image_default = isset($data['image_default']) && $data['image_default'] ? $data['image_default'] : 0;
  234. $InputLang['title'] = $data['title'];
  235. $InputLang['alias'] = $data['alias'];
  236. $InputLang['lang_id'] = $data['lang_id'];
  237. $InputLang['description'] = $data['description'];
  238. $InputLang['short_description'] = $data['short_description'];
  239. $InputLang['id'] = $product_id;
  240. $result = $product->save();
  241. $resultLang = $this->updateProductLang($InputLang);
  242. //create New Feature Value
  243. $fvModel = new FeatureValue();
  244. // $input['features'] => ['feature_id'=>'feature_value_id']
  245. $features = isset($data['features'])&&$data['features']?$data['features']:null;
  246. $new_features_value = isset($data['new_features_value'])&&$data['new_features_value']?$data['new_features_value']:null;
  247. $resSaveFeatureValue = null;
  248. foreach ($new_features_value as $key => $value) {
  249. $input = ['value' => $value, 'title' => ''];
  250. $input['feature_id'] = $key;
  251. $input['lang_id'] = $data['lang_id'];
  252. $resSaveFeatureValue = $fvModel->storeFeatureValues($input);
  253. if ($resSaveFeatureValue['valid']) {
  254. $features[$key] = $resSaveFeatureValue['feature_value_id'];
  255. }
  256. }
  257. $resFeatureAdd = $fvModel->saveFeatureValueProduct($product_id, $features);
  258. $mediaModel = new Media();
  259. if ($mediaArr['images']) {
  260. $subMediaArr = array('ids' => $mediaArr['images'], 'titles' => $mediaArr['images_title']);
  261. $mediaModel->addMediaProduct($subMediaArr, $product_id);
  262. }
  263. if ($mediaArr['videos']) {
  264. $subMediaArr = array('ids' => $mediaArr['videos'], 'titles' => $mediaArr['videos_title']);
  265. $mediaModel->addMediaProduct($subMediaArr, $product_id);
  266. }
  267. $date = date('Y-m-d H:i:s');
  268. if ($mediaArr['images_class']) {
  269. foreach ($mediaArr['images_class'] as $k => $v) {
  270. $updData = [
  271. 'product_id' => $product_id
  272. , 'class' => $v
  273. , 'updated_at' => $date
  274. ];
  275. if ($mediaArr['images_sort'][$k]) {
  276. $updData = array_merge($updData, ['sort' => $mediaArr['images_sort'][$k]]);
  277. }
  278. if ($mediaArr['images_feature_select'][$k]) {
  279. $updData = array_merge($updData, ['feature_value_selected' => $mediaArr['images_feature_select'][$k]]);
  280. }
  281. $mediaModel->updateRoomMedia($k, $updData);
  282. }
  283. }
  284. if ($mediaDelArr) {
  285. $mediaModel->removeMedia($mediaDelArr);
  286. }
  287. return array('action' => true, 'message' => Lang::get('backend/Product.messages.msgUpdateOk'));
  288. }
  289. public function del($product_id)
  290. {
  291. if (!$product_id)
  292. return 0;
  293. $Input['user_id'] = Auth::employee()->get()->id;
  294. $Input['updated_at'] = date('Y-m-d H:i:s');
  295. $Input['status'] = 0;
  296. $result = DB::table('products')
  297. ->where('id', $product_id)
  298. ->where('status', 1)
  299. ->update($Input);
  300. if ($result) {
  301. return array('action' => true, 'message' => Lang::get('backend/Product.messages.msgDelOk'));
  302. } else {
  303. return array('action' => false, 'message' => Lang::get('backend/Product.messages.msgDelNotOk'));
  304. }
  305. }
  306. public function isDefault($product_id, $media_id)
  307. {
  308. if (!$product_id || !$media_id)
  309. return 0;
  310. $Input['user_id'] = Auth::employee()->get()->id;
  311. $Input['updated_at'] = date('Y-m-d H:i:s');
  312. $result = DB::table('product_media')
  313. ->where('product_id', $product_id)
  314. ->where('status', 1)
  315. ->update($Input);
  316. $Input['is_default'] = 1;
  317. $result = DB::table('product_media')
  318. ->where('product_id', $product_id)
  319. ->where('media_id', $media_id)
  320. ->where('status', 1)
  321. ->update($Input);
  322. if ($result) {
  323. return array('action' => true, 'message' => Lang::get('backend/Product.messages.msgUpdateOk'));
  324. } else {
  325. return array('action' => false, 'message' => Lang::get('backend/Product.messages.msgUpdateNotOk'));
  326. }
  327. }
  328. public function getProductCategories($product_id, $list = false)
  329. {
  330. $result = DB::table('product_categories')
  331. ->where('product_categories.product_id', '=', $product_id)
  332. ->get();
  333. if ($list) {
  334. $resultNew = [];
  335. foreach ($result as $item) {
  336. $resultNew[] = $item->categories_id;
  337. }
  338. return $resultNew;
  339. }
  340. return $result;
  341. }
  342. }