Merge pull request #14222 from mfb/debug-var
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.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 /**
29 *
30 * @package CRM
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Class CRM_Core_BAO_WordReplacement.
36 */
37 class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 parent::__construct();
44 }
45
46 /**
47 * Function that must have never worked & should be removed.
48 *
49 * Retrieve DB object based on input parameters.
50 *
51 * It also stores all the retrieved values in the default array.
52 *
53 * @param array $params
54 * (reference ) an assoc array of name/value pairs.
55 * @param array $defaults
56 * (reference ) an assoc array to hold the flattened values.
57 *
58 * @return CRM_Core_DAO_WordReplacement
59 */
60 public static function retrieve(&$params, &$defaults) {
61 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
62 }
63
64 /**
65 * Get the domain BAO.
66 *
67 * @param null $reset
68 *
69 * @return null|CRM_Core_BAO_WordReplacement
70 */
71 public static function getWordReplacement($reset = NULL) {
72 static $wordReplacement = NULL;
73 if (!$wordReplacement || $reset) {
74 $wordReplacement = new CRM_Core_BAO_WordReplacement();
75 $wordReplacement->id = CRM_Core_Config::wordReplacementID();
76 if (!$wordReplacement->find(TRUE)) {
77 CRM_Core_Error::fatal();
78 }
79 }
80 return $wordReplacement;
81 }
82
83 /**
84 * Save the values of a WordReplacement.
85 *
86 * @param array $params
87 * @param int $id
88 *
89 * @return array
90 */
91 public static function edit(&$params, &$id) {
92 $wordReplacement = new CRM_Core_DAO_WordReplacement();
93 $wordReplacement->id = $id;
94 $wordReplacement->copyValues($params);
95 $wordReplacement->save();
96 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
97 self::rebuild();
98 }
99 return $wordReplacement;
100 }
101
102 /**
103 * Create a new WordReplacement.
104 *
105 * @param array $params
106 *
107 * @return array
108 */
109 public static function create($params) {
110 if (array_key_exists("domain_id", $params) === FALSE) {
111 $params["domain_id"] = CRM_Core_Config::domainID();
112 }
113 $wordReplacement = new CRM_Core_DAO_WordReplacement();
114 $wordReplacement->copyValues($params);
115 $wordReplacement->save();
116 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
117 self::rebuild();
118 }
119 return $wordReplacement;
120 }
121
122 /**
123 * Delete website.
124 *
125 * @param int $id
126 * WordReplacement id.
127 *
128 * @return object
129 */
130 public static function del($id) {
131 $dao = new CRM_Core_DAO_WordReplacement();
132 $dao->id = $id;
133 $dao->delete();
134 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
135 self::rebuild();
136 }
137 return $dao;
138 }
139
140 /**
141 * Get all word-replacements in the form of an array.
142 *
143 * @param int $id
144 * Domain ID.
145 *
146 * @return array
147 * @see civicrm_domain.locale_custom_strings
148 */
149 public static function getAllAsConfigArray($id) {
150 $query = "
151 SELECT find_word,replace_word,is_active,match_type
152 FROM civicrm_word_replacement
153 WHERE domain_id = %1
154 ";
155 $params = [1 => [$id, 'Integer']];
156
157 $dao = CRM_Core_DAO::executeQuery($query, $params);
158
159 $overrides = [];
160
161 while ($dao->fetch()) {
162 if ($dao->is_active == 1) {
163 $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
164 }
165 else {
166 $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
167 }
168 }
169 $config = CRM_Core_Config::singleton();
170 $domain = new CRM_Core_DAO_Domain();
171 $domain->find(TRUE);
172
173 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
174 // data isn't really stored in civicrm_word_replacements. Probably
175 // shouldn't exist.
176 $stringOverride = self::_getLocaleCustomStrings($id);
177 $stringOverride[$config->lcMessages] = $overrides;
178
179 return $stringOverride;
180 }
181
182 /**
183 * Rebuild.
184 *
185 * @param bool $clearCaches
186 *
187 * @return bool
188 */
189 public static function rebuild($clearCaches = TRUE) {
190 $id = CRM_Core_Config::domainID();
191 self::_setLocaleCustomStrings($id, self::getAllAsConfigArray($id));
192
193 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
194 if ($clearCaches) {
195 // Reset navigation
196 CRM_Core_BAO_Navigation::resetNavigation();
197 // Clear js localization
198 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
199 }
200
201 return TRUE;
202 }
203
204 /**
205 * Get word replacements for the api.
206 *
207 * Get all the word-replacements stored in config-arrays for the
208 * configured language, and convert them to params for the
209 * WordReplacement.create API.
210 *
211 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
212 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
213 * step behaves consistently even as the BAO evolves in future versions.
214 * However, if there's a bug in here prior to 4.4.0, we should apply the
215 * bug-fix in both places.
216 *
217 * @param bool $rebuildEach
218 * Whether to perform rebuild after each individual API call.
219 *
220 * @return array
221 * Each item is $params for WordReplacement.create
222 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
223 */
224 public static function getConfigArraysAsAPIParams($rebuildEach) {
225 $settingsResult = civicrm_api3('Setting', 'get', [
226 'return' => 'lcMessages',
227 ]);
228 $returnValues = CRM_Utils_Array::first($settingsResult['values']);
229 $lang = $returnValues['lcMessages'];
230
231 $wordReplacementCreateParams = [];
232 // get all domains
233 $result = civicrm_api3('domain', 'get', [
234 'return' => ['locale_custom_strings'],
235 ]);
236 if (!empty($result["values"])) {
237 foreach ($result["values"] as $value) {
238 $params = [];
239 $params["domain_id"] = $value["id"];
240 $params["options"] = ['wp-rebuild' => $rebuildEach];
241 // Unserialize word match string.
242 $localeCustomArray = unserialize($value["locale_custom_strings"]);
243 if (!empty($localeCustomArray)) {
244 $wordMatchArray = [];
245 // Only return the replacement strings of the current language,
246 // otherwise some replacements will be duplicated, which will
247 // lead to undesired results, like CRM-19683.
248 $localCustomData = $localeCustomArray[$lang];
249 // Traverse status array "enabled" "disabled"
250 foreach ($localCustomData as $status => $matchTypes) {
251 $params["is_active"] = ($status == "enabled") ? TRUE : FALSE;
252 // Traverse Match Type array "wildcardMatch" "exactMatch"
253 foreach ($matchTypes as $matchType => $words) {
254 $params["match_type"] = $matchType;
255 foreach ($words as $word => $replace) {
256 $params["find_word"] = $word;
257 $params["replace_word"] = $replace;
258 $wordReplacementCreateParams[] = $params;
259 }
260 }
261 }
262 }
263 }
264 }
265 return $wordReplacementCreateParams;
266 }
267
268 /**
269 * Rebuild word replacements.
270 *
271 * Get all the word-replacements stored in config-arrays
272 * and write them out as records in civicrm_word_replacement.
273 *
274 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
275 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
276 * step behaves consistently even as the BAO evolves in future versions.
277 * However, if there's a bug in here prior to 4.4.0, we should apply the
278 * bug-fix in both places.
279 */
280 public static function rebuildWordReplacementTable() {
281 civicrm_api3('word_replacement', 'replace', [
282 'options' => ['match' => ['domain_id', 'find_word']],
283 'values' => self::getConfigArraysAsAPIParams(FALSE),
284 ]);
285 CRM_Core_BAO_WordReplacement::rebuild();
286 }
287
288 /**
289 * Get WordReplacements for a locale.
290 *
291 * @param string $locale
292 * @param int $domainId
293 *
294 * @return array
295 * List of word replacements (enabled/disabled) for the given locale.
296 */
297 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
298 if ($domainId === NULL) {
299 $domainId = CRM_Core_Config::domainID();
300 }
301
302 return CRM_Utils_Array::value($locale, self::_getLocaleCustomStrings($domainId));
303 }
304
305 /**
306 * Get custom locale strings.
307 *
308 * @param int $domainId
309 *
310 * @return array|mixed
311 */
312 private static function _getLocaleCustomStrings($domainId) {
313 // TODO: Would it be worthwhile using memcache here?
314 $domain = CRM_Core_DAO::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', [
315 1 => [$domainId, 'Integer'],
316 ]);
317 while ($domain->fetch()) {
318 return empty($domain->locale_custom_strings) ? [] : unserialize($domain->locale_custom_strings);
319 }
320 }
321
322 /**
323 * Set locale strings.
324 *
325 * @param string $locale
326 * @param array $values
327 * @param int $domainId
328 */
329 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
330 if ($domainId === NULL) {
331 $domainId = CRM_Core_Config::domainID();
332 }
333
334 $lcs = self::_getLocaleCustomStrings($domainId);
335 $lcs[$locale] = $values;
336
337 self::_setLocaleCustomStrings($domainId, $lcs);
338 }
339
340 /**
341 * Set locale strings.
342 *
343 * @param int $domainId
344 * @param string $lcs
345 */
346 private static function _setLocaleCustomStrings($domainId, $lcs) {
347 CRM_Core_DAO::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", [
348 1 => [serialize($lcs), 'String'],
349 2 => [$domainId, 'Integer'],
350 ]);
351 }
352
353 }