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 | * |
6a0b768e TO |
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. | |
d83a3991 | 60 | * |
c490a46a | 61 | * @return CRM_Core_DAO_WordRepalcement object |
d83a3991 | 62 | * @static |
63 | */ | |
6cf5bb6f | 64 | |
00be9182 | 65 | public static function retrieve(&$params, &$defaults) { |
d83a3991 | 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 |
d83a3991 | 75 | * @static |
76 | */ | |
00be9182 | 77 | public static function getWordReplacement($reset = NULL) { |
d83a3991 | 78 | static $wordReplacement = NULL; |
79 | if (!$wordReplacement || $reset) { | |
a36ecdd0 | 80 | $wordReplacement = new CRM_Core_BAO_WordReplacement(); |
d83a3991 | 81 | $wordReplacement->id = CRM_Core_Config::wordReplacementID(); |
82 | if (!$wordReplacement->find(TRUE)) { | |
83 | CRM_Core_Error::fatal(); | |
84 | } | |
85 | } | |
86 | return $wordReplacement; | |
87 | } | |
88 | ||
89 | ||
90 | /** | |
91 | * Save the values of a WordReplacement | |
92 | * | |
c490a46a | 93 | * @param array $params |
100fef9d | 94 | * @param int $id |
77b97be7 | 95 | * |
d83a3991 | 96 | * @return WordReplacement array |
d83a3991 | 97 | */ |
00be9182 | 98 | public static function edit(&$params, &$id) { |
d83a3991 | 99 | $wordReplacement = new CRM_Core_DAO_WordReplacement(); |
100 | $wordReplacement->id = $id; | |
101 | $wordReplacement->copyValues($params); | |
102 | $wordReplacement->save(); | |
63b71ea8 TO |
103 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
104 | self::rebuild(); | |
105 | } | |
d83a3991 | 106 | return $wordReplacement; |
107 | } | |
108 | ||
109 | /** | |
110 | * Create a new WordReplacement | |
111 | * | |
c490a46a | 112 | * @param array $params |
dd244018 | 113 | * |
d83a3991 | 114 | * @return WordReplacement array |
d83a3991 | 115 | */ |
00be9182 | 116 | public static function create($params) { |
22e263ad | 117 | if (array_key_exists("domain_id", $params) === FALSE) { |
6cf5bb6f DL |
118 | $params["domain_id"] = CRM_Core_Config::domainID(); |
119 | } | |
d83a3991 | 120 | $wordReplacement = new CRM_Core_DAO_WordReplacement(); |
121 | $wordReplacement->copyValues($params); | |
122 | $wordReplacement->save(); | |
63b71ea8 TO |
123 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
124 | self::rebuild(); | |
125 | } | |
d83a3991 | 126 | return $wordReplacement; |
127 | } | |
6cf5bb6f | 128 | |
d83a3991 | 129 | /** |
130 | * Delete website | |
131 | * | |
6a0b768e TO |
132 | * @param int $id |
133 | * WordReplacement id. | |
d83a3991 | 134 | * |
135 | * @return object | |
136 | * @static | |
137 | */ | |
00be9182 | 138 | public static function del($id) { |
d83a3991 | 139 | $dao = new CRM_Core_DAO_WordReplacement(); |
140 | $dao->id = $id; | |
141 | $dao->delete(); | |
63b71ea8 TO |
142 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
143 | self::rebuild(); | |
144 | } | |
d83a3991 | 145 | return $dao; |
146 | } | |
d83a3991 | 147 | |
f01484bc TO |
148 | /** |
149 | * Get all word-replacements in the form of an array | |
150 | * | |
6a0b768e TO |
151 | * @param int $id |
152 | * Domain ID. | |
f01484bc TO |
153 | * @return array |
154 | * @see civicrm_domain.locale_custom_strings | |
155 | */ | |
156 | public static function getAllAsConfigArray($id) { | |
6cf5bb6f DL |
157 | $query = " |
158 | SELECT find_word,replace_word,is_active,match_type | |
159 | FROM civicrm_word_replacement | |
160 | WHERE domain_id = %1 | |
161 | "; | |
481a74f4 | 162 | $params = array(1 => array($id, 'Integer')); |
6cf5bb6f DL |
163 | |
164 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
165 | ||
166 | $overrides = array(); | |
167 | ||
d83a3991 | 168 | while ($dao->fetch()) { |
2aa397bc | 169 | if ($dao->is_active == 1) { |
a36ecdd0 E |
170 | $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word; |
171 | } | |
172 | else { | |
173 | $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word; | |
675605a7 | 174 | } |
d83a3991 | 175 | } |
d83a3991 | 176 | $config = CRM_Core_Config::singleton(); |
177 | $domain = new CRM_Core_DAO_Domain(); | |
178 | $domain->find(TRUE); | |
179 | ||
180 | if ($domain->locales && $config->localeCustomStrings) { | |
181 | // for multilingual | |
182 | $addReplacements = $config->localeCustomStrings; | |
183 | $addReplacements[$config->lcMessages] = $overrides; | |
f01484bc | 184 | $stringOverride = $addReplacements; |
d83a3991 | 185 | } |
186 | else { | |
187 | // for single language | |
f01484bc | 188 | $stringOverride = array($config->lcMessages => $overrides); |
d83a3991 | 189 | } |
190 | ||
f01484bc TO |
191 | return $stringOverride; |
192 | } | |
d83a3991 | 193 | |
f01484bc TO |
194 | /** |
195 | * Rebuild | |
196 | */ | |
00be9182 | 197 | public static function rebuild($clearCaches = TRUE) { |
f01484bc TO |
198 | $id = CRM_Core_Config::domainID(); |
199 | $stringOverride = self::getAllAsConfigArray($id); | |
200 | $params = array('locale_custom_strings' => serialize($stringOverride)); | |
d83a3991 | 201 | $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id); |
d83a3991 | 202 | if ($wordReplacementSettings) { |
0f65e834 TO |
203 | CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride; |
204 | ||
9762f6ff CW |
205 | // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally |
206 | if ($clearCaches) { | |
207 | // Reset navigation | |
208 | CRM_Core_BAO_Navigation::resetNavigation(); | |
209 | // Clear js localization | |
4cc9b813 | 210 | CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); |
9762f6ff | 211 | } |
f01484bc TO |
212 | |
213 | return TRUE; | |
d83a3991 | 214 | } |
f01484bc TO |
215 | |
216 | return FALSE; | |
d83a3991 | 217 | } |
0f65e834 TO |
218 | |
219 | /** | |
220 | * Get all the word-replacements stored in config-arrays | |
221 | * and convert them to params for the WordReplacement.create API. | |
222 | * | |
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. | |
228 | * | |
6a0b768e TO |
229 | * @param bool $rebuildEach |
230 | * Whether to perform rebuild after each individual API call. | |
0f65e834 TO |
231 | * @return array Each item is $params for WordReplacement.create |
232 | * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams | |
233 | */ | |
00be9182 | 234 | public static function getConfigArraysAsAPIParams($rebuildEach) { |
0f65e834 TO |
235 | $wordReplacementCreateParams = array(); |
236 | // get all domains | |
237 | $result = civicrm_api3('domain', 'get', array( | |
675605a7 | 238 | 'return' => array('locale_custom_strings'), |
239 | )); | |
0f65e834 TO |
240 | if (!empty($result["values"])) { |
241 | foreach ($result["values"] as $value) { | |
242 | $params = array(); | |
0f65e834 TO |
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(); | |
675605a7 | 249 | // Traverse Language array |
0f65e834 | 250 | foreach ($localeCustomArray as $localCustomData) { |
2aa397bc | 251 | // Traverse status array "enabled" "disabled" |
a36ecdd0 | 252 | foreach ($localCustomData as $status => $matchTypes) { |
2aa397bc | 253 | $params["is_active"] = ($status == "enabled") ? TRUE : FALSE; |
a36ecdd0 E |
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; | |
261 | } | |
262 | } | |
263 | } | |
0f65e834 TO |
264 | } |
265 | } | |
266 | } | |
267 | } | |
268 | return $wordReplacementCreateParams; | |
269 | } | |
270 | ||
271 | /** | |
272 | * Get all the word-replacements stored in config-arrays | |
273 | * and write them out as records in civicrm_word_replacement. | |
274 | * | |
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. | |
280 | */ | |
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), | |
285 | )); | |
286 | CRM_Core_BAO_WordReplacement::rebuild(); | |
287 | } | |
d83a3991 | 288 | } |