3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.5 |
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
39 class CRM_Core_BAO_WordReplacement
extends CRM_Core_DAO_WordReplacement
{
45 * @return \CRM_Core_DAO_WordReplacement
50 function __construct() {
51 parent
::__construct();
54 * Takes a bunch of params that are needed to match certain criteria and
55 * retrieves the relevant objects.
57 * @param array $params (reference ) an assoc array of name/value pairs
58 * @param array $defaults (reference ) an assoc array to hold the flattened values
60 * @return object CRM_Core_DAO_WordRepalcement object
65 static function retrieve(&$params, &$defaults) {
66 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
74 * @return null|object CRM_Core_BAO_WordRepalcement
78 static function getWordReplacement($reset = NULL) {
79 static $wordReplacement = NULL;
80 if (!$wordReplacement ||
$reset) {
81 $wordReplacement = new CRM_Core_BAO_WordReplacement();
82 $wordReplacement->id
= CRM_Core_Config
::wordReplacementID();
83 if (!$wordReplacement->find(TRUE)) {
84 CRM_Core_Error
::fatal();
87 return $wordReplacement;
92 * Save the values of a WordReplacement
97 * @return WordReplacement array
100 static function edit(&$params, &$id) {
101 $wordReplacement = new CRM_Core_DAO_WordReplacement();
102 $wordReplacement->id
= $id;
103 $wordReplacement->copyValues($params);
104 $wordReplacement->save();
105 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
108 return $wordReplacement;
112 * Create a new WordReplacement
116 * @return WordReplacement array
119 static function create($params) {
120 if(array_key_exists("domain_id",$params) === FALSE) {
121 $params["domain_id"] = CRM_Core_Config
::domainID();
123 $wordReplacement = new CRM_Core_DAO_WordReplacement();
124 $wordReplacement->copyValues($params);
125 $wordReplacement->save();
126 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
129 return $wordReplacement;
135 * @param int $id WordReplacement id
140 static function del($id) {
141 $dao = new CRM_Core_DAO_WordReplacement();
144 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
151 * Get all word-replacements in the form of an array
153 * @param int $id domain ID
155 * @see civicrm_domain.locale_custom_strings
157 public static function getAllAsConfigArray($id) {
159 SELECT find_word,replace_word,is_active,match_type
160 FROM civicrm_word_replacement
163 $params = array( 1 => array($id, 'Integer'));
165 $dao = CRM_Core_DAO
::executeQuery($query, $params);
167 $overrides = array();
169 while ($dao->fetch()) {
170 if ($dao->is_active
==1) {
171 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
174 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
177 $config = CRM_Core_Config
::singleton();
178 $domain = new CRM_Core_DAO_Domain();
181 if ($domain->locales
&& $config->localeCustomStrings
) {
183 $addReplacements = $config->localeCustomStrings
;
184 $addReplacements[$config->lcMessages
] = $overrides;
185 $stringOverride = $addReplacements;
188 // for single language
189 $stringOverride = array($config->lcMessages
=> $overrides);
192 return $stringOverride;
198 static function rebuild($clearCaches = TRUE) {
199 $id = CRM_Core_Config
::domainID();
200 $stringOverride = self
::getAllAsConfigArray($id);
201 $params = array('locale_custom_strings' => serialize($stringOverride));
202 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
203 if ($wordReplacementSettings) {
204 CRM_Core_Config
::singleton()->localeCustomStrings
= $stringOverride;
206 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
209 CRM_Core_BAO_Navigation
::resetNavigation();
210 // Clear js localization
211 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
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 * bugfix in both places.
230 * @param bool $rebuildEach whether to perform rebuild after each individual API call
231 * @return array Each item is $params for WordReplacement.create
232 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
234 static function getConfigArraysAsAPIParams($rebuildEach) {
235 $wordReplacementCreateParams = array();
237 $result = civicrm_api3('domain', 'get', array(
238 'return' => array('locale_custom_strings'),
240 if (!empty($result["values"])) {
241 foreach ($result["values"] as $value) {
243 $params["domain_id"] = $value["id"];
244 $params["options"] = array('wp-rebuild' => $rebuildEach);
245 // unserialize word match string
246 $localeCustomArray = unserialize($value["locale_custom_strings"]);
247 if (!empty($localeCustomArray)) {
248 $wordMatchArray = array();
249 // Traverse Language array
250 foreach ($localeCustomArray as $localCustomData) {
251 // Traverse status array "enabled" "disabled"
252 foreach ($localCustomData as $status => $matchTypes) {
253 $params["is_active"] = ($status == "enabled")?
TRUE:FALSE;
254 // Traverse Match Type array "wildcardMatch" "exactMatch"
255 foreach ($matchTypes as $matchType => $words) {
256 $params["match_type"] = $matchType;
257 foreach ($words as $word => $replace) {
258 $params["find_word"] = $word;
259 $params["replace_word"] = $replace;
260 $wordReplacementCreateParams[] = $params;
268 return $wordReplacementCreateParams;
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 * bugfix 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();