Commit c4094a9550bb50a85450b1de1b9942a10cdef06f

Authored by Alex Savenko
1 parent 83008cdb

add/edit

src/app/backend/controllers/DiscountController.php
... ... @@ -10,10 +10,18 @@ namespace controllers;
10 10  
11 11 use Phalcon\Mvc\Controller;
12 12  
13   -
  13 +/**
  14 + * Class DiscountController
  15 + * @package controllers
  16 + * @property \models models
  17 + */
14 18 class DiscountController extends Controller
15 19 {
16 20  
  21 + /**
  22 + * Displays all discounts with pagination
  23 + * @return \Phalcon\Http\ResponseInterface
  24 + */
17 25 public function indexAction() {
18 26  
19 27 if( !$this->session->get('isAdminAuth') ) {
... ... @@ -47,4 +55,48 @@ class DiscountController extends Controller
47 55  
48 56 }
49 57  
  58 + /**
  59 + * Add discount form
  60 + * @return \Phalcon\Http\ResponseInterface
  61 + */
  62 + public function addAction() {
  63 +
  64 + $titlecmp = function ($a, $b) {
  65 + return strcasecmp($a['title'], $b['title']);
  66 + };
  67 + $lang_id = 1; // ua language
  68 + if( !$this->session->get('isAdminAuth') )
  69 + {
  70 + return $this->response->redirect([ 'for' => 'admin_login' ]);
  71 + }
  72 +
  73 + if( $this->request->isPost() )
  74 + {
  75 +
  76 + $data['name'] = $this->request->getPost('name', 'string', NULL );
  77 + $data['description'] = $this->request->getPost('description');
  78 + $data['start_date'] = $this->request->getPost('start_date');
  79 + $data['end_date'] = $this->request->getPost('end_date');
  80 + $data['discount'] = $this->request->getPost('discount', 'string', NULL );
  81 +
  82 + if($this->models->getDiscount()->addData( $data ))
  83 + {
  84 + $this->flash->success( 'Сохранение прошло успешно' );
  85 + return $this->response->redirect([ 'for' => 'discount_index' ]);
  86 + }
  87 + else
  88 + {
  89 + $this->flash->error( 'Выберите товары для промокода' );
  90 + }
  91 + }
  92 +
  93 + $catalog_temp = $this->common->getTypeSubtype1(NULL, $lang_id)['catalog'];
  94 + usort($catalog_temp, $titlecmp);
  95 +
  96 +
  97 + $this->view->setVar('catalog_temp', $catalog_temp);
  98 + $this->view->pick( 'discount/addEdit' );
  99 +
  100 + }
  101 +
50 102 }
51 103 \ No newline at end of file
... ...
src/app/backend/views/discount/addEdit.php 0 → 100644
  1 +<div id="addEdit">
  2 + <div class="inner"><?= $this->flash->output(); ?></div>
  3 + <div class="inner">
  4 + <div class="sidebar_content_wrapper clearfix">
  5 + <div class="sidebar_wrapper float">
  6 + <div class="sidebar clearfix">
  7 + <?= $this->partial('partial/sidebar') ?>
  8 + </div>
  9 + </div>
  10 + <div class="content_wrapper float">
  11 + <div class="content_wrapper_list clearfix">
  12 + <div class="table_name header_gradient"></div>
  13 +
  14 + <div class="table_pages_wrapper">
  15 + <form enctype="multipart/form-data" method="post" action="" id="discount_add_edit">
  16 +
  17 + <div class="clearfix input_wrapper">
  18 + <div class="label"><label for="name">Название</label></div>
  19 + <div class="input"><input required type="text" name="name" id="name" value='<?= (isset( $page['0']['name'] ) && !empty( $page['0']['name'] ) ? $page['0']['name'] : '') ?>'></div>
  20 + </div>
  21 +
  22 + <div class="clearfix input_wrapper">
  23 + <div class="label"><label for="discount">Скидка</label></div>
  24 + <div class="input"><input required type="text" name="discount" id="discount" value='<?= (isset( $page['0']['discount'] ) && !empty( $page['0']['discount'] ) ? $page['0']['discount'] : '') ?>'></div>
  25 + </div>
  26 +
  27 + <div class="clearfix input_wrapper">
  28 + <div class="label"><label for="start_date">Дата начала действия скидки (включительно)</label></div>
  29 + <div class="input">
  30 + <input required type="text" name="start_date" id="start_date"
  31 + value="<?= (isset( $page['0']['start_date'] ) && !empty( $page['0']['start_date'] ) ? date('d-m-Y H:i:s', strtotime($page['0']['start_date'])) : '') ?>">
  32 +
  33 + </div>
  34 + </div>
  35 +
  36 + <div class="clearfix input_wrapper">
  37 + <div class="label"><label for="end_date">Дата окончания действия скидки (включительно)</label></div>
  38 + <div class="input">
  39 + <input required type="text" name="end_date" id="end_date"
  40 + value="<?= (isset( $page['0']['end_date'] ) && !empty( $page['0']['end_date'] ) ? date('d-m-Y H:i:s' , strtotime($page['0']['end_date'])) : '') ?>">
  41 + </div>
  42 + </div>
  43 +
  44 + <div class="clearfix input_wrapper">
  45 + <div class="label"><label for="description">Описание</label></div>
  46 + <div class="input">
  47 + <textarea name="description" id="description">
  48 + <?= (isset( $page['0']['description'] ) && !empty( $page['0']['description'] ) ? $page['0']['description'] : '') ?>
  49 + </textarea>
  50 + </div>
  51 + </div>
  52 +
  53 + <div class="clearfix submit_wrapper">
  54 + <a href="<?= $this->url->get([ 'for' => 'discount_index' ]) ?>" class="news_cancel float">Отмена</a>
  55 + <input type="submit" class="news_submit float" name="save" value="Сохранить">
  56 + </div>
  57 +
  58 + </form>
  59 + </div>
  60 + </div>
  61 + </div>
  62 + </div>
  63 + </div>
  64 +</div>
  65 +<script>
  66 +
  67 + var datepicker = {
  68 + format: "d-m-Y H:i:s"
  69 + };
  70 + var $body = $('body');
  71 + var filter = new Filter();
  72 + var catalog = new Catalog(<?= json_encode($catalog_temp) ?>);
  73 + console.log(catalog.getCatalog());
  74 +
  75 + $('#start_date').datetimepicker(datepicker);
  76 + $('#end_date').datetimepicker(datepicker);
  77 +
  78 + $("form").submit(function() {
  79 + $("input").removeAttr("disabled");
  80 + });
  81 +
  82 + $('#add_category, #add_item').click(function() {
  83 + var parent = $(this).parent();
  84 + var select = parent.find('.input');
  85 + var clone = select.clone()[0];
  86 + parent.append(clone);
  87 + });
  88 +
  89 + $('#generate').click(function() {
  90 + var length = 7;
  91 + var code = generateCode(length);
  92 + $('#code').val(code);
  93 + });
  94 +
  95 + $body.on('click', '.delete_item', function() {
  96 + var parent = $(this).parent();
  97 + parent.remove();
  98 + });
  99 +
  100 +
  101 + $body.on('change', '.catalog', function () {
  102 + var $filter = $('.filter');
  103 + var catalog_id = $(this).val();
  104 + var sub = catalog.getSub(catalog_id, catalog.getCatalog());
  105 +
  106 + if(!sub) {
  107 + $.ajax({
  108 + url: '/get_filters_by_catalog',
  109 + method: 'GET',
  110 + data: {
  111 + catalog_id : catalog_id
  112 + },
  113 + dataType: 'json'
  114 + }).done(function (data) {
  115 + filter.setFilters(data);
  116 + filter.render($filter);
  117 + });
  118 + } else {
  119 + var next = $(this).next('select');
  120 +
  121 + while(next.length) {
  122 + next.remove();
  123 + next = $(this).next('select');
  124 + }
  125 +
  126 + $(this).after(catalog.renderSub(sub));
  127 + }
  128 + });
  129 +
  130 + $('#apply_filter').click(function (e) {
  131 + e.preventDefault();
  132 + var $catalog = $('.catalog');
  133 + var l = $catalog.length;
  134 + var $input = $('.products');
  135 + var prices = [];
  136 +
  137 + prices.push(Number($('#from_price').val()) || 0);
  138 + prices.push(Number($('#to_price').val()) || Number.MAX_SAFE_INTEGER || 1000000);
  139 + $.ajax({
  140 + url: '/get_items_by_filter',
  141 + method: 'POST',
  142 + data: {
  143 + filters : filter.getFilters(),
  144 + catalog_id : $catalog[l-1].value,
  145 + prices : prices
  146 + },
  147 + dataType: 'json'
  148 + }).done(function (data) {
  149 + renderItems(data, $input);
  150 + });
  151 + });
  152 +
  153 + $body.on('click', '#add_goods', function () {
  154 + var parent_items = $('.filter_item:checked').parent().parent().clone();
  155 + parent_items.append('<button type="button" class="delete_item">Удалить</button>');
  156 + var inputs = parent_items.find('input');
  157 + inputs.prop('disabled', true);
  158 + inputs.prop('name', 'items[]');
  159 + inputs.prop('class', 'items');
  160 + inputs.prop('id', 'items');
  161 + $('.chose_items').append(parent_items);
  162 + });
  163 +
  164 + $body.on('click', '.delete_item', function () {
  165 + var parent = $(this).parent();
  166 + parent.remove();
  167 + });
  168 +
  169 + $body.on('change', '#choose_all', function () {
  170 + var checked = $(this).prop('checked');
  171 + $('.filter_item').prop( "checked", checked );
  172 + });
  173 +
  174 +
  175 + function renderItems(data, parent) {
  176 + parent.empty();
  177 +
  178 + var items = '<div><label><input style="display: inline-block" id="choose_all" type="checkbox">Выбрать все</label></div>';
  179 +
  180 + data.forEach(function(element){
  181 + items += '<div><label><input style="display:inline-block" class="filter_item" type="checkbox" value="'
  182 + + element.item_id + '">' + element.title + ' ' + element.size + ' ' + element.price2 + ' грн.'
  183 + + '</label></div>';
  184 + });
  185 + items += '<div><button type="button" id="add_goods">Добавить товары</button></div>';
  186 + parent.append(items);
  187 + }
  188 +
  189 + function Filter() {
  190 + var _filters = {};
  191 + this.render = function ($parent) {
  192 + $parent.empty();
  193 + var html = '';
  194 + forEach(_filters, function(element, key) {
  195 + html += '<div style="display: inline-block; margin: 5px;"><h4>' + key + '</h4>';
  196 + html += '<div>';
  197 + forEach(element, function (v) {
  198 + html += '<label><input type="checkbox" style="display:inline-block" id="' + v['id'] + '" value="' + v['filter_value_id'] +'">'
  199 + + v['filter_value_value'] + '</label></input><br/>';
  200 + $('body').on('change', '#' + v['id'], function() {
  201 + updateFilters(v['id']);
  202 + });
  203 + });
  204 +
  205 + html += '</div></div>';
  206 + });
  207 + $parent.append(html);
  208 +
  209 + };
  210 + this.setFilters = function (filters) {
  211 + _filters = filters;
  212 + };
  213 +
  214 + this.getFilters = function () {
  215 + var ids = [];
  216 + forEach(_filters, function(element) {
  217 + forEach(element, function (v) {
  218 + if(v.checked)
  219 + ids.push(v.id);
  220 + });
  221 + });
  222 + return ids;
  223 + };
  224 +
  225 + function updateFilters (filter_id) {
  226 + forEach(_filters, function(element) {
  227 + forEach(element, function (v) {
  228 + if(v.id == filter_id)
  229 + v.checked = !v.checked;
  230 + });
  231 + });
  232 + }
  233 + }
  234 +
  235 + function Catalog(catalog) {
  236 + var _catalog = catalog;
  237 +
  238 + this.getCatalog = function() {
  239 + return _catalog;
  240 + };
  241 +
  242 + this.getSub = function getSub(catalog_id, catalog) {
  243 + var result = null;
  244 + forEach(catalog, function (element) {
  245 + if(element.id == catalog_id) {
  246 + result = element.sub;
  247 + }
  248 + if(element.sub) {
  249 + var t = getSub(catalog_id, element.sub);
  250 + result = t || result;
  251 + }
  252 + });
  253 + return result;
  254 + };
  255 +
  256 + this.renderSub = function (sub) {
  257 + var select = '<select class="catalog" id="catalog">';
  258 + select += '<option disabled selected label=" "></option>';
  259 + forEach(sub, function (element) {
  260 + select += '<option value="' + element.id + '">' + element.title + '</option>';
  261 + });
  262 + select += '</select>';
  263 + return select;
  264 + }
  265 + }
  266 +
  267 + function generateCode(length) {
  268 + var code = '';
  269 + for(var i = 0; i < length; i++)
  270 + code += Math.floor(Math.random() * 10);
  271 + return code;
  272 + }
  273 +
  274 + function forEach(obj, callback) {
  275 + for(var key in obj) {
  276 + if(obj.hasOwnProperty(key)) {
  277 + callback(obj[key], key);
  278 + }
  279 + }
  280 + }
  281 +
  282 +
  283 +</script>
0 284 \ No newline at end of file
... ...
src/app/backend/views/discount/index.php
... ... @@ -20,7 +20,7 @@
20 20 <div class="h_700">
21 21 <div class="content_wrapper_list clearfix">
22 22 <div class="table_name header_gradient">discounts</div>
23   - <div class="table_add_page"><a href="<?= $this->url->get([ 'for' => 'promo_codes_add' ]) ?>" title="Добавить">Добавить</a></div>
  23 + <div class="table_add_page"><a href="<?= $this->url->get([ 'for' => 'discount_add' ]) ?>" title="Добавить">Добавить</a></div>
24 24  
25 25 <div class="table_pages_wrapper">
26 26  
... ...
src/app/backend/views/partial/sidebar.php
... ... @@ -57,7 +57,7 @@
57 57 <ul class="body">
58 58 <li class="point4"><a href="<?= $this->url->get([ 'for' => 'reviews_index' ]) ?>" title="Акции">Отзывы</a></li>
59 59 <li class="point4"><a href="<?= $this->url->get([ 'for' => 'sales_index' ]) ?>" title="Акции">Акции основного сайта</a></li>
60   - <li class="point4"><a href="<?= $this->url->get([ 'for' => 'discount_index' ]) ?>" title="Промокоды">Discount</a></li>
  60 + <li class="point4"><a href="<?= $this->url->get([ 'for' => 'discount_index' ]) ?>" title="Промокоды">Скидки</a></li>
61 61 <li class="point4"><a href="<?= $this->url->get([ 'for' => 'promo_codes_index' ]) ?>" title="Промокоды">Промокоды</a></li>
62 62 <li class="point4"><a href="<?= $this->url->get([ 'for' => 'action_discount_index' ]) ?>" title="Акции">Акции для магазина</a></li>
63 63 <li class="point4"><a href="<?= $this->url->get([ 'for' => 'actions_index' ]) ?>" title="Пределы">Пределы акций для магазина</a></li>
... ...
www-backend/index.php
... ... @@ -1089,6 +1089,16 @@ try
1089 1089 )
1090 1090 ->setName('discount_index_paged');
1091 1091  
  1092 + $router->add
  1093 + (
  1094 + '/discount_add',
  1095 + [
  1096 + 'controller' => 'promo_codes',
  1097 + 'action' => 'add'
  1098 + ]
  1099 + )
  1100 + ->setName('discount_add');
  1101 +
1092 1102 /* *************************************** */
1093 1103  
1094 1104 $router->add
... ...