Skip to content

Commit

Permalink
v1.3.0
Browse files Browse the repository at this point in the history
  • Loading branch information
AlekVolsk committed Sep 29, 2019
1 parent 95c9b1d commit feb10cd
Show file tree
Hide file tree
Showing 16 changed files with 241 additions and 34 deletions.
22 changes: 17 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Attributes

![Last Update](https://img.shields.io/badge/last_update-2019.09.08-28A5F5.svg?style=for-the-badge)
![Version](https://img.shields.io/badge/VERSION-1.2.5-0366d6.svg?style=for-the-badge)
![Last Update](https://img.shields.io/badge/last_update-2019.09.29-28A5F5.svg?style=for-the-badge)
![Version](https://img.shields.io/badge/VERSION-1.3.0-0366d6.svg?style=for-the-badge)
![Joomla](https://img.shields.io/badge/joomla-3.7+-1A3867.svg?style=for-the-badge)
![Php](https://img.shields.io/badge/php-5.6+-8892BF.svg?style=for-the-badge)

Expand All @@ -24,26 +24,30 @@ Attribute values are stored in the parameters of the corresponding element, the

Every attribute name is prefixed with `attrs_` prefix. You can get the attribute value by the standard way described below.

For each attribute, it is possible to specify an individual output layout. Layouts are created in the /templates/{your_template}/html/layouts/com_attrs/ folder and are assigned to each attribute separately. The attribute data inside the layout is contained in the `$displayData` structure. The default layout does not contain markup.

## Usage

### Shortcode, for use in content editor

```
{attrs;dest;id;attrName}
```text
{attrs;dest;id;attrNameЯ[;layout]}
```

- `attrs` - reserved word
- `dest` - belonging to a specific type of record, one of: system, menu, users, contacts, articles, categories, modules, plugins
- `id` - ID of the corresponding entry for the specified property, specify 0 for systems
- `attrName` - attribute system name
- `layout` - output layout (optional), if **0** or **false** is specified, the assigned layout will be ignored and the attribute value will be displayed without layout; format for specifying the layout: `template:layoutname`, where _template_ is the main template of the site that contains the override (specify the underscore to indicate the default template), _layoutname_ is the name of the overridden layout file, without extension; when specifying a nonexistent layout, the attribute value will not be displayed

**Important**: Unpublished attributes are ignored. Images are output without markup, only the path. Arrays are displayed as a list of values separated by commas.

**Example**: You have created an attribute with the name test for a material with ID = 5, to get its value, insert the following line in the editor: `{attrs;articles;5;test}`.
If you want to display the attribute indicating the override of the template created with the name mytemplate in the main protostar site template, insert the following line into the editor: `{attrs;articles;5;test;protostar:mytemplate}`.

### With helper

It is recommended because it checks the status of the attribute (published / unpublished) and it is not necessary to specify the prefix `attrs_`. Skip the third parameter to get the attribute value from the system config.
It is recommended because it checks the status of the attribute (published / unpublished) and it is not necessary to specify the prefix `attrs_`. Do not specify the third parameter or set it to zero to get the attribute value from the system configuration.

```php
/*
Expand All @@ -60,11 +64,19 @@ AttrsHelper::ATTR_DEST_TAGS = 'tags'
*/

JLoader::register('AttrsHelper', JPATH_ADMINISTRATOR . '/components/com_attrs/helpers/attrs.php');

// output with the layout specified in the attribute parameters
$attrValue = AttrsHelper::getAttr($attrName, AttrsHelper::ATTR_DEST_ARTICLES, $article->id);

// priority layout output
// the attribute layout will be loaded /templates/protostar/html/layouts/com_attrs/mytemplate.php
$attrValue = AttrsHelper::getAttr($attrName, AttrsHelper::ATTR_DEST_ARTICLES, $article->id, 'protostar:mytemplate');
```

### Without helper (not recommended)

This method does not support attribute standardization.

```php
// default
$attrValue = $item->params->get("arrts_$attrName", '');
Expand Down
22 changes: 17 additions & 5 deletions README.ru.md
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Атрибуты

![Last Update](https://img.shields.io/badge/last_update-2019.09.08-28A5F5.svg?style=for-the-badge)
![Version](https://img.shields.io/badge/VERSION-1.2.5-0366d6.svg?style=for-the-badge)
![Last Update](https://img.shields.io/badge/last_update-2019.09.29-28A5F5.svg?style=for-the-badge)
![Version](https://img.shields.io/badge/VERSION-1.3.0-0366d6.svg?style=for-the-badge)
![Joomla](https://img.shields.io/badge/joomla-3.7+-1A3867.svg?style=for-the-badge)
![Php](https://img.shields.io/badge/php-5.6+-8892BF.svg?style=for-the-badge)

Expand All @@ -22,27 +22,31 @@

К каждому указанному системному имени атрибута добавляется префикс `attrs_`. Вы можете получить значение атрибута стандартным нижеуказанным способом.

Для каждого атрибута имеется возможность указания индивидуального макета вывода. Макеты создаются в папке /templates/{ваш_шаблон}/html/layouts/com_attrs/ и назначаются каждому атрибуту отдельно. Данные атрибута внутри макета содержатся в структуре `$displayData`. Макеты по умолчанию не содержит разметки.

## Использование

### Шорткод, для использования в контент-редакторе

```
{attrs;dest;id;attrName}
```text
{attrs;dest;id;attrNameЯ[;layout]}
```

- `attrs` - зарезервированное слово
- `dest` - принадлежность к определённому типу записи, одно из значений: system, menu, users, contacts, articles, categories, modules, plugins
- `id` - ID записи соответствующей принадлежности, указывать `0` для system
- `attrName` - системное имя атрибута
- `layout` - макет вывода (необязательный параметр), при указании значения **0** или **false** назначенный макет будет проигнорирован и значение атрибута будет выведено без шаблонизации; формат указания макета: `template:layoutname`, где _template_ – основной шаблон сайта, в котором содержится переопределение (укажите нижнее подчёркивание для указания шаблона по умолчанию), _layoutname_ – имя файла переопределённого макета, без расширения; при указании несуществующего макета значение атрибута выведено не будет

**Важно**: Шорткоды, ссылающиеся на неопубликованные атрибуты, игнорируются. Изображения выводятся без разметки, только путь. Массивы выводятся как список значений через запятую.

**Пример**: Вы создали для материала с ID = 5 атрибут с именем test, чтобы получить его значение, вставьте в редактор следующую строку: `{attrs;articles;5;test}`.
Если вы хотите вывести значение атрибута с указанием конкретного переопределенного макета, созданного с именем mytemplate в основном шаблоне сайта protostar, вставьте в редактор следующую строку: `{attrs;articles;5;test;protostar:mytemplate}`.

### Через хелпер компонета

Рекомендуемый метод, поскольку производится проверка состояния атрибута (опубликован / неопубликован) и нет необходимости указывать префикс `attrs_`.
Пропустите третий параметр, чтобы получить значение атрибута из конфигурации системы.
Не указывайте третий параметр или укажите его равным нулю, чтобы получить значение атрибута из конфигурации системы.

```php
/*
Expand All @@ -59,11 +63,19 @@ AttrsHelper::ATTR_DEST_TAGS = 'tags'
*/

JLoader::register('AttrsHelper', JPATH_ADMINISTRATOR . '/components/com_attrs/helpers/attrs.php');

// вывод с макетом, указанным в параметрах атрибута
$attrValue = AttrsHelper::getAttr($attrName, AttrsHelper::ATTR_DEST_ARTICLES, $article->id);

// вывод с макетом, заданным приоритетно
// будет загружен макет атрибута /templates/protostar/html/layouts/com_attrs/mytemplate.php
$attrValue = AttrsHelper::getAttr($attrName, AttrsHelper::ATTR_DEST_ARTICLES, $article->id, 'protostar:mytemplate');
```

### Без хелпера (не рекомендуется)

Этот метод не поддерживает шаблонизацию атрибутов.

```php
// default
$attrValue = $item->params->get("arrts_$attrName", '');
Expand Down
3 changes: 2 additions & 1 deletion com_attrs/attrs.xml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
<?xml version="1.0"?>
<extension type="component" version="3.7.0" method="upgrade">
<name>COM_ATTRS</name>
<version>1.2.5</version>
<version>1.3.0</version>
<creationDate>September 2019</creationDate>
<author>Aleksey A. Morozov</author>
<authorEmail>alekvolsk@yandex.ru</authorEmail>
Expand Down Expand Up @@ -31,6 +31,7 @@
<filename>controller.php</filename>
<filename>access.xml</filename>
<folder>controllers</folder>
<folder>layouts</folder>
<folder>helpers</folder>
<folder>models</folder>
<folder>sql</folder>
Expand Down
75 changes: 60 additions & 15 deletions com_attrs/helpers/attrs.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
use Joomla\CMS\Factory;
use Joomla\CMS\Table\Table;
use Joomla\Registry\Registry;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\Layout\FileLayout;

class AttrsHelper
{
Expand All @@ -17,25 +19,18 @@ class AttrsHelper
const ATTR_DEST_FIELDS = 'fields';
const ATTR_DEST_TAGS = 'tags';

public static function isPublished($attrName)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('`published`')
->from('`#__attrs`')
->where('`name` = ' . $db->quote(self::getSystemAttrName($attrName)));
$published = $db->setQuery($query)->loadResult();
return (bool) $published;
}

public static function getAttr($attrName, $attrDest, $id = 0)
public static function getAttr($attrName, $attrDest, $id = 0, $template = '')
{
$attrValue = '';

if (!self::isPublished($attrName)) {
$attrParams = self::getAttrParams($attrName);
if (!isset($attrParams) || !$attrParams['published']) {
return $attrValue;
}

if (!$attrParams['layout']) {
$attrParams['layout'] = '_:default';
}

if (!$id && $attrDest !== self::ATTR_DEST_SYSTEM) {
return $attrValue;
}
Expand Down Expand Up @@ -138,7 +133,30 @@ public static function getAttr($attrName, $attrDest, $id = 0)
$attrValue = $params->get($attrName, '');
}

return $attrValue;
if ($template === false) {
return $attrValue;
} else {
$layoutMask = $template ? $template : $attrParams['layout'];
$fileLayout = self::getLayoutPath($layoutMask);
$layout = new FileLayout(pathinfo($fileLayout, PATHINFO_FILENAME), null, ['component' => 'com_attrs']);
$layout->addIncludePath(pathinfo($fileLayout, PATHINFO_DIRNAME));
return $layout->render(['name' => self::getSystemAttrName($attrName), 'value' => $attrValue, 'dest' => $attrDest, 'id' => $id]);
}
}

private static function getAttrParams($attrName)
{
$db = Factory::getDbo();
$query = $db->getQuery(true)
->select('`published`, `layout`')
->from('`#__attrs`')
->where('`name` = ' . $db->quote(self::getSystemAttrName($attrName)));
try {
$row = $db->setQuery($query)->loadAssoc();
} catch (\Exception $e) {
$row = [];
}
return $row;
}

private static function getSystemAttrName($attrName)
Expand All @@ -153,4 +171,31 @@ private static function getParamsAttrName($attrName)
}
return $attrName;
}

private static function getLayoutPath($layout = 'default')
{
$template = Factory::getApplication()->getTemplate();
$defaultLayout = $layout;

if (strpos($layout, ':') !== false) {
$temp = explode(':', $layout);
$template = $temp[0] === '_' ? $template : $temp[0];
$layout = $temp[1];
$defaultLayout = $temp[1] ?: 'default';
}

$tPath = JPATH_THEMES . '/' . $template . '/html/layouts/com_attrs/' . $layout . '.php';
$bPath = JPATH_ADMINISTRATOR . '/components/com_attrs/layouts/' . $defaultLayout . '.php';
$dPath = JPATH_ADMINISTRATOR . '/components/com_attrs/layouts/default.php';

if (file_exists($tPath)) {
return Path::clean($tPath);
}

if (file_exists($bPath)) {
return Path::clean($bPath);
}

return Path::clean($dPath);
}
}
4 changes: 4 additions & 0 deletions com_attrs/language/en-GB/en-GB.com_attrs.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ COM_ATTRS_VAL_MULTIPLE="multiple select"
COM_ATTRS_VAL_FILTER="Data filter"
COM_ATTRS_CLASS="Field class"

COM_ATTRS_LAYOUT="Template"
JOPTION_FROM_COMPONENT="---From component---"
COM_ATTRS_LAYOUTS_LAYOUT_DEFAULT="Default"

COM_ATTRS_DEST="Destination"
COM_ATTRS_DEST_SYSTEM="<span class='icon-joomla'></span> System"
COM_ATTRS_DEST_MENU="<span class='icon-list'></span> Menu"
Expand Down
4 changes: 4 additions & 0 deletions com_attrs/language/ru-RU/ru-RU.com_attrs.ini
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,10 @@ COM_ATTRS_VAL_MULTIPLE="Множественный выбор"
COM_ATTRS_VAL_FILTER="Фильтр данных"
COM_ATTRS_CLASS="Класс поля"

COM_ATTRS_LAYOUT="Шаблон"
JOPTION_FROM_COMPONENT="---Из компонента---"
COM_ATTRS_LAYOUTS_LAYOUT_DEFAULT="По умолчанию"

COM_ATTRS_DEST="Принадлежность"
COM_ATTRS_DEST_SYSTEM="<span class='icon-joomla'></span> Система"
COM_ATTRS_DEST_MENU="<span class='icon-list'></span> Меню"
Expand Down
8 changes: 8 additions & 0 deletions com_attrs/layouts/default.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?php defined('_JEXEC') or die;

$value = $displayData['value'];
if (is_array($value)) {
$value = implode(', ', $value);
}

echo $value;
108 changes: 108 additions & 0 deletions com_attrs/models/fields/layoutscomponent.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,108 @@
<?php defined('JPATH_PLATFORM') or die;
/**
* @package Joomla.Legacy
* @subpackage Form
* @copyright Copyright (C) 2005 - 2019 Open Source Matters, Inc; Aleksey A. Morozov. 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\Folder;
use Joomla\CMS\Filesystem\Path;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Form\FormField;

class JFormFieldLayoutsComponent extends FormField
{
protected $type = 'LayoutsComponent';

protected function getInput()
{
$component = explode('.', $this->form->getName())[0];

$component = preg_replace('#\W#', '', $component);
$client = ApplicationHelper::getClientInfo(0);
$client_administrator = ApplicationHelper::getClientInfo(1);

$lang = Factory::getLanguage();
$lang->load($component . '.sys', $client_administrator->path, null, false, true) || $lang->load($component . '.sys', $client_administrator->path . '/components/' . $component, null, false, true);

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

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

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

$component_path = Path::clean($client_administrator->path . '/components/' . $component . '/layouts');

$component_layouts = [];

$groups = [];

if (is_dir($component_path) && ($component_layouts = Folder::files($component_path, '^[^_]*\.php$'))) {
$groups['_'] = [];
$groups['_']['id'] = $this->id . '__';
$groups['_']['text'] = Text::sprintf('JOPTION_FROM_COMPONENT');
$groups['_']['items'] = [];

foreach ($component_layouts as $file) {
$value = basename($file, '.php');
$text = $lang->hasKey($key = strtoupper($component . '_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/' . $component);

if (is_dir($template_path) && ($files = Folder::files($template_path, '^[^_]*\.php$'))) {
foreach ($files as $i => $file) {
if (in_array($file, $component_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 . '_' . $component . '_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);
}
}
Loading

0 comments on commit feb10cd

Please sign in to comment.