Skip to content

Commit 5dfc57e

Browse files
author
AlekVolsk
committed
layoutsplugin && layoutsmodule
1 parent ad1c470 commit 5dfc57e

5 files changed

Lines changed: 325 additions & 3 deletions

File tree

README.md

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,4 +20,24 @@
2020
*Все значения атрибутов это пример.
2121

2222
Где можно применять?
23-
Вы можете указывать в своих JForm формах, в xml указывать или можно загружить поля в radicalmultifield.
23+
Вы можете указывать в своих JForm формах, в xml указывать или можно загружить поля в radicalmultifield.
24+
25+
---
26+
27+
#### Список слоев плагина (layoutsplugin)
28+
29+
Для выбора в плагине дополнительного слоя шаблонизации чего-либо внутри плагина.
30+
31+
Слои шаблонизации располагаются в папке */layouts* в папке плагина и переопределяются в папке */html/layouts/plugin/{тип плагина}/{имя плагина}/* в папке основного шаблона сайта.
32+
33+
Для указания слоя по умолчанию задать значение `default="_:default"`, где после двоеточия - имя файла слоя без расширения. Файл слоя, переопределённый в основном шаблоне (имеющий совпадающее имя файла), считается как слой из плагина.
34+
35+
---
36+
37+
#### Список слоев модуля (layoutsmodule)
38+
39+
Для выбора в модуле дополнительного слоя шаблонизации чего-либо внутри модуля.
40+
41+
Слои шаблонизации располагаются в папке */layouts* в папке модуля и переопределяются в папке */html/layouts/mod_{имя модуля}/* в папке основного шаблона сайта.
42+
43+
Для указания слоя по умолчанию задать значение `default="_:default"`, где после двоеточия - имя файла слоя без расширения. Файл слоя, переопределённый в основном шаблоне (имеющий совпадающее имя файла), считается как слой из модуля.

fields/layouts/layoutsmodule.php

Lines changed: 143 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,143 @@
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 JFormFieldLayoutsModule extends FormField
18+
{
19+
protected $type = 'layoutsmodule';
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+
$module = (string) $this->element['module'];
34+
35+
if (empty($module) && ($this->form instanceof Form)) {
36+
$module = $this->form->getValue('module');
37+
}
38+
39+
$module = preg_replace('#\W#', '', $module);
40+
41+
if ($module && $client) {
42+
43+
$template = (string) $this->element['template'];
44+
$template = preg_replace('#\W#', '', $template);
45+
46+
$template_style_id = '';
47+
if ($this->form instanceof Form) {
48+
$template_style_id = $this->form->getValue('template_style_id');
49+
$template_style_id = preg_replace('#\W#', '', $template_style_id);
50+
}
51+
52+
$lang = Factory::getLanguage();
53+
$lang->load($module . '.sys', $client->path, null, false, true)
54+
|| $lang->load($module . '.sys', $client->path . '/modules/' . $module, null, false, true);
55+
56+
$db = Factory::getDbo();
57+
$query = $db->getQuery(true);
58+
59+
$query
60+
->select('element, name')
61+
->from('#__extensions as e')
62+
->where('e.client_id = ' . (int) $clientId)
63+
->where('e.type = ' . $db->quote('template'))
64+
->where('e.enabled = 1');
65+
66+
if ($template) {
67+
$query->where('e.element = ' . $db->quote($template));
68+
}
69+
70+
if ($template_style_id) {
71+
$query
72+
->join('LEFT', '#__template_styles as s on s.template=e.element')
73+
->where('s.id=' . (int) $template_style_id);
74+
}
75+
76+
$db->setQuery($query);
77+
$templates = $db->loadObjectList('element');
78+
79+
$module_path = Path::clean($client->path . '/modules/' . $module . '/layouts');
80+
81+
$module_layouts = [];
82+
83+
$groups = [];
84+
85+
if (is_dir($module_path) && ($module_layouts = \JFolder::files($module_path, '^[^_]*\.php$'))) {
86+
$groups['_'] = [];
87+
$groups['_']['id'] = $this->id . '__';
88+
$groups['_']['text'] = Text::sprintf('JOPTION_FROM_MODULE');
89+
$groups['_']['items'] = [];
90+
91+
foreach ($module_layouts as $file) {
92+
$value = basename($file, '.php');
93+
$text = $lang->hasKey($key = strtoupper($module . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value;
94+
$groups['_']['items'][] = HTMLHelper::_('select.option', '_:' . $value, $text);
95+
}
96+
}
97+
98+
if ($templates) {
99+
foreach ($templates as $template) {
100+
$lang->load('tpl_' . $template->element . '.sys', $client->path, null, false, true) || $lang->load('tpl_' . $template->element . '.sys', $client->path . '/templates/' . $template->element, null, false, true);
101+
102+
$template_path = Path::clean($client->path . '/templates/' . $template->element . '/html/layouts/' . $module);
103+
104+
if (is_dir($template_path) && ($files = \JFolder::files($template_path, '^[^_]*\.php$'))) {
105+
foreach ($files as $i => $file) {
106+
if (in_array($file, $module_layouts)) {
107+
unset($files[$i]);
108+
}
109+
}
110+
111+
if (count($files)) {
112+
$groups[$template->element] = [];
113+
$groups[$template->element]['id'] = $this->id . '_' . $template->element;
114+
$groups[$template->element]['text'] = Text::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
115+
$groups[$template->element]['items'] = [];
116+
117+
foreach ($files as $file) {
118+
$value = basename($file, '.php');
119+
$text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $module . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value;
120+
$groups[$template->element]['items'][] = HTMLHelper::_('select.option', $template->element . ':' . $value, $text);
121+
}
122+
}
123+
}
124+
}
125+
}
126+
$attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
127+
$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';
128+
129+
$html = [];
130+
131+
$selected = [$this->value];
132+
133+
$html[] = HTMLHelper::_(
134+
'select.groupedlist', $groups, $this->name,
135+
['id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected]
136+
);
137+
138+
return implode($html);
139+
} else {
140+
return '';
141+
}
142+
}
143+
}

fields/layouts/layoutsplugin.php

Lines changed: 153 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,153 @@
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+
}

language/en-GB/en-GB.lib_fields.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REGISTRATION="Registration Form"
4343
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REMIND="Username Reminder Request"
4444
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_RESET="Password Reset"
4545
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER="Wrapper"
46-
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Iframe Wrapper"
46+
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Iframe Wrapper"
47+
48+
; layouts plugins
49+
JOPTION_FROM_PLUGIN="---From plugin---"

language/ru-RU/ru-RU.lib_fields.ini

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -43,4 +43,7 @@ LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REGISTRATION="Форма регистра
4343
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REMIND="Восстановление имени пользователя"
4444
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_RESET="Восстановление пароля"
4545
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER="Обёртка (Wrapper)"
46-
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Обёртка (Wrapper)"
46+
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Обёртка (Wrapper)"
47+
48+
; layouts plugins
49+
JOPTION_FROM_PLUGIN="---Из плагина---"

0 commit comments

Comments
 (0)