2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4 * SugarCRM is a customer relationship management program developed by
5 * SugarCRM, Inc. Copyright (C) 2004-2011 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']))
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';
132 function populateRequest()
136 if(isset($this->query['module']))
138 $bean = loadBean($this->query['module']);
141 foreach($this->query as $key=>$value)
143 // todo wp: remove this
144 if($key != 'advanced' && $key != 'module')
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']))
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;
175 function getSaveType($name)
177 global $sugar_config;
178 $save_query = empty($sugar_config['save_query']) ?
179 'all' : $sugar_config['save_query'];
181 if(is_array($save_query))
183 if(isset($save_query[$name]))
185 $saveType = $save_query[$name];
187 elseif(isset($save_query['default']))
189 $saveType = $save_query['default'];
198 $saveType = $save_query;
200 if($saveType == 'populate_only'){
202 $this->populate_only = true;
208 function saveFromRequest($name){
209 if(isset($_REQUEST['query'])){
210 if(!empty($_REQUEST['clear_query']) && $_REQUEST['clear_query'] == 'true'){
211 $this->clearQuery($name);
214 $saveType = $this->getSaveType($name);
216 if($saveType == 'myitems'){
217 if(!empty($_REQUEST['current_user_only'])){
218 $this->query['current_user_only'] = $_REQUEST['current_user_only'];
219 $this->query['query'] = true;
221 $this->saveQuery($name);
223 }else if($saveType == 'all'){
224 // Bug 39580 - Added 'EmailTreeLayout','EmailGridWidths' to the list as these are added merely as side-effects of the fact that we store the entire
225 // $_REQUEST object which includes all cookies. These are potentially quite long strings as well.
226 $blockVariables = array('mass', 'uid', 'massupdate', 'delete', 'merge', 'selectCount', 'current_query_by_page','EmailTreeLayout','EmailGridWidths');
227 $this->query = $_REQUEST;
228 foreach($blockVariables as $block) {
229 unset($this->query[$block]);
231 $this->saveQuery($name);
236 function saveFromGet($name){
237 if(isset($_GET['query'])){
238 if(!empty($_GET['clear_query']) && $_GET['clear_query'] == 'true'){
239 $this->clearQuery($name);
242 $saveType = $this->getSaveType($name);
244 if($saveType == 'myitems'){
245 if(!empty($_GET['current_user_only'])){
246 $this->query['current_user_only'] = $_GET['current_user_only'];
247 $this->query['query'] = true;
249 $this->saveQuery($name);
251 }else if($saveType == 'all'){
252 $this->query = $_GET;
253 $this->saveQuery($name);
259 * Static method to retrieve the user's stored query for a particular module
261 * @param string $module
264 public static function getStoredQueryForUser($module){
265 global $current_user;
266 return $current_user->getPreference($module.'Q');