| @@ -1,0 +1,185 @@ | ||
| 1 | +<?php | |
| 2 | +/** | |
| 3 | + * @package VikBooking | |
| 4 | + * @subpackage com_vikbooking | |
| 5 | + * @author Alessio Gaggii - E4J srl | |
| 6 | + * @copyright Copyright (C) 2023 E4J srl. All rights reserved. | |
| 7 | + * @license GNU General Public License version 2 or later; see LICENSE | |
| 8 | + * @link https://vikwp.com | |
| 9 | + */ | |
| 10 | + | |
| 11 | +// No direct access | |
| 12 | +defined('ABSPATH') or die('No script kiddies please!'); | |
| 13 | + | |
| 14 | +jimport('joomla.application.component.view'); | |
| 15 | +jimport('adapter.acl.access'); | |
| 16 | + | |
| 17 | +/** | |
| 18 | + * VikBooking Shortcodes view. | |
| 19 | + * @wponly | |
| 20 | + * | |
| 21 | + * @since 1.0 | |
| 22 | + */ | |
| 23 | +class VikBookingViewShortcodes extends JView | |
| 24 | +{ | |
| 25 | + /** | |
| 26 | + * @override | |
| 27 | + * View display method. | |
| 28 | + * | |
| 29 | + * @return void | |
| 30 | + */ | |
| 31 | + public function display($tpl = null) | |
| 32 | + { | |
| 33 | + $app = JFactory::getApplication(); | |
| 34 | + $user = JFactory::getUser(); | |
| 35 | + | |
| 36 | + if (!$user->authorise('core.admin', 'com_vikbooking')) | |
| 37 | + { | |
| 38 | + wp_die( | |
| 39 | + '<h1>' . JText::translate('FATAL_ERROR') . '</h1>' . | |
| 40 | + '<p>' . JText::translate('RESOURCE_AUTH_ERROR') . '</p>', | |
| 41 | + 403 | |
| 42 | + ); | |
| 43 | + } | |
| 44 | + | |
| 45 | + $this->returnLink = $app->input->getBase64('return', ''); | |
| 46 | + | |
| 47 | + // get filters | |
| 48 | + $filters = array(); | |
| 49 | + $filters['search'] = $app->getUserStateFromRequest('shortcode.filters.search', 'filter_search', '', 'string'); | |
| 50 | + $filters['lang'] = $app->getUserStateFromRequest('shortcode.filters.lang', 'filter_lang', '*', 'string'); | |
| 51 | + $filters['type'] = $app->getUserStateFromRequest('shortcode.filters.type', 'filter_type', '', 'string'); | |
| 52 | + | |
| 53 | + $this->filters = $filters; | |
| 54 | + | |
| 55 | + // get shortcodes | |
| 56 | + | |
| 57 | + $this->limit = $app->getUserStateFromRequest('shortcodes.limit', 'limit', $app->get('list_limit'), 'uint'); | |
| 58 | + $this->offset = $app->input->getUint('limitstart', 0); | |
| 59 | + $this->navbut = ''; | |
| 60 | + | |
| 61 | + $this->shortcodes = $this->hierarchicalShortcodes(); | |
| 62 | + | |
| 63 | + JLoader::import('adapter.filesystem.folder'); | |
| 64 | + | |
| 65 | + $this->views = array(); | |
| 66 | + | |
| 67 | + // get all the views that contain a default.xml file | |
| 68 | + // [0] : base path | |
| 69 | + // [1] : query | |
| 70 | + // [2] : true for recursive search | |
| 71 | + // [3] : true to return full paths | |
| 72 | + $files = JFolder::files(VBO_SITE_PATH . DIRECTORY_SEPARATOR . 'views', 'default.xml', true, true); | |
| 73 | + | |
| 74 | + foreach ($files as $f) | |
| 75 | + { | |
| 76 | + // retrieve the view ID from the path: /views/[ID]/tmpl/default.xml | |
| 77 | + if (preg_match("/[\/\\\\]views[\/\\\\](.*?)[\/\\\\]tmpl[\/\\\\]default\.xml$/i", $f, $matches)) | |
| 78 | + { | |
| 79 | + $id = $matches[1]; | |
| 80 | + // load the XML form | |
| 81 | + $form = JForm::getInstance($id, $f); | |
| 82 | + // get the view title | |
| 83 | + $this->views[$id] = (string) $form->getXml()->layout->attributes()->title; | |
| 84 | + } | |
| 85 | + } | |
| 86 | + | |
| 87 | + $this->addToolbar(); | |
| 88 | + | |
| 89 | + // display parent | |
| 90 | + parent::display($tpl); | |
| 91 | + } | |
| 92 | + | |
| 93 | + /** | |
| 94 | + * Helper method to setup the toolbar. | |
| 95 | + * | |
| 96 | + * @return void | |
| 97 | + */ | |
| 98 | + public function addToolbar() | |
| 99 | + { | |
| 100 | + JToolbarHelper::title(JText::translate('VBOSHORTCDSMENUTITLE')); | |
| 101 | + | |
| 102 | + JToolbarHelper::addNew('shortcodes.create'); | |
| 103 | + JToolbarHelper::editList('shortcodes.edit'); | |
| 104 | + JToolbarHelper::deleteList(JText::translate('VBDELCONFIRM'), 'shortcodes.delete'); | |
| 105 | + JToolbarHelper::cancel('shortcodes.back'); | |
| 106 | + } | |
| 107 | + | |
| 108 | + /** | |
| 109 | + * Retrieves the shortcodes by using a hierarchical ordering. | |
| 110 | + * | |
| 111 | + * @return array An array of shortcodes. | |
| 112 | + * | |
| 113 | + * @since 1.5 | |
| 114 | + */ | |
| 115 | + protected function hierarchicalShortcodes() | |
| 116 | + { | |
| 117 | + $dbo = JFactory::getDbo(); | |
| 118 | + | |
| 119 | + // loads all the existing shortcodes | |
| 120 | + $q = $dbo->getQuery(true) | |
| 121 | + ->select('SQL_CALC_FOUND_ROWS *') | |
| 122 | + ->from($dbo->qn('#__vikbooking_wpshortcodes')); | |
| 123 | + | |
| 124 | + /** | |
| 125 | + * Filters the shortcodes by using the requested values. | |
| 126 | + * | |
| 127 | + * @since 1.1.5 | |
| 128 | + */ | |
| 129 | + | |
| 130 | + if ($this->filters['search']) | |
| 131 | + { | |
| 132 | + $q->where($dbo->qn('name') . ' LIKE ' . $dbo->q("%{$this->filters['search']}%")); | |
| 133 | + } | |
| 134 | + | |
| 135 | + if ($this->filters['lang'] != '*') | |
| 136 | + { | |
| 137 | + $q->where($dbo->qn('lang') . ' = ' . $dbo->q($this->filters['lang'])); | |
| 138 | + } | |
| 139 | + | |
| 140 | + if ($this->filters['type']) | |
| 141 | + { | |
| 142 | + $q->where($dbo->qn('type') . ' = ' . $dbo->q($this->filters['type'])); | |
| 143 | + } | |
| 144 | + | |
| 145 | + $dbo->setQuery($q); | |
| 146 | + $dbo->execute(); | |
| 147 | + | |
| 148 | + if (!$dbo->getNumRows()) | |
| 149 | + { | |
| 150 | + return []; | |
| 151 | + } | |
| 152 | + | |
| 153 | + $model = JModel::getInstance('vikbooking', 'shortcode', 'admin'); | |
| 154 | + | |
| 155 | + $shortcodes = []; | |
| 156 | + | |
| 157 | + foreach ($dbo->loadObjectList() as $shortcode) | |
| 158 | + { | |
| 159 | + // load shortcode ancestors | |
| 160 | + $shortcode->ancestors = $model->getAncestors($shortcode); | |
| 161 | + | |
| 162 | + // create ordering leverage, based on version comparison | |
| 163 | + $tmp = array_merge([$shortcode->id], $shortcode->ancestors); | |
| 164 | + $shortcode->leverage = implode('.', array_reverse($tmp)); | |
| 165 | + | |
| 166 | + $shortcodes[] = $shortcode; | |
| 167 | + } | |
| 168 | + | |
| 169 | + // sort shortcodes by comparing the evaluated leverage | |
| 170 | + usort($shortcodes, function($a, $b) | |
| 171 | + { | |
| 172 | + return version_compare($a->leverage, $b->leverage); | |
| 173 | + }); | |
| 174 | + | |
| 175 | + // create pagination | |
| 176 | + jimport('joomla.html.pagination'); | |
| 177 | + $pageNav = new JPagination(count($shortcodes), $this->offset, $this->limit); | |
| 178 | + $this->navbut = '<table align="center"><tr><td>' . $pageNav->getListFooter() . '</td></tr></table>'; | |
| 179 | + | |
| 180 | + // take only the records that metch the pagination query | |
| 181 | + $shortcodes = array_splice($shortcodes, $this->offset, $this->limit); | |
| 182 | + | |
| 183 | + return $shortcodes; | |
| 184 | + } | |
| 185 | +} | |