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-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('modules/'.$module.'/metadata/SearchFields.php'))
75 require('modules/'.$module.'/metadata/SearchFields.php');
78 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
80 require('custom/modules/'.$module.'/metadata/SearchFields.php');
83 $field_name = $this->get_field_name($module, $_REQUEST['name']);
85 if(isset($searchFields[$module]))
87 $field_name_range = 'range_' . $field_name;
88 $field_name_start = 'start_range_' . $field_name;
89 $field_name_end = 'end_range_' . $field_name;
91 $isDateField = $this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime';
94 $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
97 $searchFields[$module][$field_name_range]['is_date_field'] = true;
100 $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
103 $searchFields[$module][$field_name_start]['is_date_field'] = true;
106 $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
109 $searchFields[$module][$field_name_end]['is_date_field'] = true;
112 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
114 mkdir_recursive('custom/modules/'.$module.'/metadata');
116 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
119 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl'))
121 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
124 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl'))
126 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
130 //Otherwise, try to restore the searchFields to their state prior to being enabled
131 if(isset($_REQUEST['view_module']))
133 $module = $_REQUEST['view_module'];
134 if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
135 require('modules/'.$module.'/metadata/SearchFields.php');
138 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
140 require('custom/modules/'.$module.'/metadata/SearchFields.php');
143 $field_name = $this->get_field_name($module, $_REQUEST['name']);
145 if(isset($searchFields[$module]))
147 $field_name_range = 'range_' . $field_name;
148 $field_name_start = 'start_range_' . $field_name;
149 $field_name_end = 'end_range_' . $field_name;
152 if(isset($searchFields[$module][$field_name_range]))
154 unset($searchFields[$module][$field_name_range]);
157 if(isset($searchFields[$module][$field_name_start]))
159 unset($searchFields[$module][$field_name_start]);
162 if(isset($searchFields[$module][$field_name_end]))
164 unset($searchFields[$module][$field_name_end]);
167 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
169 mkdir_recursive('custom/modules/'.$module.'/metadata');
171 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
174 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl'))
176 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
179 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl'))
181 unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
191 * @see parent::get_field_def
192 * This method checks to see if the enable_range_search key/value entry should be
193 * added to the vardef entry representing the module
195 function get_field_def()
197 $vardef = parent::get_field_def();
198 if(!empty($this->enable_range_search))
200 $vardef['enable_range_search'] = $this->enable_range_search;
201 $vardef['options'] = ($this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime') ? 'date_range_search_dom' : 'numeric_range_search_dom';
203 $vardef['enable_range_search'] = false;
210 * This function gets the correct name for the field name being submitted from studio
212 * @param String $module The name of the module
213 * @param String $name The field name
215 private function get_field_name($module, $name)
217 $bean = loadBean($module);
218 if(empty($bean) || is_null($bean))
223 $field_defs = $bean->field_defs;
224 $field_name = isset($field_defs[$_REQUEST['name']]) ? $_REQUEST['name'] : $_REQUEST['name'] . '_c';
228 public static function repairCustomSearchFields($vardefs, $module, $package='')
233 //Find any range search enabled fields
234 foreach($vardefs as $key=>$field)
236 if(!empty($field['enable_range_search'])) {
237 $fields[$field['name']] = $field;
243 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
245 require('custom/modules/'.$module.'/metadata/SearchFields.php');
246 } else if (file_exists('modules/'.$module.'/metadata/SearchFields.php')) {
247 require('modules/'.$module.'/metadata/SearchFields.php');
248 } else if (file_exists('custom/modulebuilder/' . $package . '/modules/' . $module . '/metadata/SearchFields.php')) {
249 require('custom/modulebuilder/' . $package . '/modules/' . $module . '/metadata/SearchFields.php');
252 foreach($fields as $field_name=>$field)
254 $field_name_range = 'range_' . $field_name;
255 $field_name_start = 'start_range_' . $field_name;
256 $field_name_end = 'end_range_' . $field_name;
258 $type = $field['type'];
260 $isDateField = $type == 'date' || $type == 'datetimecombo' || $type == 'datetime';
262 $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
265 $searchFields[$module][$field_name_range]['is_date_field'] = true;
268 $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
271 $searchFields[$module][$field_name_start]['is_date_field'] = true;
274 $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
277 $searchFields[$module][$field_name_end]['is_date_field'] = true;
281 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
283 mkdir_recursive('custom/modules/'.$module.'/metadata');
286 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');