DiscountController.php 4.82 KB
<?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
        ]);

    }
}