Skip to content

Commit

Permalink
layoutsplugin && layoutsmodule
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekVolsk committed Mar 6, 2020
1 parent ad1c470 commit 5dfc57e
Show file tree
Hide file tree
Showing 5 changed files with 325 additions and 3 deletions.
22 changes: 21 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -20,4 +20,24 @@
*Все значения атрибутов это пример.

Где можно применять?
Вы можете указывать в своих JForm формах, в xml указывать или можно загружить поля в radicalmultifield.
Вы можете указывать в своих JForm формах, в xml указывать или можно загружить поля в radicalmultifield.

---

#### Список слоев плагина (layoutsplugin)

Для выбора в плагине дополнительного слоя шаблонизации чего-либо внутри плагина.

Слои шаблонизации располагаются в папке */layouts* в папке плагина и переопределяются в папке */html/layouts/plugin/{тип плагина}/{имя плагина}/* в папке основного шаблона сайта.

Для указания слоя по умолчанию задать значение `default="_:default"`, где после двоеточия - имя файла слоя без расширения. Файл слоя, переопределённый в основном шаблоне (имеющий совпадающее имя файла), считается как слой из плагина.

---

#### Список слоев модуля (layoutsmodule)

Для выбора в модуле дополнительного слоя шаблонизации чего-либо внутри модуля.

Слои шаблонизации располагаются в папке */layouts* в папке модуля и переопределяются в папке */html/layouts/mod_{имя модуля}/* в папке основного шаблона сайта.

Для указания слоя по умолчанию задать значение `default="_:default"`, где после двоеточия - имя файла слоя без расширения. Файл слоя, переопределённый в основном шаблоне (имеющий совпадающее имя файла), считается как слой из модуля.
143 changes: 143 additions & 0 deletions fields/layouts/layoutsmodule.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
<?php defined('JPATH_PLATFORM') or die;
/**
* @package Joomla.Legacy
* @subpackage Form
* @copyright Copyright (C) Aleksey A. Morozov (AlekVolsk). All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE.txt
*/

use Joomla\CMS\Factory;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Language\Text;

class JFormFieldLayoutsModule extends FormField
{
protected $type = 'layoutsmodule';

protected function getInput()
{
$clientId = $this->element['client_id'];

if ($clientId === null && $this->form instanceof Form) {
$clientId = $this->form->getValue('client_id');
}

$clientId = (int) $clientId;

$client = ApplicationHelper::getClientInfo($clientId);

$module = (string) $this->element['module'];

if (empty($module) && ($this->form instanceof Form)) {
$module = $this->form->getValue('module');
}

$module = preg_replace('#\W#', '', $module);

if ($module && $client) {

$template = (string) $this->element['template'];
$template = preg_replace('#\W#', '', $template);

$template_style_id = '';
if ($this->form instanceof Form) {
$template_style_id = $this->form->getValue('template_style_id');
$template_style_id = preg_replace('#\W#', '', $template_style_id);
}

$lang = Factory::getLanguage();
$lang->load($module . '.sys', $client->path, null, false, true)
|| $lang->load($module . '.sys', $client->path . '/modules/' . $module, null, false, true);

$db = Factory::getDbo();
$query = $db->getQuery(true);

$query
->select('element, name')
->from('#__extensions as e')
->where('e.client_id = ' . (int) $clientId)
->where('e.type = ' . $db->quote('template'))
->where('e.enabled = 1');

if ($template) {
$query->where('e.element = ' . $db->quote($template));
}

if ($template_style_id) {
$query
->join('LEFT', '#__template_styles as s on s.template=e.element')
->where('s.id=' . (int) $template_style_id);
}

$db->setQuery($query);
$templates = $db->loadObjectList('element');

$module_path = Path::clean($client->path . '/modules/' . $module . '/layouts');

$module_layouts = [];

$groups = [];

if (is_dir($module_path) && ($module_layouts = \JFolder::files($module_path, '^[^_]*\.php$'))) {
$groups['_'] = [];
$groups['_']['id'] = $this->id . '__';
$groups['_']['text'] = Text::sprintf('JOPTION_FROM_MODULE');
$groups['_']['items'] = [];

foreach ($module_layouts as $file) {
$value = basename($file, '.php');
$text = $lang->hasKey($key = strtoupper($module . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value;
$groups['_']['items'][] = HTMLHelper::_('select.option', '_:' . $value, $text);
}
}

if ($templates) {
foreach ($templates as $template) {
$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);

$template_path = Path::clean($client->path . '/templates/' . $template->element . '/html/layouts/' . $module);

if (is_dir($template_path) && ($files = \JFolder::files($template_path, '^[^_]*\.php$'))) {
foreach ($files as $i => $file) {
if (in_array($file, $module_layouts)) {
unset($files[$i]);
}
}

if (count($files)) {
$groups[$template->element] = [];
$groups[$template->element]['id'] = $this->id . '_' . $template->element;
$groups[$template->element]['text'] = Text::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
$groups[$template->element]['items'] = [];

foreach ($files as $file) {
$value = basename($file, '.php');
$text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $module . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value;
$groups[$template->element]['items'][] = HTMLHelper::_('select.option', $template->element . ':' . $value, $text);
}
}
}
}
}
$attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';

$html = [];

$selected = [$this->value];

$html[] = HTMLHelper::_(
'select.groupedlist', $groups, $this->name,
['id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected]
);

return implode($html);
} else {
return '';
}
}
}
153 changes: 153 additions & 0 deletions fields/layouts/layoutsplugin.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,153 @@
<?php defined('JPATH_PLATFORM') or die;
/**
* @package Joomla.Legacy
* @subpackage Form
* @copyright Copyright (C) Aleksey A. Morozov (AlekVolsk). All rights reserved.
* @license GNU General Public License version 3 or later; see LICENSE.txt
*/

use Joomla\CMS\Factory;
use Joomla\CMS\Application\ApplicationHelper;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Form\Form;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Language\Text;

class JFormFieldLayoutsPlugin extends FormField
{
protected $type = 'layoutsplugin';

protected function getInput()
{
$clientId = $this->element['client_id'];

if ($clientId === null && $this->form instanceof Form) {
$clientId = $this->form->getValue('client_id');
}

$clientId = (int) $clientId;

$client = ApplicationHelper::getClientInfo($clientId);

if (($this->form instanceof Form)) {
$plugin = $this->form->getValue('type');
}

$plugin = $this->element['plugin'];

if (empty($plugin) && ($this->form instanceof Form)) {
$plugin = $this->form->getValue('element');
}

$plugin = preg_replace('#\W#', '', $plugin);

$folder = $this->form->getValue('folder');

if ($plugin && $client) {

$pluginFullName = 'plg_' . $folder . '_' . $plugin;

$template = (string) $this->element['template'];
$template = preg_replace('#\W#', '', $template);

$template_style_id = '';
if ($this->form instanceof Form) {
$template_style_id = $this->form->getValue('template_style_id');
$template_style_id = preg_replace('#\W#', '', $template_style_id);
}

$lang = Factory::getLanguage();
$lang->load($plugin . '.sys', $client->path, null, false, true)
|| $lang->load($plugin . '.sys', $client->path . '/plugins/' . $folder . '/' . $plugin, null, false, true);

$db = Factory::getDbo();
$query = $db->getQuery(true);

$query
->select('element, name')
->from('#__extensions as e')
->where('e.client_id = ' . (int) $clientId)
->where('e.type = ' . $db->quote('template'))
->where('e.enabled = 1');

if ($template) {
$query->where('e.element = ' . $db->quote($template));
}

if ($template_style_id) {
$query
->join('LEFT', '#__template_styles as s on s.template=e.element')
->where('s.id=' . (int) $template_style_id);
}

$db->setQuery($query);
$templates = $db->loadObjectList('element');

$plugin_path = realpath(Path::clean($client->path . '/plugins/' . $folder . '/' . $plugin . '/layouts'));

$plugin_layouts = [];

$groups = [];

if (is_dir($plugin_path) && ($plugin_layouts = \JFolder::files($plugin_path, '^[^_]*\.php$'))) {
$groups['_'] = [];
$groups['_']['id'] = $this->id . '__';
$groups['_']['text'] = Text::sprintf('JOPTION_FROM_PLUGIN');
$groups['_']['items'] = [];

foreach ($plugin_layouts as $file) {
$value = basename($file, '.php');
$text = $lang->hasKey($key = strtoupper($plugin . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value;
$groups['_']['items'][] = HTMLHelper::_('select.option', '_:' . $value, $text);
}
}

if ($templates) {
foreach ($templates as $template)
{
$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);

$template_path = Path::clean($client->path . '/templates/' . $template->element . '/html/layouts/plugins/' . $folder . '/' . $plugin);

if (is_dir($template_path) && ($files = \JFolder::files($template_path, '^[^_]*\.php$'))) {
foreach ($files as $i => $file) {
if (in_array($file, $plugin_layouts)) {
unset($files[$i]);
}
}

if (count($files)) {
$groups[$template->element] = [];
$groups[$template->element]['id'] = $this->id . '_' . $template->element;
$groups[$template->element]['text'] = Text::sprintf('JOPTION_FROM_TEMPLATE', $template->name);
$groups[$template->element]['items'] = [];

foreach ($files as $file) {
$value = basename($file, '.php');
$text = $lang->hasKey($key = strtoupper('TPL_' . $template->element . '_' . $plugin . '_LAYOUTS_LAYOUT_' . $value)) ? Text::_($key) : $value;
$groups[$template->element]['items'][] = HTMLHelper::_('select.option', $template->element . ':' . $value, $text);
}
}
}
}
}
$attr = $this->element['size'] ? ' size="' . (int) $this->element['size'] . '"' : '';
$attr .= $this->element['class'] ? ' class="' . (string) $this->element['class'] . '"' : '';

$html = [];

$selected = [$this->value];

$html[] = HTMLHelper::_(
'select.groupedlist', $groups, $this->name,
['id' => $this->id, 'group.id' => 'id', 'list.attr' => $attr, 'list.select' => $selected]
);

return implode($html);
} else {
return '';
}
}
}
5 changes: 4 additions & 1 deletion language/en-GB/en-GB.lib_fields.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REGISTRATION="Registration Form"
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REMIND="Username Reminder Request"
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_RESET="Password Reset"
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER="Wrapper"
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Iframe Wrapper"
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Iframe Wrapper"

; layouts plugins
JOPTION_FROM_PLUGIN="---From plugin---"
5 changes: 4 additions & 1 deletion language/ru-RU/ru-RU.lib_fields.ini
Original file line number Diff line number Diff line change
Expand Up @@ -43,4 +43,7 @@ LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REGISTRATION="Форма регистра
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_REMIND="Восстановление имени пользователя"
LIB_FIELDS_FIELD_LIST_COMPONENTS_USERS_RESET="Восстановление пароля"
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER="Обёртка (Wrapper)"
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Обёртка (Wrapper)"
LIB_FIELDS_FIELD_LIST_COMPONENTS_WRAPPER_WRAPPER="Обёртка (Wrapper)"

; layouts plugins
JOPTION_FROM_PLUGIN="---Из плагина---"

0 comments on commit 5dfc57e

Please sign in to comment.