Merge branch 'CRM-14696-v2' of https://github.com/JKingsnorth/civicrm-core into CRM...
[civicrm-core.git] / Civi / API / Subscriber / I18nSubscriber.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
9 | |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
13 | |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
18 | |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
26 */
27
28 namespace Civi\API\Subscriber;
29 use Civi\API\Events;
30 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
31
32 /**
33 * Class I18nSubscriber
34 * @package Civi\API\Subscriber
35 */
36 class I18nSubscriber implements EventSubscriberInterface {
37 /**
38 * @return array
39 */
40 public static function getSubscribedEvents() {
41 return array(
42 Events::PREPARE => array('onApiPrepare', Events::W_MIDDLE)
43 );
44 }
45
46 /**
47 * @param \Civi\API\Event\Event $event
48 *
49 * @throws \API_Exception
50 */
51 public function onApiPrepare(\Civi\API\Event\Event $event) {
52 $apiRequest = $event->getApiRequest();
53
54 // support multi-lingual requests
55 if ($language = \CRM_Utils_Array::value('option.language', $apiRequest['params'])) {
56 $this->setLocale($language);
57 }
58 }
59
60 /**
61 * Sets the tsLocale and dbLocale for multi-lingual sites.
62 * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve()
63 * to avoid regressions from refactoring.
64 */
65 public function setLocale($lcMessagesRequest) {
66 // We must validate whether the locale is valid, otherwise setting a bad
67 // dbLocale could probably lead to sql-injection.
68 $domain = new \CRM_Core_DAO_Domain();
69 $domain->id = \CRM_Core_Config::domainID();
70 $domain->find(TRUE);
71
72 if ($domain->config_backend) {
73 $defaults = unserialize($domain->config_backend);
74
75 // are we in a multi-language setup?
76 $multiLang = $domain->locales ? TRUE : FALSE;
77 $lcMessages = NULL;
78
79 // on multi-lang sites based on request and civicrm_uf_match
80 if ($multiLang) {
81 $languageLimit = array();
82 if (array_key_exists('languageLimit', $defaults) && is_array($defaults['languageLimit'])) {
83 $languageLimit = $defaults['languageLimit'];
84 }
85
86 if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
87 $lcMessages = $lcMessagesRequest;
88 }
89 else {
90 throw new \API_Exception(ts('Language not enabled: %1', array(1 => $lcMessagesRequest)));
91 }
92 }
93
94 global $dbLocale;
95
96 // set suffix for table names - use views if more than one language
97 if ($lcMessages) {
98 $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : '';
99
100 // FIXME: an ugly hack to fix CRM-4041
101 global $tsLocale;
102 $tsLocale = $lcMessages;
103 }
104 }
105 }
106 }