Skip to content

Commit

Permalink
[New Feature] Multilingual: Propagating existing associations if desi…
Browse files Browse the repository at this point in the history
…red (#21321)

* [New Feature] Multilingual: Propagating existing associations if desired

* cs

* lang change

* Change and add new messages

* lang changes

* Small corrections without effect on resulting funtionnality

* using new API for token

* modifying js function to use Joomla. instead of window.
  • Loading branch information
infograf768 authored and ReLater committed Sep 1, 2018
1 parent efaf145 commit 82042bf
Show file tree
Hide file tree
Showing 20 changed files with 704 additions and 20 deletions.
88 changes: 88 additions & 0 deletions administrator/components/com_categories/controllers/ajax.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_categories
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Language\LanguageHelper;

/**
* The categories controller for ajax requests
*
* @since __DEPLOY_VERSION__
*/
class CategoriesControllerAjax extends JControllerLegacy
{
/**
* Method to fetch associations of a category
*
* The method assumes that the following http parameters are passed in an Ajax Get request:
* token: the form token
* assocId: the id of the category whose associations are to be returned
* excludeLang: the association for this language is to be excluded
*
* @return null
*
* @since __DEPLOY_VERSION__
*/
public function fetchAssociations()
{
if (!JSession::checkToken('get'))
{
echo new JResponseJson(null, JText::_('JINVALID_TOKEN'), true);
}
else
{
$input = JFactory::getApplication()->input;
$extension = $input->get('extension');

$assocId = $input->getInt('assocId', 0);

if ($assocId == 0)
{
echo new JResponseJson(null, JText::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'assocId'), true);

return;
}

$excludeLang = $input->get('excludeLang', '', 'STRING');

$associations = JLanguageAssociations::getAssociations($extension, '#__categories', 'com_categories.item', (int) $assocId, 'id', 'alias', '');

unset($associations[$excludeLang]);

// Add the title to each of the associated records
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_categories/tables');
$categoryTable = JTable::getInstance('Category', 'JTable');

foreach ($associations as $lang => $association)
{
$categoryTable->load($association->id);
$associations[$lang]->title = $categoryTable->title;
}

$countContentLanguages = count(LanguageHelper::getContentLanguages(array(0, 1)));

if (count($associations) == 0)
{
$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_NONE');
}
elseif ($countContentLanguages > count($associations) + 2)
{
$tags = implode(', ', array_keys($associations));
$message = JText::sprintf('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_SOME', $tags);
}
else
{
$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_ALL');
}

echo new JResponseJson($associations, $message);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -455,6 +455,7 @@ protected function preprocessForm(JForm $form, $data, $group = 'content')
$field->addAttribute('new', 'true');
$field->addAttribute('edit', 'true');
$field->addAttribute('clear', 'true');
$field->addAttribute('propagate', 'true');
}

$form->load($addform, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('JPATH_BASE') or die;

use Joomla\CMS\Language\LanguageHelper;

/**
* Supports a modal category picker.
*
Expand Down Expand Up @@ -42,10 +44,13 @@ protected function getInput()
$extension = (string) JFactory::getApplication()->input->get('extension', 'com_content');
}

$allowNew = ((string) $this->element['new'] == 'true');
$allowEdit = ((string) $this->element['edit'] == 'true');
$allowClear = ((string) $this->element['clear'] != 'false');
$allowSelect = ((string) $this->element['select'] != 'false');
$allowNew = ((string) $this->element['new'] == 'true');
$allowEdit = ((string) $this->element['edit'] == 'true');
$allowClear = ((string) $this->element['clear'] != 'false');
$allowSelect = ((string) $this->element['select'] != 'false');
$allowPropagate = ((string) $this->element['propagate'] == 'true');

$languages = LanguageHelper::getContentLanguages(array(0, 1));

// Load language.
JFactory::getLanguage()->load('com_categories', JPATH_ADMINISTRATOR);
Expand Down Expand Up @@ -78,6 +83,8 @@ function jSelectCategory_" . $this->id . "(id, title, object) {
}
");

JText::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');

$scriptSelect[$this->id] = true;
}
}
Expand Down Expand Up @@ -179,6 +186,23 @@ function jSelectCategory_" . $this->id . "(id, title, object) {
. '</a>';
}

// Propagate category button
if ($allowPropagate && count($languages) > 2)
{
// Strip off language tag at the end
$tagLength = (int) strlen($this->element['language']);
$callbackFunctionStem = substr("jSelectCategory_" . $this->id, 0, -$tagLength);

$html .= '<a'
. ' class="btn hasTooltip' . ($value ? '' : ' hidden') . '"'
. ' id="' . $this->id . '_propagate"'
. ' href="#"'
. ' title="' . JHtml::tooltipText('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP') . '"'
. ' onclick="Joomla.propagateAssociation(\'' . $this->id . '\', \'' . $callbackFunctionStem . '\');">'
. '<span class="icon-refresh" aria-hidden="true"></span>' . JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON')
. '</a>';
}

$html .= '</span>';

// Select category modal.
Expand Down
87 changes: 87 additions & 0 deletions administrator/components/com_contact/controllers/ajax.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_contact
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Language\LanguageHelper;

/**
* The contact controller for ajax requests
*
* @since __DEPLOY_VERSION__
*/
class ContactControllerAjax extends JControllerLegacy
{
/**
* Method to fetch associations of a contact
*
* The method assumes that the following http parameters are passed in an Ajax Get request:
* token: the form token
* assocId: the id of the contact whose associations are to be returned
* excludeLang: the association for this language is to be excluded
*
* @return null
*
* @since __DEPLOY_VERSION__
*/
public function fetchAssociations()
{
if (!JSession::checkToken('get'))
{
echo new JResponseJson(null, JText::_('JINVALID_TOKEN'), true);
}
else
{
$input = JFactory::getApplication()->input;

$assocId = $input->getInt('assocId', 0);

if ($assocId == 0)
{
echo new JResponseJson(null, JText::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'assocId'), true);

return;
}

$excludeLang = $input->get('excludeLang', '', 'STRING');

$associations = JLanguageAssociations::getAssociations('com_contact', '#__contact_details', 'com_contact.item', (int) $assocId);

unset($associations[$excludeLang]);

// Add the title to each of the associated records
JTable::addIncludePath(JPATH_ADMINISTRATOR . '/components/com_contact/tables');
$contactTable = JTable::getInstance('Contact', 'ContactTable');

foreach ($associations as $lang => $association)
{
$contactTable->load($association->id);
$associations[$lang]->title = $contactTable->name;
}

$countContentLanguages = count(LanguageHelper::getContentLanguages(array(0, 1)));

if (count($associations) == 0)
{
$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_NONE');
}
elseif ($countContentLanguages > count($associations) + 2)
{
$tags = implode(', ', array_keys($associations));
$message = JText::sprintf('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_SOME', $tags);
}
else
{
$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_ALL');
}

echo new JResponseJson($associations, $message);
}
}
}
1 change: 1 addition & 0 deletions administrator/components/com_contact/models/contact.php
Original file line number Diff line number Diff line change
Expand Up @@ -560,6 +560,7 @@ protected function preprocessForm(JForm $form, $data, $group = 'content')
$field->addAttribute('new', 'true');
$field->addAttribute('edit', 'true');
$field->addAttribute('clear', 'true');
$field->addAttribute('propagate', 'true');
}

$form->load($addform, false);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

defined('JPATH_BASE') or die;

use Joomla\CMS\Language\LanguageHelper;

/**
* Supports a modal contact picker.
*
Expand All @@ -33,10 +35,13 @@ class JFormFieldModal_Contact extends JFormField
*/
protected function getInput()
{
$allowNew = ((string) $this->element['new'] == 'true');
$allowEdit = ((string) $this->element['edit'] == 'true');
$allowClear = ((string) $this->element['clear'] != 'false');
$allowSelect = ((string) $this->element['select'] != 'false');
$allowNew = ((string) $this->element['new'] == 'true');
$allowEdit = ((string) $this->element['edit'] == 'true');
$allowClear = ((string) $this->element['clear'] != 'false');
$allowSelect = ((string) $this->element['select'] != 'false');
$allowPropagate = ((string) $this->element['propagate'] == 'true');

$languages = LanguageHelper::getContentLanguages(array(0, 1));

// Load language
JFactory::getLanguage()->load('com_contact', JPATH_ADMINISTRATOR);
Expand Down Expand Up @@ -69,6 +74,8 @@ function jSelectContact_" . $this->id . "(id, title, object) {
}
");

JText::script('JGLOBAL_ASSOCIATIONS_PROPAGATE_FAILED');

$scriptSelect[$this->id] = true;
}
}
Expand Down Expand Up @@ -168,6 +175,23 @@ function jSelectContact_" . $this->id . "(id, title, object) {
. '</a>';
}

// Propagate contact button
if ($allowPropagate && count($languages) > 2)
{
// Strip off language tag at the end
$tagLength = (int) strlen($this->element['language']);
$callbackFunctionStem = substr("jSelectContact_" . $this->id, 0, -$tagLength);

$html .= '<a'
. ' class="btn hasTooltip' . ($value ? '' : ' hidden') . '"'
. ' id="' . $this->id . '_propagate"'
. ' href="#"'
. ' title="' . JHtml::tooltipText('JGLOBAL_ASSOCIATIONS_PROPAGATE_TIP') . '"'
. ' onclick="Joomla.propagateAssociation(\'' . $this->id . '\', \'' . $callbackFunctionStem . '\');">'
. '<span class="icon-refresh" aria-hidden="true"></span>' . JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_BUTTON')
. '</a>';
}

$html .= '</span>';

// Select contact modal
Expand Down
86 changes: 86 additions & 0 deletions administrator/components/com_content/controllers/ajax.json.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
<?php
/**
* @package Joomla.Administrator
* @subpackage com_content
*
* @copyright Copyright (C) 2005 - 2017 Open Source Matters, Inc. All rights reserved.
* @license GNU General Public License version 2 or later; see LICENSE.txt
*/

defined('_JEXEC') or die;

use Joomla\CMS\Language\LanguageHelper;

/**
* The article controller for ajax requests
*
* @since __DEPLOY_VERSION__
*/
class ContentControllerAjax extends JControllerLegacy
{
/**
* Method to fetch associations of an article
*
* The method assumes that the following http parameters are passed in an Ajax Get request:
* token: the form token
* assocId: the id of the article whose associations are to be returned
* excludeLang: the association for this language is to be excluded
*
* @return null
*
* @since __DEPLOY_VERSION__
*/
public function fetchAssociations()
{
if (!JSession::checkToken('get'))
{
echo new JResponseJson(null, JText::_('JINVALID_TOKEN'), true);
}
else
{
$input = JFactory::getApplication()->input;

$assocId = $input->getInt('assocId', 0);

if ($assocId == 0)
{
echo new JResponseJson(null, JText::sprintf('JLIB_FORM_VALIDATE_FIELD_INVALID', 'assocId'), true);

return;
}

$excludeLang = $input->get('excludeLang', '', 'STRING');

$associations = JLanguageAssociations::getAssociations('com_content', '#__content', 'com_content.item', (int) $assocId);

unset($associations[$excludeLang]);

// Add the title to each of the associated records
$contentTable = JTable::getInstance('Content', 'JTable');

foreach ($associations as $lang => $association)
{
$contentTable->load($association->id);
$associations[$lang]->title = $contentTable->title;
}

$countContentLanguages = count(LanguageHelper::getContentLanguages(array(0, 1)));

if (count($associations) == 0)
{
$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_NONE');
}
elseif ($countContentLanguages > count($associations) + 2)
{
$tags = implode(', ', array_keys($associations));
$message = JText::sprintf('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_SOME', $tags);
}
else
{
$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE_ALL');
}

echo new JResponseJson($associations, $message);
}
}
}

0 comments on commit 82042bf

Please sign in to comment.