view.html.php
205 lines
| 1 | <?php |
| 2 | /** |
| 3 | * @package VikAppointments |
| 4 | * @subpackage core |
| 5 | * @author E4J s.r.l. |
| 6 | * @copyright Copyright (C) 2021 E4J s.r.l. All Rights Reserved. |
| 7 | * @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL |
| 8 | * @link https://vikwp.com |
| 9 | */ |
| 10 | |
| 11 | // No direct access |
| 12 | defined('ABSPATH') or die('No script kiddies please!'); |
| 13 | |
| 14 | /** |
| 15 | * VikAppointments coupons view. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsViewcoupons extends JViewVAP |
| 20 | { |
| 21 | /** |
| 22 | * VikAppointments view display method. |
| 23 | * |
| 24 | * @return void |
| 25 | */ |
| 26 | function display($tpl = null) |
| 27 | { |
| 28 | $app = JFactory::getApplication(); |
| 29 | $input = $app->input; |
| 30 | $dbo = JFactory::getDbo(); |
| 31 | |
| 32 | // set the toolbar |
| 33 | $this->addToolBar(); |
| 34 | |
| 35 | /** |
| 36 | * The filters are also handled by the export gateway. |
| 37 | * |
| 38 | * @see libraries.import.classes.coupons |
| 39 | */ |
| 40 | $filters = array(); |
| 41 | $filters['keys'] = $app->getUserStateFromRequest($this->getPoolName() . '.keys', 'keys', '', 'string'); |
| 42 | $filters['type'] = $app->getUserStateFromRequest($this->getPoolName() . '.type', 'type', 0, 'uint'); |
| 43 | $filters['value'] = $app->getUserStateFromRequest($this->getPoolName() . '.value', 'value', 0, 'uint'); |
| 44 | $filters['status'] = $app->getUserStateFromRequest($this->getPoolName() . '.status', 'status', 0, 'uint'); |
| 45 | $filters['id_group'] = $app->getUserStateFromRequest($this->getPoolName() . '.group', 'id_group', -1, 'int'); |
| 46 | |
| 47 | $this->filters = $filters; |
| 48 | |
| 49 | $this->ordering = $app->getUserStateFromRequest($this->getPoolName() . '.ordering', 'filter_order', 'c.id', 'string'); |
| 50 | $this->orderDir = $app->getUserStateFromRequest($this->getPoolName() . '.orderdir', 'filter_order_Dir', 'ASC', 'string'); |
| 51 | |
| 52 | // db object |
| 53 | $lim = $app->getUserStateFromRequest('com_vikappointments.limit', 'limit', $app->get('list_limit'), 'uint'); |
| 54 | $lim0 = $this->getListLimitStart($filters); |
| 55 | $navbut = ""; |
| 56 | |
| 57 | $rows = array(); |
| 58 | |
| 59 | $q = $dbo->getQuery(true); |
| 60 | |
| 61 | $q->select('SQL_CALC_FOUND_ROWS c.*') |
| 62 | ->from($dbo->qn('#__vikappointments_coupon', 'c')) |
| 63 | ->where(1) |
| 64 | ->order($dbo->qn($this->ordering) . ' ' . $this->orderDir); |
| 65 | |
| 66 | if (strlen($filters['keys'])) |
| 67 | { |
| 68 | $q->where($dbo->qn('c.code') . ' LIKE ' . $dbo->q("%{$filters['keys']}%")); |
| 69 | } |
| 70 | |
| 71 | if ($filters['type']) |
| 72 | { |
| 73 | $q->where($dbo->qn('c.type') . ' = ' . $filters['type']); |
| 74 | } |
| 75 | |
| 76 | if ($filters['value']) |
| 77 | { |
| 78 | $q->where($dbo->qn('c.percentot') . ' = ' . $filters['value']); |
| 79 | } |
| 80 | |
| 81 | $now = JFactory::getDate()->toSql(); |
| 82 | |
| 83 | if ($filters['status'] == 1) |
| 84 | { |
| 85 | // expired status |
| 86 | $q->where('(' . |
| 87 | $dbo->qn('c.dend') . ' IS NOT NULL AND ' . |
| 88 | $dbo->qn('c.dend') . ' <> ' . $dbo->q($dbo->getNullDate()) . ' AND ' . |
| 89 | $dbo->qn('c.dend') . ' < ' . $dbo->q($now) . |
| 90 | ') OR (' . |
| 91 | $dbo->qn('c.type') . ' = 2 AND ' . |
| 92 | '(' . $dbo->qn('c.max_quantity') . ' - ' . $dbo->qn('c.used_quantity') . ') <= 0' . |
| 93 | ')'); |
| 94 | } |
| 95 | else if ($filters['status'] == 2) |
| 96 | { |
| 97 | // active |
| 98 | $q->where('(' . |
| 99 | $dbo->qn('c.dstart') . ' IS NULL OR ' . |
| 100 | $dbo->qn('c.dstart') . ' = ' . $dbo->q($dbo->getNullDate()) . ' OR ' . |
| 101 | $dbo->qn('c.dstart') . ' <= ' . $dbo->q($now) . |
| 102 | ') AND (' . |
| 103 | $dbo->qn('c.dend') . ' IS NULL OR ' . |
| 104 | $dbo->qn('c.dend') . ' = ' . $dbo->q($dbo->getNullDate()) . ' OR ' . |
| 105 | $dbo->qn('c.dend') . ' > ' . $dbo->q($now) . |
| 106 | ') AND (' . |
| 107 | $dbo->qn('c.type') . ' = 1 OR ' . |
| 108 | '(' . $dbo->qn('c.max_quantity') . ' - ' . $dbo->qn('c.used_quantity') . ') > 0' . |
| 109 | ')'); |
| 110 | } |
| 111 | else if ($filters['status'] == 3) |
| 112 | { |
| 113 | // not active |
| 114 | $q->where(array( |
| 115 | $dbo->qn('c.dstart') . ' IS NOT NULL', |
| 116 | $dbo->qn('c.dstart') . ' <> ' . $dbo->q($dbo->getNullDate()), |
| 117 | $dbo->qn('c.dstart') . ' > ' . $dbo->q($now), |
| 118 | )); |
| 119 | } |
| 120 | |
| 121 | if ($filters['id_group'] != -1) |
| 122 | { |
| 123 | $q->where($dbo->qn('c.id_group') . ' = ' . $filters['id_group']); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Add support for manipulating query through the plugins. |
| 128 | * |
| 129 | * @see /site/helpers/libraries/mvc/view.php @ JViewVAP::onBeforeListQuery() |
| 130 | * |
| 131 | * @since 1.7 |
| 132 | */ |
| 133 | $this->onBeforeListQuery($q); |
| 134 | |
| 135 | $dbo->setQuery($q, $lim0, $lim); |
| 136 | $dbo->execute(); |
| 137 | |
| 138 | // assert limit used for list query |
| 139 | $this->assertListQuery($lim0, $lim); |
| 140 | |
| 141 | if ($dbo->getNumRows()) |
| 142 | { |
| 143 | $rows = $dbo->loadAssocList(); |
| 144 | $dbo->setQuery('SELECT FOUND_ROWS();'); |
| 145 | jimport('joomla.html.pagination'); |
| 146 | $pageNav = new JPagination($dbo->loadResult(), $lim0, $lim); |
| 147 | $navbut = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]); |
| 148 | } |
| 149 | |
| 150 | $this->rows = $rows; |
| 151 | $this->navbut = $navbut; |
| 152 | |
| 153 | // display the template (default.php) |
| 154 | parent::display($tpl); |
| 155 | } |
| 156 | |
| 157 | /** |
| 158 | * Setting the toolbar. |
| 159 | * |
| 160 | * @return void |
| 161 | */ |
| 162 | protected function addToolBar() |
| 163 | { |
| 164 | // add menu title and some buttons to the page |
| 165 | JToolBarHelper::title(JText::translate('VAPMAINTITLEVIEWCOUPONS'), 'vikappointments'); |
| 166 | |
| 167 | $user = JFactory::getUser(); |
| 168 | |
| 169 | if ($user->authorise('core.create', 'com_vikappointments')) |
| 170 | { |
| 171 | JToolBarHelper::addNew('coupon.add'); |
| 172 | } |
| 173 | |
| 174 | if ($user->authorise('core.edit', 'com_vikappointments')) |
| 175 | { |
| 176 | JToolBarHelper::editList('coupon.edit'); |
| 177 | } |
| 178 | |
| 179 | if ($user->authorise('core.create', 'com_vikappointments')) |
| 180 | { |
| 181 | JToolBarHelper::custom('import', 'upload', 'upload', JText::translate('VAPIMPORT'), false); |
| 182 | } |
| 183 | |
| 184 | JToolBarHelper::custom('export', 'download', 'download', JText::translate('VAPEXPORT'), false); |
| 185 | |
| 186 | if ($user->authorise('core.delete', 'com_vikappointments')) |
| 187 | { |
| 188 | JToolBarHelper::deleteList(VikAppointments::getConfirmSystemMessage(), 'coupon.delete'); |
| 189 | } |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Checks for advanced filters set in the request. |
| 194 | * |
| 195 | * @return boolean True if active, otherwise false. |
| 196 | */ |
| 197 | protected function hasFilters() |
| 198 | { |
| 199 | return ($this->filters['type'] != 0 |
| 200 | || $this->filters['value'] != 0 |
| 201 | || $this->filters['status'] != 0 |
| 202 | || $this->filters['id_group'] != -1); |
| 203 | } |
| 204 | } |
| 205 |