CRM-13187 - CRM_Core_BAO_WordReplacement - Extract getAllAsConfigArray() from rebuild()
[civicrm-core.git] / CRM / Core / BAO / WordReplacement.php
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 */
39 class 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 */
67 static function getWordReplacement($reset = NULL) {
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();
91 self::rebuild();
92 return $wordReplacement;
93 }
94
95 /**
96 * Create a new WordReplacement
97 *
98 * @return WordReplacement array
99 * @access public
100 */
101 static function create($params) {
102 if(array_key_exists("domain_id",$params) === FALSE) {
103 $params["domain_id"] = CRM_Core_Config::domainID();
104 }
105 $wordReplacement = new CRM_Core_DAO_WordReplacement();
106 $wordReplacement->copyValues($params);
107 $wordReplacement->save();
108 self::rebuild();
109 return $wordReplacement;
110 }
111
112 /**
113 * Delete website
114 *
115 * @param int $id WordReplacement id
116 *
117 * @return object
118 * @static
119 */
120 static function del($id) {
121 $dao = new CRM_Core_DAO_WordReplacement();
122 $dao->id = $id;
123 $dao->delete();
124 self::rebuild();
125 return $dao;
126 }
127
128 /**
129 * Get all word-replacements in the form of an array
130 *
131 * @param int $id domain ID
132 * @return array
133 * @see civicrm_domain.locale_custom_strings
134 */
135 public static function getAllAsConfigArray($id) {
136 $query = "SELECT find_word,replace_word FROM civicrm_word_replacement WHERE is_active = 1 AND domain_id = ".CRM_Utils_Type::escape($id, 'Integer');
137 $dao = CRM_Core_DAO::executeQuery($query);
138 $wordReplacement = array();
139
140 while ($dao->fetch()) {
141 $wordReplacement[$dao->find_word] = $dao->replace_word;
142 }
143
144 $overrides['enabled']['wildcardMatch'] = $wordReplacement;
145
146 $config = CRM_Core_Config::singleton();
147 $domain = new CRM_Core_DAO_Domain();
148 $domain->find(TRUE);
149
150 if ($domain->locales && $config->localeCustomStrings) {
151 // for multilingual
152 $addReplacements = $config->localeCustomStrings;
153 $addReplacements[$config->lcMessages] = $overrides;
154 $stringOverride = $addReplacements;
155 }
156 else {
157 // for single language
158 $stringOverride = array($config->lcMessages => $overrides);
159 }
160
161 return $stringOverride;
162 }
163
164 /**
165 * Rebuild
166 */
167 static function rebuild() {
168 $id = CRM_Core_Config::domainID();
169 $stringOverride = self::getAllAsConfigArray($id);
170 $params = array('locale_custom_strings' => serialize($stringOverride));
171 $wordReplacementSettings = CRM_Core_BAO_Domain::edit($params, $id);
172
173 if ($wordReplacementSettings) {
174 // Reset navigation
175 CRM_Core_BAO_Navigation::resetNavigation();
176 // Clear js string cache
177 CRM_Core_Resources::singleton()->flushStrings();
178
179 return TRUE;
180 }
181
182 return FALSE;
183 }
184
185 }
186