Merge pull request #19017 from eileenmcnaughton/remove_recur
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
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 |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 *
14 * @package CRM
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
16 */
17
18 /**
19 * Class CRM_Core_BAO_WordReplacement.
20 */
21 class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
22
23 /**
24 * Class constructor.
25 */
26 public function __construct() {
27 parent::__construct();
28 }
29
30 /**
31 * Function that must have never worked & should be removed.
32 *
33 * Retrieve DB object based on input parameters.
34 *
35 * It also stores all the retrieved values in the default array.
36 *
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.
41 *
42 * @return CRM_Core_DAO_WordReplacement
43 */
44 public static function retrieve(&$params, &$defaults) {
45 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
46 }
47
48 /**
49 * Get the domain BAO.
50 *
51 * @param null $reset
52 *
53 * @return null|CRM_Core_BAO_WordReplacement
54 * @throws CRM_Core_Exception
55 */
56 public static function getWordReplacement($reset = NULL) {
57 static $wordReplacement = NULL;
58 if (!$wordReplacement || $reset) {
59 $wordReplacement = new CRM_Core_BAO_WordReplacement();
60 $wordReplacement->id = CRM_Core_Config::wordReplacementID();
61 if (!$wordReplacement->find(TRUE)) {
62 throw new CRM_Core_Exception('Unable to find word replacement');
63 }
64 }
65 return $wordReplacement;
66 }
67
68 /**
69 * Save the values of a WordReplacement.
70 *
71 * @param array $params
72 * @param int $id
73 *
74 * @return array
75 */
76 public static function edit(&$params, &$id) {
77 $wordReplacement = new CRM_Core_DAO_WordReplacement();
78 $wordReplacement->id = $id;
79 $wordReplacement->copyValues($params);
80 $wordReplacement->save();
81 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
82 self::rebuild();
83 }
84 return $wordReplacement;
85 }
86
87 /**
88 * Create a new WordReplacement.
89 *
90 * @param array $params
91 *
92 * @return array
93 */
94 public static function create($params) {
95 if (array_key_exists("domain_id", $params) === FALSE) {
96 $params["domain_id"] = CRM_Core_Config::domainID();
97 }
98 $wordReplacement = new CRM_Core_DAO_WordReplacement();
99 $wordReplacement->copyValues($params);
100 $wordReplacement->save();
101 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
102 self::rebuild();
103 }
104 return $wordReplacement;
105 }
106
107 /**
108 * Delete website.
109 *
110 * @param int $id
111 * WordReplacement id.
112 *
113 * @return object
114 */
115 public static function del($id) {
116 $dao = new CRM_Core_DAO_WordReplacement();
117 $dao->id = $id;
118 $dao->delete();
119 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
120 self::rebuild();
121 }
122 return $dao;
123 }
124
125 /**
126 * Get all word-replacements in the form of an array.
127 *
128 * @param int $id
129 * Domain ID.
130 *
131 * @return array
132 * @see civicrm_domain.locale_custom_strings
133 */
134 public static function getAllAsConfigArray($id) {
135 $query = "
136 SELECT find_word,replace_word,is_active,match_type
137 FROM civicrm_word_replacement
138 WHERE domain_id = %1
139 ";
140 $params = [1 => [$id, 'Integer']];
141
142 $dao = CRM_Core_DAO::executeQuery($query, $params);
143
144 $overrides = [];
145
146 while ($dao->fetch()) {
147 if ($dao->is_active == 1) {
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;
152 }
153 }
154 $config = CRM_Core_Config::singleton();
155
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;
161
162 return $stringOverride;
163 }
164
165 /**
166 * Rebuild.
167 *
168 * @param bool $clearCaches
169 *
170 * @return bool
171 */
172 public static function rebuild($clearCaches = TRUE) {
173 $id = CRM_Core_Config::domainID();
174 self::_setLocaleCustomStrings($id, self::getAllAsConfigArray($id));
175
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();
182 }
183
184 return TRUE;
185 }
186
187 /**
188 * Get word replacements for the api.
189 *
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.
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
198 * bug-fix in both places.
199 *
200 * @param bool $rebuildEach
201 * Whether to perform rebuild after each individual API call.
202 *
203 * @return array
204 * Each item is $params for WordReplacement.create
205 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
206 */
207 public static function getConfigArraysAsAPIParams($rebuildEach) {
208 $settingsResult = civicrm_api3('Setting', 'get', [
209 'return' => 'lcMessages',
210 ]);
211 $returnValues = CRM_Utils_Array::first($settingsResult['values']);
212 $lang = $returnValues['lcMessages'];
213
214 $wordReplacementCreateParams = [];
215 // get all domains
216 $result = civicrm_api3('domain', 'get', [
217 'return' => ['locale_custom_strings'],
218 ]);
219 if (!empty($result["values"])) {
220 foreach ($result["values"] as $value) {
221 $params = [];
222 $params["domain_id"] = $value["id"];
223 $params["options"] = ['wp-rebuild' => $rebuildEach];
224 // Unserialize word match string.
225 $localeCustomArray = CRM_Utils_String::unserialize($value["locale_custom_strings"]);
226 if (!empty($localeCustomArray)) {
227 $wordMatchArray = [];
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) {
234 $params["is_active"] = $status == "enabled";
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;
242 }
243 }
244 }
245 }
246 }
247 }
248 return $wordReplacementCreateParams;
249 }
250
251 /**
252 * Rebuild word replacements.
253 *
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
261 * bug-fix in both places.
262 */
263 public static function rebuildWordReplacementTable() {
264 civicrm_api3('word_replacement', 'replace', [
265 'options' => ['match' => ['domain_id', 'find_word']],
266 'values' => self::getConfigArraysAsAPIParams(FALSE),
267 ]);
268 CRM_Core_BAO_WordReplacement::rebuild();
269 }
270
271 /**
272 * Get WordReplacements for a locale.
273 *
274 * @param string $locale
275 * @param int $domainId
276 *
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
288 /**
289 * Get custom locale strings.
290 *
291 * @param int $domainId
292 *
293 * @return array|mixed
294 */
295 private static function _getLocaleCustomStrings($domainId) {
296 // TODO: Would it be worthwhile using memcache here?
297 $domain = CRM_Core_DAO::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', [
298 1 => [$domainId, 'Integer'],
299 ]);
300 while ($domain->fetch()) {
301 return empty($domain->locale_custom_strings) ? [] : CRM_Utils_String::unserialize($domain->locale_custom_strings);
302 }
303 }
304
305 /**
306 * Set locale strings.
307 *
308 * @param string $locale
309 * @param array $values
310 * @param int $domainId
311 */
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 /**
324 * Set locale strings.
325 *
326 * @param int $domainId
327 * @param string $lcs
328 */
329 private static function _setLocaleCustomStrings($domainId, $lcs) {
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 ]);
334 }
335
336 }