view.html.php
141 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 | VAPLoader::import('libraries.tax.factory'); |
| 15 | |
| 16 | /** |
| 17 | * VikAppointments tax management view. |
| 18 | * |
| 19 | * @since 1.0 |
| 20 | */ |
| 21 | class VikAppointmentsViewmanagetax extends JViewVAP |
| 22 | { |
| 23 | /** |
| 24 | * VikAppointments view display method. |
| 25 | * |
| 26 | * @return void |
| 27 | */ |
| 28 | function display($tpl = null) |
| 29 | { |
| 30 | $dbo = JFactory::getDbo(); |
| 31 | $app = JFactory::getApplication(); |
| 32 | $input = $app->input; |
| 33 | |
| 34 | $ids = $input->getUint('cid', array()); |
| 35 | $type = $ids ? 'edit' : 'new'; |
| 36 | |
| 37 | // set the toolbar |
| 38 | $this->addToolBar($type); |
| 39 | |
| 40 | $tax = null; |
| 41 | |
| 42 | if ($type == 'edit') |
| 43 | { |
| 44 | $q = $dbo->getQuery(true) |
| 45 | ->select('*') |
| 46 | ->from($dbo->qn('#__vikappointments_tax')) |
| 47 | ->where($dbo->qn('id') . ' = ' . $ids[0]); |
| 48 | |
| 49 | $dbo->setQuery($q, 0, 1); |
| 50 | $tax = $dbo->loadObject(); |
| 51 | |
| 52 | if ($tax) |
| 53 | { |
| 54 | $tax->rules = []; |
| 55 | |
| 56 | $q = $dbo->getQuery(true); |
| 57 | |
| 58 | $q->select('*') |
| 59 | ->from($dbo->qn('#__vikappointments_tax_rule')) |
| 60 | ->where($dbo->qn('id_tax') . ' = ' . $tax->id) |
| 61 | ->order($dbo->qn('ordering') . ' ASC'); |
| 62 | |
| 63 | $dbo->setQuery($q); |
| 64 | |
| 65 | foreach ($dbo->loadObjectList() as $rule) |
| 66 | { |
| 67 | // decode breakdown list |
| 68 | $rule->breakdown = $rule->breakdown ? json_decode($rule->breakdown) : []; |
| 69 | |
| 70 | $tax->rules[] = $rule; |
| 71 | } |
| 72 | } |
| 73 | } |
| 74 | |
| 75 | if (empty($tax)) |
| 76 | { |
| 77 | $tax = (object) $this->getBlankItem(); |
| 78 | } |
| 79 | |
| 80 | // use tax data stored in user state |
| 81 | $this->injectUserStateData($tax, 'vap.tax.data'); |
| 82 | |
| 83 | $this->tax = $tax; |
| 84 | |
| 85 | // display the template |
| 86 | parent::display($tpl); |
| 87 | } |
| 88 | |
| 89 | /** |
| 90 | * Setting the toolbar. |
| 91 | * |
| 92 | * @param string $type The request type (new or edit). |
| 93 | * |
| 94 | * @return void |
| 95 | */ |
| 96 | protected function addToolBar($type) |
| 97 | { |
| 98 | // add menu title and some buttons to the page |
| 99 | if ($type == 'edit') |
| 100 | { |
| 101 | JToolBarHelper::title(JText::translate('VAPMAINTITLEEDITTAX'), 'vikappointments'); |
| 102 | } |
| 103 | else |
| 104 | { |
| 105 | JToolBarHelper::title(JText::translate('VAPMAINTITLENEWTAX'), 'vikappointments'); |
| 106 | } |
| 107 | |
| 108 | $user = JFactory::getUser(); |
| 109 | |
| 110 | if ($user->authorise('core.edit', 'com_vikappointments') |
| 111 | || $user->authorise('core.create', 'com_vikappointments')) |
| 112 | { |
| 113 | JToolbarHelper::apply('tax.save', JText::translate('VAPSAVE')); |
| 114 | JToolbarHelper::save('tax.saveclose', JText::translate('VAPSAVEANDCLOSE')); |
| 115 | } |
| 116 | |
| 117 | if ($user->authorise('core.edit', 'com_vikappointments') |
| 118 | && $user->authorise('core.create', 'com_vikappointments')) |
| 119 | { |
| 120 | JToolbarHelper::save2new('tax.savenew', JText::translate('VAPSAVEANDNEW')); |
| 121 | } |
| 122 | |
| 123 | JToolBarHelper::cancel('tax.cancel', $type == 'edit' ? 'JTOOLBAR_CLOSE' : 'JTOOLBAR_CANCEL'); |
| 124 | } |
| 125 | |
| 126 | /** |
| 127 | * Returns a blank item. |
| 128 | * |
| 129 | * @return array A blank item for new requests. |
| 130 | */ |
| 131 | protected function getBlankItem() |
| 132 | { |
| 133 | return array( |
| 134 | 'id' => 0, |
| 135 | 'name' => '', |
| 136 | 'description' => '', |
| 137 | 'rules' => array(), |
| 138 | ); |
| 139 | } |
| 140 | } |
| 141 |