Test fixes for All kind of Custom Values
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
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 if ($domain->locales && $config->localeCustomStrings) {
176 // for multilingual
177 $addReplacements = $config->localeCustomStrings;
178 $addReplacements[$config->lcMessages] = $overrides;
179 $stringOverride = $addReplacements;
180 }
181 else {
182 // for single language
183 $stringOverride = array($config->lcMessages => $overrides);
184 }
185
186 return $stringOverride;
187 }
188
189 /**
190 * Rebuild.
191 *
192 * @param bool $clearCaches
193 *
194 * @return bool
195 */
196 public static function rebuild($clearCaches = TRUE) {
197 $id = CRM_Core_Config::domainID();
198 $stringOverride = self::getAllAsConfigArray($id);
199 $params = array('locale_custom_strings' => serialize($stringOverride));
200 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
201 if ($wordReplacementSettings) {
202 CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride;
203
204 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
205 if ($clearCaches) {
206 // Reset navigation
207 CRM_Core_BAO_Navigation::resetNavigation();
208 // Clear js localization
209 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
210 }
211
212 return TRUE;
213 }
214
215 return FALSE;
216 }
217
218 /**
219 * Get word replacements for the api.
220 *
221 * Get all the word-replacements stored in config-arrays
222 * and convert them to params for the WordReplacement.create API.
223 *
224 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
225 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
226 * step behaves consistently even as the BAO evolves in future versions.
227 * However, if there's a bug in here prior to 4.4.0, we should apply the
228 * bug-fix in both places.
229 *
230 * @param bool $rebuildEach
231 * Whether to perform rebuild after each individual API call.
232 *
233 * @return array
234 * Each item is $params for WordReplacement.create
235 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
236 */
237 public static function getConfigArraysAsAPIParams($rebuildEach) {
238 $wordReplacementCreateParams = array();
239 // get all domains
240 $result = civicrm_api3('domain', 'get', array(
241 'return' => array('locale_custom_strings'),
242 ));
243 if (!empty($result["values"])) {
244 foreach ($result["values"] as $value) {
245 $params = array();
246 $params["domain_id"] = $value["id"];
247 $params["options"] = array('wp-rebuild' => $rebuildEach);
248 // Unserialize word match string.
249 $localeCustomArray = unserialize($value["locale_custom_strings"]);
250 if (!empty($localeCustomArray)) {
251 $wordMatchArray = array();
252 // Traverse Language array
253 foreach ($localeCustomArray as $localCustomData) {
254 // Traverse status array "enabled" "disabled"
255 foreach ($localCustomData as $status => $matchTypes) {
256 $params["is_active"] = ($status == "enabled") ? TRUE : FALSE;
257 // Traverse Match Type array "wildcardMatch" "exactMatch"
258 foreach ($matchTypes as $matchType => $words) {
259 $params["match_type"] = $matchType;
260 foreach ($words as $word => $replace) {
261 $params["find_word"] = $word;
262 $params["replace_word"] = $replace;
263 $wordReplacementCreateParams[] = $params;
264 }
265 }
266 }
267 }
268 }
269 }
270 }
271 return $wordReplacementCreateParams;
272 }
273
274 /**
275 * Rebuild word replacements.
276 *
277 * Get all the word-replacements stored in config-arrays
278 * and write them out as records in civicrm_word_replacement.
279 *
280 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
281 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
282 * step behaves consistently even as the BAO evolves in future versions.
283 * However, if there's a bug in here prior to 4.4.0, we should apply the
284 * bug-fix in both places.
285 */
286 public static function rebuildWordReplacementTable() {
287 civicrm_api3('word_replacement', 'replace', array(
288 'options' => array('match' => array('domain_id', 'find_word')),
289 'values' => self::getConfigArraysAsAPIParams(FALSE),
290 ));
291 CRM_Core_BAO_WordReplacement::rebuild();
292 }
293
294 }