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 ********************************************************************************/
39 class TemplateRange extends TemplateText
45 * Constructor for class. This constructor ensures that TemplateRanage instances have the
46 * enable_range_search vardef value.
48 function __construct()
50 $this->vardef_map['enable_range_search'] = 'enable_range_search';
51 $this->vardef_map['options'] = 'options';
58 * @see parent::populateFromPost
59 * This method checks to see if enable_range_search is set. If so, ensure that the
60 * searchdefs for the module include the additional range fields.
62 function populateFromPost() {
63 parent::populateFromPost();
64 //If we are enabling range search, make sure we add the start and end range fields
65 if (!empty($this->enable_range_search))
67 //If range search is enabled, set the options attribute for the dropdown choice selections
68 $this->options = ($this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime') ? 'date_range_search_dom' : 'numeric_range_search_dom';
70 if(isset($_REQUEST['view_module']))
72 $module = $_REQUEST['view_module'];
73 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
75 require('custom/modules/'.$module.'/metadata/SearchFields.php');
76 } else if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
77 require('modules/'.$module.'/metadata/SearchFields.php');
80 $field_name = $this->get_field_name($module, $_REQUEST['name']);
82 if(isset($searchFields[$module]))
84 $field_name_range = 'range_' . $field_name;
85 $field_name_start = 'start_range_' . $field_name;
86 $field_name_end = 'end_range_' . $field_name;
88 $isDateField = $this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime';
91 $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
94 $searchFields[$module][$field_name_range]['is_date_field'] = true;
97 $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
100 $searchFields[$module][$field_name_start]['is_date_field'] = true;
103 $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
106 $searchFields[$module][$field_name_end]['is_date_field'] = true;
109 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
111 mkdir_recursive('custom/modules/'.$module.'/metadata');
113 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
116 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl'))
118 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
121 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl'))
123 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
127 //Otherwise, try to restore the searchFields to their state prior to being enabled
128 if(isset($_REQUEST['view_module']))
130 $module = $_REQUEST['view_module'];
131 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
133 require('custom/modules/'.$module.'/metadata/SearchFields.php');
134 } else if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
135 require('modules/'.$module.'/metadata/SearchFields.php');
138 $field_name = $this->get_field_name($module, $_REQUEST['name']);
140 if(isset($searchFields[$module]))
142 $field_name_range = 'range_' . $field_name;
143 $field_name_start = 'start_range_' . $field_name;
144 $field_name_end = 'end_range_' . $field_name;
147 if(isset($searchFields[$module][$field_name_range]))
149 unset($searchFields[$module][$field_name_range]);
152 if(isset($searchFields[$module][$field_name_start]))
154 unset($searchFields[$module][$field_name_start]);
157 if(isset($searchFields[$module][$field_name_end]))
159 unset($searchFields[$module][$field_name_end]);
162 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
164 mkdir_recursive('custom/modules/'.$module.'/metadata');
166 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
169 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl'))
171 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
174 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl'))
176 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
186 * @see parent::get_field_def
187 * This method checks to see if the enable_range_search key/value entry should be
188 * added to the vardef entry representing the module
190 function get_field_def()
192 $vardef = parent::get_field_def();
193 if(!empty($this->enable_range_search))
195 $vardef['enable_range_search'] = $this->enable_range_search;
196 $vardef['options'] = ($this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime') ? 'date_range_search_dom' : 'numeric_range_search_dom';
198 $vardef['enable_range_search'] = false;
205 * This function gets the correct name for the field name being submitted from studio
207 * @param String $module The name of the module
208 * @param String $name The field name
210 private function get_field_name($module, $name)
212 $bean = loadBean($module);
213 if(empty($bean) || is_null($bean))
218 $field_defs = $bean->field_defs;
219 $field_name = isset($field_defs[$_REQUEST['name']]) ? $_REQUEST['name'] : $_REQUEST['name'] . '_c';
223 public static function repairCustomSearchFields($vardefs, $module, $package='')
228 foreach($vardefs as $key=>$field)
230 if(!empty($field['enable_range_search'])) {
231 $fields[$field['name']] = $field;
237 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
239 require('custom/modules/'.$module.'/metadata/SearchFields.php');
240 } else if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
241 require('modules/'.$module.'/metadata/SearchFields.php');
242 } else if (file_exists('custom/modulebuilder/' . $package . '/modules/' . $module . '/metadata/SearchFields.php')) {
243 require('custom/modulebuilder/' . $package . '/modules/' . $module . '/metadata/SearchFields.php');
246 foreach($fields as $field_name=>$field)
248 $field_name_range = 'range_' . $field_name;
249 $field_name_start = 'start_range_' . $field_name;
250 $field_name_end = 'end_range_' . $field_name;
252 $type = $field['type'];
254 $isDateField = $type == 'date' || $type == 'datetimecombo' || $type == 'datetime';
256 $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
259 $searchFields[$module][$field_name_range]['is_date_field'] = true;
262 $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
265 $searchFields[$module][$field_name_start]['is_date_field'] = true;
268 $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
271 $searchFields[$module][$field_name_end]['is_date_field'] = true;
275 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
277 mkdir_recursive('custom/modules/'.$module.'/metadata');
280 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');