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