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