|
| 1 | +<?php defined('JPATH_PLATFORM') or die; |
| 2 | +/** |
| 3 | + * @package Joomla.Legacy |
| 4 | + * @subpackage Form |
| 5 | + * @copyright Copyright (C) Aleksey A. Morozov (AlekVolsk). All rights reserved. |
| 6 | + * @license GNU General Public License version 3 or later; see LICENSE.txt |
| 7 | + */ |
| 8 | + |
| 9 | +use Joomla\CMS\Factory; |
| 10 | +use Joomla\CMS\Application\ApplicationHelper; |
| 11 | +use Joomla\CMS\Filesystem\Path; |
| 12 | +use Joomla\CMS\HTML\HTMLHelper; |
| 13 | +use Joomla\CMS\Form\Form; |
| 14 | +use Joomla\CMS\Form\FormField; |
| 15 | +use Joomla\CMS\Language\Text; |
| 16 | + |
| 17 | +class JFormFieldLayoutsPlugin extends FormField |
| 18 | +{ |
| 19 | + protected $type = 'layoutsplugin'; |
| 20 | + |
| 21 | + protected function getInput() |
| 22 | + { |
| 23 | + $clientId = $this->element['client_id']; |
| 24 | + |
| 25 | + if ($clientId === null && $this->form instanceof Form) { |
| 26 | + $clientId = $this->form->getValue('client_id'); |
| 27 | + } |
| 28 | + |
| 29 | + $clientId = (int) $clientId; |
| 30 | + |
| 31 | + $client = ApplicationHelper::getClientInfo($clientId); |
| 32 | + |
| 33 | + if (($this->form instanceof Form)) { |
| 34 | + $plugin = $this->form->getValue('type'); |
| 35 | + } |
| 36 | + |
| 37 | + $plugin = $this->element['plugin']; |
| 38 | + |
| 39 | + if (empty($plugin) && ($this->form instanceof Form)) { |
| 40 | + $plugin = $this->form->getValue('element'); |
| 41 | + } |
| 42 | + |
| 43 | + $plugin = preg_replace('#\W#', '', $plugin); |
| 44 | + |
| 45 | + $folder = $this->form->getValue('folder'); |
| 46 | + |
| 47 | + if ($plugin && $client) { |
| 48 | + |
| 49 | + $pluginFullName = 'plg_' . $folder . '_' . $plugin; |
| 50 | + |
| 51 | + $template = (string) $this->element['template']; |
| 52 | + $template = preg_replace('#\W#', '', $template); |
| 53 | + |
| 54 | + $template_style_id = ''; |
| 55 | + if ($this->form instanceof Form) { |
| 56 | + $template_style_id = $this->form->getValue('template_style_id'); |
| 57 | + $template_style_id = preg_replace('#\W#', '', $template_style_id); |
| 58 | + } |
| 59 | + |
| 60 | + $lang = Factory::getLanguage(); |
| 61 | + $lang->load($plugin . '.sys', $client->path, null, false, true) |
| 62 | + || $lang->load($plugin . '.sys', $client->path . '/plugins/' . $folder . '/' . $plugin, null, false, true); |
| 63 | + |
| 64 | + $db = Factory::getDbo(); |
| 65 | + $query = $db->getQuery(true); |
| 66 | + |
| 67 | + $query |
| 68 | + ->select('element, name') |
| 69 | + ->from('#__extensions as e') |
| 70 | + ->where('e.client_id = ' . (int) $clientId) |
| 71 | + ->where('e.type = ' . $db->quote('template')) |
| 72 | + ->where('e.enabled = 1'); |
| 73 | + |
| 74 | + if ($template) { |
| 75 | + $query->where('e.element = ' . $db->quote($template)); |
| 76 | + } |
| 77 | + |
| 78 | + if ($template_style_id) { |
| 79 | + $query |
| 80 | + ->join('LEFT', '#__template_styles as s on s.template=e.element') |
| 81 | + ->where('s.id=' . (int) $template_style_id); |
| 82 | + } |
| 83 | + |
| 84 | + $db->setQuery($query); |
| 85 | + $templates = $db->loadObjectList('element'); |
| 86 | + |
| 87 | + $plugin_path = realpath(Path::clean($client->path . '/plugins/' . $folder . '/' . $plugin . '/layouts')); |
| 88 | + |
| 89 | + $plugin_layouts = []; |
| 90 | + |
| 91 | + $groups = []; |
| 92 | + |
| 93 | + if (is_dir($plugin_path) && ($plugin_layouts = \JFolder::files($plugin_path, '^[^_]*\.php$'))) { |
| 94 | + $groups['_'] = []; |
| 95 | + $groups['_']['id'] = $this->id . '__'; |
| 96 | + $groups['_']['text'] = Text::sprintf('JOPTION_FROM_PLUGIN'); |
| 97 | + $groups['_']['items'] = []; |
| 98 | + |
| 99 | + foreach ($plugin_layouts as $file) { |
| 100 | + $value = basename($file, '.php'); |
| 101 | + $text = $lang->hasKey($key = strtoupper($plugin . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value; |
| 102 | + $groups['_']['items'][] = HTMLHelper::_('select.option', '_:' . $value, $text); |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + if ($templates) { |
| 107 | + foreach ($templates as $template) |
| 108 | + { |
| 109 | + $lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, true) |
| 110 | + || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, true); |
| 111 | + |
| 112 | + $template_path = Path::clean($client->path . '/templates/' . $template->element . '/html/layouts/plugins/' . $folder . '/' . $plugin); |
| 113 | + |
| 114 | + if (is_dir($template_path) && ($files = \JFolder::files($template_path, '^[^_]*\.php$'))) { |
| 115 | + foreach ($files as $i => $file) { |
| 116 | + if (in_array($file, $plugin_layouts)) { |
| 117 | + unset($files[$i]); |
| 118 | + } |
| 119 | + } |
| 120 | + |
| 121 | + if (count($files)) { |
| 122 | + $groups[$template->element] = []; |
| 123 | + $groups[$template->element]['id'] = $this->id . '_' . $template->element; |
| 124 | + $groups[$template->element]['text'] = Text::sprintf('JOPTION_FROM_TEMPLATE', $template->name); |
| 125 | + $groups[$template->element]['items'] = []; |
| 126 | + |
| 127 | + foreach ($files as $file) { |
| 128 | + $value = basename($file, '.php'); |
| 129 | + $text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $plugin . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value; |
| 130 | + $groups[$template->element]['items'][] = HTMLHelper::_('select.option', $template->element . ':' . $value, $text); |
| 131 | + } |
| 132 | + } |
| 133 | + } |
| 134 | + } |
| 135 | + } |
| 136 | + $attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : ''; |
| 137 | + $attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : ''; |
| 138 | + |
| 139 | + $html = []; |
| 140 | + |
| 141 | + $selected = [$this->value]; |
| 142 | + |
| 143 | + $html[] = HTMLHelper::_( |
| 144 | + 'select.groupedlist', $groups, $this->name, |
| 145 | + ['id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected] |
| 146 | + ); |
| 147 | + |
| 148 | + return implode($html); |
| 149 | + } else { |
| 150 | + return ''; |
| 151 | + } |
| 152 | + } |
| 153 | +} |
0 commit comments