From 07844ccf58c7447e89b759fd85a0fe96cd5c3187 Mon Sep 17 00:00:00 2001 From: Samuel Vanhove Date: Sun, 26 Apr 2015 23:18:13 -0400 Subject: [PATCH] CRM-16355 - add the contact filter based on language and dynamic language change --- CRM/Admin/Form/ScheduleReminders.php | 4 +- CRM/Core/BAO/ActionSchedule.php | 55 ++++++++++++++++++++ CRM/Core/DAO.php | 1 + CRM/Core/I18n.php | 77 ++++++++++++++++++++++++++++ 4 files changed, 135 insertions(+), 2 deletions(-) diff --git a/CRM/Admin/Form/ScheduleReminders.php b/CRM/Admin/Form/ScheduleReminders.php index bc78ea71e8..c5f90aa21f 100644 --- a/CRM/Admin/Form/ScheduleReminders.php +++ b/CRM/Admin/Form/ScheduleReminders.php @@ -268,13 +268,13 @@ class CRM_Admin_Form_ScheduleReminders extends CRM_Admin_Form { foreach ($locales as $locale) { $languageFilter[$locale] = $languages[$locale]; } - $languageFilter['none'] = ts('Contacts with no preferred language'); + $languageFilter[CRM_Core_I18n::NONE] = ts('Contacts with no preferred language'); $element = $this->add('select', 'filter_contact_language', ts('Recipients language'), $languageFilter, FALSE, array('multiple' => TRUE, 'class' => 'crm-select2', 'placeholder' => TRUE)); $communicationLanguage = array(); $communicationLanguage[''] = ts('System default language'); - $communicationLanguage['auto'] = ts('Follow recipient preferred language'); + $communicationLanguage[CRM_Core_I18n::AUTO] = ts('Follow recipient preferred language'); foreach ($locales as $locale) { $communicationLanguage[$locale] = $languages[$locale]; } diff --git a/CRM/Core/BAO/ActionSchedule.php b/CRM/Core/BAO/ActionSchedule.php index 12b8bc1293..b99e8e253d 100755 --- a/CRM/Core/BAO/ActionSchedule.php +++ b/CRM/Core/BAO/ActionSchedule.php @@ -814,6 +814,7 @@ LEFT JOIN civicrm_phone phone ON phone.id = lb.phone_id } $entityJoinClause .= $extraOn; + $query = " SELECT reminder.id as reminderID, reminder.contact_id as contactID, reminder.entity_table as entityTable, reminder.*, e.id as entityID, e.* {$extraSelect} FROM civicrm_action_log reminder @@ -826,7 +827,15 @@ WHERE reminder.action_schedule_id = %1 AND reminder.action_date_time IS NULL array(1 => array($actionSchedule->id, 'Integer')) ); + $multilingual = CRM_Core_I18n::isMultilingual(); while ($dao->fetch()) { + // switch language if necessary + if ($multilingual) { + $cid = $dao->contactID; + $preferred_language = CRM_Core_DAO::getFieldValue('CRM_Contact_DAO_Contact', $cid, 'preferred_language'); + CRM_Core_BAO_ActionSchedule::setCommunicationLanguage($actionSchedule->communication_language, $preferred_language); + } + $entityTokenParams = array(); foreach ($tokenFields as $field) { if ($field == 'location') { @@ -1232,6 +1241,23 @@ reminder.action_schedule_id = %1"; if ($table != 'civicrm_contact e') { $join[] = "INNER JOIN civicrm_contact c ON c.id = {$contactField} AND c.is_deleted = 0 AND c.is_deceased = 0 "; + } + + $multilingual = CRM_Core_I18n::isMultilingual(); + if ($multilingual && !empty($actionSchedule->filter_contact_language)) { + $tableAlias = ($table != 'civicrm_contact e') ? 'c' : 'e'; + + // get language filter for the schedule + $filter_contact_language = explode(CRM_Core_DAO::VALUE_SEPARATOR, $actionSchedule->filter_contact_language); + $w = ''; + if (($key = array_search(CRM_Core_I18n::NONE, $filter_contact_language)) !== false) { + $w .= "{$tableAlias}.preferred_language IS NULL OR {$tableAlias}.preferred_language = '' OR "; + unset($filter_contact_language[$key]); + } + if (count($filter_contact_language) > 0) { + $w .= "{$tableAlias}.preferred_language IN ('" . implode("','", $filter_contact_language) . "')"; + } + $where[] = "($w)"; } if ($actionSchedule->start_action_date) { @@ -1532,4 +1558,33 @@ WHERE m.owner_membership_id IS NOT NULL AND return $options; } + /** + * @param $communication_language + * @param $preferred_language + */ + public static function setCommunicationLanguage($communication_language, $preferred_language) { + $config = CRM_Core_Config::singleton(); + $language = $config->lcMessages; + + // prepare the language for the email + if ($communication_language == CRM_Core_I18n::AUTO) { + if (!empty($preferred_language)) { + $language = $preferred_language; + } + } + else { + $language = $communication_language; + } + + // language not in the existing language, use default + $locales = CRM_Core_I18n::getLocales(); + if (!in_array($language, $locales)) { + $language = $config->lcMessages; + } + + // change the language + $i18n = CRM_Core_I18n::singleton(); + $i18n->setLanguage($language); + } + } diff --git a/CRM/Core/DAO.php b/CRM/Core/DAO.php index baa3033c9d..df78fbb883 100644 --- a/CRM/Core/DAO.php +++ b/CRM/Core/DAO.php @@ -320,6 +320,7 @@ class CRM_Core_DAO extends DB_DataObject { global $dbLocale; if ($i18nRewrite and $dbLocale) { $query = CRM_Core_I18n_Schema::rewriteQuery($query); +//watchdog('debug', $query); } return parent::query($query); diff --git a/CRM/Core/I18n.php b/CRM/Core/I18n.php index 845e377f02..b4a9a14e2f 100644 --- a/CRM/Core/I18n.php +++ b/CRM/Core/I18n.php @@ -32,6 +32,14 @@ */ class CRM_Core_I18n { + /** + * Constants for communication preferences. + * + * @var int + */ + const NONE = 'none', AUTO = 'auto'; + + /** * A PHP-gettext instance for string translation; * should stay null if the strings are not to be translated (en_US). @@ -520,6 +528,75 @@ class CRM_Core_I18n { return FALSE; } + + /** + * Is the CiviCRM in multilingual mode. + * + * @return Bool + * True if CiviCRM is in multilingual mode. + */ + public static function isMultilingual() { + $domain = new CRM_Core_DAO_Domain(); + $domain->find(TRUE); + return (bool) $domain->locales; + } + + /** + * Get the enabled locales list + * + * @return array + */ + public static function getLocales() { + $domain = new CRM_Core_DAO_Domain(); + $domain->find(TRUE); + return $domain->locales; + } + + /** + * Change the processing language without changing the current user language + * + * @param $language + * Language (for example 'en_US', or 'fr_CA'). + * True if the domain was changed for an extension. + */ + public function setLanguage($language) { + + + $config = CRM_Core_Config::singleton(); + if ($this->_nativegettext) { + $locale = $language . '.utf8'; + putenv("LANG=$locale"); + + setlocale(LC_TIME, $locale); + setlocale(LC_MESSAGES, $locale); + setlocale(LC_CTYPE, $locale); + + bindtextdomain('civicrm', $config->gettextResourceDir); + bind_textdomain_codeset('civicrm', 'UTF-8'); + textdomain('civicrm'); + + $this->_phpgettext = new CRM_Core_I18n_NativeGettext(); + $this->_extensioncache['civicrm'] = 'civicrm'; + } + else { + // phpgettext + require_once 'PHPgettext/streams.php'; + require_once 'PHPgettext/gettext.php'; + + $mo_file = $config->gettextResourceDir . $language . DIRECTORY_SEPARATOR . 'LC_MESSAGES' . DIRECTORY_SEPARATOR . 'civicrm.mo'; + + $streamer = new FileReader($mo_file); + $this->_phpgettext = new gettext_reader($streamer); + $this->_extensioncache['civicrm'] = $this->_phpgettext; + + } + + // for sql queries + global $dbLocale; + $dbLocale = "_{$language}"; + + } + /** * Static instance provider - return the instance for the current locale. * -- 2.25.1