CRM-13187 - CRM_Admin_Form_WordReplacements
[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();
43 }
44 /**
45 * Takes a bunch of params that are needed to match certain criteria and
46 * retrieves the relevant objects.
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 */
55
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) {
70 $wordReplacement = new CRM_Core_BAO_WordRepalcement();
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) {
d83a3991 105 $params["domain_id"] = CRM_Core_Config::domainID();
106 }
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 }
115
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) {
d83a3991 142 $query = "SELECT find_word,replace_word FROM civicrm_word_replacement WHERE is_active = 1 AND domain_id = ".CRM_Utils_Type::escape($id, 'Integer');
143 $dao = CRM_Core_DAO::executeQuery($query);
f01484bc
TO
144 $wordReplacement = array();
145
d83a3991 146 while ($dao->fetch()) {
147 $wordReplacement[$dao->find_word] = $dao->replace_word;
148 }
f01484bc 149
d83a3991 150 $overrides['enabled']['wildcardMatch'] = $wordReplacement;
f01484bc 151
d83a3991 152 $config = CRM_Core_Config::singleton();
153 $domain = new CRM_Core_DAO_Domain();
154 $domain->find(TRUE);
155
156 if ($domain->locales && $config->localeCustomStrings) {
157 // for multilingual
158 $addReplacements = $config->localeCustomStrings;
159 $addReplacements[$config->lcMessages] = $overrides;
f01484bc 160 $stringOverride = $addReplacements;
d83a3991 161 }
162 else {
163 // for single language
f01484bc 164 $stringOverride = array($config->lcMessages => $overrides);
d83a3991 165 }
166
f01484bc
TO
167 return $stringOverride;
168 }
d83a3991 169
f01484bc
TO
170 /**
171 * Rebuild
172 */
173 static function rebuild() {
174 $id = CRM_Core_Config::domainID();
175 $stringOverride = self::getAllAsConfigArray($id);
176 $params = array('locale_custom_strings' => serialize($stringOverride));
d83a3991 177 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
178
179 if ($wordReplacementSettings) {
0f65e834
TO
180 CRM_Core_Config::singleton()->localeCustomStrings = $stringOverride;
181
d83a3991 182 // Reset navigation
183 CRM_Core_BAO_Navigation::resetNavigation();
184 // Clear js string cache
185 CRM_Core_Resources::singleton()->flushStrings();
f01484bc
TO
186
187 return TRUE;
d83a3991 188 }
f01484bc
TO
189
190 return FALSE;
d83a3991 191 }
0f65e834
TO
192
193 /**
194 * Get all the word-replacements stored in config-arrays
195 * and convert them to params for the WordReplacement.create API.
196 *
197 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
198 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
199 * step behaves consistently even as the BAO evolves in future versions.
200 * However, if there's a bug in here prior to 4.4.0, we should apply the
201 * bugfix in both places.
202 *
203 * @param bool $rebuildEach whether to perform rebuild after each individual API call
204 * @return array Each item is $params for WordReplacement.create
205 * @see CRM_Core_BAO_WordReplacement::convertConfigArraysToAPIParams
206 */
207 static function getConfigArraysAsAPIParams($rebuildEach) {
208 $wordReplacementCreateParams = array();
209 // get all domains
210 $result = civicrm_api3('domain', 'get', array(
211 'return' => array('locale_custom_strings'),
212 ));
213 if (!empty($result["values"])) {
214 foreach ($result["values"] as $value) {
215 $params = array();
216 $params["is_active"] = TRUE;
217 $params["domain_id"] = $value["id"];
218 $params["options"] = array('wp-rebuild' => $rebuildEach);
219 // unserialize word match string
220 $localeCustomArray = unserialize($value["locale_custom_strings"]);
221 if (!empty($localeCustomArray)) {
222 $wordMatchArray = array();
223 foreach ($localeCustomArray as $localCustomData) {
224 $wordMatchArray = $localCustomData["enabled"]["wildcardMatch"];
225 }
226
227 if (!empty($wordMatchArray)) {
228 foreach ($wordMatchArray as $word => $replace) {
229 $params["find_word"] = $word;
230 $params["replace_word"] = $replace;
231 $wordReplacementCreateParams[] = $params;
232 }
233 }
234 }
235 }
236 }
237 return $wordReplacementCreateParams;
238 }
239
240 /**
241 * Get all the word-replacements stored in config-arrays
242 * and write them out as records in civicrm_word_replacement.
243 *
244 * Note: This function is duplicated in CRM_Core_BAO_WordReplacement and
245 * CRM_Upgrade_Incremental_php_FourFour to ensure that the incremental upgrade
246 * step behaves consistently even as the BAO evolves in future versions.
247 * However, if there's a bug in here prior to 4.4.0, we should apply the
248 * bugfix in both places.
249 */
250 public static function rebuildWordReplacementTable() {
251 civicrm_api3('word_replacement', 'replace', array(
252 'options' => array('match' => array('domain_id', 'find_word')),
253 'values' => self::getConfigArraysAsAPIParams(FALSE),
254 ));
255 CRM_Core_BAO_WordReplacement::rebuild();
256 }
d83a3991 257}
258