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