3 +--------------------------------------------------------------------+
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2018 |
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-2018
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;
85 * Save the values of a WordReplacement.
87 * @param array $params
92 public static function edit(&$params, &$id) {
93 $wordReplacement = new CRM_Core_DAO_WordReplacement();
94 $wordReplacement->id
= $id;
95 $wordReplacement->copyValues($params);
96 $wordReplacement->save();
97 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
100 return $wordReplacement;
104 * Create a new WordReplacement.
106 * @param array $params
110 public static function create($params) {
111 if (array_key_exists("domain_id", $params) === FALSE) {
112 $params["domain_id"] = CRM_Core_Config
::domainID();
114 $wordReplacement = new CRM_Core_DAO_WordReplacement();
115 $wordReplacement->copyValues($params);
116 $wordReplacement->save();
117 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
120 return $wordReplacement;
127 * WordReplacement id.
131 public static function del($id) {
132 $dao = new CRM_Core_DAO_WordReplacement();
135 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
142 * Get all word-replacements in the form of an array.
148 * @see civicrm_domain.locale_custom_strings
150 public static function getAllAsConfigArray($id) {
152 SELECT find_word,replace_word,is_active,match_type
153 FROM civicrm_word_replacement
156 $params = array(1 => array($id, 'Integer'));
158 $dao = CRM_Core_DAO
::executeQuery($query, $params);
160 $overrides = array();
162 while ($dao->fetch()) {
163 if ($dao->is_active
== 1) {
164 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
167 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
170 $config = CRM_Core_Config
::singleton();
171 $domain = new CRM_Core_DAO_Domain();
174 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
175 // data isn't really stored in civicrm_word_replacements. Probably
177 $stringOverride = self
::_getLocaleCustomStrings($id);
178 $stringOverride[$config->lcMessages
] = $overrides;
180 return $stringOverride;
186 * @param bool $clearCaches
190 public static function rebuild($clearCaches = TRUE) {
191 $id = CRM_Core_Config
::domainID();
192 self
::_setLocaleCustomStrings($id, self
::getAllAsConfigArray($id));
194 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
197 CRM_Core_BAO_Navigation
::resetNavigation();
198 // Clear js localization
199 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
206 * Get word replacements for the api.
208 * Get all the word-replacements stored in config-arrays for the
209 * configured language, and convert them to params for the
210 * WordReplacement.create API.
212 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
213 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
214 * step behaves consistently even as the BAO evolves in future versions.
215 * However, if there's a bug in here prior to 4.4.0, we should apply the
216 * bug-fix in both places.
218 * @param bool $rebuildEach
219 * Whether to perform rebuild after each individual API call.
222 * Each item is $params for WordReplacement.create
223 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
225 public static function getConfigArraysAsAPIParams($rebuildEach) {
226 $settingsResult = civicrm_api3('Setting', 'get', array(
227 'return' => 'lcMessages',
229 $returnValues = CRM_Utils_Array
::first($settingsResult['values']);
230 $lang = $returnValues['lcMessages'];
232 $wordReplacementCreateParams = array();
234 $result = civicrm_api3('domain', 'get', array(
235 'return' => array('locale_custom_strings'),
237 if (!empty($result["values"])) {
238 foreach ($result["values"] as $value) {
240 $params["domain_id"] = $value["id"];
241 $params["options"] = array('wp-rebuild' => $rebuildEach);
242 // Unserialize word match string.
243 $localeCustomArray = unserialize($value["locale_custom_strings"]);
244 if (!empty($localeCustomArray)) {
245 $wordMatchArray = array();
246 // Only return the replacement strings of the current language,
247 // otherwise some replacements will be duplicated, which will
248 // lead to undesired results, like CRM-19683.
249 $localCustomData = $localeCustomArray[$lang];
250 // Traverse status array "enabled" "disabled"
251 foreach ($localCustomData as $status => $matchTypes) {
252 $params["is_active"] = ($status == "enabled") ?
TRUE : FALSE;
253 // Traverse Match Type array "wildcardMatch" "exactMatch"
254 foreach ($matchTypes as $matchType => $words) {
255 $params["match_type"] = $matchType;
256 foreach ($words as $word => $replace) {
257 $params["find_word"] = $word;
258 $params["replace_word"] = $replace;
259 $wordReplacementCreateParams[] = $params;
266 return $wordReplacementCreateParams;
270 * Rebuild word replacements.
272 * Get all the word-replacements stored in config-arrays
273 * and write them out as records in civicrm_word_replacement.
275 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
276 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
277 * step behaves consistently even as the BAO evolves in future versions.
278 * However, if there's a bug in here prior to 4.4.0, we should apply the
279 * bug-fix in both places.
281 public static function rebuildWordReplacementTable() {
282 civicrm_api3('word_replacement', 'replace', array(
283 'options' => array('match' => array('domain_id', 'find_word')),
284 'values' => self
::getConfigArraysAsAPIParams(FALSE),
286 CRM_Core_BAO_WordReplacement
::rebuild();
290 * Get WordReplacements for a locale.
292 * @param string $locale
293 * @param int $domainId
296 * List of word replacements (enabled/disabled) for the given locale.
298 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
299 if ($domainId === NULL) {
300 $domainId = CRM_Core_Config
::domainID();
303 return CRM_Utils_Array
::value($locale, self
::_getLocaleCustomStrings($domainId));
307 * Get custom locale strings.
309 * @param int $domainId
311 * @return array|mixed
313 private static function _getLocaleCustomStrings($domainId) {
314 // TODO: Would it be worthwhile using memcache here?
315 $domain = CRM_Core_DAO
::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', array(
316 1 => array($domainId, 'Integer'),
318 while ($domain->fetch()) {
319 return empty($domain->locale_custom_strings
) ?
array() : unserialize($domain->locale_custom_strings
);
324 * Set locale strings.
326 * @param string $locale
327 * @param array $values
328 * @param int $domainId
330 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
331 if ($domainId === NULL) {
332 $domainId = CRM_Core_Config
::domainID();
335 $lcs = self
::_getLocaleCustomStrings($domainId);
336 $lcs[$locale] = $values;
338 self
::_setLocaleCustomStrings($domainId, $lcs);
342 * Set locale strings.
344 * @param int $domainId
347 private static function _setLocaleCustomStrings($domainId, $lcs) {
348 CRM_Core_DAO
::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", array(
349 1 => array(serialize($lcs), 'String'),
350 2 => array($domainId, 'Integer'),