fix error where 4.6 change was not merged correctly to master.
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
CommitLineData
d83a3991 1<?php
2/*
3 +--------------------------------------------------------------------+
39de6fd5 4 | CiviCRM version 4.6 |
d83a3991 5 +--------------------------------------------------------------------+
e7112fa7 6 | Copyright CiviCRM LLC (c) 2004-2015 |
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
e7112fa7 31 * @copyright CiviCRM LLC (c) 2004-2015
d83a3991 32 * $Id$
d83a3991 33 */
34
35/**
d09edf64 36 * Class CRM_Core_BAO_WordReplacement.
d83a3991 37 */
38class CRM_Core_BAO_WordReplacement extends CRM_Core_DAO_WordReplacement {
39
b5c2afd0 40 /**
d09edf64 41 * Class constructor.
b5c2afd0 42 */
00be9182 43 public function __construct() {
d83a3991 44 parent::__construct();
6cf5bb6f 45 }
353ffa53 46
d83a3991 47 /**
d09edf64
EM
48 * Function that must have never worked & should be removed.
49 *
fe482240
EM
50 * Retrieve DB object based on input parameters.
51 *
52 * It also stores all the retrieved values in the default array.
d83a3991 53 *
6a0b768e
TO
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.
d83a3991 58 *
16b10e64 59 * @return CRM_Core_DAO_WordRepalcement
d83a3991 60 */
00be9182 61 public static function retrieve(&$params, &$defaults) {
d83a3991 62 return CRM_Core_DAO::commonRetrieve('CRM_Core_DAO_WordRepalcement', $params, $defaults);
63 }
64
65 /**
d09edf64 66 * Get the domain BAO.
d83a3991 67 *
77b97be7
EM
68 * @param null $reset
69 *
d09edf64 70 * @return null|CRM_Core_BAO_WordReplacement
d83a3991 71 */
00be9182 72 public static function getWordReplacement($reset = NULL) {
d83a3991 73 static $wordReplacement = NULL;
74 if (!$wordReplacement || $reset) {
a36ecdd0 75 $wordReplacement = new CRM_Core_BAO_WordReplacement();
d83a3991 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 /**
d09edf64 86 * Save the values of a WordReplacement.
d83a3991 87 *
c490a46a 88 * @param array $params
100fef9d 89 * @param int $id
77b97be7 90 *
72b3a70c 91 * @return array
d83a3991 92 */
00be9182 93 public static function edit(&$params, &$id) {
d83a3991 94 $wordReplacement = new CRM_Core_DAO_WordReplacement();
95 $wordReplacement->id = $id;
96 $wordReplacement->copyValues($params);
97 $wordReplacement->save();
63b71ea8
TO
98 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
99 self::rebuild();
100 }
d83a3991 101 return $wordReplacement;
102 }
103
104 /**
d09edf64 105 * Create a new WordReplacement.
d83a3991 106 *
c490a46a 107 * @param array $params
dd244018 108 *
72b3a70c 109 * @return array
d83a3991 110 */
00be9182 111 public static function create($params) {
22e263ad 112 if (array_key_exists("domain_id", $params) === FALSE) {
6cf5bb6f
DL
113 $params["domain_id"] = CRM_Core_Config::domainID();
114 }
d83a3991 115 $wordReplacement = new CRM_Core_DAO_WordReplacement();
116 $wordReplacement->copyValues($params);
117 $wordReplacement->save();
63b71ea8
TO
118 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
119 self::rebuild();
120 }
d83a3991 121 return $wordReplacement;
122 }
6cf5bb6f 123
d83a3991 124 /**
d09edf64 125 * Delete website.
d83a3991 126 *
6a0b768e
TO
127 * @param int $id
128 * WordReplacement id.
d83a3991 129 *
130 * @return object
d83a3991 131 */
00be9182 132 public static function del($id) {
d83a3991 133 $dao = new CRM_Core_DAO_WordReplacement();
134 $dao->id = $id;
135 $dao->delete();
63b71ea8
TO
136 if (!isset($params['options']) || CRM_Utils_Array::value('wp-rebuild', $params['options'], TRUE)) {
137 self::rebuild();
138 }
d83a3991 139 return $dao;
140 }
d83a3991 141
f01484bc 142 /**
d09edf64 143 * Get all word-replacements in the form of an array.
f01484bc 144 *
6a0b768e
TO
145 * @param int $id
146 * Domain ID.
d09edf64 147 *
f01484bc
TO
148 * @return array
149 * @see civicrm_domain.locale_custom_strings
150 */
151 public static function getAllAsConfigArray($id) {
6cf5bb6f
DL
152 $query = "
153SELECT find_word,replace_word,is_active,match_type
154FROM civicrm_word_replacement
155WHERE domain_id = %1
156";
481a74f4 157 $params = array(1 => array($id, 'Integer'));
6cf5bb6f
DL
158
159 $dao = CRM_Core_DAO::executeQuery($query, $params);
160
161 $overrides = array();
162
d83a3991 163 while ($dao->fetch()) {
2aa397bc 164 if ($dao->is_active == 1) {
a36ecdd0
E
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;
675605a7 169 }
d83a3991 170 }
d83a3991 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;
f01484bc 179 $stringOverride = $addReplacements;
d83a3991 180 }
181 else {
182 // for single language
f01484bc 183 $stringOverride = array($config->lcMessages => $overrides);
d83a3991 184 }
185
f01484bc
TO
186 return $stringOverride;
187 }
d83a3991 188
f01484bc 189 /**
d09edf64
EM
190 * Rebuild.
191 *
192 * @param bool $clearCaches
193 *
194 * @return bool
f01484bc 195 */
00be9182 196 public static function rebuild($clearCaches = TRUE) {
f01484bc
TO
197 $id = CRM_Core_Config::domainID();
198 $stringOverride = self::getAllAsConfigArray($id);
199 $params = array('locale_custom_strings' => serialize($stringOverride));
d83a3991 200 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
d83a3991 201 if ($wordReplacementSettings) {
0f65e834
TO
202 CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride;
203
9762f6ff
CW
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
4cc9b813 209 CRM_Core_Resources::singleton()->flushStrings()->resetCacheCode();
9762f6ff 210 }
f01484bc
TO
211
212 return TRUE;
d83a3991 213 }
f01484bc
TO
214
215 return FALSE;
d83a3991 216 }
0f65e834
TO
217
218 /**
d09edf64
EM
219 * Get word replacements for the api.
220 *
0f65e834
TO
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
d09edf64 228 * bug-fix in both places.
0f65e834 229 *
6a0b768e
TO
230 * @param bool $rebuildEach
231 * Whether to perform rebuild after each individual API call.
d09edf64 232 *
a6c01b45
CW
233 * @return array
234 * Each item is $params for WordReplacement.create
0f65e834
TO
235 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
236 */
00be9182 237 public static function getConfigArraysAsAPIParams($rebuildEach) {
0f65e834
TO
238 $wordReplacementCreateParams = array();
239 // get all domains
240 $result = civicrm_api3('domain', 'get', array(
353ffa53
TO
241 'return' => array('locale_custom_strings'),
242 ));
0f65e834
TO
243 if (!empty($result["values"])) {
244 foreach ($result["values"] as $value) {
245 $params = array();
0f65e834
TO
246 $params["domain_id"] = $value["id"];
247 $params["options"] = array('wp-rebuild' => $rebuildEach);
d09edf64 248 // Unserialize word match string.
0f65e834
TO
249 $localeCustomArray = unserialize($value["locale_custom_strings"]);
250 if (!empty($localeCustomArray)) {
251 $wordMatchArray = array();
675605a7 252 // Traverse Language array
0f65e834 253 foreach ($localeCustomArray as $localCustomData) {
2aa397bc 254 // Traverse status array "enabled" "disabled"
a36ecdd0 255 foreach ($localCustomData as $status => $matchTypes) {
2aa397bc 256 $params["is_active"] = ($status == "enabled") ? TRUE : FALSE;
a36ecdd0
E
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 }
0f65e834
TO
267 }
268 }
269 }
270 }
271 return $wordReplacementCreateParams;
272 }
273
274 /**
d09edf64
EM
275 * Rebuild word replacements.
276 *
0f65e834
TO
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
d09edf64 284 * bug-fix in both places.
0f65e834
TO
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 }
96025800 293
d83a3991 294}