Merge pull request #13980 from seamuslee001/new_coder_style_check_api
[civicrm-core.git] / Civi / API / Subscriber / I18nSubscriber.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
30 use Civi\API\Events;
31 use Symfony\Component\EventDispatcher\EventSubscriberInterface;
32
33 /**
34 * Class I18nSubscriber
35 * @package Civi\API\Subscriber
36 */
37 class I18nSubscriber implements EventSubscriberInterface {
38
39 /**
40 * @return array
41 */
42 public static function getSubscribedEvents() {
43 return [
44 Events::PREPARE => ['onApiPrepare', Events::W_MIDDLE],
45 ];
46 }
47
48 /**
49 * @param \Civi\API\Event\Event $event
50 * API preparation event.
51 *
52 * @throws \API_Exception
53 */
54 public function onApiPrepare(\Civi\API\Event\Event $event) {
55 $apiRequest = $event->getApiRequest();
56
57 // support multi-lingual requests
58 if ($language = \CRM_Utils_Array::value('option.language', $apiRequest['params'])) {
59 $this->setLocale($language);
60 }
61 }
62
63 /**
64 * Sets the tsLocale and dbLocale for multi-lingual sites.
65 * Some code duplication from CRM/Core/BAO/ConfigSetting.php retrieve()
66 * to avoid regressions from refactoring.
67 * @param $lcMessagesRequest
68 * @throws \API_Exception
69 */
70 public function setLocale($lcMessagesRequest) {
71 // We must validate whether the locale is valid, otherwise setting a bad
72 // dbLocale could probably lead to sql-injection.
73 $domain = new \CRM_Core_DAO_Domain();
74 $domain->id = \CRM_Core_Config::domainID();
75 $domain->find(TRUE);
76
77 // are we in a multi-language setup?
78 $multiLang = $domain->locales ? TRUE : FALSE;
79 $lcMessages = NULL;
80
81 // on multi-lang sites based on request and civicrm_uf_match
82 if ($multiLang) {
83 $config = \CRM_Core_Config::singleton();
84 $languageLimit = [];
85 if (isset($config->languageLimit) and $config->languageLimit) {
86 $languageLimit = $config->languageLimit;
87 }
88
89 if (in_array($lcMessagesRequest, array_keys($languageLimit))) {
90 $lcMessages = $lcMessagesRequest;
91 }
92 else {
93 throw new \API_Exception(ts('Language not enabled: %1', [1 => $lcMessagesRequest]));
94 }
95 }
96
97 global $dbLocale;
98
99 // set suffix for table names - use views if more than one language
100 if ($lcMessages) {
101 $dbLocale = $multiLang && $lcMessages ? "_{$lcMessages}" : '';
102
103 // FIXME: an ugly hack to fix CRM-4041
104 global $tsLocale;
105 $tsLocale = $lcMessages;
106 }
107 }
108
109 }