first commit
This commit is contained in:
@ -0,0 +1,104 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_associations
|
||||
*
|
||||
* @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\Component\Associations\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\Field\ListField;
|
||||
use Joomla\CMS\Language\LanguageHelper;
|
||||
use Joomla\Component\Associations\Administrator\Helper\AssociationsHelper;
|
||||
use Joomla\Utilities\ArrayHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Field listing item languages
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
class ItemlanguageField extends ListField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected $type = 'Itemlanguage';
|
||||
|
||||
/**
|
||||
* Method to get the field options.
|
||||
*
|
||||
* @return array The field option objects.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getOptions()
|
||||
{
|
||||
$input = Factory::getApplication()->getInput();
|
||||
|
||||
list($extensionName, $typeName) = explode('.', $input->get('itemtype', '', 'string'), 2);
|
||||
|
||||
// Get the extension specific helper method
|
||||
$helper = AssociationsHelper::getExtensionHelper($extensionName);
|
||||
|
||||
$languageField = $helper->getTypeFieldName($typeName, 'language');
|
||||
$referenceId = $input->get('id', 0, 'int');
|
||||
$reference = ArrayHelper::fromObject(AssociationsHelper::getItem($extensionName, $typeName, $referenceId));
|
||||
$referenceLang = $reference[$languageField];
|
||||
|
||||
// Get item associations given ID and item type
|
||||
$associations = AssociationsHelper::getAssociationList($extensionName, $typeName, $referenceId);
|
||||
|
||||
// Check if user can create items in this component item type.
|
||||
$canCreate = AssociationsHelper::allowAdd($extensionName, $typeName);
|
||||
|
||||
// Gets existing languages.
|
||||
$existingLanguages = LanguageHelper::getContentLanguages([0, 1], false);
|
||||
|
||||
$options = [];
|
||||
|
||||
// Each option has the format "<lang>|<id>", example: "en-GB|1"
|
||||
foreach ($existingLanguages as $langCode => $language) {
|
||||
// If language code is equal to reference language we don't need it.
|
||||
if ($language->lang_code == $referenceLang) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$options[$langCode] = new \stdClass();
|
||||
$options[$langCode]->text = $language->title;
|
||||
|
||||
// If association exists in this language.
|
||||
if (isset($associations[$language->lang_code])) {
|
||||
$itemId = (int) $associations[$language->lang_code]['id'];
|
||||
$options[$langCode]->value = $language->lang_code . ':' . $itemId . ':edit';
|
||||
|
||||
// Check if user does have permission to edit the associated item.
|
||||
$canEdit = AssociationsHelper::allowEdit($extensionName, $typeName, $itemId);
|
||||
|
||||
// Check if item can be checked out
|
||||
$canCheckout = AssociationsHelper::canCheckinItem($extensionName, $typeName, $itemId);
|
||||
|
||||
// Disable language if user is not allowed to edit the item associated to it.
|
||||
$options[$langCode]->disable = !($canEdit && $canCheckout);
|
||||
} else {
|
||||
// New item, id = 0 and disabled if user is not allowed to create new items.
|
||||
$options[$langCode]->value = $language->lang_code . ':0:add';
|
||||
|
||||
// Disable language if user is not allowed to create items.
|
||||
$options[$langCode]->disable = !$canCreate;
|
||||
}
|
||||
}
|
||||
|
||||
return array_merge(parent::getOptions(), $options);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,66 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_associations
|
||||
*
|
||||
* @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\Component\Associations\Administrator\Field;
|
||||
|
||||
use Joomla\CMS\Form\Field\GroupedlistField;
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\Component\Associations\Administrator\Helper\AssociationsHelper;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* A drop down containing all component item types that implement associations.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
class ItemtypeField extends GroupedlistField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected $type = 'Itemtype';
|
||||
|
||||
/**
|
||||
* Method to get the field input markup.
|
||||
*
|
||||
* @return array The field option objects as a nested array in groups.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*
|
||||
* @throws \UnexpectedValueException
|
||||
*/
|
||||
protected function getGroups()
|
||||
{
|
||||
$options = [];
|
||||
$extensions = AssociationsHelper::getSupportedExtensions();
|
||||
|
||||
foreach ($extensions as $extension) {
|
||||
if ($extension->get('associationssupport') === true) {
|
||||
foreach ($extension->get('types') as $type) {
|
||||
$context = $extension->get('component') . '.' . $type->get('name');
|
||||
$options[$extension->get('title')][] = HTMLHelper::_('select.option', $context, $type->get('title'));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Sort by alpha order.
|
||||
uksort($options, 'strnatcmp');
|
||||
|
||||
// Add options to parent array.
|
||||
return array_merge(parent::getGroups(), $options);
|
||||
}
|
||||
}
|
||||
@ -0,0 +1,110 @@
|
||||
<?php
|
||||
|
||||
/**
|
||||
* @package Joomla.Administrator
|
||||
* @subpackage com_associations
|
||||
*
|
||||
* @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\Component\Associations\Administrator\Field\Modal;
|
||||
|
||||
use Joomla\CMS\Factory;
|
||||
use Joomla\CMS\Form\FormField;
|
||||
use Joomla\CMS\HTML\HTMLHelper;
|
||||
use Joomla\CMS\Language\Text;
|
||||
use Joomla\CMS\Session\Session;
|
||||
|
||||
// phpcs:disable PSR1.Files.SideEffects
|
||||
\defined('_JEXEC') or die;
|
||||
// phpcs:enable PSR1.Files.SideEffects
|
||||
|
||||
/**
|
||||
* Supports a modal item picker.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
class AssociationField extends FormField
|
||||
{
|
||||
/**
|
||||
* The form field type.
|
||||
*
|
||||
* @var string
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected $type = 'Modal_Association';
|
||||
|
||||
/**
|
||||
* Method to get the field input markup.
|
||||
*
|
||||
* @return string The field input markup.
|
||||
*
|
||||
* @since 3.7.0
|
||||
*/
|
||||
protected function getInput()
|
||||
{
|
||||
// @todo USE Layouts here!!!
|
||||
// The active item id field.
|
||||
$value = (int) $this->value ?: '';
|
||||
|
||||
$doc = Factory::getApplication()->getDocument();
|
||||
$wa = $doc->getWebAssetManager();
|
||||
|
||||
$doc->addScriptOptions('admin_associations_modal', ['itemId' => $value]);
|
||||
$wa->useScript('com_associations.admin-associations-modal');
|
||||
|
||||
// Setup variables for display.
|
||||
$html = [];
|
||||
|
||||
$linkAssociations = 'index.php?option=com_associations&view=associations&layout=modal&tmpl=component'
|
||||
. '&forcedItemType=' . Factory::getApplication()->getInput()->get('itemtype', '', 'string') . '&function=jSelectAssociation_' . $this->id;
|
||||
|
||||
$linkAssociations .= "&forcedLanguage=' + document.getElementById('target-association').getAttribute('data-language') + '";
|
||||
|
||||
$urlSelect = $linkAssociations . '&' . Session::getFormToken() . '=1';
|
||||
|
||||
// Select custom association button
|
||||
$html[] = '<button'
|
||||
. ' type="button"'
|
||||
. ' id="select-change"'
|
||||
. ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
|
||||
. ' data-bs-toggle="modal"'
|
||||
. ' data-select="' . Text::_('COM_ASSOCIATIONS_SELECT_TARGET') . '"'
|
||||
. ' data-change="' . Text::_('COM_ASSOCIATIONS_CHANGE_TARGET') . '"'
|
||||
. ' data-bs-target="#associationSelect' . $this->id . 'Modal">'
|
||||
. '<span class="icon-file" aria-hidden="true"></span> '
|
||||
. '<span id="select-change-text"></span>'
|
||||
. '</button>';
|
||||
|
||||
// Clear association button
|
||||
$html[] = '<button'
|
||||
. ' type="button"'
|
||||
. ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
|
||||
. ' onclick="return Joomla.submitbutton(\'undo-association\');"'
|
||||
. ' id="remove-assoc">'
|
||||
. '<span class="icon-times" aria-hidden="true"></span> ' . Text::_('JCLEAR')
|
||||
. '</button>';
|
||||
|
||||
$html[] = '<input type="hidden" id="' . $this->id . '_id" name="' . $this->name . '" value="' . $value . '">';
|
||||
|
||||
// Select custom association modal
|
||||
$html[] = HTMLHelper::_(
|
||||
'bootstrap.renderModal',
|
||||
'associationSelect' . $this->id . 'Modal',
|
||||
[
|
||||
'title' => Text::_('COM_ASSOCIATIONS_SELECT_TARGET'),
|
||||
'backdrop' => 'static',
|
||||
'url' => $urlSelect,
|
||||
'height' => '400px',
|
||||
'width' => '800px',
|
||||
'bodyHeight' => 70,
|
||||
'modalWidth' => 80,
|
||||
'footer' => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">'
|
||||
. Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>',
|
||||
]
|
||||
);
|
||||
|
||||
return implode("\n", $html);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user