DiscountController.php 7.15 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 );
            $data['status']         = 1;


            //$data['catalog_ids']    = $this->request->getPost('catalog', 'string', NULL );
            $data['group_ids']      = $this->request->getPost('items', 'string', NULL );
            //$data['all_items']      = $this->request->getPost('all_items', 'int', NULL);

            if ($data['discount'] > 100) {

                $this->flash->error( 'Размер скидки не может привышать 100' );

            }
            else {

                if(!empty($data['group_ids']) && $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', 'int', 0 );
            $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');
            $data[0]['group_ids']       = $this->request->getPost('items', 'string', NULL );

            if ($data[0]['discount'] > 100) {

                $this->flash->error( 'Размер скидки не может привышать 100' );

            }
            else {

                if($this->models->getDiscount()->updateData( $data[0], $id ) ) {

                    $this->flash->success( 'Сохранение прошло успешно' );
                    return $this->response->redirect([ 'for' => 'discount_index' ]);

                }
                else {

                    $this->flash->error( 'Update error.' );

                }

            }


        }

        $data[0]['group_ids'] = $this->common->parseArray($data[0]['group_ids']);
        //$data[0]['catalog_ids'] = $this->common->parseArray($data[0]['catalog_ids']);

        $catalog_temp = $this->common->getTypeSubtype1(NULL, $lang_id)['catalog'];
        usort($catalog_temp, $titlecmp);

        foreach($catalog_temp as &$group) {

            usort($group['sub'], $titlecmp);

        }

        if(!empty($data[0]['group_ids'][0])) {

            $groups = $this->models->getItems()->getItemsByIds($lang_id, $data[0]['group_ids']);
            usort($groups, $titlecmp);

        }

        $this->view->pick( 'discount/addEdit' );

        $this->view->setVars([
            'page' => $data,
            'catalog_temp' => $catalog_temp,
            'groups' => !empty($groups) ? $groups : null
        ]);

    }

    /**
     * Switch status indicator
     * @param $id
     * @return \Phalcon\Http\ResponseInterface
     */
    public function switchAction($id) {

        if( !$this->session->get('isAdminAuth') ) {

            return $this->response->redirect([ 'for' => 'admin_login' ]);

        }

        $status = $this->models->getDiscount()->getStatus($id);

        if ($status == 1) {

            $this->models->getDiscount()->updateStatus('0', $id);

        }
        elseif ($status == 0) {

            $this->models->getDiscount()->updateStatus('1', $id);

        }
        else {

            $this->flash->error(var_dump($status));

        }

        return $this->response->redirect([ 'for' => 'discount_index' ]);

    }
}