forked from VividWeb/vivid_store
/
products.php
283 lines (247 loc) · 9.14 KB
/
products.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
<?php
namespace Concrete\Package\VividStore\Controller\SinglePage\Dashboard\Store;
use \Concrete\Core\Page\Controller\DashboardPageController;
use Core;
use View;
use FilePermissions;
use TaskPermission;
use File;
use Loader;
use PageType;
use GroupList;
use \Concrete\Package\VividStore\Src\VividStore\Product\Product as VividProduct;
use \Concrete\Package\VividStore\Src\VividStore\Product\ProductList as VividProductList;
use \Concrete\Package\VividStore\Src\VividStore\Groups\ProductGroup as VividProductGroup;
use \Concrete\Package\VividStore\Src\VividStore\Groups\GroupList as VividProductGroupList;
use \Concrete\Package\VividStore\Src\Attribute\Key\StoreProductKey;
use \Concrete\Package\VividStore\Src\VividStore\Tax\TaxClass;
defined('C5_EXECUTE') or die("Access Denied.");
class Products extends DashboardPageController
{
public function view($gID=null){
$products = new VividProductList();
$products->setItemsPerPage(10);
$products->setGroupID($gID);
$products->activeOnly(false);
$products->setShowOutOfStock(true);
if ($this->get('keywords')) {
$products->setSearch($this->get('keywords'));
}
$paginator = $products->getPagination();
$pagination = $paginator->renderDefaultView();
$this->set('products',$paginator->getCurrentPageResults());
$this->set('pagination',$pagination);
$this->set('paginator', $paginator);
$this->requireAsset('css', 'vividStoreDashboard');
$this->requireAsset('javascript', 'vividStoreFunctions');
$grouplist = VividProductGroupList::getGroupList();
$this->set("grouplist",$grouplist);
}
public function success(){
$this->set("success",t("Product Added"));
$this->view();
}
public function updated()
{
$this->set("success",t("Product Updated"));
$this->view();
}
public function removed(){
$this->set("success",t("Product Removed"));
$this->view();
}
public function add()
{
$this->loadFormAssets();
$this->set("actionType",t("Add"));
$grouplist = VividProductGroupList::getGroupList();
$this->set("grouplist",$grouplist);
foreach($grouplist as $productgroup){
$productgroups[$productgroup->getGroupID()] = $productgroup->getGroupName();
}
$this->set("productgroups",$productgroups);
$gl = new GroupList();
$gl->setItemsPerPage(1000);
$gl->filterByAssignable();
$usergroups = $gl->get();
$usergrouparray = array();
foreach($usergroups as $ug) {
if ( $ug->gName != 'Administrators') {
$usergrouparray[$ug->gID] = $ug->gName;
}
}
$this->set('pageTitle', t('Add Product'));
$this->set('usergroups', $usergrouparray);
}
public function edit($pID)
{
$this->loadFormAssets();
$this->set("actionType",t("Update"));
//get the product
$product = VividProduct::getByID($pID);
if (!$product) {
$this->redirect('/dashboard/store/products/');
}
$this->set('p',$product);
$this->set("images",$product->getProductImages());
$this->set("groups",$product->getProductOptionGroups());
$this->set('optItems',$product->getProductOptionItems());
$this->set('pages', $product->getProductPages());
$this->set('pgroups', $product->getProductGroupIDs());
//populate "Groups" select box options
$grouplist = VividProductGroupList::getGroupList();
foreach($grouplist as $productgroup){
$productgroups[$productgroup->getGroupID()] = $productgroup->getGroupName();
}
$this->set("productgroups",$productgroups);
$gl = new GroupList();
$gl->setItemsPerPage(1000);
$gl->filterByAssignable();
$usergroups = $gl->get();
$usergrouparray = array();
foreach($usergroups as $ug) {
if ( $ug->gName != 'Administrators') {
$usergrouparray[$ug->gID] = $ug->gName;
}
}
$this->set('pageTitle', t('Edit Product'));
$this->set('usergroups', $usergrouparray);
}
public function generate($pID,$templateID=null)
{
VividProduct::getByID($pID)->generatePage($templateID);
$this->redirect('/dashboard/store/products/edit',$pID);
}
public function delete($pID)
{
$product = VividProduct::getByID($pID);
$product->remove();
$this->redirect('/dashboard/store/products/removed');
}
public function loadFormAssets()
{
$this->requireAsset('redactor');
$this->requireAsset('core/file-manager');
$this->requireAsset('core/sitemap');
$this->set('fp',FilePermissions::getGlobal());
$this->set('tp', new TaskPermission());
$this->set('al', Core::make('helper/concrete/asset_library'));
$this->addHeaderItem('<style type="text/css">.redactor_editor{padding:20px}</style>');
$this->requireAsset('css', 'vividStoreDashboard');
$this->requireAsset('javascript', 'vividStoreFunctions');
$attrList = StoreProductKey::getList();
$this->set('attribs',$attrList);
$pageType = PageType::getByHandle("store_product");
$pageTemplates = $pageType->getPageTypePageTemplateObjects();
$templates = array();
foreach($pageTemplates as $pt){
$templates[$pt->getPageTemplateID()] = $pt->getPageTemplateName();
}
$this->set('pageTemplates',$templates);
$taxClasses = array();
foreach(TaxClass::getTaxClasses() as $taxClass){
$taxClasses[$taxClass->getTaxClassID()] = $taxClass->getTaxClassName();
}
$this->set('taxClasses',$taxClasses);
}
public function save()
{
$data = $this->post();
if($data['pID']){
$this->edit($data['pID']);
} else{
$this->add();
}
if ($this->isPost()) {
$errors = $this->validate($data);
$this->error = null; //clear errors
$this->error = $errors;
if (!$errors->has()) {
$product = VividProduct::save($data);
$aks = StoreProductKey::getList();
foreach($aks as $uak) {
$uak->saveAttributeForm($product);
}
if($data['pID']){
$this->redirect('/dashboard/store/products/', 'updated');
} else {
$this->redirect('/dashboard/store/products/', 'success');
}
}//if no errors
}//if post
}
public function validate($args)
{
$e = Loader::helper('validation/error');
if($args['pName']==""){
$e->add(t('You must have a Product Name'));
}
if(strlen($args['pName']) > 255){
$e->add(t('Keep the Product name under 255 Characters'));
}
if(!is_numeric($args['pPrice'])){
$e->add(t('The Price must be set, and numeric'));
}
if(!is_numeric($args['pQty']) && !$args['pQtyUnlim']){
$e->add(t('The Quantity must be set, and numeric'));
}
if(!is_numeric($args['pWidth'])){
$e->add(t('The Product Width must be a number'));
}
if(!is_numeric($args['pHeight'])){
$e->add(t('The Product Height must be a number'));
}
if(!is_numeric($args['pLength'])){
$e->add(t('The Product Length must be a number'));
}
if(!is_numeric($args['pWeight'])){
$e->add(t('The Product Weight must be a number'));
}
return $e;
}
// GROUPS PAGE
public function groups()
{
$grouplist = VividProductGroupList::getGroupList();
$this->set("grouplist",$grouplist);
$this->requireAsset('css', 'vividStoreDashboard');
$this->requireAsset('javascript', 'vividStoreFunctions');
}
public function groupadded()
{
$this->set('success',"Group Successfully Added!");
$this->groups();
}
public function addgroup()
{
$this->groups();
$data = $this->post();
$this->error = null; //clear errors
$errors = $this->validateGroup($data);
$this->error = $errors;
if (!$errors->has()) {
VividProductGroup::add($data);
$this->redirect('/dashboard/store/products/', 'groupadded');
}
}
public function editgroup($gID)
{
$data = $this->post();
VividProductGroup::getByID($gID)->update($data);
}
public function validateGroup($args)
{
$e = Loader::helper('validation/error');
if($args['groupName']==""){
$e->add(t('You did not enter anything for the Group Name'));
}
if(strlen($args['groupName']) > 100){
$e->add(t('Keep the Group Name under 100 Characters'));
}
return $e;
}
public function deletegroup($gID)
{
VividProductGroup::getByID($gID)->remove();
}
}