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