Skip to content

Commit

Permalink
[New Feature] Multilingual: Propagating existing associations if desired
Browse files Browse the repository at this point in the history
  • Loading branch information
infograf768 committed Jul 31, 2018
1 parent aa34c79 commit ffc424c
Show file tree
Hide file tree
Showing 25 changed files with 627 additions and 20 deletions.
72 changes: 72 additions & 0 deletions administrator/components/com_categories/controllers/ajax.json.php
@@ -0,0 +1,72 @@
<?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;

/**
* 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::_('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;
}

$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE');

echo new JResponseJson($associations, $message);
}
}
}
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
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="window.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
Expand Up @@ -101,6 +101,7 @@
<?php echo $this->form->getInput('extension'); ?>
<input type="hidden" name="task" value="" />
<input type="hidden" name="forcedLanguage" value="<?php echo $input->get('forcedLanguage', '', 'cmd'); ?>" />
<?php echo '<input id="token" type="hidden" name="' . JSession::getFormToken() . '" value="1" />'; ?>
<?php echo JHtml::_('form.token'); ?>
</div>
</form>
71 changes: 71 additions & 0 deletions administrator/components/com_contact/controllers/ajax.json.php
@@ -0,0 +1,71 @@
<?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;

/**
* 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::_('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;
}

$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE');

echo new JResponseJson($associations, $message);
}
}
}
1 change: 1 addition & 0 deletions administrator/components/com_contact/models/contact.php
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
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="window.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
Expand Up @@ -120,5 +120,6 @@
</div>
<input type="hidden" name="task" value="" />
<input type="hidden" name="forcedLanguage" value="<?php echo $input->get('forcedLanguage', '', 'cmd'); ?>" />
<?php echo '<input id="token" type="hidden" name="' . JSession::getFormToken() . '" value="1" />'; ?>
<?php echo JHtml::_('form.token'); ?>
</form>
70 changes: 70 additions & 0 deletions administrator/components/com_content/controllers/ajax.json.php
@@ -0,0 +1,70 @@
<?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;

/**
* 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::_('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;
}

$message = JText::_('JGLOBAL_ASSOCIATIONS_PROPAGATE_MESSAGE');

echo new JResponseJson($associations, $message);
}
}
}
1 change: 1 addition & 0 deletions administrator/components/com_content/models/article.php
Expand Up @@ -913,6 +913,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

0 comments on commit ffc424c

Please sign in to comment.