first commit
This commit is contained in:
49
plugins/fields/sql/params/sql.xml
Normal file
49
plugins/fields/sql/params/sql.xml
Normal file
@ -0,0 +1,49 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<form>
|
||||
<fields name="fieldparams">
|
||||
<fieldset name="fieldparams">
|
||||
<field
|
||||
name="query"
|
||||
type="textarea"
|
||||
label="PLG_FIELDS_SQL_PARAMS_QUERY_LABEL"
|
||||
description="PLG_FIELDS_SQL_PARAMS_QUERY_DESC"
|
||||
filter="raw"
|
||||
rows="10"
|
||||
required="true"
|
||||
/>
|
||||
<field
|
||||
name="header"
|
||||
type="text"
|
||||
label="PLG_FIELDS_SQL_PARAMS_HEADER_LABEL"
|
||||
description="PLG_FIELDS_SQL_PARAMS_HEADER_DESC"
|
||||
filter="string"
|
||||
/>
|
||||
<field
|
||||
name="multiple"
|
||||
type="list"
|
||||
label="PLG_FIELDS_SQL_PARAMS_MULTIPLE_LABEL"
|
||||
filter="integer"
|
||||
validate="options"
|
||||
>
|
||||
<option value="">COM_FIELDS_FIELD_USE_GLOBAL</option>
|
||||
<option value="1">JYES</option>
|
||||
<option value="0">JNO</option>
|
||||
</field>
|
||||
</fieldset>
|
||||
</fields>
|
||||
<fields name="params">
|
||||
<fieldset name="basic">
|
||||
<fieldset name="formoptions">
|
||||
<field
|
||||
name="form_layout"
|
||||
type="list"
|
||||
label="COM_FIELDS_FIELD_FORM_LAYOUT_LABEL"
|
||||
class="form-select"
|
||||
>
|
||||
<option value="joomla.form.field.list">JDEFAULT</option>
|
||||
<option value="joomla.form.field.list-fancy-select">PLG_FIELDS_SQL_PARAMS_FORM_LAYOUT_FANCY_SELECT</option>
|
||||
</field>
|
||||
</fieldset>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</form>
|
||||
46
plugins/fields/sql/services/provider.php
Normal file
46
plugins/fields/sql/services/provider.php
Normal file
@ -0,0 +1,46 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Fields.sql
|
||||
*
|
||||
* @copyright (C) 2023 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
\defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Extension\PluginInterface;
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Plugin\PluginHelper;
|
||||
use Joomla\DI\Container;
|
||||
use Joomla\DI\ServiceProviderInterface;
|
||||
use Joomla\Event\DispatcherInterface;
|
||||
use Joomla\Plugin\Fields\SQL\Extension\SQL;
|
||||
|
||||
return new class () implements ServiceProviderInterface {
|
||||
/**
|
||||
* Registers the service provider with a DI container.
|
||||
*
|
||||
* @param Container $container The DI container.
|
||||
*
|
||||
* @return void
|
||||
*
|
||||
* @since 4.3.0
|
||||
*/
|
||||
public function register(Container $container)
|
||||
{
|
||||
$container->set(
|
||||
PluginInterface::class,
|
||||
function (Container $container) {
|
||||
$plugin = new SQL(
|
||||
$container->get(DispatcherInterface::class),
|
||||
(array) PluginHelper::getPlugin('fields', 'sql')
|
||||
);
|
||||
$plugin->setApplication(Factory::getApplication());
|
||||
|
||||
return $plugin;
|
||||
}
|
||||
);
|
||||
}
|
||||
};
|
||||
58
plugins/fields/sql/sql.xml
Normal file
58
plugins/fields/sql/sql.xml
Normal file
@ -0,0 +1,58 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<extension type="plugin" group="fields" method="upgrade">
|
||||
<name>plg_fields_sql</name>
|
||||
<author>Joomla! Project</author>
|
||||
<creationDate>2016-03</creationDate>
|
||||
<copyright>(C) 2016 Open Source Matters, Inc.</copyright>
|
||||
<license>GNU General Public License version 2 or later; see LICENSE.txt</license>
|
||||
<authorEmail>admin@joomla.org</authorEmail>
|
||||
<authorUrl>www.joomla.org</authorUrl>
|
||||
<version>3.7.0</version>
|
||||
<description>PLG_FIELDS_SQL_XML_DESCRIPTION</description>
|
||||
<namespace path="src">Joomla\Plugin\Fields\SQL</namespace>
|
||||
<files>
|
||||
<folder>params</folder>
|
||||
<folder plugin="sql">services</folder>
|
||||
<folder>src</folder>
|
||||
<folder>tmpl</folder>
|
||||
</files>
|
||||
<languages>
|
||||
<language tag="en-GB">language/en-GB/plg_fields_sql.ini</language>
|
||||
<language tag="en-GB">language/en-GB/plg_fields_sql.sys.ini</language>
|
||||
</languages>
|
||||
<config>
|
||||
<fields name="params">
|
||||
<fieldset name="basic">
|
||||
<field
|
||||
name="query"
|
||||
type="textarea"
|
||||
label="PLG_FIELDS_SQL_PARAMS_QUERY_LABEL"
|
||||
description="PLG_FIELDS_SQL_PARAMS_QUERY_DESC"
|
||||
rows="10"
|
||||
filter="raw"
|
||||
required="true"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="header"
|
||||
type="text"
|
||||
label="PLG_FIELDS_SQL_PARAMS_HEADER_LABEL"
|
||||
description="PLG_FIELDS_SQL_PARAMS_HEADER_DESC"
|
||||
filter="string"
|
||||
/>
|
||||
|
||||
<field
|
||||
name="multiple"
|
||||
type="radio"
|
||||
layout="joomla.form.field.radio.switcher"
|
||||
default="0"
|
||||
label="PLG_FIELDS_SQL_PARAMS_MULTIPLE_LABEL"
|
||||
filter="integer"
|
||||
>
|
||||
<option value="0">JNO</option>
|
||||
<option value="1">JYES</option>
|
||||
</field>
|
||||
</fieldset>
|
||||
</fields>
|
||||
</config>
|
||||
</extension>
|
||||
81
plugins/fields/sql/src/Extension/SQL.php
Normal file
81
plugins/fields/sql/src/Extension/SQL.php
Normal file
@ -0,0 +1,81 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Fields.sql
|
||||
*
|
||||
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
namespace Joomla\Plugin\Fields\SQL\Extension;
|
||||
|
||||
use Joomla\CMS\Access\Access;
|
||||
use Joomla\CMS\Form\Form;
|
||||
use Joomla\Component\Fields\Administrator\Plugin\FieldsListPlugin;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Fields SQL Plugin
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
final class SQL extends FieldsListPlugin
|
||||
{
|
||||
/**
|
||||
* Transforms the field into a DOM XML element and appends it as a child on the given parent.
|
||||
*
|
||||
* @param stdClass $field The field.
|
||||
* @param \DOMElement $parent The field node parent.
|
||||
* @param Form $form The form.
|
||||
*
|
||||
* @return \DOMElement
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function onCustomFieldsPrepareDom($field, \DOMElement $parent, Form $form)
|
||||
{
|
||||
$fieldNode = parent::onCustomFieldsPrepareDom($field, $parent, $form);
|
||||
|
||||
if (!$fieldNode) {
|
||||
return $fieldNode;
|
||||
}
|
||||
|
||||
$fieldNode->setAttribute('value_field', 'text');
|
||||
$fieldNode->setAttribute('key_field', 'value');
|
||||
|
||||
return $fieldNode;
|
||||
}
|
||||
|
||||
/**
|
||||
* The save event.
|
||||
*
|
||||
* @param string $context The context
|
||||
* @param \Joomla\CMS\Table\Table $item The table
|
||||
* @param boolean $isNew Is new item
|
||||
* @param array $data The validated data
|
||||
*
|
||||
* @return boolean
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
public function onContentBeforeSave($context, $item, $isNew, $data = [])
|
||||
{
|
||||
// Only work on new SQL fields
|
||||
if ($context != 'com_fields.field' || !isset($item->type) || $item->type != 'sql') {
|
||||
return true;
|
||||
}
|
||||
|
||||
// If we are not a super admin, don't let the user create or update a SQL field
|
||||
if (!Access::getAssetRules(1)->allow('core.admin', $this->getApplication()->getIdentity()->getAuthorisedGroups())) {
|
||||
$item->setError($this->getApplication()->getLanguage()->_('PLG_FIELDS_SQL_CREATE_NOT_POSSIBLE'));
|
||||
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
}
|
||||
49
plugins/fields/sql/tmpl/sql.php
Normal file
49
plugins/fields/sql/tmpl/sql.php
Normal file
@ -0,0 +1,49 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Plugin
|
||||
* @subpackage Fields.Sql
|
||||
*
|
||||
* @copyright (C) 2017 Open Source Matters, Inc. <https://www.joomla.org>
|
||||
* @license GNU General Public License version 2 or later; see LICENSE.txt
|
||||
*/
|
||||
|
||||
defined('_JEXEC') or die;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\Database\ParameterType;
|
||||
|
||||
$value = $field->value;
|
||||
|
||||
if ($value == '') {
|
||||
return;
|
||||
}
|
||||
|
||||
$db = Factory::getDbo();
|
||||
$value = (array) $value;
|
||||
$query = $db->getQuery(true);
|
||||
$sql = $fieldParams->get('query', '');
|
||||
|
||||
$bindNames = $query->bindArray($value, ParameterType::STRING);
|
||||
|
||||
// Run the query with a having condition because it supports aliases
|
||||
$query->setQuery($sql . ' HAVING ' . $db->quoteName('value') . ' IN (' . implode(',', $bindNames) . ')');
|
||||
|
||||
try {
|
||||
$db->setQuery($query);
|
||||
$items = $db->loadObjectList();
|
||||
} catch (Exception $e) {
|
||||
// If the query failed, we fetch all elements
|
||||
$db->setQuery($sql);
|
||||
$items = $db->loadObjectList();
|
||||
}
|
||||
|
||||
$texts = [];
|
||||
|
||||
foreach ($items as $item) {
|
||||
if (in_array($item->value, $value)) {
|
||||
$texts[] = $item->text;
|
||||
}
|
||||
}
|
||||
|
||||
echo htmlentities(implode(', ', $texts));
|
||||
Reference in New Issue
Block a user