CRM-19298 - hopefully last syntax error
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2016 |
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-2016
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
209 * and convert them to params for the WordReplacement.create API.
210 *
211 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
212 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
213 * step behaves consistently even as the BAO evolves in future versions.
214 * However, if there's a bug in here prior to 4.4.0, we should apply the
215 * bug-fix in both places.
216 *
217 * @param bool $rebuildEach
218 * Whether to perform rebuild after each individual API call.
219 *
220 * @return array
221 * Each item is $params for WordReplacement.create
222 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
223 */
224 public static function getConfigArraysAsAPIParams($rebuildEach) {
225 $wordReplacementCreateParams = array();
226 // get all domains
227 $result = civicrm_api3('domain', 'get', array(
228 'return' => array('locale_custom_strings'),
229 ));
230 if (!empty($result["values"])) {
231 foreach ($result["values"] as $value) {
232 $params = array();
233 $params["domain_id"] = $value["id"];
234 $params["options"] = array('wp-rebuild' => $rebuildEach);
235 // Unserialize word match string.
236 $localeCustomArray = unserialize($value["locale_custom_strings"]);
237 if (!empty($localeCustomArray)) {
238 $wordMatchArray = array();
239 // Traverse Language array
240 foreach ($localeCustomArray as $localCustomData) {
241 // Traverse status array "enabled" "disabled"
242 foreach ($localCustomData as $status => $matchTypes) {
243 $params["is_active"] = ($status == "enabled") ? TRUE : FALSE;
244 // Traverse Match Type array "wildcardMatch" "exactMatch"
245 foreach ($matchTypes as $matchType => $words) {
246 $params["match_type"] = $matchType;
247 foreach ($words as $word => $replace) {
248 $params["find_word"] = $word;
249 $params["replace_word"] = $replace;
250 $wordReplacementCreateParams[] = $params;
251 }
252 }
253 }
254 }
255 }
256 }
257 }
258 return $wordReplacementCreateParams;
259 }
260
261 /**
262 * Rebuild word replacements.
263 *
264 * Get all the word-replacements stored in config-arrays
265 * and write them out as records in civicrm_word_replacement.
266 *
267 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
268 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
269 * step behaves consistently even as the BAO evolves in future versions.
270 * However, if there's a bug in here prior to 4.4.0, we should apply the
271 * bug-fix in both places.
272 */
273 public static function rebuildWordReplacementTable() {
274 civicrm_api3('word_replacement', 'replace', array(
275 'options' => array('match' => array('domain_id', 'find_word')),
276 'values' => self::getConfigArraysAsAPIParams(FALSE),
277 ));
278 CRM_Core_BAO_WordReplacement::rebuild();
279 }
280
281 /**
282 * Get WordReplacements for a locale.
283 *
284 * @param string $locale
285 * @param int $domainId
286 *
287 * @return array
288 * List of word replacements (enabled/disabled) for the given locale.
289 */
290 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
291 if ($domainId === NULL) {
292 $domainId = CRM_Core_Config::domainID();
293 }
294
295 return CRM_Utils_Array::value($locale, self::_getLocaleCustomStrings($domainId));
296 }
297
298 /**
299 * Get custom locale strings.
300 *
301 * @param int $domainId
302 *
303 * @return array|mixed
304 */
305 private static function _getLocaleCustomStrings($domainId) {
306 // TODO: Would it be worthwhile using memcache here?
307 $domain = CRM_Core_DAO::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', array(
308 1 => array($domainId, 'Integer'),
309 ));
310 while ($domain->fetch()) {
311 return empty($domain->locale_custom_strings) ? array() : unserialize($domain->locale_custom_strings);
312 }
313 }
314
315 /**
316 * Set locale strings.
317 *
318 * @param string $locale
319 * @param array $values
320 * @param int $domainId
321 */
322 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
323 if ($domainId === NULL) {
324 $domainId = CRM_Core_Config::domainID();
325 }
326
327 $lcs = self::_getLocaleCustomStrings($domainId);
328 $lcs[$locale] = $values;
329
330 self::_setLocaleCustomStrings($domainId, $lcs);
331 }
332
333 /**
334 * Set locale strings.
335 *
336 * @param int $domainId
337 * @param string $lcs
338 */
339 private static function _setLocaleCustomStrings($domainId, $lcs) {
340 CRM_Core_DAO::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", array(
341 1 => array(serialize($lcs), 'String'),
342 2 => array($domainId, 'Integer'),
343 ));
344 }
345
346 }