DiscountController.php
3.02 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
<?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' );
}
}