3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
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 +--------------------------------------------------------------------+
15 * @copyright CiviCRM LLC https://civicrm.org/licensing
19 * Class CRM_Core_BAO_WordReplacement.
21 class CRM_Core_BAO_WordReplacement
extends CRM_Core_DAO_WordReplacement
{
26 public function __construct() {
27 parent
::__construct();
31 * Function that must have never worked & should be removed.
33 * Retrieve DB object based on input parameters.
35 * It also stores all the retrieved values in the default array.
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.
42 * @return CRM_Core_DAO_WordReplacement
44 public static function retrieve(&$params, &$defaults) {
45 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
53 * @return null|CRM_Core_BAO_WordReplacement
54 * @throws CRM_Core_Exception
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');
65 return $wordReplacement;
69 * Save the values of a WordReplacement.
71 * @param array $params
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)) {
84 return $wordReplacement;
88 * Create a new WordReplacement.
90 * @param array $params
94 public static function create($params) {
95 if (array_key_exists("domain_id", $params) === FALSE) {
96 $params["domain_id"] = CRM_Core_Config
::domainID();
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)) {
104 return $wordReplacement;
111 * WordReplacement id.
115 public static function del($id) {
116 $dao = new CRM_Core_DAO_WordReplacement();
119 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
126 * Get all word-replacements in the form of an array.
132 * @see civicrm_domain.locale_custom_strings
134 public static function getAllAsConfigArray($id) {
136 SELECT find_word,replace_word,is_active,match_type
137 FROM civicrm_word_replacement
140 $params = [1 => [$id, 'Integer']];
142 $dao = CRM_Core_DAO
::executeQuery($query, $params);
146 while ($dao->fetch()) {
147 if ($dao->is_active
== 1) {
148 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
151 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
154 $config = CRM_Core_Config
::singleton();
156 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
157 // data isn't really stored in civicrm_word_replacements. Probably
159 $stringOverride = self
::_getLocaleCustomStrings($id);
160 $stringOverride[$config->lcMessages
] = $overrides;
162 return $stringOverride;
168 * @param bool $clearCaches
172 public static function rebuild($clearCaches = TRUE) {
173 $id = CRM_Core_Config
::domainID();
174 self
::_setLocaleCustomStrings($id, self
::getAllAsConfigArray($id));
176 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
179 CRM_Core_BAO_Navigation
::resetNavigation();
180 // Clear js localization
181 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
188 * Get word replacements for the api.
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.
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.
200 * @param bool $rebuildEach
201 * Whether to perform rebuild after each individual API call.
204 * Each item is $params for WordReplacement.create
205 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
207 public static function getConfigArraysAsAPIParams($rebuildEach) {
208 $settingsResult = civicrm_api3('Setting', 'get', [
209 'return' => 'lcMessages',
211 $returnValues = CRM_Utils_Array
::first($settingsResult['values']);
212 $lang = $returnValues['lcMessages'];
214 $wordReplacementCreateParams = [];
216 $result = civicrm_api3('domain', 'get', [
217 'return' => ['locale_custom_strings'],
219 if (!empty($result["values"])) {
220 foreach ($result["values"] as $value) {
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;
248 return $wordReplacementCreateParams;
252 * Rebuild word replacements.
254 * Get all the word-replacements stored in config-arrays
255 * and write them out as records in civicrm_word_replacement.
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.
263 public static function rebuildWordReplacementTable() {
264 civicrm_api3('word_replacement', 'replace', [
265 'options' => ['match' => ['domain_id', 'find_word']],
266 'values' => self
::getConfigArraysAsAPIParams(FALSE),
268 CRM_Core_BAO_WordReplacement
::rebuild();
272 * Get WordReplacements for a locale.
274 * @param string $locale
275 * @param int $domainId
278 * List of word replacements (enabled/disabled) for the given locale.
280 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
281 if ($domainId === NULL) {
282 $domainId = CRM_Core_Config
::domainID();
285 return CRM_Utils_Array
::value($locale, self
::_getLocaleCustomStrings($domainId));
289 * Get custom locale strings.
291 * @param int $domainId
293 * @return array|mixed
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'],
300 while ($domain->fetch()) {
301 return empty($domain->locale_custom_strings
) ?
[] : CRM_Utils_String
::unserialize($domain->locale_custom_strings
);
306 * Set locale strings.
308 * @param string $locale
309 * @param array $values
310 * @param int $domainId
312 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
313 if ($domainId === NULL) {
314 $domainId = CRM_Core_Config
::domainID();
317 $lcs = self
::_getLocaleCustomStrings($domainId);
318 $lcs[$locale] = $values;
320 self
::_setLocaleCustomStrings($domainId, $lcs);
324 * Set locale strings.
326 * @param int $domainId
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'],