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