view.html.php
199 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 Shortcodes view. |
| 16 | * |
| 17 | * @since 1.0 |
| 18 | */ |
| 19 | class VikAppointmentsViewShortcodes extends JViewVAP |
| 20 | { |
| 21 | /** |
| 22 | * @override |
| 23 | * View display method. |
| 24 | * |
| 25 | * @return void |
| 26 | */ |
| 27 | public function display($tpl = null) |
| 28 | { |
| 29 | $app = JFactory::getApplication(); |
| 30 | $input = $app->input; |
| 31 | $user = JFactory::getUser(); |
| 32 | |
| 33 | if (!$user->authorise('core.admin', 'com_vikappointments')) |
| 34 | { |
| 35 | wp_die( |
| 36 | '<h1>' . JText::translate('FATAL_ERROR') . '</h1>' . |
| 37 | '<p>' . JText::translate('RESOURCE_AUTH_ERROR') . '</p>', |
| 38 | 403 |
| 39 | ); |
| 40 | } |
| 41 | |
| 42 | $this->returnLink = $input->getBase64('return', ''); |
| 43 | |
| 44 | // get filters |
| 45 | $filters = array(); |
| 46 | $filters['search'] = $app->getUserStateFromRequest('shortcode.filters.search', 'filter_search', '', 'string'); |
| 47 | $filters['lang'] = $app->getUserStateFromRequest('shortcode.filters.lang', 'filter_lang', '*', 'string'); |
| 48 | $filters['type'] = $app->getUserStateFromRequest('shortcode.filters.type', 'filter_type', '', 'string'); |
| 49 | |
| 50 | $this->filters = $filters; |
| 51 | |
| 52 | // get shortcodes |
| 53 | |
| 54 | $this->limit = $app->getUserStateFromRequest('shortcodes.limit', 'limit', $app->get('list_limit'), 'uint'); |
| 55 | $this->offset = $this->getListLimitStart($filters); |
| 56 | $this->navbut = ''; |
| 57 | |
| 58 | $this->shortcodes = $this->hierarchicalShortcodes(); |
| 59 | |
| 60 | JLoader::import('adapter.filesystem.folder'); |
| 61 | |
| 62 | $this->views = array(); |
| 63 | |
| 64 | // get all the views that contain a default.xml file |
| 65 | // [0] : base path |
| 66 | // [1] : query |
| 67 | // [2] : true for recursive search |
| 68 | // [3] : true to return full paths |
| 69 | $files = JFolder::files(VAPBASE . DIRECTORY_SEPARATOR . 'views', 'default.xml', true, true); |
| 70 | |
| 71 | foreach ($files as $f) |
| 72 | { |
| 73 | // retrieve the view ID from the path: /views/[ID]/tmpl/default.xml |
| 74 | if (preg_match("/[\/\\\\]views[\/\\\\](.*?)[\/\\\\]tmpl[\/\\\\]default\.xml$/i", $f, $matches)) |
| 75 | { |
| 76 | $id = $matches[1]; |
| 77 | // load the XML form |
| 78 | $form = JForm::getInstance($id, $f); |
| 79 | // get the view title |
| 80 | $this->views[$id] = (string) $form->getXml()->layout->attributes()->title; |
| 81 | } |
| 82 | } |
| 83 | |
| 84 | $this->addToolbar(); |
| 85 | |
| 86 | // display parent |
| 87 | parent::display($tpl); |
| 88 | } |
| 89 | |
| 90 | /** |
| 91 | * Helper method to setup the toolbar. |
| 92 | * |
| 93 | * @return void |
| 94 | */ |
| 95 | public function addToolbar() |
| 96 | { |
| 97 | JToolbarHelper::title(JText::translate('VAPSHORTCDSMENUTITLE')); |
| 98 | |
| 99 | JToolbarHelper::addNew('shortcodes.add'); |
| 100 | JToolbarHelper::editList('shortcodes.edit'); |
| 101 | JToolbarHelper::deleteList(VikAppointments::getConfirmSystemMessage(), 'shortcodes.delete'); |
| 102 | JToolbarHelper::cancel('shortcodes.back'); |
| 103 | } |
| 104 | |
| 105 | /** |
| 106 | * Checks for advanced filters set in the request. |
| 107 | * |
| 108 | * @return boolean True if active, otherwise false. |
| 109 | */ |
| 110 | protected function hasFilters() |
| 111 | { |
| 112 | return ($this->filters['lang'] != '*' |
| 113 | || !empty($this->filters['type'])); |
| 114 | } |
| 115 | |
| 116 | /** |
| 117 | * Retrieves the shortcodes by using a hierarchical ordering. |
| 118 | * |
| 119 | * @return array An array of shortcodes. |
| 120 | * |
| 121 | * @since 1.2.3 |
| 122 | */ |
| 123 | protected function hierarchicalShortcodes() |
| 124 | { |
| 125 | $dbo = JFactory::getDbo(); |
| 126 | |
| 127 | /** |
| 128 | * Loads all the existing shortcodes. |
| 129 | * |
| 130 | * @since 1.2.3 |
| 131 | */ |
| 132 | |
| 133 | $q = $dbo->getQuery(true) |
| 134 | ->select('SQL_CALC_FOUND_ROWS *') |
| 135 | ->from($dbo->qn('#__vikappointments_wpshortcodes')); |
| 136 | |
| 137 | /** |
| 138 | * Filters the shortcodes by using the requested values. |
| 139 | * |
| 140 | * @since 1.1.5 |
| 141 | */ |
| 142 | |
| 143 | if ($this->filters['search']) |
| 144 | { |
| 145 | $q->where($dbo->qn('name') . ' LIKE ' . $dbo->q("%{$this->filters['search']}%")); |
| 146 | } |
| 147 | |
| 148 | if ($this->filters['lang'] != '*') |
| 149 | { |
| 150 | $q->where($dbo->qn('lang') . ' = ' . $dbo->q($this->filters['lang'])); |
| 151 | } |
| 152 | |
| 153 | if ($this->filters['type']) |
| 154 | { |
| 155 | $q->where($dbo->qn('type') . ' = ' . $dbo->q($this->filters['type'])); |
| 156 | } |
| 157 | |
| 158 | $dbo->setQuery($q); |
| 159 | $dbo->execute(); |
| 160 | |
| 161 | if (!$dbo->getNumRows()) |
| 162 | { |
| 163 | return []; |
| 164 | } |
| 165 | |
| 166 | $model = JModelVAP::getInstance('vikappointments', 'shortcode', 'admin'); |
| 167 | |
| 168 | $shortcodes = []; |
| 169 | |
| 170 | foreach ($dbo->loadObjectList() as $shortcode) |
| 171 | { |
| 172 | // load shortcode ancestors |
| 173 | $shortcode->ancestors = $model->getAncestors($shortcode); |
| 174 | |
| 175 | // create ordering leverage, based on version comparison |
| 176 | $tmp = array_merge([$shortcode->id], $shortcode->ancestors); |
| 177 | $shortcode->leverage = implode('.', array_reverse($tmp)); |
| 178 | |
| 179 | $shortcodes[] = $shortcode; |
| 180 | } |
| 181 | |
| 182 | // sort shortcodes by comparing the evaluated leverage |
| 183 | usort($shortcodes, function($a, $b) |
| 184 | { |
| 185 | return version_compare($a->leverage, $b->leverage); |
| 186 | }); |
| 187 | |
| 188 | // create pagination |
| 189 | jimport('joomla.html.pagination'); |
| 190 | $pageNav = new JPagination(count($shortcodes), $this->offset, $this->limit); |
| 191 | $this->navbut = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]); |
| 192 | |
| 193 | // take only the records that metch the pagination query |
| 194 | $shortcodes = array_splice($shortcodes, $this->offset, $this->limit); |
| 195 | |
| 196 | return $shortcodes; |
| 197 | } |
| 198 | } |
| 199 |