]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/MySettings/StoreQuery.php
Release 6.5.0
[Github/sugarcrm.git] / modules / MySettings / StoreQuery.php
1 <?php
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.
6  * 
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.
13  * 
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
17  * details.
18  * 
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
22  * 02110-1301 USA.
23  * 
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.
26  * 
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.
30  * 
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  ********************************************************************************/
37
38
39
40
41 class StoreQuery{
42         var $query = array();
43         
44         function addToQuery($name, $val){
45                 $this->query[$name] = $val;     
46         }
47         
48         /**
49          * SaveQuery
50          *  
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
53          * 
54          * @see SavedSearch
55          * @param $name String name  to identify this query
56          */
57         function SaveQuery($name)
58         {
59                 global $current_user, $timedate;
60                 if(isset($this->query['module']))
61                 {
62                    $bean = loadBean($this->query['module']);
63                    if(!empty($bean))
64                    {
65                           foreach($this->query as $key=>$value)
66                           {
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))
69                                 {
70                                    $field = $match[2];
71                                    if(isset($bean->field_defs[$field]['type']) && empty($bean->field_defs[$field]['disable_num_format']))
72                                    {
73                                           $type = $bean->field_defs[$field]['type'];
74                                           
75                                           if(($type == 'date' || $type == 'datetime' || $type == 'datetimecombo') && !preg_match('/^\[.*?\]$/', $value))
76                                           {
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))
87                                                                  {
88                                                                         $this->query[$key . '_currency_symbol'] = $match[1];
89                                                                  }
90                                                         } else {
91                                                                  //unset any flags
92                                                                  if(isset($this->query[$key . '_unformatted_number']))
93                                                                  {
94                                                                         unset($this->query[$key . '_unformatted_number']);
95                                                                  }
96                                                                  
97                                                                  if(isset($this->query[$key . '_currency_symbol']))
98                                                                  {
99                                                                         unset($this->query[$key . '_currency_symbol']);
100                                                                  }
101                                                         }
102                                           }
103                                    }
104                                 }
105                           }
106                    }
107                 }
108
109                 $current_user->setPreference($name.'Q', $this->query);
110         }
111         
112         function clearQuery($name){
113                 $this->query = array();
114                 $this->saveQuery($name);        
115         }
116         
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(); 
124                         }
125                         if(!empty($this->populate_only) && !empty($this->query['query'])){
126                                 $this->query['query'] = 'MSI';
127                         }
128                 }
129         }
130         
131         function populateRequest()
132         {
133                 global $timedate;
134                 
135                 if(isset($this->query['module']))
136                 {
137                    $bean = loadBean($this->query['module']);
138                 }
139
140
141                 foreach($this->query as $key=>$value)
142                 {
143             // todo wp: remove this
144             if($key != 'advanced' && $key != 'module' && $key != 'lvso')
145             {   
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))
148                                 {
149                                    $field = $match[2];
150                                    if(isset($bean->field_defs[$field]['type']) && empty($bean->field_defs[$field]['disable_num_format']))
151                                    {
152                                           $type = $bean->field_defs[$field]['type'];
153                                           
154                                           if(($type == 'date' || $type == 'datetime' || $type == 'datetimecombo') && preg_match('/^\d{4}-\d{2}-\d{2}$/', $value) && !preg_match('/^\[.*?\]$/', $value))
155                                           {
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']))
161                                                  {
162                                                         $value = $this->query[$key . '_currency_symbol'] . $value;
163                                                  }
164                                           }
165                                    }
166                                 }               
167                 
168                 // cn: bug 6546 storequery stomps correct value for 'module' in Activities
169                         $_REQUEST[$key] = $value;       
170                         $_GET[$key] = $value;
171
172             }
173         }
174         }
175         
176         function getSaveType($name)
177         {
178                 global $sugar_config;
179                 $save_query = empty($sugar_config['save_query']) ?
180                         'all' : $sugar_config['save_query'];
181
182                 if(is_array($save_query))
183                 {
184                         if(isset($save_query[$name]))
185                         {
186                                 $saveType = $save_query[$name];
187                         }
188                         elseif(isset($save_query['default']))
189                         {
190                                 $saveType = $save_query['default'];
191                         }
192                         else
193                         {
194                                 $saveType = 'all';
195                         }       
196                 }
197                 else
198                 {
199                         $saveType = $save_query;
200                 }       
201                 if($saveType == 'populate_only'){
202                         $saveType = 'all';
203                         $this->populate_only = true;
204                 }
205                 return $saveType;
206         }
207
208         
209         function saveFromRequest($name){
210                 if(isset($_REQUEST['query'])){
211                         if(!empty($_REQUEST['clear_query']) && $_REQUEST['clear_query'] == 'true'){
212                                 $this->clearQuery($name);
213                                 return; 
214                         }
215                         $saveType = $this->getSaveType($name);
216                         
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;
221                                 }
222                                 $this->saveQuery($name);
223                                 
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]);
231                 }
232                                 $this->saveQuery($name);        
233                         }
234                 }
235         }
236         
237         function saveFromGet($name){
238                 if(isset($_GET['query'])){
239                         if(!empty($_GET['clear_query']) && $_GET['clear_query'] == 'true'){
240                                 $this->clearQuery($name);
241                                 return; 
242                         }
243                         $saveType = $this->getSaveType($name);
244                         
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;
249                                 }
250                                 $this->saveQuery($name);
251                                 
252                         }else if($saveType == 'all'){
253                                 $this->query = $_GET;
254                                 $this->saveQuery($name);        
255                         }
256                 }
257         }
258
259         /**
260          * Static method to retrieve the user's stored query for a particular module
261          *
262          * @param string $module
263          * @return array
264          */
265         public static function getStoredQueryForUser($module)
266         {
267                 global $current_user;
268                 return $current_user->getPreference($module.'Q');
269         }       
270 }
271 ?>