/
Product.php
230 lines (204 loc) · 5.4 KB
/
Product.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
<?php
namespace Claremontdesign\Ecommerce\Model;
/**
* Dx
*
* @link http://dennesabing.com
* @author Dennes B Abing <dennes.b.abing@gmail.com>
* @license proprietary
* @copyright Copyright (c) 2015 ClaremontDesign/MadLabs-Dx
* @version 0.0.0.1
* @since Dec 17, 2015 1:43:45 PM
* @file routes.php
* @project Claremontdesign
* @package Ecommerce
*/
use Claremontdesign\Cdbase\Repository\Contracts\FilterableInterface;
use Claremontdesign\Cdbase\Repository\Contracts\JoinableInterface;
use Claremontdesign\Cdbase\Repository\Contracts\SortableInterface;
use Claremontdesign\Cdbase\Repository\Contracts\CategorybleInterface;
use Claremontdesign\Cdbase\Repository\Traits\Filterable;
use Claremontdesign\Cdbase\Repository\Traits\Joinable;
use Claremontdesign\Cdbase\Repository\Traits\Sortable;
use Claremontdesign\Cdbase\Repository\Contracts\PositionableInterface;
use Claremontdesign\Cdbase\Model\Model;
use Illuminate\Database\Eloquent\SoftDeletes;
use Claremontdesign\Cdbase\Widgets\ModelInterface as WidgetModelInterface;
use Claremontdesign\Cdbase\Widgets\WidgetTypes\WidgetTypeInterface;
class Product extends Model implements WidgetModelInterface, FilterableInterface, JoinableInterface, PositionableInterface, SortableInterface, CategorybleInterface
{
use Filterable,
SoftDeletes,
Joinable,
Sortable;
const STATUS_ENABLE = 1;
const STATUS_DISABLE = 0;
/**
* Create a new Eloquent model instance.
*
* @param array $attributes
* @return void
*/
public function __construct(array $attributes = [])
{
$this->table = cd_config('database.e.product.table.name');
$this->primaryKey = cd_config('database.e.product.table.primary');
$this->fillable = cd_config('database.e.product.model.fillable');
parent::__construct($attributes);
}
// <editor-fold defaultstate="collapsed" desc="RELATIONSHIPS">
/**
* Categories
* @return Collection
*/
public function categories()
{
return $this->belongsToMany(Category::class, cd_config('database.e.productCategoryPivot.table.name'), cd_config('database.e.product.table.primary'), cd_config('database.e.productCategory.table.primary'));
}
/**
* Product Categories
* @return Collection
*/
public function scopeCategories($query = null)
{
return $this->categories();
}
/**
* Product Categories
*
* @return Collection
*/
public function getCategories()
{
return $this->categories()->get();
}
/**
* By Category
* @param type $query
* @param \Claremontdesign\Ecommerce\Model\Category $category
* @return type
*/
public function scopeCategorized($query, Category $category = null)
{
if(is_null($category))
{
return $query->with(cd_config('database.e.productCategory.table.name'));
}
$categoryIds = $category->getDescendantsAndSelf()->lists(cd_config('database.e.productCategory.table.primary'));
return $query->with('categories')
->join(
cd_config('database.e.productCategoryPivot.table.name'), cd_config('database.e.productCategoryPivot.table.name') . '.' . cd_config('database.e.product.table.primary'), '=', cd_config('database.e.product.table.name') . '.' . cd_config('database.e.product.table.primary'))
->whereIn(cd_config('database.e.productCategoryPivot.table.name') . '.' . cd_config('database.e.productCategory.table.primary'), $categoryIds);
}
// </editor-fold>
/**
* REturn the Item Position
* @return integer
*/
public function position()
{
if(isset($this->position))
{
return $this->position;
}
return null;
}
public function pivotItemId()
{
return $this->id();
}
public function pivotCategoryId()
{
if(isset($this->pivot_category_id))
{
return $this->pivot_category_id;
}
return null;
}
public function updatePivotPosition($categoryId, $position)
{
$this->categories()->updateExistingPivot([$categoryId], ['position' => $position]);
return $this->save();
}
/**
* Return the name of the 'status' column
* @return string
*/
public function getStatusColumn()
{
return 'status';
}
/**
* Item Id
* @return integer
*/
public function id()
{
return (int) $this->{$this->primaryKey};
}
/**
* Title
* @return string
*/
public function title()
{
return $this->title;
}
/**
* Description
* @return string
*/
public function description()
{
return $this->description;
}
// <editor-fold defaultstate="collapsed" desc="WIDGET">
/**
* Check widget access
* @return boolean
*/
public function checkWidgetAccess(WidgetTypeInterface $widget, $crud, $access = null)
{
return true;
}
/**
*
* @param \Claremontdesign\Cdbase\Widgets\WidgetTypes\WidgetTypeInterface $widget
* @param type $crud
* @param type $data
*/
public function widgetPreProcess(WidgetTypeInterface $widget, $crud, $modelId, $data)
{
}
/**
*
* @param \Claremontdesign\Cdbase\Widgets\WidgetTypes\WidgetTypeInterface $widget
* @param type $crud
* @param type $data
*/
public function widgetPostProcess(WidgetTypeInterface $widget, $crud, $modelId, $data, $result)
{
if($crud == 'update')
{
if(!empty($data['category']))
{
$this->categories()->sync($data['category']);
}
else
{
$this->categories()->detach();
}
}
}
/**
* Fix array values to column values
* Map the given values to column values
* @param array $assocArray [description]
* @return array
*/
public function fixValueToColumnValue($assocArray, $mode = null)
{
return parent::fixValueToColumnValue($assocArray, $mode);
}
// </editor-fold>
}