Merge pull request #3838 from totten/master-phpunit-misc
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
CommitLineData
d83a3991 1<?php
2/*
3 +--------------------------------------------------------------------+
06b69b18 4 | CiviCRM version 4.5 |
d83a3991 5 +--------------------------------------------------------------------+
06b69b18 6 | Copyright CiviCRM LLC (c) 2004-2014 |
d83a3991 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
06b69b18 31 * @copyright CiviCRM LLC (c) 2004-2014
d83a3991 32 * $Id$
33 *
34 */
35
36/**
37 *
38 */
39class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
40
b5c2afd0
EM
41 /**
42 * class constructor
43 *
44 * @access public
45 * @return \CRM_Core_DAO_WordReplacement
46 */
47 /**
48 *
49 */
d83a3991 50 function __construct() {
51 parent::__construct();
6cf5bb6f 52 }
d83a3991 53 /**
54 * Takes a bunch of params that are needed to match certain criteria and
6cf5bb6f 55 * retrieves the relevant objects.
d83a3991 56 *
57 * @param array $params (reference ) an assoc array of name/value pairs
58 * @param array $defaults (reference ) an assoc array to hold the flattened values
59 *
60 * @return object CRM_Core_DAO_WordRepalcement object
61 * @access public
62 * @static
63 */
6cf5bb6f 64
d83a3991 65 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 *
77b97be7
EM
72 * @param null $reset
73 *
d83a3991 74 * @return null|object CRM_Core_BAO_WordRepalcement
75 * @access public
76 * @static
77 */
f01484bc 78 static function getWordReplacement($reset = NULL) {
d83a3991 79 static $wordReplacement = NULL;
80 if (!$wordReplacement || $reset) {
a36ecdd0 81 $wordReplacement = new CRM_Core_BAO_WordReplacement();
d83a3991 82 $wordReplacement->id = CRM_Core_Config::wordReplacementID();
83 if (!$wordReplacement->find(TRUE)) {
84 CRM_Core_Error::fatal();
85 }
86 }
87 return $wordReplacement;
88 }
89
90
91 /**
92 * Save the values of a WordReplacement
93 *
77b97be7
EM
94 * @param $params
95 * @param $id
96 *
d83a3991 97 * @return WordReplacement array
98 * @access public
99 */
100 static function edit(&$params, &$id) {
101 $wordReplacement = new CRM_Core_DAO_WordReplacement();
102 $wordReplacement->id = $id;
103 $wordReplacement->copyValues($params);
104 $wordReplacement->save();
63b71ea8
TO
105 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
106 self::rebuild();
107 }
d83a3991 108 return $wordReplacement;
109 }
110
111 /**
112 * Create a new WordReplacement
113 *
dd244018
EM
114 * @param $params
115 *
d83a3991 116 * @return WordReplacement array
117 * @access public
118 */
119 static function create($params) {
f01484bc 120 if(array_key_exists("domain_id",$params) === FALSE) {
6cf5bb6f
DL
121 $params["domain_id"] = CRM_Core_Config::domainID();
122 }
d83a3991 123 $wordReplacement = new CRM_Core_DAO_WordReplacement();
124 $wordReplacement->copyValues($params);
125 $wordReplacement->save();
63b71ea8
TO
126 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
127 self::rebuild();
128 }
d83a3991 129 return $wordReplacement;
130 }
6cf5bb6f 131
d83a3991 132 /**
133 * Delete website
134 *
135 * @param int $id WordReplacement id
136 *
137 * @return object
138 * @static
139 */
140 static function del($id) {
141 $dao = new CRM_Core_DAO_WordReplacement();
142 $dao->id = $id;
143 $dao->delete();
63b71ea8
TO
144 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
145 self::rebuild();
146 }
d83a3991 147 return $dao;
148 }
d83a3991 149
f01484bc
TO
150 /**
151 * Get all word-replacements in the form of an array
152 *
153 * @param int $id domain ID
154 * @return array
155 * @see civicrm_domain.locale_custom_strings
156 */
157 public static function getAllAsConfigArray($id) {
6cf5bb6f
DL
158 $query = "
159SELECT find_word,replace_word,is_active,match_type
160FROM civicrm_word_replacement
161WHERE domain_id = %1
162";
163 $params = array( 1 => array($id, 'Integer'));
164
165 $dao = CRM_Core_DAO::executeQuery($query, $params);
166
167 $overrides = array();
168
d83a3991 169 while ($dao->fetch()) {
675605a7 170 if ($dao->is_active==1) {
a36ecdd0
E
171 $overrides['enabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
172 }
173 else {
174 $overrides['disabled'][$dao->match_type][$dao->find_word] = $dao->replace_word;
675605a7 175 }
d83a3991 176 }
d83a3991 177 $config = CRM_Core_Config::singleton();
178 $domain = new CRM_Core_DAO_Domain();
179 $domain->find(TRUE);
180
181 if ($domain->locales && $config->localeCustomStrings) {
182 // for multilingual
183 $addReplacements = $config->localeCustomStrings;
184 $addReplacements[$config->lcMessages] = $overrides;
f01484bc 185 $stringOverride = $addReplacements;
d83a3991 186 }
187 else {
188 // for single language
f01484bc 189 $stringOverride = array($config->lcMessages => $overrides);
d83a3991 190 }
191
f01484bc
TO
192 return $stringOverride;
193 }
d83a3991 194
f01484bc
TO
195 /**
196 * Rebuild
197 */
198 static function rebuild() {
199 $id = CRM_Core_Config::domainID();
200 $stringOverride = self::getAllAsConfigArray($id);
201 $params = array('locale_custom_strings' => serialize($stringOverride));
d83a3991 202 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
d83a3991 203 if ($wordReplacementSettings) {
0f65e834
TO
204 CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride;
205
d83a3991 206 // Reset navigation
207 CRM_Core_BAO_Navigation::resetNavigation();
208 // Clear js string cache
209 CRM_Core_Resources::singleton()->flushStrings();
76dca235
CW
210 // Clear dynamic js files which may contain localization
211 CRM_Utils_File::flushDynamicResources();
f01484bc
TO
212
213 return TRUE;
d83a3991 214 }
f01484bc
TO
215
216 return FALSE;
d83a3991 217 }
0f65e834
TO
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 whether to perform rebuild after each individual API call
230 * @return array Each item is $params for WordReplacement.create
231 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
232 */
233 static function getConfigArraysAsAPIParams($rebuildEach) {
234 $wordReplacementCreateParams = array();
235 // get all domains
236 $result = civicrm_api3('domain', 'get', array(
675605a7 237 'return' => array('locale_custom_strings'),
238 ));
0f65e834
TO
239 if (!empty($result["values"])) {
240 foreach ($result["values"] as $value) {
241 $params = array();
0f65e834
TO
242 $params["domain_id"] = $value["id"];
243 $params["options"] = array('wp-rebuild' => $rebuildEach);
244 // unserialize word match string
245 $localeCustomArray = unserialize($value["locale_custom_strings"]);
246 if (!empty($localeCustomArray)) {
247 $wordMatchArray = array();
675605a7 248 // Traverse Language array
0f65e834 249 foreach ($localeCustomArray as $localCustomData) {
a36ecdd0
E
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 }
0f65e834
TO
263 }
264 }
265 }
266 }
267 return $wordReplacementCreateParams;
268 }
269
270 /**
271 * Get all the word-replacements stored in config-arrays
272 * and write them out as records in civicrm_word_replacement.
273 *
274 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
275 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
276 * step behaves consistently even as the BAO evolves in future versions.
277 * However, if there's a bug in here prior to 4.4.0, we should apply the
278 * bugfix in both places.
279 */
280 public static function rebuildWordReplacementTable() {
281 civicrm_api3('word_replacement', 'replace', array(
282 'options' => array('match' => array('domain_id', 'find_word')),
283 'values' => self::getConfigArraysAsAPIParams(FALSE),
284 ));
285 CRM_Core_BAO_WordReplacement::rebuild();
286 }
d83a3991 287}
288