Skip to content

Commit

Permalink
Contact Modal use ModalSelectField
Browse files Browse the repository at this point in the history
  • Loading branch information
Fedik committed Nov 9, 2023
1 parent 4dd90b4 commit c42bcff
Show file tree
Hide file tree
Showing 3 changed files with 239 additions and 4 deletions.
1 change: 1 addition & 0 deletions administrator/components/com_contact/contact.xml
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@
<filename>contact.xml</filename>
<folder>forms</folder>
<folder>helpers</folder>
<folder>layouts</folder>
<folder>services</folder>
<folder>sql</folder>
<folder>src</folder>
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
<?php

/**
* @package Joomla.Site
* @subpackage Layout
*
* @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\Factory;
use Joomla\CMS\Language\Text;

extract($displayData);

/**
* Layout variables
* -----------------
* @var string $autocomplete Autocomplete attribute for the field.
* @var boolean $autofocus Is autofocus enabled?
* @var string $class Classes for the input.
* @var string $description Description of the field.
* @var boolean $disabled Is this field disabled?
* @var string $group Group the field belongs to. <fields> section in form XML.
* @var boolean $hidden Is this field hidden in the form?
* @var string $hint Placeholder for the field.
* @var string $id DOM id of the field.
* @var string $label Label of the field.
* @var string $labelclass Classes to apply to the label.
* @var boolean $multiple Does this field support multiple values?
* @var string $name Name of the input field.
* @var string $onchange Onchange attribute for the field.
* @var string $onclick Onclick attribute for the field.
* @var string $pattern Pattern (Reg Ex) of value of the form field.
* @var boolean $readonly Is this field read only?
* @var boolean $repeat Allows extensions to duplicate elements.
* @var boolean $required Is this field required?
* @var integer $size Size attribute of the input.
* @var boolean $spellcheck Spellcheck state for the form field.
* @var string $validate Validation rules to apply.
* @var string $value Value attribute of the field.
* @var string $dataAttribute Miscellaneous data attributes preprocessed for HTML output
* @var array $dataAttributes Miscellaneous data attribute for eg, data-*
* @var string $valueTitle
* @var array $canDo
* @var string[] $urls
* @var string[] $modalTitles
* @var string $language
*/

// Do nothing when propagate is disabled
if (empty($canDo['propagate'])) {
return;
}

// Scripts for backward compatibility
/** @var \Joomla\CMS\WebAsset\WebAssetManager $wa */
$wa = Factory::getApplication()->getDocument()->getWebAssetManager();
$wa->useScript('field.modal-fields');
$wa->addInlineScript(
'window.jSelectContact_' . $id . ' = function (id, title, object) {
window.processModalSelect("Contact", "' . $id . '", id, title, "", object);
}',
['name' => 'inline.select_contact_' . $id],
['type' => 'module']
);
Text::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');

// Language propagate callback name
// Strip off language tag at the end
$tagLength = strlen($language);
$callbackFunctionStem = substr("jSelectContact_" . $id, 0, -$tagLength);

?>

<button type="button" class="btn btn-primary" <?php echo $value ? '' : 'hidden'; ?>
title="<?php echo $this->escape(Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP')); ?>"
data-show-when-value="1"
onclick="Joomla.propagateAssociation('<?php echo $id; ?>', '<?php echo $callbackFunctionStem; ?>')">
<span class="icon-sync" aria-hidden="true"></span> <?php echo Text::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON'); ?>
</button>
159 changes: 155 additions & 4 deletions administrator/components/com_contact/src/Field/Modal/ContactField.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
namespace Joomla\Component\Contact\Administrator\Field\Modal;

use Joomla\CMS\Factory;
use Joomla\CMS\Form\FormField;
use Joomla\CMS\Form\Field\ModalSelectField;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\Language\Text;
use Joomla\CMS\Layout\FileLayout;
use Joomla\CMS\Session\Session;
use Joomla\CMS\Uri\Uri;
use Joomla\Database\ParameterType;

// phpcs:disable PSR1.Files.SideEffects
Expand All @@ -27,7 +29,7 @@
*
* @since 1.6
*/
class ContactField extends FormField
class ContactField extends ModalSelectField
{
/**
* The form field type.
Expand All @@ -37,14 +39,99 @@ class ContactField extends FormField
*/
protected $type = 'Modal_Contact';

/**
* Method to attach a Form object to the field.
*
* @param \SimpleXMLElement $element The SimpleXMLElement object representing the `<field>` tag for the form field object.
* @param mixed $value The form field value to validate.
* @param string $group The field name group control value.
*
* @return boolean True on success.
*
* @see FormField::setup()
* @since __DEPLOY_VERSION__
*/
public function setup(\SimpleXMLElement $element, $value, $group = null)
{
// Check if the value consist with id:alias, extract the id only
if ($value && str_contains($value, ':')) {
[$id] = explode(':', $value, 2);
$value = (int) $id;
}

$result = parent::setup($element, $value, $group);

if (!$result) {
return $result;
}

Factory::getApplication()->getLanguage()->load('com_contact', JPATH_ADMINISTRATOR);

$languages = LanguageHelper::getContentLanguages([0, 1], false);
$language = (string) $this->element['language'];

// Prepare enabled actions
$this->canDo['propagate'] = ((string) $this->element['propagate'] == 'true') && \count($languages) > 2;

// Prepare Urls
$linkItems = (new Uri())->setPath(Uri::base(true) . '/index.php');
$linkItems->setQuery([
'option' => 'com_contact',
'view' => 'contacts',
'layout' => 'modal',
'tmpl' => 'component',
Session::getFormToken() => 1,
]);
$linkItem = clone $linkItems;
$linkItem->setVar('view', 'contact');
$linkCheckin = (new Uri())->setPath(Uri::base(true) . '/index.php');
$linkCheckin->setQuery([
'option' => 'com_contact',
'task' => 'contacts.checkin',
'format' => 'json',
Session::getFormToken() => 1,
]);

if ($language) {
$linkItems->setVar('forcedLanguage', $language);
$linkItem->setVar('forcedLanguage', $language);

$modalTitle = Text::_('COM_CONTACT_SELECT_A_CONTACT') . ' &#8212; ' . $this->getTitle();

$this->dataAttributes['data-language'] = $language;
} else {
$modalTitle = Text::_('COM_CONTACT_SELECT_A_CONTACT');
}

$urlSelect = $linkItems;
$urlEdit = clone $linkItem;
$urlEdit->setVar('task', 'contact.edit');
$urlNew = clone $linkItem;
$urlNew->setVar('task', 'contact.add');

$this->urls['select'] = (string) $urlSelect;
$this->urls['new'] = (string) $urlNew;
$this->urls['edit'] = (string) $urlEdit;
$this->urls['checkin'] = (string) $linkCheckin;

// Prepare titles
$this->modalTitles['select'] = $modalTitle;
$this->modalTitles['new'] = Text::_('COM_CONTACT_NEW_CONTACT');
$this->modalTitles['edit'] = Text::_('COM_CONTACT_EDIT_CONTACT');

$this->hint = $this->hint ?: Text::_('COM_CONTACT_SELECT_A_CONTACT');

return $result;
}

/**
* Method to get the field input markup.
*
* @return string The field input markup.
*
* @since 1.6
*/
protected function getInput()
protected function getInput1()
{
$allowNew = ((string) $this->element['new'] == 'true');
$allowEdit = ((string) $this->element['edit'] == 'true');
Expand Down Expand Up @@ -298,8 +385,72 @@ protected function getInput()
*
* @since 3.4
*/
protected function getLabel()
protected function getLabel1()
{
return str_replace($this->id, $this->id . '_name', parent::getLabel());
}

/**
* Method to retrieve the title of selected item.
*
* @return string
*
* @since __DEPLOY_VERSION__
*/
protected function getValueTitle()
{
$value = (int) $this->value ?: '';
$title = '';

if ($value) {
try {
$db = $this->getDatabase();
$query = $db->getQuery(true)
->select($db->quoteName('name'))
->from($db->quoteName('#__contact_details'))
->where($db->quoteName('id') . ' = :value')
->bind(':value', $value, ParameterType::INTEGER);
$db->setQuery($query);

$title = $db->loadResult();
} catch (\Throwable $e) {
Factory::getApplication()->enqueueMessage($e->getMessage(), 'error');
}
}

return $title ?: $value;
}

/**
* Method to get the data to be passed to the layout for rendering.
*
* @return array
*
* @since __DEPLOY_VERSION__
*/
protected function getLayoutData()
{
$data = parent::getLayoutData();
$data['language'] = (string) $this->element['language'];

return $data;
}

/**
* Get the renderer
*
* @param string $layoutId Id to load
*
* @return FileLayout
*
* @since __DEPLOY_VERSION__
*/
protected function getRenderer($layoutId = 'default')
{
$layout = parent::getRenderer($layoutId);
$layout->setComponent('com_contact');
$layout->setClient(1);

return $layout;
}
}

0 comments on commit c42bcff

Please sign in to comment.