3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
7 +--------------------------------------------------------------------+
8 | This file is a part of CiviCRM. |
10 | CiviCRM is free software; you can copy, modify, and distribute it |
11 | under the terms of the GNU Affero General Public License |
12 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
14 | CiviCRM is distributed in the hope that it will be useful, but |
15 | WITHOUT ANY WARRANTY; without even the implied warranty of |
16 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
17 | See the GNU Affero General Public License for more details. |
19 | You should have received a copy of the GNU Affero General Public |
20 | License and the CiviCRM Licensing Exception along |
21 | with this program; if not, contact CiviCRM LLC |
22 | at info[AT]civicrm[DOT]org. If you have questions about the |
23 | GNU Affero General Public License or the licensing of CiviCRM, |
24 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
25 +--------------------------------------------------------------------+
31 * @copyright CiviCRM LLC (c) 2004-2019
35 * Class CRM_Core_BAO_WordReplacement.
37 class CRM_Core_BAO_WordReplacement
extends CRM_Core_DAO_WordReplacement
{
42 public function __construct() {
43 parent
::__construct();
47 * Function that must have never worked & should be removed.
49 * Retrieve DB object based on input parameters.
51 * It also stores all the retrieved values in the default array.
53 * @param array $params
54 * (reference ) an assoc array of name/value pairs.
55 * @param array $defaults
56 * (reference ) an assoc array to hold the flattened values.
58 * @return CRM_Core_DAO_WordReplacement
60 public static function retrieve(&$params, &$defaults) {
61 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
69 * @return null|CRM_Core_BAO_WordReplacement
71 public static function getWordReplacement($reset = NULL) {
72 static $wordReplacement = NULL;
73 if (!$wordReplacement ||
$reset) {
74 $wordReplacement = new CRM_Core_BAO_WordReplacement();
75 $wordReplacement->id
= CRM_Core_Config
::wordReplacementID();
76 if (!$wordReplacement->find(TRUE)) {
77 CRM_Core_Error
::fatal();
80 return $wordReplacement;
84 * Save the values of a WordReplacement.
86 * @param array $params
91 public static function edit(&$params, &$id) {
92 $wordReplacement = new CRM_Core_DAO_WordReplacement();
93 $wordReplacement->id
= $id;
94 $wordReplacement->copyValues($params);
95 $wordReplacement->save();
96 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
99 return $wordReplacement;
103 * Create a new WordReplacement.
105 * @param array $params
109 public static function create($params) {
110 if (array_key_exists("domain_id", $params) === FALSE) {
111 $params["domain_id"] = CRM_Core_Config
::domainID();
113 $wordReplacement = new CRM_Core_DAO_WordReplacement();
114 $wordReplacement->copyValues($params);
115 $wordReplacement->save();
116 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
119 return $wordReplacement;
126 * WordReplacement id.
130 public static function del($id) {
131 $dao = new CRM_Core_DAO_WordReplacement();
134 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
141 * Get all word-replacements in the form of an array.
147 * @see civicrm_domain.locale_custom_strings
149 public static function getAllAsConfigArray($id) {
151 SELECT find_word,replace_word,is_active,match_type
152 FROM civicrm_word_replacement
155 $params = [1 => [$id, 'Integer']];
157 $dao = CRM_Core_DAO
::executeQuery($query, $params);
161 while ($dao->fetch()) {
162 if ($dao->is_active
== 1) {
163 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
166 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
169 $config = CRM_Core_Config
::singleton();
170 $domain = new CRM_Core_DAO_Domain();
173 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
174 // data isn't really stored in civicrm_word_replacements. Probably
176 $stringOverride = self
::_getLocaleCustomStrings($id);
177 $stringOverride[$config->lcMessages
] = $overrides;
179 return $stringOverride;
185 * @param bool $clearCaches
189 public static function rebuild($clearCaches = TRUE) {
190 $id = CRM_Core_Config
::domainID();
191 self
::_setLocaleCustomStrings($id, self
::getAllAsConfigArray($id));
193 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
196 CRM_Core_BAO_Navigation
::resetNavigation();
197 // Clear js localization
198 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
205 * Get word replacements for the api.
207 * Get all the word-replacements stored in config-arrays for the
208 * configured language, and convert them to params for the
209 * WordReplacement.create API.
211 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
212 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
213 * step behaves consistently even as the BAO evolves in future versions.
214 * However, if there's a bug in here prior to 4.4.0, we should apply the
215 * bug-fix in both places.
217 * @param bool $rebuildEach
218 * Whether to perform rebuild after each individual API call.
221 * Each item is $params for WordReplacement.create
222 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
224 public static function getConfigArraysAsAPIParams($rebuildEach) {
225 $settingsResult = civicrm_api3('Setting', 'get', [
226 'return' => 'lcMessages',
228 $returnValues = CRM_Utils_Array
::first($settingsResult['values']);
229 $lang = $returnValues['lcMessages'];
231 $wordReplacementCreateParams = [];
233 $result = civicrm_api3('domain', 'get', [
234 'return' => ['locale_custom_strings'],
236 if (!empty($result["values"])) {
237 foreach ($result["values"] as $value) {
239 $params["domain_id"] = $value["id"];
240 $params["options"] = ['wp-rebuild' => $rebuildEach];
241 // Unserialize word match string.
242 $localeCustomArray = unserialize($value["locale_custom_strings"]);
243 if (!empty($localeCustomArray)) {
244 $wordMatchArray = [];
245 // Only return the replacement strings of the current language,
246 // otherwise some replacements will be duplicated, which will
247 // lead to undesired results, like CRM-19683.
248 $localCustomData = $localeCustomArray[$lang];
249 // Traverse status array "enabled" "disabled"
250 foreach ($localCustomData as $status => $matchTypes) {
251 $params["is_active"] = ($status == "enabled") ?
TRUE : FALSE;
252 // Traverse Match Type array "wildcardMatch" "exactMatch"
253 foreach ($matchTypes as $matchType => $words) {
254 $params["match_type"] = $matchType;
255 foreach ($words as $word => $replace) {
256 $params["find_word"] = $word;
257 $params["replace_word"] = $replace;
258 $wordReplacementCreateParams[] = $params;
265 return $wordReplacementCreateParams;
269 * Rebuild word replacements.
271 * Get all the word-replacements stored in config-arrays
272 * and write them out as records in civicrm_word_replacement.
274 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
275 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
276 * step behaves consistently even as the BAO evolves in future versions.
277 * However, if there's a bug in here prior to 4.4.0, we should apply the
278 * bug-fix in both places.
280 public static function rebuildWordReplacementTable() {
281 civicrm_api3('word_replacement', 'replace', [
282 'options' => ['match' => ['domain_id', 'find_word']],
283 'values' => self
::getConfigArraysAsAPIParams(FALSE),
285 CRM_Core_BAO_WordReplacement
::rebuild();
289 * Get WordReplacements for a locale.
291 * @param string $locale
292 * @param int $domainId
295 * List of word replacements (enabled/disabled) for the given locale.
297 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
298 if ($domainId === NULL) {
299 $domainId = CRM_Core_Config
::domainID();
302 return CRM_Utils_Array
::value($locale, self
::_getLocaleCustomStrings($domainId));
306 * Get custom locale strings.
308 * @param int $domainId
310 * @return array|mixed
312 private static function _getLocaleCustomStrings($domainId) {
313 // TODO: Would it be worthwhile using memcache here?
314 $domain = CRM_Core_DAO
::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', [
315 1 => [$domainId, 'Integer'],
317 while ($domain->fetch()) {
318 return empty($domain->locale_custom_strings
) ?
[] : unserialize($domain->locale_custom_strings
);
323 * Set locale strings.
325 * @param string $locale
326 * @param array $values
327 * @param int $domainId
329 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
330 if ($domainId === NULL) {
331 $domainId = CRM_Core_Config
::domainID();
334 $lcs = self
::_getLocaleCustomStrings($domainId);
335 $lcs[$locale] = $values;
337 self
::_setLocaleCustomStrings($domainId, $lcs);
341 * Set locale strings.
343 * @param int $domainId
346 private static function _setLocaleCustomStrings($domainId, $lcs) {
347 CRM_Core_DAO
::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", [
348 1 => [serialize($lcs), 'String'],
349 2 => [$domainId, 'Integer'],