3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
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 if ($domain->locales
&& $config->localeCustomStrings
) {
177 $addReplacements = $config->localeCustomStrings
;
178 $addReplacements[$config->lcMessages
] = $overrides;
179 $stringOverride = $addReplacements;
182 // for single language
183 $stringOverride = array($config->lcMessages
=> $overrides);
186 return $stringOverride;
192 * @param bool $clearCaches
196 public static function rebuild($clearCaches = TRUE) {
197 $id = CRM_Core_Config
::domainID();
198 $stringOverride = self
::getAllAsConfigArray($id);
199 $params = array('locale_custom_strings' => serialize($stringOverride));
200 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
201 if ($wordReplacementSettings) {
202 CRM_Core_Config
::singleton()->localeCustomStrings
= $stringOverride;
204 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
207 CRM_Core_BAO_Navigation
::resetNavigation();
208 // Clear js localization
209 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
219 * Get word replacements for the api.
221 * Get all the word-replacements stored in config-arrays
222 * and convert them to params for the WordReplacement.create API.
224 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
225 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
226 * step behaves consistently even as the BAO evolves in future versions.
227 * However, if there's a bug in here prior to 4.4.0, we should apply the
228 * bug-fix in both places.
230 * @param bool $rebuildEach
231 * Whether to perform rebuild after each individual API call.
234 * Each item is $params for WordReplacement.create
235 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
237 public static function getConfigArraysAsAPIParams($rebuildEach) {
238 $wordReplacementCreateParams = array();
240 $result = civicrm_api3('domain', 'get', array(
241 'return' => array('locale_custom_strings'),
243 if (!empty($result["values"])) {
244 foreach ($result["values"] as $value) {
246 $params["domain_id"] = $value["id"];
247 $params["options"] = array('wp-rebuild' => $rebuildEach);
248 // Unserialize word match string.
249 $localeCustomArray = unserialize($value["locale_custom_strings"]);
250 if (!empty($localeCustomArray)) {
251 $wordMatchArray = array();
252 // Traverse Language array
253 foreach ($localeCustomArray as $localCustomData) {
254 // Traverse status array "enabled" "disabled"
255 foreach ($localCustomData as $status => $matchTypes) {
256 $params["is_active"] = ($status == "enabled") ?
TRUE : FALSE;
257 // Traverse Match Type array "wildcardMatch" "exactMatch"
258 foreach ($matchTypes as $matchType => $words) {
259 $params["match_type"] = $matchType;
260 foreach ($words as $word => $replace) {
261 $params["find_word"] = $word;
262 $params["replace_word"] = $replace;
263 $wordReplacementCreateParams[] = $params;
271 return $wordReplacementCreateParams;
275 * Rebuild word replacements.
277 * Get all the word-replacements stored in config-arrays
278 * and write them out as records in civicrm_word_replacement.
280 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
281 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
282 * step behaves consistently even as the BAO evolves in future versions.
283 * However, if there's a bug in here prior to 4.4.0, we should apply the
284 * bug-fix in both places.
286 public static function rebuildWordReplacementTable() {
287 civicrm_api3('word_replacement', 'replace', array(
288 'options' => array('match' => array('domain_id', 'find_word')),
289 'values' => self
::getConfigArraysAsAPIParams(FALSE),
291 CRM_Core_BAO_WordReplacement
::rebuild();