Do not attempt to store out-of-range street number.
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 5 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2019 |
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
31 * @copyright CiviCRM LLC (c) 2004-2019
32 */
33
34 /**
35 * Class CRM_Core_BAO_WordReplacement.
36 */
37 class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
38
39 /**
40 * Class constructor.
41 */
42 public function __construct() {
43 parent::__construct();
44 }
45
46 /**
47 * Function that must have never worked & should be removed.
48 *
49 * Retrieve DB object based on input parameters.
50 *
51 * It also stores all the retrieved values in the default array.
52 *
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.
57 *
58 * @return CRM_Core_DAO_WordReplacement
59 */
60 public static function retrieve(&$params, &$defaults) {
61 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
62 }
63
64 /**
65 * Get the domain BAO.
66 *
67 * @param null $reset
68 *
69 * @return null|CRM_Core_BAO_WordReplacement
70 */
71 public static function getWordReplacement($reset = NULL) {
72 static $wordReplacement = NULL;
73 if (!$wordReplacement || $reset) {
74 $wordReplacement = new CRM_Core_BAO_WordReplacement();
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 /**
85 * Save the values of a WordReplacement.
86 *
87 * @param array $params
88 * @param int $id
89 *
90 * @return array
91 */
92 public static function edit(&$params, &$id) {
93 $wordReplacement = new CRM_Core_DAO_WordReplacement();
94 $wordReplacement->id = $id;
95 $wordReplacement->copyValues($params);
96 $wordReplacement->save();
97 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
98 self::rebuild();
99 }
100 return $wordReplacement;
101 }
102
103 /**
104 * Create a new WordReplacement.
105 *
106 * @param array $params
107 *
108 * @return array
109 */
110 public static function create($params) {
111 if (array_key_exists("domain_id", $params) === FALSE) {
112 $params["domain_id"] = CRM_Core_Config::domainID();
113 }
114 $wordReplacement = new CRM_Core_DAO_WordReplacement();
115 $wordReplacement->copyValues($params);
116 $wordReplacement->save();
117 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
118 self::rebuild();
119 }
120 return $wordReplacement;
121 }
122
123 /**
124 * Delete website.
125 *
126 * @param int $id
127 * WordReplacement id.
128 *
129 * @return object
130 */
131 public static function del($id) {
132 $dao = new CRM_Core_DAO_WordReplacement();
133 $dao->id = $id;
134 $dao->delete();
135 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
136 self::rebuild();
137 }
138 return $dao;
139 }
140
141 /**
142 * Get all word-replacements in the form of an array.
143 *
144 * @param int $id
145 * Domain ID.
146 *
147 * @return array
148 * @see civicrm_domain.locale_custom_strings
149 */
150 public static function getAllAsConfigArray($id) {
151 $query = "
152 SELECT find_word,replace_word,is_active,match_type
153 FROM civicrm_word_replacement
154 WHERE domain_id = %1
155 ";
156 $params = array(1 => array($id, 'Integer'));
157
158 $dao = CRM_Core_DAO::executeQuery($query, $params);
159
160 $overrides = array();
161
162 while ($dao->fetch()) {
163 if ($dao->is_active == 1) {
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;
168 }
169 }
170 $config = CRM_Core_Config::singleton();
171 $domain = new CRM_Core_DAO_Domain();
172 $domain->find(TRUE);
173
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;
179
180 return $stringOverride;
181 }
182
183 /**
184 * Rebuild.
185 *
186 * @param bool $clearCaches
187 *
188 * @return bool
189 */
190 public static function rebuild($clearCaches = TRUE) {
191 $id = CRM_Core_Config::domainID();
192 self::_setLocaleCustomStrings($id, self::getAllAsConfigArray($id));
193
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();
200 }
201
202 return TRUE;
203 }
204
205 /**
206 * Get word replacements for the api.
207 *
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.
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
216 * bug-fix in both places.
217 *
218 * @param bool $rebuildEach
219 * Whether to perform rebuild after each individual API call.
220 *
221 * @return array
222 * Each item is $params for WordReplacement.create
223 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
224 */
225 public static function getConfigArraysAsAPIParams($rebuildEach) {
226 $settingsResult = civicrm_api3('Setting', 'get', array(
227 'return' => 'lcMessages',
228 ));
229 $returnValues = CRM_Utils_Array::first($settingsResult['values']);
230 $lang = $returnValues['lcMessages'];
231
232 $wordReplacementCreateParams = array();
233 // get all domains
234 $result = civicrm_api3('domain', 'get', array(
235 'return' => array('locale_custom_strings'),
236 ));
237 if (!empty($result["values"])) {
238 foreach ($result["values"] as $value) {
239 $params = array();
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();
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;
260 }
261 }
262 }
263 }
264 }
265 }
266 return $wordReplacementCreateParams;
267 }
268
269 /**
270 * Rebuild word replacements.
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 * bug-fix 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 }
288
289 /**
290 * Get WordReplacements for a locale.
291 *
292 * @param string $locale
293 * @param int $domainId
294 *
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
306 /**
307 * Get custom locale strings.
308 *
309 * @param int $domainId
310 *
311 * @return array|mixed
312 */
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
323 /**
324 * Set locale strings.
325 *
326 * @param string $locale
327 * @param array $values
328 * @param int $domainId
329 */
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 /**
342 * Set locale strings.
343 *
344 * @param int $domainId
345 * @param string $lcs
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
354 }