INFRA-132 - Comment grammar cleanup
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | CiviCRM version 4.6 |
5 +--------------------------------------------------------------------+
6 | Copyright CiviCRM LLC (c) 2004-2014 |
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-2014
32 * $Id$
33 *
34 */
35
36 /**
37 *
38 */
39 class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
40
41 /**
42 * Class constructor
43 *
44 * @return \CRM_Core_DAO_WordReplacement
45 */
46 /**
47 *
48 */
49 public function __construct() {
50 parent::__construct();
51 }
52 /**
53 * Takes a bunch of params that are needed to match certain criteria and
54 * retrieves the relevant objects.
55 *
56 * @param array $params
57 * (reference ) an assoc array of name/value pairs.
58 * @param array $defaults
59 * (reference ) an assoc array to hold the flattened values.
60 *
61 * @return CRM_Core_DAO_WordRepalcement object
62 * @static
63 */
64
65 public static function retrieve(&$params, &$defaults) {
66 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
67 }
68
69 /**
70 * Get the domain BAO
71 *
72 * @param null $reset
73 *
74 * @return null|object CRM_Core_BAO_WordRepalcement
75 * @static
76 */
77 public static function getWordReplacement($reset = NULL) {
78 static $wordReplacement = NULL;
79 if (!$wordReplacement || $reset) {
80 $wordReplacement = new CRM_Core_BAO_WordReplacement();
81 $wordReplacement->id = CRM_Core_Config::wordReplacementID();
82 if (!$wordReplacement->find(TRUE)) {
83 CRM_Core_Error::fatal();
84 }
85 }
86 return $wordReplacement;
87 }
88
89
90 /**
91 * Save the values of a WordReplacement
92 *
93 * @param array $params
94 * @param int $id
95 *
96 * @return WordReplacement array
97 */
98 public static function edit(&$params, &$id) {
99 $wordReplacement = new CRM_Core_DAO_WordReplacement();
100 $wordReplacement->id = $id;
101 $wordReplacement->copyValues($params);
102 $wordReplacement->save();
103 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
104 self::rebuild();
105 }
106 return $wordReplacement;
107 }
108
109 /**
110 * Create a new WordReplacement
111 *
112 * @param array $params
113 *
114 * @return WordReplacement array
115 */
116 public static function create($params) {
117 if (array_key_exists("domain_id", $params) === FALSE) {
118 $params["domain_id"] = CRM_Core_Config::domainID();
119 }
120 $wordReplacement = new CRM_Core_DAO_WordReplacement();
121 $wordReplacement->copyValues($params);
122 $wordReplacement->save();
123 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
124 self::rebuild();
125 }
126 return $wordReplacement;
127 }
128
129 /**
130 * Delete website
131 *
132 * @param int $id
133 * WordReplacement id.
134 *
135 * @return object
136 * @static
137 */
138 public static function del($id) {
139 $dao = new CRM_Core_DAO_WordReplacement();
140 $dao->id = $id;
141 $dao->delete();
142 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
143 self::rebuild();
144 }
145 return $dao;
146 }
147
148 /**
149 * Get all word-replacements in the form of an array
150 *
151 * @param int $id
152 * Domain ID.
153 * @return array
154 * @see civicrm_domain.locale_custom_strings
155 */
156 public static function getAllAsConfigArray($id) {
157 $query = "
158 SELECT find_word,replace_word,is_active,match_type
159 FROM civicrm_word_replacement
160 WHERE domain_id = %1
161 ";
162 $params = array(1 => array($id, 'Integer'));
163
164 $dao = CRM_Core_DAO::executeQuery($query, $params);
165
166 $overrides = array();
167
168 while ($dao->fetch()) {
169 if ($dao->is_active == 1) {
170 $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
171 }
172 else {
173 $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
174 }
175 }
176 $config = CRM_Core_Config::singleton();
177 $domain = new CRM_Core_DAO_Domain();
178 $domain->find(TRUE);
179
180 if ($domain->locales && $config->localeCustomStrings) {
181 // for multilingual
182 $addReplacements = $config->localeCustomStrings;
183 $addReplacements[$config->lcMessages] = $overrides;
184 $stringOverride = $addReplacements;
185 }
186 else {
187 // for single language
188 $stringOverride = array($config->lcMessages => $overrides);
189 }
190
191 return $stringOverride;
192 }
193
194 /**
195 * Rebuild
196 */
197 public static function rebuild($clearCaches = TRUE) {
198 $id = CRM_Core_Config::domainID();
199 $stringOverride = self::getAllAsConfigArray($id);
200 $params = array('locale_custom_strings' => serialize($stringOverride));
201 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
202 if ($wordReplacementSettings) {
203 CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride;
204
205 // Partially mitigate the inefficiency introduced in CRM-13187 by doing this conditionally
206 if ($clearCaches) {
207 // Reset navigation
208 CRM_Core_BAO_Navigation::resetNavigation();
209 // Clear js localization
210 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
211 }
212
213 return TRUE;
214 }
215
216 return FALSE;
217 }
218
219 /**
220 * Get all the word-replacements stored in config-arrays
221 * and convert them to params for the WordReplacement.create API.
222 *
223 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
224 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
225 * step behaves consistently even as the BAO evolves in future versions.
226 * However, if there's a bug in here prior to 4.4.0, we should apply the
227 * bugfix in both places.
228 *
229 * @param bool $rebuildEach
230 * Whether to perform rebuild after each individual API call.
231 * @return array Each item is $params for WordReplacement.create
232 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
233 */
234 public static function getConfigArraysAsAPIParams($rebuildEach) {
235 $wordReplacementCreateParams = array();
236 // get all domains
237 $result = civicrm_api3('domain', 'get', array(
238 'return' => array('locale_custom_strings'),
239 ));
240 if (!empty($result["values"])) {
241 foreach ($result["values"] as $value) {
242 $params = array();
243 $params["domain_id"] = $value["id"];
244 $params["options"] = array('wp-rebuild' => $rebuildEach);
245 // unserialize word match string
246 $localeCustomArray = unserialize($value["locale_custom_strings"]);
247 if (!empty($localeCustomArray)) {
248 $wordMatchArray = array();
249 // Traverse Language array
250 foreach ($localeCustomArray as $localCustomData) {
251 // Traverse status array "enabled" "disabled"
252 foreach ($localCustomData as $status => $matchTypes) {
253 $params["is_active"] = ($status == "enabled") ? TRUE : FALSE;
254 // Traverse Match Type array "wildcardMatch" "exactMatch"
255 foreach ($matchTypes as $matchType => $words) {
256 $params["match_type"] = $matchType;
257 foreach ($words as $word => $replace) {
258 $params["find_word"] = $word;
259 $params["replace_word"] = $replace;
260 $wordReplacementCreateParams[] = $params;
261 }
262 }
263 }
264 }
265 }
266 }
267 }
268 return $wordReplacementCreateParams;
269 }
270
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 * bugfix 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 }