3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
36 * Class CRM_Core_BAO_WordReplacement.
38 class CRM_Core_BAO_WordReplacement
extends CRM_Core_DAO_WordReplacement
{
43 public function __construct() {
44 parent
::__construct();
48 * Function that must have never worked & should be removed.
50 * Retrieve DB object based on input parameters.
52 * It also stores all the retrieved values in the default array.
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
59 * @return CRM_Core_DAO_WordRepalcement
61 public static function retrieve(&$params, &$defaults) {
62 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
70 * @return null|CRM_Core_BAO_WordReplacement
72 public static function getWordReplacement($reset = NULL) {
73 static $wordReplacement = NULL;
74 if (!$wordReplacement ||
$reset) {
75 $wordReplacement = new CRM_Core_BAO_WordReplacement();
76 $wordReplacement->id
= CRM_Core_Config
::wordReplacementID();
77 if (!$wordReplacement->find(TRUE)) {
78 CRM_Core_Error
::fatal();
81 return $wordReplacement;
86 * Save the values of a WordReplacement.
88 * @param array $params
93 public static function edit(&$params, &$id) {
94 $wordReplacement = new CRM_Core_DAO_WordReplacement();
95 $wordReplacement->id
= $id;
96 $wordReplacement->copyValues($params);
97 $wordReplacement->save();
98 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
101 return $wordReplacement;
105 * Create a new WordReplacement.
107 * @param array $params
111 public static function create($params) {
112 if (array_key_exists("domain_id", $params) === FALSE) {
113 $params["domain_id"] = CRM_Core_Config
::domainID();
115 $wordReplacement = new CRM_Core_DAO_WordReplacement();
116 $wordReplacement->copyValues($params);
117 $wordReplacement->save();
118 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
121 return $wordReplacement;
128 * WordReplacement id.
132 public static function del($id) {
133 $dao = new CRM_Core_DAO_WordReplacement();
136 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
143 * Get all word-replacements in the form of an array.
149 * @see civicrm_domain.locale_custom_strings
151 public static function getAllAsConfigArray($id) {
153 SELECT find_word,replace_word,is_active,match_type
154 FROM civicrm_word_replacement
157 $params = array(1 => array($id, 'Integer'));
159 $dao = CRM_Core_DAO
::executeQuery($query, $params);
161 $overrides = array();
163 while ($dao->fetch()) {
164 if ($dao->is_active
== 1) {
165 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
168 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
171 $config = CRM_Core_Config
::singleton();
172 $domain = new CRM_Core_DAO_Domain();
175 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
176 // data isn't really stored in civicrm_word_replacements. Probably
178 $stringOverride = self
::_getLocaleCustomStrings($id);
179 $stringOverride[$config->lcMessages
] = $overrides;
181 return $stringOverride;
187 * @param bool $clearCaches
191 public static function rebuild($clearCaches = TRUE) {
192 $id = CRM_Core_Config
::domainID();
193 self
::_setLocaleCustomStrings($id, self
::getAllAsConfigArray($id));
195 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
198 CRM_Core_BAO_Navigation
::resetNavigation();
199 // Clear js localization
200 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
207 * Get word replacements for the api.
209 * Get all the word-replacements stored in config-arrays
210 * and convert them to params for the 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 $wordReplacementCreateParams = array();
228 $result = civicrm_api3('domain', 'get', array(
229 'return' => array('locale_custom_strings'),
231 if (!empty($result["values"])) {
232 foreach ($result["values"] as $value) {
234 $params["domain_id"] = $value["id"];
235 $params["options"] = array('wp-rebuild' => $rebuildEach);
236 // Unserialize word match string.
237 $localeCustomArray = unserialize($value["locale_custom_strings"]);
238 if (!empty($localeCustomArray)) {
239 $wordMatchArray = array();
240 // Traverse Language array
241 foreach ($localeCustomArray as $localCustomData) {
242 // Traverse status array "enabled" "disabled"
243 foreach ($localCustomData as $status => $matchTypes) {
244 $params["is_active"] = ($status == "enabled") ?
TRUE : FALSE;
245 // Traverse Match Type array "wildcardMatch" "exactMatch"
246 foreach ($matchTypes as $matchType => $words) {
247 $params["match_type"] = $matchType;
248 foreach ($words as $word => $replace) {
249 $params["find_word"] = $word;
250 $params["replace_word"] = $replace;
251 $wordReplacementCreateParams[] = $params;
259 return $wordReplacementCreateParams;
263 * Rebuild word replacements.
265 * Get all the word-replacements stored in config-arrays
266 * and write them out as records in civicrm_word_replacement.
268 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
269 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
270 * step behaves consistently even as the BAO evolves in future versions.
271 * However, if there's a bug in here prior to 4.4.0, we should apply the
272 * bug-fix in both places.
274 public static function rebuildWordReplacementTable() {
275 civicrm_api3('word_replacement', 'replace', array(
276 'options' => array('match' => array('domain_id', 'find_word')),
277 'values' => self
::getConfigArraysAsAPIParams(FALSE),
279 CRM_Core_BAO_WordReplacement
::rebuild();
283 * Get WordReplacements for a locale.
285 * @param string $locale
287 * List of word replacements (enabled/disabled) for the given locale.
289 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
290 if ($domainId === NULL) {
291 $domainId = CRM_Core_Config
::domainID();
294 return CRM_Utils_Array
::value($locale, self
::_getLocaleCustomStrings($domainId));
297 private static function _getLocaleCustomStrings($domainId) {
298 // TODO: Would it be worthwhile using memcache here?
299 $domain = CRM_Core_DAO
::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', array(
300 1 => array($domainId, 'Integer'),
302 while ($domain->fetch()) {
303 return empty($domain->locale_custom_strings
) ?
array() : unserialize($domain->locale_custom_strings
);
307 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
308 if ($domainId === NULL) {
309 $domainId = CRM_Core_Config
::domainID();
312 $lcs = self
::_getLocaleCustomStrings($domainId);
313 $lcs[$locale] = $values;
315 self
::_setLocaleCustomStrings($domainId, $lcs);
322 private static function _setLocaleCustomStrings($domainId, $lcs) {
323 CRM_Core_DAO
::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", array(
324 1 => array(serialize($lcs), 'String'),
325 2 => array($domainId, 'Integer'),