3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.4 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2013 |
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-2013
39 class CRM_Core_BAO_WordReplacement
extends CRM_Core_DAO_WordReplacement
{
41 function __construct() {
42 parent
::__construct();
45 * Takes a bunch of params that are needed to match certain criteria and
46 * retrieves the relevant objects.
48 * @param array $params (reference ) an assoc array of name/value pairs
49 * @param array $defaults (reference ) an assoc array to hold the flattened values
51 * @return object CRM_Core_DAO_WordRepalcement object
56 static function retrieve(&$params, &$defaults) {
57 return CRM_Core_DAO
::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
63 * @return null|object CRM_Core_BAO_WordRepalcement
67 static function getWordReplacement($reset = NULL) {
68 static $wordReplacement = NULL;
69 if (!$wordReplacement ||
$reset) {
70 $wordReplacement = new CRM_Core_BAO_WordRepalcement();
71 $wordReplacement->id
= CRM_Core_Config
::wordReplacementID();
72 if (!$wordReplacement->find(TRUE)) {
73 CRM_Core_Error
::fatal();
76 return $wordReplacement;
81 * Save the values of a WordReplacement
83 * @return WordReplacement array
86 static function edit(&$params, &$id) {
87 $wordReplacement = new CRM_Core_DAO_WordReplacement();
88 $wordReplacement->id
= $id;
89 $wordReplacement->copyValues($params);
90 $wordReplacement->save();
91 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
94 return $wordReplacement;
98 * Create a new WordReplacement
100 * @return WordReplacement array
103 static function create($params) {
104 if(array_key_exists("domain_id",$params) === FALSE) {
105 $params["domain_id"] = CRM_Core_Config
::domainID();
107 $wordReplacement = new CRM_Core_DAO_WordReplacement();
108 $wordReplacement->copyValues($params);
109 $wordReplacement->save();
110 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
113 return $wordReplacement;
119 * @param int $id WordReplacement id
124 static function del($id) {
125 $dao = new CRM_Core_DAO_WordReplacement();
128 if (!isset($params['options']) || CRM_Utils_Array
::value('wp-rebuild', $params['options'], TRUE)) {
135 * Get all word-replacements in the form of an array
137 * @param int $id domain ID
139 * @see civicrm_domain.locale_custom_strings
141 public static function getAllAsConfigArray($id) {
143 SELECT find_word,replace_word,is_active,match_type
144 FROM civicrm_word_replacement
147 $params = array( 1 => array($id, 'Integer'));
149 $dao = CRM_Core_DAO
::executeQuery($query, $params);
151 $overrides = array();
153 while ($dao->fetch()) {
154 if ($dao->is_active
==1) {
155 $overrides['enabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
157 $overrides['disabled'][$dao->match_type
][$dao->find_word
] = $dao->replace_word
;
160 $config = CRM_Core_Config
::singleton();
161 $domain = new CRM_Core_DAO_Domain();
164 if ($domain->locales
&& $config->localeCustomStrings
) {
166 $addReplacements = $config->localeCustomStrings
;
167 $addReplacements[$config->lcMessages
] = $overrides;
168 $stringOverride = $addReplacements;
171 // for single language
172 $stringOverride = array($config->lcMessages
=> $overrides);
175 return $stringOverride;
181 static function rebuild() {
182 $id = CRM_Core_Config
::domainID();
183 $stringOverride = self
::getAllAsConfigArray($id);
184 $params = array('locale_custom_strings' => serialize($stringOverride));
185 $wordReplacementSettings = CRM_Core_BAO_Domain
::edit($params, $id);
186 if ($wordReplacementSettings) {
187 CRM_Core_Config
::singleton()->localeCustomStrings
= $stringOverride;
190 CRM_Core_BAO_Navigation
::resetNavigation();
191 // Clear js string cache
192 CRM_Core_Resources
::singleton()->flushStrings();
201 * Get all the word-replacements stored in config-arrays
202 * and convert them to params for the WordReplacement.create API.
204 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
205 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
206 * step behaves consistently even as the BAO evolves in future versions.
207 * However, if there's a bug in here prior to 4.4.0, we should apply the
208 * bugfix in both places.
210 * @param bool $rebuildEach whether to perform rebuild after each individual API call
211 * @return array Each item is $params for WordReplacement.create
212 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
214 static function getConfigArraysAsAPIParams($rebuildEach) {
215 $wordReplacementCreateParams = array();
217 $result = civicrm_api3('domain', 'get', array(
218 'return' => array('locale_custom_strings'),
220 if (!empty($result["values"])) {
221 foreach ($result["values"] as $value) {
223 $params["domain_id"] = $value["id"];
224 $params["options"] = array('wp-rebuild' => $rebuildEach);
225 // unserialize word match string
226 $localeCustomArray = unserialize($value["locale_custom_strings"]);
227 if (!empty($localeCustomArray)) {
228 $wordMatchArray = array();
229 // Traverse Language array
230 foreach ($localeCustomArray as $localCustomData) {
231 // Traverse status array "enabled" "disabled"
232 foreach ($localCustomData as $status => $matchTypes) {
233 $params["is_active"] = ($status == "enabled")?
TRUE:FALSE;
234 // Traverse Match Type array "wildcardMatch" "exactMatch"
235 foreach ($matchTypes as $matchType => $words) {
236 $params["match_type"] = $matchType;
237 foreach ($words as $word => $replace) {
238 $params["find_word"] = $word;
239 $params["replace_word"] = $replace;
240 $wordReplacementCreateParams[] = $params;
248 return $wordReplacementCreateParams;
252 * Get all the word-replacements stored in config-arrays
253 * and write them out as records in civicrm_word_replacement.
255 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
256 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
257 * step behaves consistently even as the BAO evolves in future versions.
258 * However, if there's a bug in here prior to 4.4.0, we should apply the
259 * bugfix in both places.
261 public static function rebuildWordReplacementTable() {
262 civicrm_api3('word_replacement', 'replace', array(
263 'options' => array('match' => array('domain_id', 'find_word')),
264 'values' => self
::getConfigArraysAsAPIParams(FALSE),
266 CRM_Core_BAO_WordReplacement
::rebuild();