Skip to content

Commit

Permalink
Task #159601 chore: Multilingual support - Email sending changes (#94)
Browse files Browse the repository at this point in the history
  • Loading branch information
manojLondhe authored Jun 6, 2020
1 parent 956b272 commit 1591458
Showing 1 changed file with 164 additions and 90 deletions.
254 changes: 164 additions & 90 deletions src/com_tjnotifications/admin/models/notifications.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@
* @subpackage com_tjnotifications
*
* @author Techjoomla <[email protected]>
* @copyright Copyright (C) 2009 - 2019 Techjoomla. All rights reserved.
* @copyright Copyright (C) 2009 - 2020 Techjoomla. All rights reserved.
* @license http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
*/

// No direct access to this file
defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Table\Table;
use Joomla\CMS\Language\LanguageHelper;
use Joomla\CMS\MVC\Model\ListModel;
use Joomla\CMS\Table\Table;

jimport('joomla.application.component.model');
/**
* notifications model.
* Notifications model
*
* @since 1.6
*/
class TjnotificationsModelNotifications extends Joomla\CMS\MVC\Model\ListModel
class TjnotificationsModelNotifications extends ListModel
{
/**
* Constructor.
Expand All @@ -47,6 +47,55 @@ public function __construct($config = array())
parent::__construct($config);
}

/**
* Method to auto-populate the model state.
*
* Note. Calling getState in this method will result in recursion.
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
*
* @since 1.6
*/
protected function populateState($ordering = 'id', $direction = 'asc')
{
$app = Factory::getApplication();
$ordering = $app->input->get('filter_order', 'id', 'string');
$direction = $app->input->get('filter_order_Dir', 'desc', 'string');

if (!empty($ordering) && in_array($ordering, $this->filter_fields))
{
$this->setState('list.ordering', $ordering);
}

if (!empty($direction))
{
if (!in_array(strtolower($direction), array('asc', 'desc')))
{
$direction = 'desc';
}

$this->setState('list.direction', $direction);
}

$language = $app->getUserStateFromRequest($this->context . '.filter.language', 'filter_language', '', 'string');
$this->setState('filter.language', $language);

parent::populateState($ordering, $direction);

// Get pagination request variables
$limit = $app->getUserStateFromRequest('global.list.limit', 'limit', $app->getCfg('list_limit'), 'int');
$limitstart = $app->input->get('limitstart', 0, 'int');

// In case limit has been changed, adjust it
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);

$this->setState('list.limit', $limit);
$this->setState('list.start', $limitstart);
}

/**
* Build an SQL query to load the list data.
*
Expand Down Expand Up @@ -104,6 +153,16 @@ protected function getListQuery()
$query->order($db->quoteName('key'), 'ASC');
}

// Filter by language
$language = $this->getState('filter.language');

if ($language !== '')
{
$query->select('ntc.language');
$query->join('LEFT', '#__tj_notification_template_configs AS ntc ON ntc.template_id = t.id');
$query->where($db->qn('ntc.language') . '=' . $db->quote($language));
}

$orderCol = $this->getState('list.ordering');
$orderDirn = $this->getState('list.direction');

Expand All @@ -116,135 +175,150 @@ protected function getListQuery()
}

/**
* Method to get the record form.
*
* @param String $client An optional array of data for the form to interogate.
* @param String $key True if the form is to load its own data (default case), false if not.
* Get items
*
* @return JForm A JForm object on success, false on failure
* @return mixed Object on success, false on failure.
*
* @since 1.6
* @since 1.0.0
*/
public function getTemplate($client, $key)
public function getItems()
{
$object = clone $this;
$items = parent::getItems();

$this->setState('filter.key', $key);
$this->setState('filter.client', $client);
foreach ($items as $key => $item)
{
if (empty($item->id))
{
return false;
}

// Return exact template according key and client
$templates = $this->getItems();
// Processing to get language specific template data
$db = Factory::getDBO();
$query = $db->getQuery(true);
$query->select('ntc.*');
$query->from($db->qn('#__tj_notification_template_configs', 'ntc'));
$query->where($db->qn('ntc.template_id') . '=' . (int) $item->id);
$query->order($db->q('ntc.language'));

// If templates object is empty and key contain # then check for default (fallback) template.
if (empty($templates) && strpos($key, '#'))
{
// Regex for removing last part of the string
// Eg if input string is global#vendor#course then the output is global#vendor
$db->setQuery($query);
$templateConfigs = $db->loadObjectlist();

$key = preg_replace('/#[^#]*$/', '', $key);
foreach (TJNOTIFICATIONS_CONST_PROVIDERS_ARRAY as $keyProvider => $provider)
{
$item->$provider['state'] = $templateConfigs[0]->state;
$item->$provider['languages'] = array();

// Call function recursively with modified key
return $object->getTemplate($client, $key);
foreach ($templateConfigs as $keytemplate => $tConfig)
{
if ($tConfig->provider == $provider && !in_array($tConfig->language, $item->$provider['languages']))
{
$item->$provider['languages'][] = $tConfig->language;
}
}
}
}

return $templates[0];
return $items;
}

/**
* Method to auto-populate the model state.
* Get matching templates
*
* Note. Calling getState in this method will result in recursion.
* @param string $provider Notification provider
*
* @param string $ordering An optional ordering field.
* @param string $direction An optional direction (asc|desc).
*
* @return void
* @return mixed Object on success, false on failure.
*
* @since 1.6
* @since 1.0.0
*/
protected function populateState($ordering = 'id', $direction = 'asc')
public function getMatchingTemplates($provider)
{
$app = Factory::getApplication();
$ordering = $app->input->get('filter_order', 'id', 'STRING');
$direction = $app->input->get('filter_order_Dir', 'desc', 'STRING');
// Initialize variables.
$db = Factory::getDbo();
$query = $db->getQuery(true);
$query = $this->getListQuery();
$db->setQuery($query);
$items = $db->loadObjectList();

if (!empty($ordering) && in_array($ordering, $this->filter_fields))
if (empty($items))
{
$this->setState('list.ordering', $ordering);
return false;
}

if (!empty($direction))
foreach ($items as $key => $item)
{
if (!in_array(strtolower($direction), array('asc', 'desc')))
if (empty($item->id))
{
$direction = 'desc';
return false;
}

$this->setState('list.direction', $direction);
}

parent::populateState($ordering, $direction);
$db = Factory::getDBO();
$query = $db->getQuery(true);
$query->select('ntc.*');
$query->from($db->qn('#__tj_notification_template_configs', 'ntc'));
$query->where($db->qn('ntc.template_id') . '=' . (int) $item->id);
$query->where($db->qn('ntc.provider') . '=' . $db->quote($provider));
$query->where($db->qn('ntc.language') . '=' . $db->quote($this->getState('filter.language')));
$query->order($db->q('ntc.language'));

$mainframe = Factory::getApplication();
$db->setQuery($query);
$templateConfigs = $db->loadObjectlist();

// Get pagination request variables
$limit = $mainframe->getUserStateFromRequest('global.list.limit', 'limit', $mainframe->getCfg('list_limit'), 'int');
$limitstart = JRequest::getVar('limitstart', 0, '', 'int');

// In case limit has been changed, adjust it
$limitstart = ($limit != 0 ? (floor($limitstart / $limit) * $limit) : 0);
foreach ($templateConfigs as $keytemplate => $tConfig)
{
foreach ($tConfig as $configKey => $configValue)
{
$item->$provider[$configKey] = $configValue;
}
}
}

$this->setState('list.limit', $limit);
$this->setState('list.start', $limitstart);
return $items;
}

/**
* get items
* Method to get the record form.
*
* @return mixed Object on success, false on failure.
* @param string $client An optional array of data for the form to interogate.
* @param string $key True if the form is to load its own data (default case), false if not.
* @param string $language Template language
* @param string $provider Notification provider
*
* @since 1.0.0
* @return JForm A JForm object on success, false on failure
*
* @since 1.6
*/
public function getItems()
public function getTemplate($client, $key, $language, $provider = 'email')
{
$items = parent::getItems();
$object = clone $this;

foreach ($items as $key => $item)
{
if (empty($item->id))
{
return false;
}
$this->setState('filter.key', $key);
$this->setState('filter.client', $client);
$this->setState('filter.language', $language);

if (!empty($item->id))
{
$db = Factory::getDBO();
$query = $db->getQuery(true);
$query->select('ntc.*');
$query->from($db->qn('#__tj_notification_template_configs', 'ntc'));
$query->where($db->qn('ntc.template_id') . '=' . (int) $item->id);
$query->order($db->q('ntc.language'));
// Find matching template for current language of user
$templates = $this->getMatchingTemplates($provider);

$db->setQuery($query);
$templateConfigs = $db->loadObjectlist();
// If no matching template found, look for template with lang = *
if (empty($templates))
{
// Setting $this vars don't work
$object->setState('filter.language', '*');
$templates = $object->getMatchingTemplates($provider);
}

foreach (TJNOTIFICATIONS_CONST_PROVIDERS_ARRAY as $keyProvider => $provider)
{
$item->$provider['state'] = $templateConfigs[0]->state;
$item->$provider['languages'] = array();
// If templates object is empty and key contain # then check for default (fallback) template.
if (empty($templates) && strpos($key, '#'))
{
// Regex for removing last part of the string
// Eg if input string is global#vendor#course then the output is global#vendor

foreach ($templateConfigs as $keytemplate => $tConfig)
{
if ($tConfig->provider == $provider && !in_array($tConfig->language, $item->$provider['languages']))
{
$item->$provider['languages'][] = $tConfig->language;
}
}
}
}
$key = preg_replace('/#[^#]*$/', '', $key);

// Call function recursively with modified key
return $object->getTemplate($client, $key, $language, $provider);
}

return $items;
return $templates[0];
}

/**
Expand Down

0 comments on commit 1591458

Please sign in to comment.