view.html.php
226 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 media manager view. |
| 16 | * |
| 17 | * @since 1.2 |
| 18 | */ |
| 19 | class VikAppointmentsViewmedia 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 | |
| 31 | $path = $input->getBase64('path', null); |
| 32 | |
| 33 | if ($path) |
| 34 | { |
| 35 | $path = rtrim(base64_decode($path), DIRECTORY_SEPARATOR); |
| 36 | } |
| 37 | else |
| 38 | { |
| 39 | $path = VAPMEDIA; |
| 40 | } |
| 41 | |
| 42 | if ($input->get('layout') != 'modal') |
| 43 | { |
| 44 | $this->ordering = $app->getUserStateFromRequest('vap.media.ordering', 'filter_order', 'date', 'string'); |
| 45 | $this->orderDir = $app->getUserStateFromRequest('vap.media.orderdir', 'filter_order_Dir', 'DESC', 'string'); |
| 46 | } |
| 47 | else |
| 48 | { |
| 49 | // always sort by descending creation date when |
| 50 | // accessing the media manager modal |
| 51 | $this->ordering = 'date'; |
| 52 | $this->orderDir = 'DESC'; |
| 53 | } |
| 54 | |
| 55 | // retrieve all images and apply filters |
| 56 | $all_img = AppointmentsHelper::getMediaFromPath($path, array($this->ordering, $this->orderDir)); |
| 57 | |
| 58 | if ($input->get('layout') != 'modal') |
| 59 | { |
| 60 | // set the toolbar |
| 61 | $this->addToolBar(); |
| 62 | |
| 63 | $filters = array(); |
| 64 | $filters['keysearch'] = $app->getUserStateFromRequest('vap.media.keysearch', 'keysearch', '', 'string'); |
| 65 | |
| 66 | // pagination |
| 67 | $lim = $app->getUserStateFromRequest('com_vikappointments.limit', 'limit', $app->get('list_limit'), 'int'); |
| 68 | $lim0 = $app->getUserStateFromRequest('vap.media.limitstart', 'limitstart', 0, 'uint'); |
| 69 | $navbut = ""; |
| 70 | |
| 71 | if (!empty($filters['keysearch'])) |
| 72 | { |
| 73 | $app = array(); |
| 74 | |
| 75 | foreach ($all_img as $img) |
| 76 | { |
| 77 | $file_name = basename($img); |
| 78 | |
| 79 | if (strpos($file_name, $filters['keysearch']) !== false) |
| 80 | { |
| 81 | array_push($app, $img); |
| 82 | } |
| 83 | } |
| 84 | $all_img = $app; |
| 85 | unset($app); |
| 86 | } |
| 87 | |
| 88 | $tot_count = count($all_img); |
| 89 | |
| 90 | if ($tot_count) |
| 91 | { |
| 92 | if ($lim == 0 || $lim0 % $lim) |
| 93 | { |
| 94 | /** |
| 95 | * The current offset is not divisible by the selected limit. For this reason, |
| 96 | * we need to reset the offset in order to properly display all the items. |
| 97 | * |
| 98 | * @since 1.8 |
| 99 | */ |
| 100 | $lim0 = 0; |
| 101 | } |
| 102 | |
| 103 | if ($lim0 >= $tot_count) |
| 104 | { |
| 105 | /** |
| 106 | * We exceeded the pagination, probably because we deleted all the images of the last page |
| 107 | * or we changed the search parameters. For this reason, we need to go back to the last |
| 108 | * available page. |
| 109 | * |
| 110 | * @since 1.8 |
| 111 | */ |
| 112 | $lim0 = floor($tot_count / $lim) * $lim; |
| 113 | } |
| 114 | |
| 115 | if ($lim) |
| 116 | { |
| 117 | $all_img = array_slice($all_img, $lim0, $lim); |
| 118 | } |
| 119 | |
| 120 | jimport('joomla.html.pagination'); |
| 121 | $pageNav = new JPagination($tot_count, $lim0, $lim); |
| 122 | $navbut = JLayoutHelper::render('blocks.pagination', ['pageNav' => $pageNav]); |
| 123 | } |
| 124 | |
| 125 | $this->navbut = $navbut; |
| 126 | $this->filters = $filters; |
| 127 | } |
| 128 | else |
| 129 | { |
| 130 | /** |
| 131 | * Added support for 'modal' layout. |
| 132 | * |
| 133 | * @since 1.7 |
| 134 | */ |
| 135 | $this->setLayout('modal'); |
| 136 | |
| 137 | // retrieve selected media |
| 138 | $this->selected = $input->get('media', array(), 'string'); |
| 139 | // check if multi-selection is allowed |
| 140 | $this->multiple = $input->get('multiple', false, 'bool'); |
| 141 | // check if we should accept also documents |
| 142 | $this->noFilter = $input->get('nofilter', false, 'bool'); |
| 143 | |
| 144 | // unset images that don't exist |
| 145 | $this->selected = array_filter($this->selected, function($elem) use ($path) |
| 146 | { |
| 147 | return $elem && is_file($path . DIRECTORY_SEPARATOR . $elem); |
| 148 | }); |
| 149 | |
| 150 | if ($path === VAPMEDIA) |
| 151 | { |
| 152 | // check if we are uploading the media files for the first time |
| 153 | $this->firstConfig = count($all_img) == 0; |
| 154 | } |
| 155 | else |
| 156 | { |
| 157 | // disable first config when we are outside the default media path, |
| 158 | // because we are not going to create any thumbnails |
| 159 | $this->firstConfig = false; |
| 160 | } |
| 161 | } |
| 162 | |
| 163 | $attr = AppointmentsHelper::getDefaultFileAttributes(); |
| 164 | |
| 165 | foreach ($all_img as $i => $f) |
| 166 | { |
| 167 | $all_img[$i] = AppointmentsHelper::getFileProperties($f, $attr); |
| 168 | } |
| 169 | |
| 170 | if (VikAppointments::isMultilanguage() && $input->get('layout') != 'modal') |
| 171 | { |
| 172 | $translator = VAPFactory::getTranslator(); |
| 173 | |
| 174 | // find available translations |
| 175 | $lang = $translator->getAvailableLang( |
| 176 | 'media', |
| 177 | array_map(function($row) { |
| 178 | return $row['name']; |
| 179 | }, $all_img) |
| 180 | ); |
| 181 | |
| 182 | // assign languages found to the related elements |
| 183 | foreach ($all_img as $k => $row) |
| 184 | { |
| 185 | $all_img[$k]['languages'] = isset($lang[$row['name']]) ? $lang[$row['name']] : array(); |
| 186 | } |
| 187 | } |
| 188 | |
| 189 | $this->rows = $all_img; |
| 190 | $this->path = ($path === VAPMEDIA ? '' : $path); |
| 191 | |
| 192 | // display the template (default.php) |
| 193 | parent::display($tpl); |
| 194 | } |
| 195 | |
| 196 | /** |
| 197 | * Setting the toolbar. |
| 198 | * |
| 199 | * @return void |
| 200 | */ |
| 201 | protected function addToolBar() |
| 202 | { |
| 203 | // add menu title and some buttons to the page |
| 204 | JToolBarHelper::title(JText::translate('VAPMAINTITLEVIEWMEDIA'), 'vikappointments'); |
| 205 | |
| 206 | $user = JFactory::getUser(); |
| 207 | |
| 208 | if ($user->authorise('core.create', 'com_vikappointments')) |
| 209 | { |
| 210 | JToolBarHelper::addNew('media.add', JText::translate('VAPNEW')); |
| 211 | JToolBarHelper::divider(); |
| 212 | } |
| 213 | |
| 214 | if ($user->authorise('core.edit', 'com_vikappointments')) |
| 215 | { |
| 216 | JToolbarHelper::editList('media.edit', JText::translate('VAPEDIT')); |
| 217 | JToolbarHelper::spacer(); |
| 218 | } |
| 219 | |
| 220 | if ($user->authorise('core.delete', 'com_vikappointments')) |
| 221 | { |
| 222 | JToolBarHelper::deleteList(VikAppointments::getConfirmSystemMessage(), 'media.delete', JText::translate('VAPDELETE')); |
| 223 | } |
| 224 | } |
| 225 | } |
| 226 |