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