From 5d41bb08d4e7cf69dbbcb51f4f42143a4c2757b3 Mon Sep 17 00:00:00 2001 From: Alex Date: Tue, 20 Dec 2016 17:21:03 +0200 Subject: [PATCH] add/edit --- src/app/backend/controllers/DiscountController.php | 97 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++----------- src/app/backend/views/discount/index.php | 6 +++--- www-backend/index.php | 20 ++++++++++++++++++++ 3 files changed, 109 insertions(+), 14 deletions(-) diff --git a/src/app/backend/controllers/DiscountController.php b/src/app/backend/controllers/DiscountController.php index bdf8716..7039cd6 100644 --- a/src/app/backend/controllers/DiscountController.php +++ b/src/app/backend/controllers/DiscountController.php @@ -32,12 +32,11 @@ class DiscountController extends Controller $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') ) - { + if( $total['0']['total'] > \config::get( 'limits/items') ) { + $paginate = $this->common->paginate( [ 'page' => $page, @@ -47,6 +46,7 @@ class DiscountController extends Controller 'index_page' => 'discount_index' ], true ); + } $this->view->setVars([ 'info' => $data, @@ -62,16 +62,19 @@ class DiscountController extends Controller public function addAction() { $titlecmp = function ($a, $b) { + return strcasecmp($a['title'], $b['title']); + }; + $lang_id = 1; // ua language - if( !$this->session->get('isAdminAuth') ) - { + if( !$this->session->get('isAdminAuth') ) { + return $this->response->redirect([ 'for' => 'admin_login' ]); + } - if( $this->request->isPost() ) - { + if( $this->request->isPost() ) { $data['name'] = $this->request->getPost('name', 'string', NULL ); $data['description'] = $this->request->getPost('description'); @@ -79,14 +82,16 @@ class DiscountController extends Controller $data['end_date'] = $this->request->getPost('end_date'); $data['discount'] = $this->request->getPost('discount', 'string', NULL ); - if($this->models->getDiscount()->addData( $data )) - { + if($this->models->getDiscount()->addData( $data )) { + $this->flash->success( 'Сохранение прошло успешно' ); return $this->response->redirect([ 'for' => 'discount_index' ]); + } - else - { + else { + $this->flash->error( 'Выберите товары для промокода' ); + } } @@ -99,4 +104,74 @@ class DiscountController extends Controller } + /** + * 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 + ]); + + } } \ No newline at end of file diff --git a/src/app/backend/views/discount/index.php b/src/app/backend/views/discount/index.php index 87d5129..77e0ec7 100644 --- a/src/app/backend/views/discount/index.php +++ b/src/app/backend/views/discount/index.php @@ -35,9 +35,9 @@ $data_pages .= '
'. '
'. - ''. - '
'. - '
'. + ''. + '
'. + '
'. '
'; } diff --git a/www-backend/index.php b/www-backend/index.php index f073b89..c5a6262 100644 --- a/www-backend/index.php +++ b/www-backend/index.php @@ -1099,6 +1099,26 @@ try ) ->setName('discount_add'); + $router->add + ( + '/discount_update', + [ + 'controller' => 'discount', + 'action' => 'update' + ] + ) + ->setName('discount_update'); + + $router->add + ( + '/discount_delete', + [ + 'controller' => 'discount', + 'action' => 'delete' + ] + ) + ->setName('discount_delete'); + /* *************************************** */ $router->add -- libgit2 0.21.4