32
1
mirror of https://github.com/joomla-extensions/weblinks.git synced 2025-01-12 16:01:40 +00:00

Weblink Model Field

This commit is contained in:
Tuan Pham Ngoc 2021-06-20 17:58:47 +07:00
parent 703477306a
commit 08fe10e0ef
3 changed files with 106 additions and 79 deletions

View File

@ -9,6 +9,12 @@
namespace Joomla\Component\Weblinks\Administrator\Field\Modal; namespace Joomla\Component\Weblinks\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;
defined('JPATH_BASE') or die; defined('JPATH_BASE') or die;
/** /**
@ -16,7 +22,7 @@ defined('JPATH_BASE') or die;
* *
* @since __DEPLOY_VERSION__ * @since __DEPLOY_VERSION__
*/ */
class WeblinkField extends \JFormField class WeblinkField extends FormField
{ {
/** /**
* The form field type. * The form field type.
@ -41,7 +47,7 @@ class WeblinkField extends \JFormField
$allowSelect = ((string) $this->element['select'] != 'false'); $allowSelect = ((string) $this->element['select'] != 'false');
// Load language // Load language
\JFactory::getLanguage()->load('com_weblinks', JPATH_ADMINISTRATOR); Factory::getLanguage()->load('com_weblinks', JPATH_ADMINISTRATOR);
// The active weblink id field. // The active weblink id field.
$value = (int) $this->value > 0 ? (int) $this->value : ''; $value = (int) $this->value > 0 ? (int) $this->value : '';
@ -49,9 +55,11 @@ class WeblinkField extends \JFormField
// Create the modal id. // Create the modal id.
$modalId = 'Weblink_' . $this->id; $modalId = 'Weblink_' . $this->id;
/** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
// Add the modal field script to the document head. // Add the modal field script to the document head.
\JHtml::_('jquery.framework'); $wa->useScript('field.modal-fields');
\JHtml::_('script', 'system/modal-fields.js', array('version' => 'auto', 'relative' => true));
// Script to proxy the select modal function to the modal-fields.js file. // Script to proxy the select modal function to the modal-fields.js file.
if ($allowSelect) if ($allowSelect)
@ -65,19 +73,24 @@ class WeblinkField extends \JFormField
if (!isset($scriptSelect[$this->id])) if (!isset($scriptSelect[$this->id]))
{ {
\JFactory::getDocument()->addScriptDeclaration(" $wa->addInlineScript("
function jSelectWeblink_" . $this->id . "(id, title, catid, object, url, language) { window.jSelectWeblink_" . $this->id . " = function (id, title, catid, object, url, language) {
window.processModalSelect('Weblink', '" . $this->id . "', id, title, catid, object, url, language); window.processModalSelect('Article', '" . $this->id . "', id, title, catid, object, url, language);
} }",
"); [],
['type' => 'module']
);
Text::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');
$scriptSelect[$this->id] = true; $scriptSelect[$this->id] = true;
} }
} }
// Setup variables for display. // Setup variables for display.
$linkWeblinks = 'index.php?option=com_weblinks&view=weblinks&layout=modal&tmpl=component&' . \JSession::getFormToken() . '=1'; $linkWeblinks = 'index.php?option=com_weblinks&view=weblinks&layout=modal&tmpl=component&' . Session::getFormToken() . '=1';
$linkWeblink = 'index.php?option=com_weblinks&view=weblink&layout=modal&tmpl=component&' . \JSession::getFormToken() . '=1'; $linkWeblink = 'index.php?option=com_weblinks&view=weblink&layout=modal&tmpl=component&' . Session::getFormToken() . '=1';
$modalTitle = \JText::_('COM_WEBLINKS_CHANGE_WEBLINK'); $modalTitle = Text::_('COM_WEBLINKS_CHANGE_WEBLINK');
if (isset($this->element['language'])) if (isset($this->element['language']))
{ {
@ -92,7 +105,7 @@ class WeblinkField extends \JFormField
if ($value) if ($value)
{ {
$db = \JFactory::getDbo(); $db = Factory::getDbo();
$query = $db->getQuery(true) $query = $db->getQuery(true)
->select($db->quoteName('title')) ->select($db->quoteName('title'))
->from($db->quoteName('#__weblinks')) ->from($db->quoteName('#__weblinks'))
@ -104,81 +117,93 @@ class WeblinkField extends \JFormField
} }
catch (\RuntimeException $e) catch (\RuntimeException $e)
{ {
\JError::raiseWarning(500, $e->getMessage()); Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
} }
} }
$title = empty($title) ? \JText::_('COM_WEBLINKS_SELECT_A_WEBLINK') : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
$title = empty($title) ? Text::_('COM_WEBLINKS_SELECT_A_WEBLINK') : htmlspecialchars($title, ENT_QUOTES, 'UTF-8');
// The current weblink display field. // The current weblink display field.
$html = '<span class="input-append">'; $html = '';
$html .= '<input class="input-medium" id="' . $this->id . '_name" type="text" value="' . $title . '" disabled="disabled" size="35" />';
if ($allowSelect || $allowNew || $allowEdit || $allowClear)
{
$html .= '<span class="input-group">';
}
$html .= '<input class="form-control" id="' . $this->id . '_name" type="text" value="' . $title . '" readonly size="35">';
// Select weblink button // Select weblink button
if ($allowSelect) if ($allowSelect)
{ {
$html .= '<a' $html .= '<button'
. ' class="btn hasTooltip' . ($value ? ' hidden' : '') . '"' . ' class="btn btn-primary' . ($value ? ' hidden' : '') . '"'
. ' id="' . $this->id . '_select"' . ' id="' . $this->id . '_select"'
. ' data-toggle="modal"' . ' data-bs-toggle="modal"'
. ' role="button"' . ' type="button"'
. ' href="#ModalSelect' . $modalId . '"' . ' data-bs-target="#ModalSelect' . $modalId . '">'
. ' title="' . \JHtml::tooltipText('COM_WEBLINKS_CHANGE_WEBLINK') . '">' . '<span class="icon-file" aria-hidden="true"></span> ' . Text::_('JSELECT')
. '<span class="icon-file" aria-hidden="true"></span> ' . \JText::_('JSELECT') . '</button>';
. '</a>';
} }
// New weblink button // New weblink button
if ($allowNew) if ($allowNew)
{ {
$html .= '<a' $html .= '<button'
. ' class="btn hasTooltip' . ($value ? ' hidden' : '') . '"' . ' class="btn btn-secondary' . ($value ? ' hidden' : '') . '"'
. ' id="' . $this->id . '_new"' . ' id="' . $this->id . '_new"'
. ' data-toggle="modal"' . ' data-bs-toggle="modal"'
. ' role="button"' . ' type="button"'
. ' href="#ModalNew' . $modalId . '"' . ' data-bs-target="#ModalNew' . $modalId . '">'
. ' title="' . \JHtml::tooltipText('COM_WEBLINKS_NEW_WEBLINK') . '">' . '<span class="icon-plus" aria-hidden="true"></span> ' . Text::_('JACTION_CREATE')
. '<span class="icon-new" aria-hidden="true"></span> ' . \JText::_('JACTION_CREATE') . '</button>';
. '</a>';
} }
// Edit weblink button // Edit weblink button
if ($allowEdit) if ($allowEdit)
{ {
$html .= '<a' $html .= '<button'
. ' class="btn hasTooltip' . ($value ? '' : ' hidden') . '"' . ' class="btn btn-primary' . ($value ? '' : ' hidden') . '"'
. ' id="' . $this->id . '_edit"' . ' id="' . $this->id . '_edit"'
. ' data-toggle="modal"' . ' data-bs-toggle="modal"'
. ' role="button"' . ' type="button"'
. ' href="#ModalEdit' . $modalId . '"' . ' data-bs-target="#ModalEdit' . $modalId . '">'
. ' title="' . \JHtml::tooltipText('COM_WEBLINKS_EDIT_WEBLINK') . '">' . '<span class="icon-pen-square" aria-hidden="true"></span> ' . Text::_('JACTION_EDIT')
. '<span class="icon-edit" aria-hidden="true"></span> ' . \JText::_('JACTION_EDIT') . '</button>';
. '</a>';
} }
// Clear weblink button // Clear weblink button
if ($allowClear) if ($allowClear)
{ {
$html .= '<a' $html .= '<button'
. ' class="btn' . ($value ? '' : ' hidden') . '"' . ' class="btn btn-secondary' . ($value ? '' : ' hidden') . '"'
. ' id="' . $this->id . '_clear"' . ' id="' . $this->id . '_clear"'
. ' href="#"' . ' type="button"'
. ' onclick="window.processModalParent(\'' . $this->id . '\'); return false;">' . ' onclick="window.processModalParent(\'' . $this->id . '\'); return false;">'
. '<span class="icon-remove" aria-hidden="true"></span>' . \JText::_('JCLEAR') . '<span class="icon-times" aria-hidden="true"></span> ' . Text::_('JCLEAR')
. '</a>'; . '</button>';
}
if ($allowSelect || $allowNew || $allowEdit || $allowClear)
{
$html .= '</span>';
} }
$html .= '</span>';
// Select weblink modal // Select weblink modal
if ($allowSelect) if ($allowSelect)
{ {
$html .= \JHtml::_( $html .= HTMLHelper::_(
'bootstrap.renderModal', 'bootstrap.renderModal',
'ModalSelect' . $modalId, 'ModalSelect' . $modalId,
array( array(
'title' => $modalTitle, 'title' => $modalTitle,
'url' => $urlSelect, 'url' => $urlSelect,
'height' => '400px', 'height' => '400px',
'width' => '800px', 'width' => '800px',
'bodyHeight' => '70', 'bodyHeight' => 70,
'modalWidth' => '80', 'modalWidth' => 80,
'footer' => '<a role="button" class="btn" data-dismiss="modal" aria-hidden="true">' . \JText::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</a>', 'footer' => '<button type="button" class="btn btn-secondary" data-bs-dismiss="modal">'
. Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</button>',
) )
); );
} }
@ -190,7 +215,7 @@ class WeblinkField extends \JFormField
'bootstrap.renderModal', 'bootstrap.renderModal',
'ModalNew' . $modalId, 'ModalNew' . $modalId,
array( array(
'title' => JText::_('COM_WEBLINKS_NEW_WEBLINK'), 'title' => Text::_('COM_WEBLINKS_NEW_WEBLINK'),
'backdrop' => 'static', 'backdrop' => 'static',
'keyboard' => false, 'keyboard' => false,
'closeButton' => false, 'closeButton' => false,
@ -200,14 +225,14 @@ class WeblinkField extends \JFormField
'bodyHeight' => '70', 'bodyHeight' => '70',
'modalWidth' => '80', 'modalWidth' => '80',
'footer' => '<a role="button" class="btn" aria-hidden="true"' 'footer' => '<a role="button" class="btn" aria-hidden="true"'
. ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'weblink\', \'cancel\', \'weblink-form\'); return false;">' . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'weblink\', \'cancel\', \'weblink-form\'); return false;">'
. \JText::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</a>' . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</a>'
. '<a role="button" class="btn btn-primary" aria-hidden="true"' . '<a role="button" class="btn btn-primary" aria-hidden="true"'
. ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'weblink\', \'save\', \'weblink-form\'); return false;">' . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'weblink\', \'save\', \'weblink-form\'); return false;">'
. \JText::_('JSAVE') . '</a>' . Text::_('JSAVE') . '</a>'
. '<a role="button" class="btn btn-success" aria-hidden="true"' . '<a role="button" class="btn btn-success" aria-hidden="true"'
. ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'weblink\', \'apply\', \'weblink-form\'); return false;">' . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'add\', \'weblink\', \'apply\', \'weblink-form\'); return false;">'
. \JText::_('JAPPLY') . '</a>', . Text::_('JAPPLY') . '</a>',
) )
); );
} }
@ -215,11 +240,11 @@ class WeblinkField extends \JFormField
// Edit weblink modal // Edit weblink modal
if ($allowEdit) if ($allowEdit)
{ {
$html .= \JHtml::_( $html .= HTMLHelper::_(
'bootstrap.renderModal', 'bootstrap.renderModal',
'ModalEdit' . $modalId, 'ModalEdit' . $modalId,
array( array(
'title' => \JText::_('COM_WEBLINKS_EDIT_WEBLINK'), 'title' => Text::_('COM_WEBLINKS_EDIT_WEBLINK'),
'backdrop' => 'static', 'backdrop' => 'static',
'keyboard' => false, 'keyboard' => false,
'closeButton' => false, 'closeButton' => false,
@ -229,21 +254,22 @@ class WeblinkField extends \JFormField
'bodyHeight' => '70', 'bodyHeight' => '70',
'modalWidth' => '80', 'modalWidth' => '80',
'footer' => '<a role="button" class="btn" aria-hidden="true"' 'footer' => '<a role="button" class="btn" aria-hidden="true"'
. ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'weblink\', \'cancel\', \'weblink-form\'); return false;">' . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'weblink\', \'cancel\', \'weblink-form\'); return false;">'
. \JText::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</a>' . Text::_('JLIB_HTML_BEHAVIOR_CLOSE') . '</a>'
. '<a role="button" class="btn btn-primary" aria-hidden="true"' . '<a role="button" class="btn btn-primary" aria-hidden="true"'
. ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'weblink\', \'save\', \'weblink-form\'); return false;">' . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'weblink\', \'save\', \'weblink-form\'); return false;">'
. \JText::_('JSAVE') . '</a>' . Text::_('JSAVE') . '</a>'
. '<a role="button" class="btn btn-success" aria-hidden="true"' . '<a role="button" class="btn btn-success" aria-hidden="true"'
. ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'weblink\', \'apply\', \'weblink-form\'); return false;">' . ' onclick="window.processModalEdit(this, \'' . $this->id . '\', \'edit\', \'weblink\', \'apply\', \'weblink-form\'); return false;">'
. \JText::_('JAPPLY') . '</a>', . Text::_('JAPPLY') . '</a>',
) )
); );
} }
// Note: class='required' for client side validation. // Note: class='required' for client side validation.
$class = $this->required ? ' class="required modal-value"' : ''; $class = $this->required ? ' class="required modal-value"' : '';
$html .= '<input type="hidden" id="' . $this->id . '_id" ' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name $html .= '<input type="hidden" id="' . $this->id . '_id" ' . $class . ' data-required="' . (int) $this->required . '" name="' . $this->name
. '" data-text="' . htmlspecialchars(\JText::_('COM_WEBLINKS_SELECT_A_WEBLINK', true), ENT_COMPAT, 'UTF-8') . '" value="' . $value . '" />'; . '" data-text="' . htmlspecialchars(Text::_('COM_WEBLINKS_SELECT_A_WEBLINK', true), ENT_COMPAT, 'UTF-8') . '" value="' . $value . '" />';
return $html; return $html;
} }

View File

@ -8,13 +8,14 @@
*/ */
defined('_JEXEC') or die; defined('_JEXEC') or die;
use Joomla\Component\Weblinks\Site\Helper\RouteHelper;
$app = JFactory::getApplication(); $app = JFactory::getApplication();
if ($app->isClient('site')) if ($app->isClient('site'))
{ {
JSession::checkToken('get') or die(JText::_('JINVALID_TOKEN')); JSession::checkToken('get') or die(JText::_('JINVALID_TOKEN'));
} }
JLoader::register('WeblinksHelperRoute', JPATH_ROOT . '/components/com_weblinks/helpers/route.php');
// Include the component HTML helpers. // Include the component HTML helpers.
@ -110,7 +111,7 @@ $iconStates = array(
. ' data-id="' . $item->id . '"' . ' data-id="' . $item->id . '"'
. ' data-title="' . $this->escape(addslashes($item->title)) . '"' . ' data-title="' . $this->escape(addslashes($item->title)) . '"'
. ' data-cat-id="' . $this->escape($item->catid) . '"' . ' data-cat-id="' . $this->escape($item->catid) . '"'
. ' data-uri="' . $this->escape(WeblinksHelperRoute::getWeblinkRoute($item->id, $item->catid, $item->language)) . '"' . ' data-uri="' . $this->escape(RouteHelper::getWeblinkRoute($item->id, $item->catid, $item->language)) . '"'
. ' data-language="' . $this->escape($lang) . '"'; . ' data-language="' . $this->escape($lang) . '"';
?> ?>
<a class="select-link" href="javascript:void(0)" <?php echo $attribs; ?>> <a class="select-link" href="javascript:void(0)" <?php echo $attribs; ?>>

View File

@ -12,7 +12,7 @@
<fields name="request"> <fields name="request">
<fieldset <fieldset
name="request" name="request"
addfieldpath="/administrator/components/com_weblinks/models/fields" addfieldprefix="Joomla\Component\Weblinks\Administrator\Field"
> >
<field name="id" <field name="id"