MagicMerge - Per-request cache of path/url properties
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.7 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2015 |
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-2015
32 * $Id$
33 */
34
35 /**
36 * Class CRM_Core_BAO_WordReplacement.
37 */
38 class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
39
40 /**
41 * Class constructor.
42 */
43 public function __construct() {
44 parent::__construct();
45 }
46
47 /**
48 * Function that must have never worked & should be removed.
49 *
50 * Retrieve DB object based on input parameters.
51 *
52 * It also stores all the retrieved values in the default array.
53 *
54 * @param array $params
55 * (reference ) an assoc array of name/value pairs.
56 * @param array $defaults
57 * (reference ) an assoc array to hold the flattened values.
58 *
59 * @return CRM_Core_DAO_WordRepalcement
60 */
61 public static function retrieve(&$params, &$defaults) {
62 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
63 }
64
65 /**
66 * Get the domain BAO.
67 *
68 * @param null $reset
69 *
70 * @return null|CRM_Core_BAO_WordReplacement
71 */
72 public static function getWordReplacement($reset = NULL) {
73 static $wordReplacement = NULL;
74 if (!$wordReplacement || $reset) {
75 $wordReplacement = new CRM_Core_BAO_WordReplacement();
76 $wordReplacement->id = CRM_Core_Config::wordReplacementID();
77 if (!$wordReplacement->find(TRUE)) {
78 CRM_Core_Error::fatal();
79 }
80 }
81 return $wordReplacement;
82 }
83
84
85 /**
86 * Save the values of a WordReplacement.
87 *
88 * @param array $params
89 * @param int $id
90 *
91 * @return array
92 */
93 public static function edit(&$params, &$id) {
94 $wordReplacement = new CRM_Core_DAO_WordReplacement();
95 $wordReplacement->id = $id;
96 $wordReplacement->copyValues($params);
97 $wordReplacement->save();
98 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
99 self::rebuild();
100 }
101 return $wordReplacement;
102 }
103
104 /**
105 * Create a new WordReplacement.
106 *
107 * @param array $params
108 *
109 * @return array
110 */
111 public static function create($params) {
112 if (array_key_exists("domain_id", $params) === FALSE) {
113 $params["domain_id"] = CRM_Core_Config::domainID();
114 }
115 $wordReplacement = new CRM_Core_DAO_WordReplacement();
116 $wordReplacement->copyValues($params);
117 $wordReplacement->save();
118 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
119 self::rebuild();
120 }
121 return $wordReplacement;
122 }
123
124 /**
125 * Delete website.
126 *
127 * @param int $id
128 * WordReplacement id.
129 *
130 * @return object
131 */
132 public static function del($id) {
133 $dao = new CRM_Core_DAO_WordReplacement();
134 $dao->id = $id;
135 $dao->delete();
136 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
137 self::rebuild();
138 }
139 return $dao;
140 }
141
142 /**
143 * Get all word-replacements in the form of an array.
144 *
145 * @param int $id
146 * Domain ID.
147 *
148 * @return array
149 * @see civicrm_domain.locale_custom_strings
150 */
151 public static function getAllAsConfigArray($id) {
152 $query = "
153 SELECT find_word,replace_word,is_active,match_type
154 FROM civicrm_word_replacement
155 WHERE domain_id = %1
156 ";
157 $params = array(1 => array($id, 'Integer'));
158
159 $dao = CRM_Core_DAO::executeQuery($query, $params);
160
161 $overrides = array();
162
163 while ($dao->fetch()) {
164 if ($dao->is_active == 1) {
165 $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
166 }
167 else {
168 $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
169 }
170 }
171 $config = CRM_Core_Config::singleton();
172 $domain = new CRM_Core_DAO_Domain();
173 $domain->find(TRUE);
174
175 // So. Weird. Some bizarre/probably-broken multi-lingual thing where
176 // data isn't really stored in civicrm_word_replacements. Probably
177 // shouldn't exist.
178 $stringOverride = self::_getLocaleCustomStrings($id);
179 $stringOverride[$config->lcMessages] = $overrides;
180
181 return $stringOverride;
182 }
183
184 /**
185 * Rebuild.
186 *
187 * @param bool $clearCaches
188 *
189 * @return bool
190 */
191 public static function rebuild($clearCaches = TRUE) {
192 $id = CRM_Core_Config::domainID();
193 self::_setLocaleCustomStrings($id, self::getAllAsConfigArray($id));
194
195 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
196 if ($clearCaches) {
197 // Reset navigation
198 CRM_Core_BAO_Navigation::resetNavigation();
199 // Clear js localization
200 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
201 }
202
203 return TRUE;
204 }
205
206 /**
207 * Get word replacements for the api.
208 *
209 * Get all the word-replacements stored in config-arrays
210 * and convert them to params for the 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 $wordReplacementCreateParams = array();
227 // get all domains
228 $result = civicrm_api3('domain', 'get', array(
229 'return' => array('locale_custom_strings'),
230 ));
231 if (!empty($result["values"])) {
232 foreach ($result["values"] as $value) {
233 $params = array();
234 $params["domain_id"] = $value["id"];
235 $params["options"] = array('wp-rebuild' => $rebuildEach);
236 // Unserialize word match string.
237 $localeCustomArray = unserialize($value["locale_custom_strings"]);
238 if (!empty($localeCustomArray)) {
239 $wordMatchArray = array();
240 // Traverse Language array
241 foreach ($localeCustomArray as $localCustomData) {
242 // Traverse status array "enabled" "disabled"
243 foreach ($localCustomData as $status => $matchTypes) {
244 $params["is_active"] = ($status == "enabled") ? TRUE : FALSE;
245 // Traverse Match Type array "wildcardMatch" "exactMatch"
246 foreach ($matchTypes as $matchType => $words) {
247 $params["match_type"] = $matchType;
248 foreach ($words as $word => $replace) {
249 $params["find_word"] = $word;
250 $params["replace_word"] = $replace;
251 $wordReplacementCreateParams[] = $params;
252 }
253 }
254 }
255 }
256 }
257 }
258 }
259 return $wordReplacementCreateParams;
260 }
261
262 /**
263 * Rebuild word replacements.
264 *
265 * Get all the word-replacements stored in config-arrays
266 * and write them out as records in civicrm_word_replacement.
267 *
268 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
269 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
270 * step behaves consistently even as the BAO evolves in future versions.
271 * However, if there's a bug in here prior to 4.4.0, we should apply the
272 * bug-fix in both places.
273 */
274 public static function rebuildWordReplacementTable() {
275 civicrm_api3('word_replacement', 'replace', array(
276 'options' => array('match' => array('domain_id', 'find_word')),
277 'values' => self::getConfigArraysAsAPIParams(FALSE),
278 ));
279 CRM_Core_BAO_WordReplacement::rebuild();
280 }
281
282 /**
283 * Get WordReplacements for a locale.
284 *
285 * @param string $locale
286 * @return array
287 * List of word replacements (enabled/disabled) for the given locale.
288 */
289 public static function getLocaleCustomStrings($locale, $domainId = NULL) {
290 if ($domainId === NULL) {
291 $domainId = CRM_Core_Config::domainID();
292 }
293
294 return CRM_Utils_Array::value($locale, self::_getLocaleCustomStrings($domainId));
295 }
296
297 private static function _getLocaleCustomStrings($domainId) {
298 // TODO: Would it be worthwhile using memcache here?
299 $domain = CRM_Core_DAO::executeQuery('SELECT locale_custom_strings FROM civicrm_domain WHERE id = %1', array(
300 1 => array($domainId, 'Integer'),
301 ));
302 while ($domain->fetch()) {
303 return empty($domain->locale_custom_strings) ? array() : unserialize($domain->locale_custom_strings);
304 }
305 }
306
307 public static function setLocaleCustomStrings($locale, $values, $domainId = NULL) {
308 if ($domainId === NULL) {
309 $domainId = CRM_Core_Config::domainID();
310 }
311
312 $lcs = self::_getLocaleCustomStrings($domainId);
313 $lcs[$locale] = $values;
314
315 self::_setLocaleCustomStrings($domainId, $lcs);
316 }
317
318 /**
319 * @param $domainId
320 * @param $lcs
321 */
322 private static function _setLocaleCustomStrings($domainId, $lcs) {
323 CRM_Core_DAO::executeQuery("UPDATE civicrm_domain SET locale_custom_strings = %1 WHERE id = %2", array(
324 1 => array(serialize($lcs), 'String'),
325 2 => array($domainId, 'Integer'),
326 ));
327 }
328
329 }