2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM Community Edition is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
7 * This program is free software; you can redistribute it and/or modify it under
8 * the terms of the GNU Affero General Public License version 3 as published by the
9 * Free Software Foundation with the addition of the following permission added
10 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
14 * This program is distributed in the hope that it will be useful, but WITHOUT
15 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16 * FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
19 * You should have received a copy of the GNU Affero General Public License along with
20 * this program; if not, see http://www.gnu.org/licenses or write to the Free
21 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25 * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
27 * The interactive user interfaces in modified source and object code versions
28 * of this program must display Appropriate Legal Notices, as required under
29 * Section 5 of the GNU Affero General Public License version 3.
31 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32 * these Appropriate Legal Notices must retain the display of the "Powered by
33 * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34 * technical reasons, the Appropriate Legal Notices must display the words
35 * "Powered by SugarCRM".
36 ********************************************************************************/
44 function addToQuery($name, $val){
45 $this->query[$name] = $val;
51 * This function handles saving the query parameters to the user preferences
52 * SavedSearch.php does something very similar when saving saved searches as well
55 * @param $name String name to identify this query
57 function SaveQuery($name)
59 global $current_user, $timedate;
60 if(isset($this->query['module']))
62 $bean = loadBean($this->query['module']);
65 foreach($this->query as $key=>$value)
67 //Filter date fields to ensure it is saved to DB format, but also avoid empty values
68 if(!empty($value) && preg_match('/^(start_range_|end_range_|range_)?(.*?)(_advanced|_basic)$/', $key, $match))
71 if(isset($bean->field_defs[$field]['type']) && empty($bean->field_defs[$field]['disable_num_format']))
73 $type = $bean->field_defs[$field]['type'];
75 if(($type == 'date' || $type == 'datetime' || $type == 'datetimecombo') && !preg_match('/^\[.*?\]$/', $value))
77 $db_format = $timedate->to_db_date($value, false);
78 $this->query[$key] = $db_format;
79 } else if ($type == 'int' || $type == 'currency' || $type == 'decimal' || $type == 'float') {
80 if(preg_match('/[^\d]/', $value)) {
81 require_once('modules/Currencies/Currency.php');
82 $this->query[$key] = unformat_number($value);
83 //Flag this value as having been unformatted
84 $this->query[$key . '_unformatted_number'] = true;
85 //If the type is of currency and there was a currency symbol (non-digit), save the symbol
86 if($type == 'currency' && preg_match('/^([^\d])/', $value, $match))
88 $this->query[$key . '_currency_symbol'] = $match[1];
92 if(isset($this->query[$key . '_unformatted_number']))
94 unset($this->query[$key . '_unformatted_number']);
97 if(isset($this->query[$key . '_currency_symbol']))
99 unset($this->query[$key . '_currency_symbol']);
109 $current_user->setPreference($name.'Q', $this->query);
112 function clearQuery($name){
113 $this->query = array();
114 $this->saveQuery($name);
117 function loadQuery($name){
118 $saveType = $this->getSaveType($name);
119 if($saveType == 'all' || $saveType == 'myitems'){
120 global $current_user;
121 $this->query = StoreQuery::getStoredQueryForUser($name);
122 if(empty($this->query)){
123 $this->query = array();
125 if(!empty($this->populate_only) && !empty($this->query['query'])){
126 $this->query['query'] = 'MSI';
131 function populateRequest()
135 if(isset($this->query['module']))
137 $bean = loadBean($this->query['module']);
141 foreach($this->query as $key=>$value)
143 // todo wp: remove this
144 if($key != 'advanced' && $key != 'module' && $key != 'lvso')
146 //Filter date fields to ensure it is saved to DB format, but also avoid empty values
147 if(!empty($value) && !empty($bean) && preg_match('/^(start_range_|end_range_|range_)?(.*?)(_advanced|_basic)$/', $key, $match))
150 if(isset($bean->field_defs[$field]['type']) && empty($bean->field_defs[$field]['disable_num_format']))
152 $type = $bean->field_defs[$field]['type'];
154 if(($type == 'date' || $type == 'datetime' || $type == 'datetimecombo') && preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) && !preg_match('/^\[.*?\]$/', $value))
156 $value = $timedate->to_display_date($value, false);
157 } else if (($type == 'int' || $type == 'currency' || $type == 'decimal' || $type == 'float') && isset($this->query[$key . '_unformatted_number']) && preg_match('/^\d+$/', $value)) {
158 require_once('modules/Currencies/Currency.php');
159 $value = format_number($value);
160 if($type == 'currency' && isset($this->query[$key . '_currency_symbol']))
162 $value = $this->query[$key . '_currency_symbol'] . $value;
168 // cn: bug 6546 storequery stomps correct value for 'module' in Activities
169 $_REQUEST[$key] = $value;
170 $_GET[$key] = $value;
176 function getSaveType($name)
178 global $sugar_config;
179 $save_query = empty($sugar_config['save_query']) ?
180 'all' : $sugar_config['save_query'];
182 if(is_array($save_query))
184 if(isset($save_query[$name]))
186 $saveType = $save_query[$name];
188 elseif(isset($save_query['default']))
190 $saveType = $save_query['default'];
199 $saveType = $save_query;
201 if($saveType == 'populate_only'){
203 $this->populate_only = true;
209 function saveFromRequest($name){
210 if(isset($_REQUEST['query'])){
211 if(!empty($_REQUEST['clear_query']) && $_REQUEST['clear_query'] == 'true'){
212 $this->clearQuery($name);
215 $saveType = $this->getSaveType($name);
217 if($saveType == 'myitems'){
218 if(!empty($_REQUEST['current_user_only'])){
219 $this->query['current_user_only'] = $_REQUEST['current_user_only'];
220 $this->query['query'] = true;
222 $this->saveQuery($name);
224 }else if($saveType == 'all'){
225 // Bug 39580 - Added 'EmailTreeLayout','EmailGridWidths' to the list as these are added merely as side-effects of the fact that we store the entire
226 // $_REQUEST object which includes all cookies. These are potentially quite long strings as well.
227 $blockVariables = array('mass', 'uid', 'massupdate', 'delete', 'merge', 'selectCount', 'current_query_by_page','EmailTreeLayout','EmailGridWidths');
228 $this->query = $_REQUEST;
229 foreach($blockVariables as $block) {
230 unset($this->query[$block]);
232 $this->saveQuery($name);
237 function saveFromGet($name){
238 if(isset($_GET['query'])){
239 if(!empty($_GET['clear_query']) && $_GET['clear_query'] == 'true'){
240 $this->clearQuery($name);
243 $saveType = $this->getSaveType($name);
245 if($saveType == 'myitems'){
246 if(!empty($_GET['current_user_only'])){
247 $this->query['current_user_only'] = $_GET['current_user_only'];
248 $this->query['query'] = true;
250 $this->saveQuery($name);
252 }else if($saveType == 'all'){
253 $this->query = $_GET;
254 $this->saveQuery($name);
260 * Static method to retrieve the user's stored query for a particular module
262 * @param string $module
265 public static function getStoredQueryForUser($module)
267 global $current_user;
268 return $current_user->getPreference($module.'Q');