DiscountController.php
4.82 KB
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
<?php
/**
* Created by PhpStorm.
* User: Alex Savenko
* Date: 20.12.2016
* Time: 13:35
*/
namespace controllers;
use Phalcon\Mvc\Controller;
/**
* Class DiscountController
* @package controllers
* @property \models models
*/
class DiscountController extends Controller
{
/**
* Displays all discounts with pagination
* @return \Phalcon\Http\ResponseInterface
*/
public function indexAction() {
if( !$this->session->get('isAdminAuth') ) {
return $this->response->redirect([ 'for' => 'admin_login' ]);
}
$params = $this->dispatcher->getParams();
$page = !empty( $params['page'] ) ? $params['page'] : 1;
$data = $this->models->getDiscount()->getAllData();
$total = $this->models->getDiscount()->countData();
if( $total['0']['total'] > \config::get( 'limits/items') ) {
$paginate = $this->common->paginate(
[
'page' => $page,
'items_per_page' => \config::get( 'limits/admin_orders', 5),
'total_items' => $total[0]['total'],
'url_for' => [ 'for' => 'discount_index_paged', 'page' => $page ],
'index_page' => 'discount_index'
], true
);
}
$this->view->setVars([
'info' => $data,
'paginate' => !empty($paginate['output']) ? $paginate['output'] : '' ,
]);
}
/**
* Add discount form
* @return \Phalcon\Http\ResponseInterface
*/
public function addAction() {
$titlecmp = function ($a, $b) {
return strcasecmp($a['title'], $b['title']);
};
$lang_id = 1; // ua language
if( !$this->session->get('isAdminAuth') ) {
return $this->response->redirect([ 'for' => 'admin_login' ]);
}
if( $this->request->isPost() ) {
$data['name'] = $this->request->getPost('name', 'string', NULL );
$data['description'] = $this->request->getPost('description');
$data['start_date'] = $this->request->getPost('start_date');
$data['end_date'] = $this->request->getPost('end_date');
$data['discount'] = $this->request->getPost('discount', 'string', NULL );
if($this->models->getDiscount()->addData( $data )) {
$this->flash->success( 'Сохранение прошло успешно' );
return $this->response->redirect([ 'for' => 'discount_index' ]);
}
else {
$this->flash->error( 'Выберите товары для промокода' );
}
}
$catalog_temp = $this->common->getTypeSubtype1(NULL, $lang_id)['catalog'];
usort($catalog_temp, $titlecmp);
$this->view->setVar('catalog_temp', $catalog_temp);
$this->view->pick( 'discount/addEdit' );
}
/**
* Delete discount by $id
* @param $id
* @return \Phalcon\Http\ResponseInterface
*/
public function deleteAction($id) {
if( !$this->session->get('isAdminAuth') ) {
return $this->response->redirect([ 'for' => 'admin_login' ]);
}
$this->models->getDiscount()->deleteData($id);
return $this->response->redirect([ 'for' => 'discount_index' ]);
}
/**
* Update discount form
* @param $id
* @return \Phalcon\Http\ResponseInterface
*/
public function updateAction($id) {
$titlecmp = function ($a, $b) {
return strcasecmp($a['title'], $b['title']);
};
$lang_id = 1; // ua language
if( !$this->session->get('isAdminAuth') ) {
return $this->response->redirect([ 'for' => 'admin_login' ]);
}
$data = $this->models->getDiscount()->getOneData($id);
if( $this->request->isPost() ) {
$data[0]['name'] = $this->request->getPost('name', 'string', NULL );
$data[0]['discount'] = $this->request->getPost('discount', 'string', NULL );
$data[0]['start_date'] = $this->request->getPost('start_date');
$data[0]['end_date'] = $this->request->getPost('end_date');
$data[0]['description'] = $this->request->getPost('description');
if($this->models->getDiscount()->updateData( $data[0], $id ) ) {
$this->flash->success( 'Сохранение прошло успешно' );
return $this->response->redirect([ 'for' => 'discount_index' ]);
}
else {
$this->flash->error( 'Update error.' );
}
}
$this->view->pick( 'discount/addEdit' );
$this->view->setVars([
'page' => $data
]);
}
}