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
39 class CRM_Core_BAO_WordReplacement
extends CRM_Core_DAO_WordReplacement
{
44 * @return \CRM_Core_DAO_WordReplacement
49 public function __construct() {
50 parent
::__construct();
53 * Takes a bunch of params that are needed to match certain criteria and
54 * retrieves the relevant objects.
56 * @param array $params
57 * (reference ) an assoc array of name/value pairs.
58 * @param array $defaults
59 * (reference ) an assoc array to hold the flattened values.
61 * @return CRM_Core_DAO_WordRepalcement object
65 public 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
77 public static function getWordReplacement($reset = NULL) {
78 static $wordReplacement = NULL;
79 if (!$wordReplacement ||
$reset) {
80 $wordReplacement = new CRM_Core_BAO_WordReplacement();
81 $wordReplacement->id
= CRM_Core_Config
::wordReplacementID();
82 if (!$wordReplacement->find(TRUE)) {
83 CRM_Core_Error
::fatal();
86 return $wordReplacement;
91 * Save the values of a WordReplacement
93 * @param array $params
96 * @return WordReplacement array
98 public static function edit(&$params, &$id) {
99 $wordReplacement = new CRM_Core_DAO_WordReplacement();
100 $wordReplacement->id
= $id;
101 $wordReplacement->copyValues($params);
102 $wordReplacement->save();
103 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
106 return $wordReplacement;
110 * Create a new WordReplacement
112 * @param array $params
114 * @return WordReplacement array
116 public static function create($params) {
117 if (array_key_exists("domain_id", $params) === FALSE) {
118 $params["domain_id"] = CRM_Core_Config
::domainID();
120 $wordReplacement = new CRM_Core_DAO_WordReplacement();
121 $wordReplacement->copyValues($params);
122 $wordReplacement->save();
123 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
126 return $wordReplacement;
133 * WordReplacement id.
138 public static function del($id) {
139 $dao = new CRM_Core_DAO_WordReplacement();
142 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
149 * Get all word-replacements in the form of an array
154 * @see civicrm_domain.locale_custom_strings
156 public static function getAllAsConfigArray($id) {
158 SELECT find_word,replace_word,is_active,match_type
159 FROM civicrm_word_replacement
162 $params = array(1 => array($id, 'Integer'));
164 $dao = CRM_Core_DAO
::executeQuery($query, $params);
166 $overrides = array();
168 while ($dao->fetch()) {
169 if ($dao->is_active
== 1) {
170 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
173 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
176 $config = CRM_Core_Config
::singleton();
177 $domain = new CRM_Core_DAO_Domain();
180 if ($domain->locales
&& $config->localeCustomStrings
) {
182 $addReplacements = $config->localeCustomStrings
;
183 $addReplacements[$config->lcMessages
] = $overrides;
184 $stringOverride = $addReplacements;
187 // for single language
188 $stringOverride = array($config->lcMessages
=> $overrides);
191 return $stringOverride;
197 public static function rebuild($clearCaches = TRUE) {
198 $id = CRM_Core_Config
::domainID();
199 $stringOverride = self
::getAllAsConfigArray($id);
200 $params = array('locale_custom_strings' => serialize($stringOverride));
201 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
202 if ($wordReplacementSettings) {
203 CRM_Core_Config
::singleton()->localeCustomStrings
= $stringOverride;
205 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
208 CRM_Core_BAO_Navigation
::resetNavigation();
209 // Clear js localization
210 CRM_Core_Resources
::singleton()->flushStrings()->resetCacheCode();
220 * Get all the word-replacements stored in config-arrays
221 * and convert them to params for the WordReplacement.create API.
223 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
224 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
225 * step behaves consistently even as the BAO evolves in future versions.
226 * However, if there's a bug in here prior to 4.4.0, we should apply the
227 * bugfix in both places.
229 * @param bool $rebuildEach
230 * 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 public 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();