Commit | Line | Data |
---|---|---|
d83a3991 | 1 | <?php |
2 | /* | |
3 | +--------------------------------------------------------------------+ | |
7e9e8871 | 4 | | CiviCRM version 4.7 | |
d83a3991 | 5 | +--------------------------------------------------------------------+ |
0f03f337 | 6 | | Copyright CiviCRM LLC (c) 2004-2017 | |
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 | +--------------------------------------------------------------------+ | |
d25dd0ee | 26 | */ |
d83a3991 | 27 | |
28 | /** | |
29 | * | |
30 | * @package CRM | |
0f03f337 | 31 | * @copyright CiviCRM LLC (c) 2004-2017 |
d83a3991 | 32 | */ |
33 | ||
34 | /** | |
d09edf64 | 35 | * Class CRM_Core_BAO_WordReplacement. |
d83a3991 | 36 | */ |
37 | class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement { | |
38 | ||
b5c2afd0 | 39 | /** |
d09edf64 | 40 | * Class constructor. |
b5c2afd0 | 41 | */ |
00be9182 | 42 | public function __construct() { |
d83a3991 | 43 | parent::__construct(); |
6cf5bb6f | 44 | } |
353ffa53 | 45 | |
d83a3991 | 46 | /** |
d09edf64 EM |
47 | * Function that must have never worked & should be removed. |
48 | * | |
fe482240 EM |
49 | * Retrieve DB object based on input parameters. |
50 | * | |
51 | * It also stores all the retrieved values in the default array. | |
d83a3991 | 52 | * |
6a0b768e TO |
53 | * @param array $params |
54 | * (reference ) an assoc array of name/value pairs. | |
55 | * @param array $defaults | |
56 | * (reference ) an assoc array to hold the flattened values. | |
d83a3991 | 57 | * |
ad37ac8e | 58 | * @return CRM_Core_DAO_WordReplacement |
d83a3991 | 59 | */ |
00be9182 | 60 | public static function retrieve(&$params, &$defaults) { |
d83a3991 | 61 | return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults); |
62 | } | |
63 | ||
64 | /** | |
d09edf64 | 65 | * Get the domain BAO. |
d83a3991 | 66 | * |
77b97be7 EM |
67 | * @param null $reset |
68 | * | |
d09edf64 | 69 | * @return null|CRM_Core_BAO_WordReplacement |
d83a3991 | 70 | */ |
00be9182 | 71 | public static function getWordReplacement($reset = NULL) { |
d83a3991 | 72 | static $wordReplacement = NULL; |
73 | if (!$wordReplacement || $reset) { | |
a36ecdd0 | 74 | $wordReplacement = new CRM_Core_BAO_WordReplacement(); |
d83a3991 | 75 | $wordReplacement->id = CRM_Core_Config::wordReplacementID(); |
76 | if (!$wordReplacement->find(TRUE)) { | |
77 | CRM_Core_Error::fatal(); | |
78 | } | |
79 | } | |
80 | return $wordReplacement; | |
81 | } | |
82 | ||
83 | ||
84 | /** | |
d09edf64 | 85 | * Save the values of a WordReplacement. |
d83a3991 | 86 | * |
c490a46a | 87 | * @param array $params |
100fef9d | 88 | * @param int $id |
77b97be7 | 89 | * |
72b3a70c | 90 | * @return array |
d83a3991 | 91 | */ |
00be9182 | 92 | public static function edit(&$params, &$id) { |
d83a3991 | 93 | $wordReplacement = new CRM_Core_DAO_WordReplacement(); |
94 | $wordReplacement->id = $id; | |
95 | $wordReplacement->copyValues($params); | |
96 | $wordReplacement->save(); | |
63b71ea8 TO |
97 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
98 | self::rebuild(); | |
99 | } | |
d83a3991 | 100 | return $wordReplacement; |
101 | } | |
102 | ||
103 | /** | |
d09edf64 | 104 | * Create a new WordReplacement. |
d83a3991 | 105 | * |
c490a46a | 106 | * @param array $params |
dd244018 | 107 | * |
72b3a70c | 108 | * @return array |
d83a3991 | 109 | */ |
00be9182 | 110 | public static function create($params) { |
22e263ad | 111 | if (array_key_exists("domain_id", $params) === FALSE) { |
6cf5bb6f DL |
112 | $params["domain_id"] = CRM_Core_Config::domainID(); |
113 | } | |
d83a3991 | 114 | $wordReplacement = new CRM_Core_DAO_WordReplacement(); |
115 | $wordReplacement->copyValues($params); | |
116 | $wordReplacement->save(); | |
63b71ea8 TO |
117 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
118 | self::rebuild(); | |
119 | } | |
d83a3991 | 120 | return $wordReplacement; |
121 | } | |
6cf5bb6f | 122 | |
d83a3991 | 123 | /** |
d09edf64 | 124 | * Delete website. |
d83a3991 | 125 | * |
6a0b768e TO |
126 | * @param int $id |
127 | * WordReplacement id. | |
d83a3991 | 128 | * |
129 | * @return object | |
d83a3991 | 130 | */ |
00be9182 | 131 | public static function del($id) { |
d83a3991 | 132 | $dao = new CRM_Core_DAO_WordReplacement(); |
133 | $dao->id = $id; | |
134 | $dao->delete(); | |
63b71ea8 TO |
135 | if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) { |
136 | self::rebuild(); | |
137 | } | |
d83a3991 | 138 | return $dao; |
139 | } | |
d83a3991 | 140 | |
f01484bc | 141 | /** |
d09edf64 | 142 | * Get all word-replacements in the form of an array. |
f01484bc | 143 | * |
6a0b768e TO |
144 | * @param int $id |
145 | * Domain ID. | |
d09edf64 | 146 | * |
f01484bc TO |
147 | * @return array |
148 | * @see civicrm_domain.locale_custom_strings | |
149 | */ | |
150 | public static function getAllAsConfigArray($id) { | |
6cf5bb6f DL |
151 | $query = " |
152 | SELECT find_word,replace_word,is_active,match_type | |
153 | FROM civicrm_word_replacement | |
154 | WHERE domain_id = %1 | |
155 | "; | |
481a74f4 | 156 | $params = array(1 => array($id, 'Integer')); |
6cf5bb6f DL |
157 | |
158 | $dao = CRM_Core_DAO::executeQuery($query, $params); | |
159 | ||
160 | $overrides = array(); | |
161 | ||
d83a3991 | 162 | while ($dao->fetch()) { |
2aa397bc | 163 | if ($dao->is_active == 1) { |
a36ecdd0 E |
164 | $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word; |
165 | } | |
166 | else { | |
167 | $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word; | |
675605a7 | 168 | } |
d83a3991 | 169 | } |
d83a3991 | 170 | $config = CRM_Core_Config::singleton(); |
171 | $domain = new CRM_Core_DAO_Domain(); | |
172 | $domain->find(TRUE); | |
173 | ||
234d8f09 TO |
174 | // So. Weird. Some bizarre/probably-broken multi-lingual thing where |
175 | // data isn't really stored in civicrm_word_replacements. Probably | |
176 | // shouldn't exist. | |
177 | $stringOverride = self::_getLocaleCustomStrings($id); | |
178 | $stringOverride[$config->lcMessages] = $overrides; | |
d83a3991 | 179 | |
f01484bc TO |
180 | return $stringOverride; |
181 | } | |
d83a3991 | 182 | |
f01484bc | 183 | /** |
d09edf64 EM |
184 | * Rebuild. |
185 | * | |
186 | * @param bool $clearCaches | |
187 | * | |
188 | * @return bool | |
f01484bc | 189 | */ |
00be9182 | 190 | public static function rebuild($clearCaches = TRUE) { |
f01484bc | 191 | $id = CRM_Core_Config::domainID(); |
234d8f09 | 192 | self::_setLocaleCustomStrings($id, self::getAllAsConfigArray($id)); |
f01484bc | 193 | |
234d8f09 TO |
194 | // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally |
195 | if ($clearCaches) { | |
196 | // Reset navigation | |
197 | CRM_Core_BAO_Navigation::resetNavigation(); | |
198 | // Clear js localization | |
199 | CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode(); | |
d83a3991 | 200 | } |
f01484bc | 201 | |
234d8f09 | 202 | return TRUE; |
d83a3991 | 203 | } |
0f65e834 TO |
204 | |
205 | /** | |
d09edf64 EM |
206 | * Get word replacements for the api. |
207 | * | |
b37b9bef JV |
208 | * Get all the word-replacements stored in config-arrays for the |
209 | * configured language, and convert them to params for the | |
210 | * WordReplacement.create API. | |
0f65e834 TO |
211 | * |
212 | * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and | |
213 | * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade | |
214 | * step behaves consistently even as the BAO evolves in future versions. | |
215 | * However, if there's a bug in here prior to 4.4.0, we should apply the | |
d09edf64 | 216 | * bug-fix in both places. |
0f65e834 | 217 | * |
6a0b768e TO |
218 | * @param bool $rebuildEach |
219 | * Whether to perform rebuild after each individual API call. | |
d09edf64 | 220 | * |
a6c01b45 CW |
221 | * @return array |
222 | * Each item is $params for WordReplacement.create | |
0f65e834 TO |
223 | * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams |
224 | */ | |
00be9182 | 225 | public static function getConfigArraysAsAPIParams($rebuildEach) { |
b37b9bef JV |
226 | $settingsResult = civicrm_api3('Setting', 'get', array( |
227 | 'return' => 'lcMessages', | |
228 | )); | |
d6ef088f JV |
229 | $returnValues = CRM_Utils_Array::first($settingsResult['values']); |
230 | $lang = $returnValues['lcMessages']; | |
b37b9bef | 231 | |
0f65e834 TO |
232 | $wordReplacementCreateParams = array(); |
233 | // get all domains | |
234 | $result = civicrm_api3('domain', 'get', array( | |
353ffa53 TO |
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); | |
d09edf64 | 242 | // Unserialize word match string. |
0f65e834 TO |
243 | $localeCustomArray = unserialize($value["locale_custom_strings"]); |
244 | if (!empty($localeCustomArray)) { | |
245 | $wordMatchArray = array(); | |
b37b9bef JV |
246 | // Only return the replacement strings of the current language, |
247 | // otherwise some replacements will be duplicated, which will | |
248 | // lead to undesired results, like CRM-19683. | |
249 | $localCustomData = $localeCustomArray[$lang]; | |
250 | // Traverse status array "enabled" "disabled" | |
251 | foreach ($localCustomData as $status => $matchTypes) { | |
252 | $params["is_active"] = ($status == "enabled") ? TRUE : FALSE; | |
253 | // Traverse Match Type array "wildcardMatch" "exactMatch" | |
254 | foreach ($matchTypes as $matchType => $words) { | |
255 | $params["match_type"] = $matchType; | |
256 | foreach ($words as $word => $replace) { | |
257 | $params["find_word"] = $word; | |
258 | $params["replace_word"] = $replace; | |
259 | $wordReplacementCreateParams[] = $params; | |
a36ecdd0 E |
260 | } |
261 | } | |
0f65e834 TO |
262 | } |
263 | } | |
264 | } | |
265 | } | |
266 | return $wordReplacementCreateParams; | |
267 | } | |
268 | ||
269 | /** | |
d09edf64 EM |
270 | * Rebuild word replacements. |
271 | * | |
0f65e834 TO |
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 | |
d09edf64 | 279 | * bug-fix in both places. |
0f65e834 TO |
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 | } | |
96025800 | 288 | |
234d8f09 TO |
289 | /** |
290 | * Get WordReplacements for a locale. | |
291 | * | |
292 | * @param string $locale | |
ad37ac8e | 293 | * @param int $domainId |
294 | * | |
234d8f09 TO |
295 | * @return array |
296 | * List of word replacements (enabled/disabled) for the given locale. | |
297 | */ | |
298 | public static function getLocaleCustomStrings($locale, $domainId = NULL) { | |
299 | if ($domainId === NULL) { | |
300 | $domainId = CRM_Core_Config::domainID(); | |
301 | } | |
302 | ||
303 | return CRM_Utils_Array::value($locale, self::_getLocaleCustomStrings($domainId)); | |
304 | } | |
305 | ||
ad37ac8e | 306 | /** |
307 | * Get custom locale strings. | |
308 | * | |
309 | * @param int $domainId | |
310 | * | |
311 | * @return array|mixed | |
312 | */ | |
234d8f09 TO |
313 | private static function _getLocaleCustomStrings($domainId) { |
314 | // TODO: Would it be worthwhile using memcache here? | |
315 | $domain = CRM_Core_DAO::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', array( | |
316 | 1 => array($domainId, 'Integer'), | |
317 | )); | |
318 | while ($domain->fetch()) { | |
319 | return empty($domain->locale_custom_strings) ? array() : unserialize($domain->locale_custom_strings); | |
320 | } | |
321 | } | |
322 | ||
ad37ac8e | 323 | /** |
324 | * Set locale strings. | |
325 | * | |
326 | * @param string $locale | |
327 | * @param array $values | |
328 | * @param int $domainId | |
329 | */ | |
234d8f09 TO |
330 | public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) { |
331 | if ($domainId === NULL) { | |
332 | $domainId = CRM_Core_Config::domainID(); | |
333 | } | |
334 | ||
335 | $lcs = self::_getLocaleCustomStrings($domainId); | |
336 | $lcs[$locale] = $values; | |
337 | ||
338 | self::_setLocaleCustomStrings($domainId, $lcs); | |
339 | } | |
340 | ||
341 | /** | |
ad37ac8e | 342 | * Set locale strings. |
343 | * | |
344 | * @param int $domainId | |
345 | * @param string $lcs | |
234d8f09 TO |
346 | */ |
347 | private static function _setLocaleCustomStrings($domainId, $lcs) { | |
348 | CRM_Core_DAO::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", array( | |
349 | 1 => array(serialize($lcs), 'String'), | |
350 | 2 => array($domainId, 'Integer'), | |
351 | )); | |
352 | } | |
353 | ||
d83a3991 | 354 | } |