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 ********************************************************************************/
38 /*********************************************************************************
40 * Description: TODO: To be written.
41 * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
42 * All Rights Reserved.
43 * Contributor(s): ______________________________________..
44 ********************************************************************************/
45 require_once('include/templates/TemplateGroupChooser.php');
48 class SavedSearch extends SugarBean {
56 var $assigned_user_id;
57 var $assigned_user_name;
58 var $modified_by_name;
64 var $object_name = 'SavedSearch';
65 var $table_name = 'saved_search';
67 var $module_dir = 'SavedSearch';
68 var $field_defs = array();
69 var $field_defs_map = array();
73 function SavedSearch($columns = array(), $orderBy = null, $sortOrder = 'DESC') {
75 $this->columns = $columns;
76 $this->orderBy = $orderBy;
77 $this->sortOrder = $sortOrder;
78 $this->setupCustomFields('SavedSearch');
79 foreach ($this->field_defs as $field) {
80 $this->field_name_map[$field['name']] = $field;
86 function getForm($module, $inline = true) {
87 global $db, $current_user, $currentModule, $current_language, $app_strings;
90 $saved_search_mod_strings = return_module_language($current_language, 'SavedSearch');
92 $query = 'SELECT id, name FROM saved_search
95 assigned_user_id = \'' . $current_user->id . '\' AND
96 search_module = \'' . $module . '\'
98 $result = $db->query($query, true, "Error filling in saved search list: ");
100 $savedSearchArray['_none'] = $app_strings['LBL_NONE'];
101 while ($row = $db->fetchByAssoc($result, -1, FALSE)) {
102 $savedSearchArray[$row['id']] = htmlspecialchars($row['name'], ENT_QUOTES);
104 $sugarSmarty = new Sugar_Smarty();
105 $sugarSmarty->assign('SEARCH_MODULE', $module);
106 $sugarSmarty->assign('MOD', $saved_search_mod_strings);
107 $sugarSmarty->assign('DELETE', $app_strings['LBL_DELETE_BUTTON_LABEL']);
108 $sugarSmarty->assign('UPDATE', $app_strings['LBL_UPDATE']);
109 $sugarSmarty->assign('SAVE', $app_strings['LBL_SAVE_BUTTON_LABEL']);
112 $chooser = new TemplateGroupChooser();
114 $chooser->args['id'] = 'edit_tabs';
115 $chooser->args['left_size'] = 7;
116 $chooser->args['right_size'] = 7;
117 $chooser->args['values_array'][0] = array();
118 $chooser->args['values_array'][1] = array();
120 if(isset($_REQUEST['saved_search_select']) && $_REQUEST['saved_search_select']!='_none') {
121 $this->retrieveSavedSearch($_REQUEST['saved_search_select']);
124 if((!empty($_REQUEST['displayColumns']) && $_REQUEST['displayColumns'] != 'undefined') || (isset($this->contents['displayColumns']) && $this->contents['displayColumns'] != 'undefined')) {
125 // columns to display
126 if(!empty($_REQUEST['displayColumns']) && $_REQUEST['displayColumns'] != 'undefined') $temp_displayColumns = $_REQUEST['displayColumns'];
127 else $temp_displayColumns = $this->contents['displayColumns'];
128 foreach(explode('|', $temp_displayColumns) as $num => $name) {
129 if (!isset($this->columns[$name])) {
130 // Ignore any column that is not on the list.
133 $chooser->args['values_array'][0][$name] = trim(translate($this->columns[$name]['label'], $module), ':');
135 // columns not displayed
136 foreach(array_diff(array_keys($this->columns), array_values(explode('|', $temp_displayColumns))) as $num => $name) {
137 $chooser->args['values_array'][1][$name] = trim(translate($this->columns[$name]['label'], $module), ':');
141 foreach($this->columns as $name => $val) {
142 if(!empty($val['default']) && $val['default'])
143 $chooser->args['values_array'][0][$name] = trim(translate($val['label'], $module), ':');
145 $chooser->args['values_array'][1][$name] = trim(translate($val['label'], $module), ':');
149 if(!empty($_REQUEST['sortOrder'])) $this->sortOrder = $_REQUEST['sortOrder'];
150 if(!empty($_REQUEST['orderBy'])) $this->orderBy = $_REQUEST['orderBy'];
152 $chooser->args['left_name'] = 'display_tabs';
153 $chooser->args['right_name'] = 'hide_tabs';
154 $chooser->args['alt_tip'] = $app_strings['LBL_SORT'];
156 $chooser->args['left_label'] = $app_strings['LBL_DISPLAY_COLUMNS'];
157 $chooser->args['right_label'] = $app_strings['LBL_HIDE_COLUMNS'];
158 $chooser->args['title'] = '';
159 $sugarSmarty->assign('columnChooser', $chooser->display());
161 $sugarSmarty->assign('selectedOrderBy', $this->orderBy);
162 if(empty($this->sortOrder)) $this->sortOrder = 'ASC';
163 $sugarSmarty->assign('selectedSortOrder', $this->sortOrder);
165 $lastSavedView = (empty($_SESSION['LastSavedView'][$module]) ? '' : $_SESSION['LastSavedView'][$module]);
166 $sugarSmarty->assign('columnsMeta', $json->encode($this->columns));
167 $sugarSmarty->assign('lastSavedView', $lastSavedView);
168 $sugarSmarty->assign('SAVED_SEARCHES_OPTIONS', get_select_options_with_id($savedSearchArray, $lastSavedView));
170 $json = getJSONobj();
172 return $sugarSmarty->fetch('modules/SavedSearch/SavedSearchForm.tpl');
175 function getSelect($module) {
178 global $db, $current_user, $currentModule, $current_lang, $app_strings;
179 $saved_search_mod_strings = return_module_language($current_lang, 'SavedSearch');
181 $query = 'SELECT id, name FROM saved_search
184 assigned_user_id = \'' . $current_user->id . '\' AND
185 search_module = \'' . $module . '\'
187 $result = $db->query($query, true, "Error filling in saved search list: ");
189 $savedSearchArray['_none'] = $app_strings['LBL_NONE'];
190 while ($row = $db->fetchByAssoc($result, -1, FALSE)) {
191 $savedSearchArray[$row['id']] = htmlspecialchars($row['name'], ENT_QUOTES);
194 $sugarSmarty = new Sugar_Smarty();
195 $sugarSmarty->assign('SEARCH_MODULE', $module);
196 $sugarSmarty->assign('MOD', $saved_search_mod_strings);
198 if(!empty($_SESSION['LastSavedView'][$module]) && (($_REQUEST['action'] == 'ListView') || ($_REQUEST['action'] == 'index')))
199 $selectedSearch = $_SESSION['LastSavedView'][$module];
201 $selectedSearch = '';
203 $sugarSmarty->assign('SAVED_SEARCHES_OPTIONS', get_select_options_with_id($savedSearchArray, $selectedSearch));
205 return $sugarSmarty->fetch('modules/SavedSearch/SavedSearchSelects.tpl');
208 function returnSavedSearch($id, $searchFormTab = 'advanced_search', $showDiv='no') {
209 global $db, $current_user, $currentModule;
210 $this->retrieveSavedSearch($id);
212 $header = 'Location: index.php?action=index&module=';
214 $saved_search_name = '';
215 $header .= $this->contents['search_module'];
216 if(empty($_SESSION['LastSavedView'])) $_SESSION['LastSavedView'] = array();
217 $_SESSION['LastSavedView'][$this->contents['search_module']] = $id;
218 $saved_search_id = $id;
219 $saved_search_name = $this->name;
220 $search_form_tab = $this->contents['searchFormTab'];
221 $query = $this->contents['query'];
222 $orderBy = empty($this->contents['orderBy'])? 'name' : $this->contents['orderBy'];
223 //Reduce the params to avoid the problems caused by URL max length in IE.
224 header($header . '&saved_search_select=' . $saved_search_id . '&saved_search_select_name=' . $saved_search_name . '&orderBy=' . $orderBy . '&sortOrder=' . $this->contents['sortOrder'] . '&query=' . $query . '&searchFormTab='. $search_form_tab .'&showSSDIV=' . $showDiv);
227 function returnSavedSearchContents($id) {
228 global $db, $current_user, $currentModule;
229 $query = 'SELECT id, name, contents, search_module FROM saved_search
231 id = \'' . $id . '\'';
232 $result = $db->query($query, true, "Error filling in saved search list: ");
234 $header = 'Location: index.php?action=index&module=';
236 $saved_search_name = '';
237 while ($row = $db->fetchByAssoc($result, false)) {
238 $header .= $row['search_module'];
239 if(empty($_SESSION['LastSavedView'])) $_SESSION['LastSavedView'] = array();
240 $_SESSION['LastSavedView'][$row['search_module']] = $row['id'];
241 $contents = unserialize(base64_decode($row['contents']));
242 $saved_search_id = $row['id'];
243 $saved_search_name = $row['name'];
249 function handleDelete($id) {
250 $this->mark_deleted($id);
251 header("Location: index.php?action=index&module={$_REQUEST['search_module']}&advanced={$_REQUEST['advanced']}&query=true&clear_query=true");
254 function handleSave($prefix, $redirect = true, $useRequired = false, $id = null, $searchModuleBean) {
256 global $current_user, $timedate;
258 $focus = new SavedSearch();
259 if($id) $focus->retrieve($id);
261 if($useRequired && !checkRequired($prefix, array_keys($focus->required_fields))) {
265 $ignored_inputs = array('PHPSESSID', 'module', 'action', 'saved_search_name', 'saved_search_select', 'advanced', 'Calls_divs', 'ACLRoles_divs');
267 $contents = $_REQUEST;
268 if($id == null) $focus->name = $contents['saved_search_name'];
269 $focus->search_module = $contents['search_module'];
271 foreach($contents as $input=>$value)
273 if(in_array($input, $ignored_inputs))
275 unset($contents[$input]);
279 //Filter date fields to ensure it is saved to DB format, but also avoid empty values
280 if(!empty($value) && preg_match('/^(start_range_|end_range_|range_)?(.*?)(_advanced|_basic)$/', $input, $match))
283 if(isset($searchModuleBean->field_defs[$field]['type']) && empty($searchModuleBean->field_defs[$field]['disable_num_format']))
285 $type = $searchModuleBean->field_defs[$field]['type'];
287 //Avoid macro values for the date types
288 if(($type == 'date' || $type == 'datetime' || $type == 'datetimecombo') && !preg_match('/^\[.*?\]$/', $value))
290 $db_format = $timedate->to_db_date($value, false);
291 $contents[$input] = $db_format;
292 } else if ($type == 'int' || $type == 'currency' || $type == 'decimal' || $type == 'float') {
294 if(preg_match('/[^\d]/', $value)) {
295 require_once('modules/Currencies/Currency.php');
296 $contents[$input] = unformat_number($value);
297 //Flag this value as having been unformatted
298 $contents[$input . '_unformatted_number'] = true;
299 //If the type is of currency and there was a currency symbol (non-digit), save the symbol
300 if($type == 'currency' && preg_match('/^([^\d])/', $value, $match))
302 $contents[$input . '_currency_symbol'] = $match[1];
306 if(isset($contents[$input . '_unformatted_number']))
308 unset($contents[$input . '_unformatted_number']);
311 if(isset($contents[$input . '_currency_symbol']))
313 unset($contents[$input . '_currency_symbol']);
322 $contents['advanced'] = true;
324 $focus->contents = base64_encode(serialize($contents));
326 $focus->assigned_user_id = $current_user->id;
327 $focus->new_schema = true;
329 $saved_search_id = $focus->save();
331 $GLOBALS['log']->debug("Saved record with id of " . $focus->id);
332 $orderBy = empty($contents['orderBy'])? 'name' : $contents['orderBy'];
333 $search_query = "&orderBy=" . $orderBy . "&sortOrder=".$contents['sortOrder'] . "&query=" . $_REQUEST['query'] . "&searchFormTab=" . $_REQUEST['searchFormTab'].'&showSSDIV=' . $contents['showSSDIV'];
337 $this->handleRedirect($focus->search_module, $search_query, $saved_search_id, 'true');
341 function handleRedirect($return_module, $search_query, $saved_search_id, $advanced = 'false') {
342 $_SESSION['LastSavedView'][$return_module] = $saved_search_id;
343 $return_action = 'index';
344 $ajaxLoad = empty($_REQUEST['ajax_load']) ? "" : "&ajax_load=" . $_REQUEST['ajax_load'];
345 //Reduce the params to avoid the problems caused by URL max length in IE ( the reduced params can be get from saved search according to saved_search_id).
346 header("Location: index.php?action=$return_action&module=$return_module&saved_search_select={$saved_search_id}{$search_query}&advanced={$advanced}$ajaxLoad");
350 function fill_in_additional_list_fields() {
351 global $app_list_strings;
352 // Fill in the assigned_user_name
353 $this->search_module = $app_list_strings['moduleList'][$this->contents['search_module']];
354 $this->assigned_user_name = get_assigned_user_name($this->assigned_user_id);
359 function retrieveSavedSearch($id) {
360 parent::retrieve($id);
361 $this->contents = unserialize(base64_decode($this->contents));
364 function populateRequest(){
368 if(isset($this->contents['search_module']))
370 $searchModuleBean = loadBean($this->contents['search_module']);
373 foreach($this->contents as $key=>$val){
374 if($key != 'advanced' && $key != 'module' && !strpos($key, '_ORDER_BY') && $key != 'lvso') {
375 if(isset($searchModuleBean) && !empty($val) && preg_match('/^(start_range_|end_range_|range_)?(.*?)(_advanced|_basic)$/', $key, $match))
378 if(isset($searchModuleBean->field_defs[$field]['type']) && empty($searchModuleBean->field_defs[$field]['disable_num_format']))
380 $type = $searchModuleBean->field_defs[$field]['type'];
382 //Avoid macro values for the date types
383 if(($type == 'date' || $type == 'datetime' || $type == 'datetimecombo') && preg_match('/^\d{4}-\d{2}-\d{2}$/', $val) && !preg_match('/^\[.*?\]$/', $val))
385 $val = $timedate->to_display_date($val, false);
386 } else if (($type == 'int' || $type == 'currency' || $type == 'decimal' || $type == 'float') && isset($this->contents[$key . '_unformatted_number']) && preg_match('/^\d+$/', $val)) {
387 require_once('modules/Currencies/Currency.php');
388 $val = format_number($val);
389 if($type == 'currency' && isset($this->contents[$key . '_currency_symbol']))
391 $val = $this->contents[$key . '_currency_symbol'] . $val;
397 $_REQUEST[$key] = $val;