Commit | Line | Data |
---|---|---|
d83a3991 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
06b69b18 | 4 | | CiviCRM version 4.5 | |
d83a3991 | 5 | +--------------------------------------------------------------------+ |
06b69b18 | 6 | | Copyright CiviCRM LLC (c) 2004-2014 | |
d83a3991 | 7 | +--------------------------------------------------------------------+ |
8 | | This file is a part of CiviCRM. | | |
9 | | | | |
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. | | |
13 | | | | |
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. | | |
18 | | | | |
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 | +--------------------------------------------------------------------+ | |
26 | */ | |
27 | ||
28 | /** | |
29 | * | |
30 | * @package CRM | |
06b69b18 | 31 | * @copyright CiviCRM LLC (c) 2004-2014 |
d83a3991 | 32 | * $Id$ |
33 | * | |
34 | */ | |
35 | ||
36 | /** | |
37 | * | |
38 | */ | |
39 | class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement { | |
40 | ||
b5c2afd0 EM |
41 | /** |
42 | * class constructor | |
43 | * | |
44 | * @access public | |
45 | * @return \CRM_Core_DAO_WordReplacement | |
46 | */ | |
47 | /** | |
48 | * | |
49 | */ | |
d83a3991 | 50 | function __construct() { |
51 | parent::__construct(); | |
6cf5bb6f | 52 | } |
d83a3991 | 53 | /** |
54 | * Takes a bunch of params that are needed to match certain criteria and | |
6cf5bb6f | 55 | * retrieves the relevant objects. |
d83a3991 | 56 | * |
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 | |
59 | * | |
60 | * @return object CRM_Core_DAO_WordRepalcement object | |
61 | * @access public | |
62 | * @static | |
63 | */ | |
6cf5bb6f | 64 | |
d83a3991 | 65 | static function retrieve(&$params, &$defaults) { |
66 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults); | |
67 | } | |
68 | ||
69 | /** | |
70 | * Get the domain BAO | |
71 | * | |
77b97be7 EM |
72 | * @param null $reset |
73 | * | |
d83a3991 | 74 | * @return null|object CRM_Core_BAO_WordRepalcement |
75 | * @access public | |
76 | * @static | |
77 | */ | |
f01484bc | 78 | static function getWordReplacement($reset = NULL) { |
d83a3991 | 79 | static $wordReplacement = NULL; |
80 | if (!$wordReplacement || $reset) { | |
a36ecdd0 | 81 | $wordReplacement = new CRM_Core_BAO_WordReplacement(); |
d83a3991 | 82 | $wordReplacement->id = CRM_Core_Config::wordReplacementID(); |
83 | if (!$wordReplacement->find(TRUE)) { | |
84 | CRM_Core_Error::fatal(); | |
85 | } | |
86 | } | |
87 | return $wordReplacement; | |
88 | } | |
89 | ||
90 | ||
91 | /** | |
92 | * Save the values of a WordReplacement | |
93 | * | |
77b97be7 EM |
94 | * @param $params |
95 | * @param $id | |
96 | * | |
d83a3991 | 97 | * @return WordReplacement array |
98 | * @access public | |
99 | */ | |
100 | static function edit(&$params, &$id) { | |
101 | $wordReplacement = new CRM_Core_DAO_WordReplacement(); | |
102 | $wordReplacement->id = $id; | |
103 | $wordReplacement->copyValues($params); | |
104 | $wordReplacement->save(); | |
63b71ea8 TO |
105 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
106 | self::rebuild(); | |
107 | } | |
d83a3991 | 108 | return $wordReplacement; |
109 | } | |
110 | ||
111 | /** | |
112 | * Create a new WordReplacement | |
113 | * | |
dd244018 EM |
114 | * @param $params |
115 | * | |
d83a3991 | 116 | * @return WordReplacement array |
117 | * @access public | |
118 | */ | |
119 | static function create($params) { | |
f01484bc | 120 | if(array_key_exists("domain_id",$params) === FALSE) { |
6cf5bb6f DL |
121 | $params["domain_id"] = CRM_Core_Config::domainID(); |
122 | } | |
d83a3991 | 123 | $wordReplacement = new CRM_Core_DAO_WordReplacement(); |
124 | $wordReplacement->copyValues($params); | |
125 | $wordReplacement->save(); | |
63b71ea8 TO |
126 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
127 | self::rebuild(); | |
128 | } | |
d83a3991 | 129 | return $wordReplacement; |
130 | } | |
6cf5bb6f | 131 | |
d83a3991 | 132 | /** |
133 | * Delete website | |
134 | * | |
135 | * @param int $id WordReplacement id | |
136 | * | |
137 | * @return object | |
138 | * @static | |
139 | */ | |
140 | static function del($id) { | |
141 | $dao = new CRM_Core_DAO_WordReplacement(); | |
142 | $dao->id = $id; | |
143 | $dao->delete(); | |
63b71ea8 TO |
144 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
145 | self::rebuild(); | |
146 | } | |
d83a3991 | 147 | return $dao; |
148 | } | |
d83a3991 | 149 | |
f01484bc TO |
150 | /** |
151 | * Get all word-replacements in the form of an array | |
152 | * | |
153 | * @param int $id domain ID | |
154 | * @return array | |
155 | * @see civicrm_domain.locale_custom_strings | |
156 | */ | |
157 | public static function getAllAsConfigArray($id) { | |
6cf5bb6f DL |
158 | $query = " |
159 | SELECT find_word,replace_word,is_active,match_type | |
160 | FROM civicrm_word_replacement | |
161 | WHERE domain_id = %1 | |
162 | "; | |
163 | $params = array( 1 => array($id, 'Integer')); | |
164 | ||
165 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
166 | ||
167 | $overrides = array(); | |
168 | ||
d83a3991 | 169 | while ($dao->fetch()) { |
675605a7 | 170 | if ($dao->is_active==1) { |
a36ecdd0 E |
171 | $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word; |
172 | } | |
173 | else { | |
174 | $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word; | |
675605a7 | 175 | } |
d83a3991 | 176 | } |
d83a3991 | 177 | $config = CRM_Core_Config::singleton(); |
178 | $domain = new CRM_Core_DAO_Domain(); | |
179 | $domain->find(TRUE); | |
180 | ||
181 | if ($domain->locales && $config->localeCustomStrings) { | |
182 | // for multilingual | |
183 | $addReplacements = $config->localeCustomStrings; | |
184 | $addReplacements[$config->lcMessages] = $overrides; | |
f01484bc | 185 | $stringOverride = $addReplacements; |
d83a3991 | 186 | } |
187 | else { | |
188 | // for single language | |
f01484bc | 189 | $stringOverride = array($config->lcMessages => $overrides); |
d83a3991 | 190 | } |
191 | ||
f01484bc TO |
192 | return $stringOverride; |
193 | } | |
d83a3991 | 194 | |
f01484bc TO |
195 | /** |
196 | * Rebuild | |
197 | */ | |
198 | static function rebuild() { | |
199 | $id = CRM_Core_Config::domainID(); | |
200 | $stringOverride = self::getAllAsConfigArray($id); | |
201 | $params = array('locale_custom_strings' => serialize($stringOverride)); | |
d83a3991 | 202 | $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); |
d83a3991 | 203 | if ($wordReplacementSettings) { |
0f65e834 TO |
204 | CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride; |
205 | ||
d83a3991 | 206 | // Reset navigation |
207 | CRM_Core_BAO_Navigation::resetNavigation(); | |
208 | // Clear js string cache | |
209 | CRM_Core_Resources::singleton()->flushStrings(); | |
f01484bc TO |
210 | |
211 | return TRUE; | |
d83a3991 | 212 | } |
f01484bc TO |
213 | |
214 | return FALSE; | |
d83a3991 | 215 | } |
0f65e834 TO |
216 | |
217 | /** | |
218 | * Get all the word-replacements stored in config-arrays | |
219 | * and convert them to params for the WordReplacement.create API. | |
220 | * | |
221 | * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and | |
222 | * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade | |
223 | * step behaves consistently even as the BAO evolves in future versions. | |
224 | * However, if there's a bug in here prior to 4.4.0, we should apply the | |
225 | * bugfix in both places. | |
226 | * | |
227 | * @param bool $rebuildEach whether to perform rebuild after each individual API call | |
228 | * @return array Each item is $params for WordReplacement.create | |
229 | * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams | |
230 | */ | |
231 | static function getConfigArraysAsAPIParams($rebuildEach) { | |
232 | $wordReplacementCreateParams = array(); | |
233 | // get all domains | |
234 | $result = civicrm_api3('domain', 'get', array( | |
675605a7 | 235 | 'return' => array('locale_custom_strings'), |
236 | )); | |
0f65e834 TO |
237 | if (!empty($result["values"])) { |
238 | foreach ($result["values"] as $value) { | |
239 | $params = array(); | |
0f65e834 TO |
240 | $params["domain_id"] = $value["id"]; |
241 | $params["options"] = array('wp-rebuild' => $rebuildEach); | |
242 | // unserialize word match string | |
243 | $localeCustomArray = unserialize($value["locale_custom_strings"]); | |
244 | if (!empty($localeCustomArray)) { | |
245 | $wordMatchArray = array(); | |
675605a7 | 246 | // Traverse Language array |
0f65e834 | 247 | foreach ($localeCustomArray as $localCustomData) { |
a36ecdd0 E |
248 | // Traverse status array "enabled" "disabled" |
249 | foreach ($localCustomData as $status => $matchTypes) { | |
250 | $params["is_active"] = ($status == "enabled")?TRUE:FALSE; | |
251 | // Traverse Match Type array "wildcardMatch" "exactMatch" | |
252 | foreach ($matchTypes as $matchType => $words) { | |
253 | $params["match_type"] = $matchType; | |
254 | foreach ($words as $word => $replace) { | |
255 | $params["find_word"] = $word; | |
256 | $params["replace_word"] = $replace; | |
257 | $wordReplacementCreateParams[] = $params; | |
258 | } | |
259 | } | |
260 | } | |
0f65e834 TO |
261 | } |
262 | } | |
263 | } | |
264 | } | |
265 | return $wordReplacementCreateParams; | |
266 | } | |
267 | ||
268 | /** | |
269 | * Get all the word-replacements stored in config-arrays | |
270 | * and write them out as records in civicrm_word_replacement. | |
271 | * | |
272 | * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and | |
273 | * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade | |
274 | * step behaves consistently even as the BAO evolves in future versions. | |
275 | * However, if there's a bug in here prior to 4.4.0, we should apply the | |
276 | * bugfix in both places. | |
277 | */ | |
278 | public static function rebuildWordReplacementTable() { | |
279 | civicrm_api3('word_replacement', 'replace', array( | |
280 | 'options' => array('match' => array('domain_id', 'find_word')), | |
281 | 'values' => self::getConfigArraysAsAPIParams(FALSE), | |
282 | )); | |
283 | CRM_Core_BAO_WordReplacement::rebuild(); | |
284 | } | |
d83a3991 | 285 | } |
286 |