]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/DynamicFields/templates/Fields/TemplateRange.php
Release 6.2.0beta4
[Github/sugarcrm.git] / modules / DynamicFields / templates / Fields / TemplateRange.php
1 <?php
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.
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 class TemplateRange extends TemplateText 
40 {
41         
42         /**
43          * __construct
44          * 
45          * Constructor for class.  This constructor ensures that TemplateRanage instances have the
46          * enable_range_search vardef value.
47          */
48         function __construct()
49         {
50                 $this->vardef_map['enable_range_search'] = 'enable_range_search';
51                 $this->vardef_map['options'] = 'options';
52         }       
53         
54         
55         /**
56          * populateFromPost
57          * 
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.
61          */
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))
66                 {
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'; 
69                         
70                         if(isset($_REQUEST['view_module']))
71                         {
72                                 $module = $_REQUEST['view_module'];
73                             if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
74                             {
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');
78                 }
79                 
80                 $field_name = $this->get_field_name($module, $_REQUEST['name']);
81                 
82                 if(isset($searchFields[$module]))
83                 {
84                         $field_name_range = 'range_' . $field_name;
85                         $field_name_start = 'start_range_' . $field_name;
86                         $field_name_end = 'end_range_' . $field_name;
87                         
88                         $isDateField = $this->type == 'date' || $this->type == 'datetimecombo' || $this->type == 'datetime';
89                         
90
91                     $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
92                     if($isDateField)
93                     {
94                            $searchFields[$module][$field_name_range]['is_date_field'] = true;
95                     }
96                         
97                     $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
98                     if($isDateField)
99                     {
100                            $searchFields[$module][$field_name_start]['is_date_field'] = true;
101                     }
102                         
103                     $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
104                     if($isDateField)
105                     {
106                            $searchFields[$module][$field_name_end]['is_date_field'] = true;
107                     }
108                         
109                         if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
110                         {
111                            mkdir_recursive('custom/modules/'.$module.'/metadata');
112                         }
113                         write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');                       
114                 }       
115
116                             if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl')) 
117                 {
118                    unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
119                 }                
120                 
121                 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl')) 
122                 {
123                    unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
124                 }
125                         }
126                 } else {
127                 //Otherwise, try to restore the searchFields to their state prior to being enabled
128                         if(isset($_REQUEST['view_module']))
129                         {
130                                 $module = $_REQUEST['view_module'];
131                             if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
132                             {
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');
136                 }
137
138                 $field_name = $this->get_field_name($module, $_REQUEST['name']);
139                 
140                 if(isset($searchFields[$module]))
141                 {
142                         $field_name_range = 'range_' . $field_name;
143                         $field_name_start = 'start_range_' . $field_name;
144                         $field_name_end = 'end_range_' . $field_name;
145                         
146                         
147                     if(isset($searchFields[$module][$field_name_range]))
148                         {
149                            unset($searchFields[$module][$field_name_range]);
150                         }                       
151                         
152                         if(isset($searchFields[$module][$field_name_start]))
153                         {
154                            unset($searchFields[$module][$field_name_start]);
155                         }
156                         
157                     if(isset($searchFields[$module][$field_name_end]))
158                         {
159                            unset($searchFields[$module][$field_name_end]);
160                         }
161                         
162                     if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
163                         {
164                            mkdir_recursive('custom/modules/'.$module.'/metadata');
165                         }
166                         write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');                       
167                 }       
168
169                             if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl')) 
170                 {
171                    unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_basic.tpl');
172                 }                
173                 
174                 if(file_exists($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl')) 
175                 {
176                    unlink($GLOBALS['sugar_config']['cache_dir'] . 'modules/' . $module . '/SearchForm_advanced.tpl');
177                 }
178                         }                       
179                 }
180         }       
181         
182         
183         /**
184          * get_field_def
185          * 
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
189          */
190     function get_field_def()
191     {
192                 $vardef = parent::get_field_def();
193         if(!empty($this->enable_range_search))
194         {
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'; 
197                 } else {
198                    $vardef['enable_range_search'] = false;
199                 }               
200                 return $vardef;
201     }
202     
203     /**
204      * get_field_name
205      * This function gets the correct name for the field name being submitted from studio
206      * 
207      * @param String $module The name of the module
208      * @param String $name The field name
209      */
210     private function get_field_name($module, $name)
211     {
212        $bean = loadBean($module);
213        if(empty($bean) || is_null($bean))
214        {
215           return $name;
216        }
217        
218        $field_defs = $bean->field_defs;
219        $field_name = isset($field_defs[$_REQUEST['name']]) ? $_REQUEST['name'] : $_REQUEST['name'] . '_c'; 
220        return $field_name;      
221     }
222     
223     public static function repairCustomSearchFields($vardefs, $module, $package='')
224     {
225         
226         $fields = array();
227         
228                 foreach($vardefs as $key=>$field)
229                 {                               
230                         if(!empty($field['enable_range_search'])) {
231                            $fields[$field['name']] = $field;    
232                         }
233                 }
234                 
235                 if(!empty($fields))
236                 {
237                                 if(file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
238                             {
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');
244                 }               
245                 
246                         foreach($fields as $field_name=>$field)
247                         {
248                         $field_name_range = 'range_' . $field_name;
249                         $field_name_start = 'start_range_' . $field_name;
250                         $field_name_end = 'end_range_' . $field_name;
251                         
252                         $type = $field['type'];
253                         
254                         $isDateField = $type == 'date' || $type == 'datetimecombo' || $type == 'datetime';
255                         
256                             $searchFields[$module][$field_name_range] = array('query_type'=>'default', 'enable_range_search'=>true);
257                     if($isDateField)
258                     {
259                            $searchFields[$module][$field_name_range]['is_date_field'] = true;
260                     }
261                         
262                     $searchFields[$module][$field_name_start] = array('query_type'=>'default', 'enable_range_search'=>true);
263                     if($isDateField)
264                     {
265                            $searchFields[$module][$field_name_start]['is_date_field'] = true;
266                     }
267                         
268                     $searchFields[$module][$field_name_end] = array('query_type'=>'default', 'enable_range_search'=>true);
269                     if($isDateField)
270                     {
271                            $searchFields[$module][$field_name_end]['is_date_field'] = true;
272                     }
273                         }
274                         
275                 if(!file_exists('custom/modules/'.$module.'/metadata/SearchFields.php'))
276                 {
277                    mkdir_recursive('custom/modules/'.$module.'/metadata');
278                 }
279                 
280                 write_array_to_file("searchFields['{$module}']", $searchFields[$module], 'custom/modules/'.$module.'/metadata/SearchFields.php');
281                         
282                 }       
283     }
284     
285      
286 }
287 ?>