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
55 public static function getWordReplacement($reset = NULL) {
56 static $wordReplacement = NULL;
57 if (!$wordReplacement ||
$reset) {
58 $wordReplacement = new CRM_Core_BAO_WordReplacement();
59 $wordReplacement->id
= CRM_Core_Config
::wordReplacementID();
60 if (!$wordReplacement->find(TRUE)) {
61 CRM_Core_Error
::fatal();
64 return $wordReplacement;
68 * Save the values of a WordReplacement.
70 * @param array $params
75 public static function edit(&$params, &$id) {
76 $wordReplacement = new CRM_Core_DAO_WordReplacement();
77 $wordReplacement->id
= $id;
78 $wordReplacement->copyValues($params);
79 $wordReplacement->save();
80 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
83 return $wordReplacement;
87 * Create a new WordReplacement.
89 * @param array $params
93 public static function create($params) {
94 if (array_key_exists("domain_id", $params) === FALSE) {
95 $params["domain_id"] = CRM_Core_Config
::domainID();
97 $wordReplacement = new CRM_Core_DAO_WordReplacement();
98 $wordReplacement->copyValues($params);
99 $wordReplacement->save();
100 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
103 return $wordReplacement;
110 * WordReplacement id.
114 public static function del($id) {
115 $dao = new CRM_Core_DAO_WordReplacement();
118 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
125 * Get all word-replacements in the form of an array.
131 * @see civicrm_domain.locale_custom_strings
133 public static function getAllAsConfigArray($id) {
135 SELECT find_word,replace_word,is_active,match_type
136 FROM civicrm_word_replacement
139 $params = [1 => [$id, 'Integer']];
141 $dao = CRM_Core_DAO
::executeQuery($query, $params);
145 while ($dao->fetch()) {
146 if ($dao->is_active
== 1) {
147 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
150 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
153 $config = CRM_Core_Config
::singleton();
154 $domain = new CRM_Core_DAO_Domain();
157 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
158 // data isn't really stored in civicrm_word_replacements. Probably
160 $stringOverride = self
::_getLocaleCustomStrings($id);
161 $stringOverride[$config->lcMessages
] = $overrides;
163 return $stringOverride;
169 * @param bool $clearCaches
173 public static function rebuild($clearCaches = TRUE) {
174 $id = CRM_Core_Config
::domainID();
175 self
::_setLocaleCustomStrings($id, self
::getAllAsConfigArray($id));
177 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
180 CRM_Core_BAO_Navigation
::resetNavigation();
181 // Clear js localization
182 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
189 * Get word replacements for the api.
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.
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
199 * bug-fix in both places.
201 * @param bool $rebuildEach
202 * Whether to perform rebuild after each individual API call.
205 * Each item is $params for WordReplacement.create
206 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
208 public static function getConfigArraysAsAPIParams($rebuildEach) {
209 $settingsResult = civicrm_api3('Setting', 'get', [
210 'return' => 'lcMessages',
212 $returnValues = CRM_Utils_Array
::first($settingsResult['values']);
213 $lang = $returnValues['lcMessages'];
215 $wordReplacementCreateParams = [];
217 $result = civicrm_api3('domain', 'get', [
218 'return' => ['locale_custom_strings'],
220 if (!empty($result["values"])) {
221 foreach ($result["values"] as $value) {
223 $params["domain_id"] = $value["id"];
224 $params["options"] = ['wp-rebuild' => $rebuildEach];
225 // Unserialize word match string.
226 $localeCustomArray = CRM_Utils_String
::unserialize($value["locale_custom_strings"]);
227 if (!empty($localeCustomArray)) {
228 $wordMatchArray = [];
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;
249 return $wordReplacementCreateParams;
253 * Rebuild word replacements.
255 * Get all the word-replacements stored in config-arrays
256 * and write them out as records in civicrm_word_replacement.
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
262 * bug-fix in both places.
264 public static function rebuildWordReplacementTable() {
265 civicrm_api3('word_replacement', 'replace', [
266 'options' => ['match' => ['domain_id', 'find_word']],
267 'values' => self
::getConfigArraysAsAPIParams(FALSE),
269 CRM_Core_BAO_WordReplacement
::rebuild();
273 * Get WordReplacements for a locale.
275 * @param string $locale
276 * @param int $domainId
279 * List of word replacements (enabled/disabled) for the given locale.
281 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
282 if ($domainId === NULL) {
283 $domainId = CRM_Core_Config
::domainID();
286 return CRM_Utils_Array
::value($locale, self
::_getLocaleCustomStrings($domainId));
290 * Get custom locale strings.
292 * @param int $domainId
294 * @return array|mixed
296 private static function _getLocaleCustomStrings($domainId) {
297 // TODO: Would it be worthwhile using memcache here?
298 $domain = CRM_Core_DAO
::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', [
299 1 => [$domainId, 'Integer'],
301 while ($domain->fetch()) {
302 return empty($domain->locale_custom_strings
) ?
[] : CRM_Utils_String
::unserialize($domain->locale_custom_strings
);
307 * Set locale strings.
309 * @param string $locale
310 * @param array $values
311 * @param int $domainId
313 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
314 if ($domainId === NULL) {
315 $domainId = CRM_Core_Config
::domainID();
318 $lcs = self
::_getLocaleCustomStrings($domainId);
319 $lcs[$locale] = $values;
321 self
::_setLocaleCustomStrings($domainId, $lcs);
325 * Set locale strings.
327 * @param int $domainId
330 private static function _setLocaleCustomStrings($domainId, $lcs) {
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'],