admin.php
4 days ago
index.html
4 days ago
inspector.php
4 days ago
mediamanager.php
4 days ago
scripts.php
4 days ago
mediamanager.php
284 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 HTML helper. |
| 16 | * |
| 17 | * @since 1.7 |
| 18 | */ |
| 19 | abstract class VAPHtmlMediaManager |
| 20 | { |
| 21 | /** |
| 22 | * A list of IDs used by the fields. |
| 23 | * Useful to avoid duplicate IDs in case of |
| 24 | * same names and missing ID attributes. |
| 25 | * |
| 26 | * @var array |
| 27 | */ |
| 28 | protected static $ids = array(); |
| 29 | |
| 30 | /** |
| 31 | * Displays a field to handle the media manager. |
| 32 | * |
| 33 | * @param string $name The field name. |
| 34 | * @param mixed $value The field value or an array of images. |
| 35 | * @param string $id The field ID attribute. Leave empty to |
| 36 | * let the system generates a unique one. |
| 37 | * @param array $attributes An array of input attributes. |
| 38 | * |
| 39 | * @return string The field HTML. |
| 40 | * |
| 41 | * @uses script() |
| 42 | * @uses modal() |
| 43 | */ |
| 44 | public static function field($name, $value = null, $id = '', array $attributes = array()) |
| 45 | { |
| 46 | // make sure the needed scripts have been loaded |
| 47 | self::script(); |
| 48 | |
| 49 | // create field layout |
| 50 | $layout = new JLayoutFile('mediamanager.field'); |
| 51 | |
| 52 | // configure field attributes |
| 53 | $data = array(); |
| 54 | $data['name'] = $name; |
| 55 | $data['value'] = $value; |
| 56 | |
| 57 | $id = $id ? $id : 'vap-' . preg_replace("/[^a-zA-Z0-9\-_]+/", '-', $name) . '-mediamanager'; |
| 58 | $data['id'] = $id; |
| 59 | |
| 60 | $cont = 1; |
| 61 | |
| 62 | // make sure the ID hasn't been used yet |
| 63 | while (in_array($data['id'], static::$ids)) |
| 64 | { |
| 65 | $data['id'] = $id . '-' . (++$cont); |
| 66 | } |
| 67 | |
| 68 | static::$ids[] = $data['id']; |
| 69 | |
| 70 | // unset from attributes to avoid displaying the same attributes twice |
| 71 | unset($attributes['name']); |
| 72 | unset($attributes['value']); |
| 73 | unset($attributes['id']); |
| 74 | |
| 75 | if (!isset($attributes['size'])) |
| 76 | { |
| 77 | $attributes['size'] = 28; |
| 78 | } |
| 79 | |
| 80 | // append "[]" if the field should support multiple files |
| 81 | if (!empty($attributes['multiple']) && !preg_match("/\[\]$/", $data['name'])) |
| 82 | { |
| 83 | $data['name'] .= '[]'; |
| 84 | } |
| 85 | |
| 86 | if (!isset($attributes['placeholder'])) |
| 87 | { |
| 88 | $attributes['placeholder'] = JText::translate('VAP_DEF_N_SELECTED_0'); |
| 89 | } |
| 90 | |
| 91 | // check whether the preview button should be hidden |
| 92 | if (isset($attributes['preview']) && !$attributes['preview']) |
| 93 | { |
| 94 | $data['preview'] = false; |
| 95 | } |
| 96 | else |
| 97 | { |
| 98 | $data['preview'] = true; |
| 99 | } |
| 100 | unset($attributes['preview']); |
| 101 | |
| 102 | // extract modal title from attributes |
| 103 | if (isset($attributes['modaltitle'])) |
| 104 | { |
| 105 | $modal_title = $attributes['modaltitle']; |
| 106 | unset($attributes['modaltitle']); |
| 107 | } |
| 108 | else |
| 109 | { |
| 110 | $modal_title = null; |
| 111 | } |
| 112 | |
| 113 | // look for a custom path |
| 114 | if (isset($attributes['path'])) |
| 115 | { |
| 116 | $data['path'] = $attributes['path']; |
| 117 | unset($attributes['path']); |
| 118 | } |
| 119 | else |
| 120 | { |
| 121 | $data['path'] = null; |
| 122 | } |
| 123 | |
| 124 | // check whether we should accept only images |
| 125 | if (isset($attributes['filter'])) |
| 126 | { |
| 127 | $data['filter'] = (bool) $attributes['filter']; |
| 128 | unset($attributes['filter']); |
| 129 | } |
| 130 | else |
| 131 | { |
| 132 | // accept only images by default |
| 133 | $data['filter'] = true; |
| 134 | } |
| 135 | |
| 136 | // check whether we should use a custom upload image |
| 137 | if (isset($attributes['icon'])) |
| 138 | { |
| 139 | $data['icon'] = $attributes['icon']; |
| 140 | unset($attributes['icon']); |
| 141 | } |
| 142 | else |
| 143 | { |
| 144 | // use default icon |
| 145 | $data['icon'] = null; |
| 146 | } |
| 147 | |
| 148 | // assign attributes array |
| 149 | $data['attrs'] = $attributes; |
| 150 | |
| 151 | // fetch modal HTML |
| 152 | $data['modal'] = self::modal($modal_title); |
| 153 | |
| 154 | // return HTML of the field |
| 155 | return $layout->render($data); |
| 156 | } |
| 157 | |
| 158 | /** |
| 159 | * Returns the HTML of the modal containing the media manager. |
| 160 | * |
| 161 | * @param sring $title An optional title for the modal box. |
| 162 | * |
| 163 | * @return mixed The modal HTML or false in case the modal was already loaded. |
| 164 | */ |
| 165 | public static function modal($title = null) |
| 166 | { |
| 167 | // display modal only once |
| 168 | static $modal = 0; |
| 169 | |
| 170 | if ($modal) |
| 171 | { |
| 172 | return false; |
| 173 | } |
| 174 | |
| 175 | $modal = 1; |
| 176 | |
| 177 | // fetch modal |
| 178 | return JHtml::fetch( |
| 179 | 'bootstrap.renderModal', |
| 180 | 'jmodal-mediamanager', |
| 181 | array( |
| 182 | 'title' => $title ? $title : JText::translate('VAPMENUMEDIA'), |
| 183 | 'closeButton' => true, |
| 184 | 'keyboard' => true, |
| 185 | 'bodyHeight' => 80, |
| 186 | 'url' => '', |
| 187 | 'footer' => '<button type="button" class="btn btn-success" id="media-manager-save">' . JText::translate('JAPPLY') . '</button>', |
| 188 | ) |
| 189 | ); |
| 190 | } |
| 191 | |
| 192 | /** |
| 193 | * Includes the scripts needed to support a media manager. |
| 194 | * |
| 195 | * @return boolean True if loaded, false otherwise. |
| 196 | */ |
| 197 | public static function script() |
| 198 | { |
| 199 | static $loaded = 0; |
| 200 | |
| 201 | // load only once |
| 202 | if ($loaded) |
| 203 | { |
| 204 | return false; |
| 205 | } |
| 206 | |
| 207 | $loaded = 1; |
| 208 | |
| 209 | $vik = VAPApplication::getInstance(); |
| 210 | |
| 211 | // make sure fancybox is loaded for images preview |
| 212 | JHtml::fetch('vaphtml.assets.fancybox'); |
| 213 | // make sure fontawesome is loaded for buttons |
| 214 | JHtml::fetch('vaphtml.assets.fontawesome'); |
| 215 | |
| 216 | // include media manager script |
| 217 | $vik->addScript(VAPASSETS_ADMIN_URI . 'js/mediamanager.js'); |
| 218 | |
| 219 | $openModalJS = $vik->bootOpenModalJS(); |
| 220 | $closeModalJS = $vik->bootDismissModalJS(); |
| 221 | |
| 222 | $folder = addslashes(VAPMEDIA_URI); |
| 223 | |
| 224 | // add support for placeholder translation |
| 225 | JText::script('VAP_DEF_N_SELECTED'); |
| 226 | JText::script('VAP_DEF_N_SELECTED_0'); |
| 227 | JText::script('VAP_DEF_N_SELECTED_1'); |
| 228 | |
| 229 | // add declaration to support dynamic scripts |
| 230 | JFactory::getDocument()->addScriptDeclaration( |
| 231 | <<<JS |
| 232 | VAP_MEDIA_FOLDER = '{$folder}'; |
| 233 | |
| 234 | function vapMediaOpenJModal(id, path) { |
| 235 | VAP_SELECTED_FIELD = id; |
| 236 | |
| 237 | var url = 'index.php?option=com_vikappointments&view=media&layout=modal&tmpl=component'; |
| 238 | var id = 'mediamanager'; |
| 239 | var jqmodal = true; |
| 240 | |
| 241 | var val = jQuery(VAP_SELECTED_FIELD).mediamanager('val'); |
| 242 | |
| 243 | if (!Array.isArray(val)) { |
| 244 | val = [val]; |
| 245 | } |
| 246 | |
| 247 | // clear selected files before opening modal |
| 248 | VAP_TMP_FILES = []; |
| 249 | |
| 250 | for (var i = 0; i < val.length; i++) { |
| 251 | if (val[i]) { |
| 252 | url += '&media[]=' + val[i]; |
| 253 | |
| 254 | VAP_TMP_FILES.push(val[i]); |
| 255 | } |
| 256 | } |
| 257 | |
| 258 | if (jQuery(VAP_SELECTED_FIELD).attr('multiple')) { |
| 259 | url += '&multiple=1'; |
| 260 | } |
| 261 | |
| 262 | if (jQuery(VAP_SELECTED_FIELD).data('filter') == 0) { |
| 263 | url += '&nofilter=1'; |
| 264 | } |
| 265 | |
| 266 | if (path) { |
| 267 | // include path if specified (base64 encoded) |
| 268 | url += '&path=' + path; |
| 269 | } |
| 270 | |
| 271 | {$openModalJS} |
| 272 | } |
| 273 | |
| 274 | function vapMediaCloseJModal() { |
| 275 | id = 'mediamanager'; |
| 276 | {$closeModalJS} |
| 277 | } |
| 278 | JS |
| 279 | ); |
| 280 | |
| 281 | return true; |
| 282 | } |
| 283 | } |
| 284 |