]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - modules/DynamicFields/templates/Fields/TemplateField.php
Release 6.5.0beta1
[Github/sugarcrm.git] / modules / DynamicFields / templates / Fields / TemplateField.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-2012 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 $GLOBALS['studioReadOnlyFields'] = array('date_entered'=>1, 'date_modified'=>1, 'created_by'=>1, 'id'=>1, 'modified_user_id'=>1);
39 class TemplateField{
40         /*
41                 The view is the context this field will be used in
42                 -edit
43                 -list
44                 -detail
45                 -search
46                 */
47         var $view = 'edit';
48         var $name = '';
49         var $vname = '';
50         var $id = '';
51         var $size = '20';
52         var $len = '255';
53         var $required = false;
54         var $default = null;
55         var $default_value = null;
56         var $type = 'varchar';
57         var $comment = '';
58         var $bean;
59         var $ext1 = '';
60         var $ext2 = '';
61         var $ext3 = '';
62         var $ext4 = '';
63         var $audited= 0;
64         var $massupdate = 0;
65         var $importable = 'true' ;
66         var $duplicate_merge=0;
67         var $new_field_definition;
68         var $reportable = true;
69         var $label_value = '';
70         var $help = '';
71         var $formula = '';
72     var $unified_search = 0;
73     var $supports_unified_search = false;
74         var $vardef_map = array(
75                 'name'=>'name',
76                 'label'=>'vname',
77         // bug 15801 - need to ALWAYS keep default and default_value consistent as some methods/classes use one, some use another...
78                 'default_value'=>'default',
79                 'default'=>'default_value',
80                 'display_default'=>'default_value',
81         //              'default_value'=>'default_value',
82         //              'default'=>'default_value',
83                 'len'=>'len',
84                 'required'=>'required',
85                 'type'=>'type',
86                 'audited'=>'audited',
87                 'massupdate'=>'massupdate',
88                 'options'=>'ext1',
89                 'help'=>'help',
90             'comments'=>'comment',
91             'importable'=>'importable',
92                 'duplicate_merge'=>'duplicate_merge',
93                 'duplicate_merge_dom_value'=>'duplicate_merge_dom_value', //bug #14897
94                 'merge_filter'=>'merge_filter',
95                 'reportable' => 'reportable',
96                 'min'=>'ext1',
97                 'max'=>'ext2',
98                 'ext2'=>'ext2',
99                 'ext4'=>'ext4',
100         //'disable_num_format'=>'ext3',
101             'ext3'=>'ext3',
102                 'label_value'=>'label_value',
103                 'unified_search'=>'unified_search',
104         'full_text_search'=>'full_text_search',
105         );
106         /*
107                 HTML FUNCTIONS
108                 */
109         function get_html(){
110                 $view = $this->view;
111                 if(!empty($GLOBALS['studioReadOnlyFields'][$this->name]))$view = 'detail';
112                 switch($view){
113                         case 'search':return $this->get_html_search();
114                         case 'edit': return $this->get_html_edit();
115                         case 'list': return $this->get_html_list();
116                         case 'detail': return $this->get_html_detail();
117
118                 }
119         }
120         function set($values){
121                 foreach($values as $name=>$value){
122                         $this->$name = $value;
123                 }
124
125         }
126
127         function get_html_edit(){
128                 return 'not implemented';
129         }
130
131         function get_html_list(){
132                 return $this->get_html_detail();
133         }
134
135         function get_html_detail(){
136                 return 'not implemented';
137         }
138
139         function get_html_search(){
140                 return $this->get_html_edit();
141         }
142         function get_html_label(){
143
144                 $label =  "{MOD." .$this->vname . "}";
145                 if(!empty($GLOBALS['app_strings'][$this->vname])){
146                         $label = "{APP." .$this->label . "}";
147                 }
148                 if($this->view == 'edit' && $this->is_required()){
149                         $label .= '<span class="required">*</span>';
150                 }
151                 if($this->view == 'list'){
152                         if(isset($this->bean)){
153                                 if(!empty($this->id)){
154                                         $name = $this->bean->table_name . '_cstm.'. $this->name;
155                                         $arrow = $this->bean->table_name . '_cstm_'. $this->name;
156                                 }else{
157                                         $name = $this->bean->table_name . '.'. $this->name;
158                                         $arrow = $this->bean->table_name . '_'. $this->name;
159                                 }
160                         }else{
161                                 $name = $this->name;
162                                 $arrow = $name;
163                         }
164                         $label = "<a href='{ORDER_BY}$name' class='listViewThLinkS1'>{MOD.$this->label}{arrow_start}{".$arrow."_arrow}{arrow_end}</a>";
165                 }
166                 return $label;
167
168         }
169
170         /*
171                 XTPL FUNCTIONS
172                 */
173
174         function get_xtpl($bean = false){
175                 if($bean)
176                 $this->bean = $bean;
177                 $view = $this->view;
178                 if(!empty($GLOBALS['studioReadOnlyFields'][$this->name]))$view = 'detail';
179                 switch($view){
180                         case 'search':return $this->get_xtpl_search();
181                         case 'edit': return $this->get_xtpl_edit();
182                         case 'list': return $this->get_xtpl_list();
183                         case 'detail': return $this->get_xtpl_detail();
184
185                 }
186         }
187
188         function get_xtpl_edit(){
189                 return '/*not implemented*/';
190         }
191
192         function get_xtpl_list(){
193                 return get_xtpl_detail();
194         }
195
196         function get_xtpl_detail(){
197                 return '/*not implemented*/';
198         }
199
200         function get_xtpl_search(){
201                 //return get_xtpl_edit();
202         }
203
204         function is_required(){
205                 if($this->required){
206                         return true;
207                 }
208                 return false;
209
210         }
211
212
213
214
215         /*
216                 DB FUNCTIONS
217                 */
218
219         function get_db_type(){
220             if(!empty($this->type)) {
221                 $type = $GLOBALS['db']->getColumnType($this->type);
222             }
223             if(!empty($type)) return " $type";
224             $type = $GLOBALS['db']->getColumnType("varchar");
225         return " $type({$this->len})";
226         }
227
228         function get_db_default($modify=false){
229                 $GLOBALS['log']->debug('get_db_default(): default_value='.$this->default_value);
230                 if (!$modify or empty($this->new_field_definition['default_value']) or $this->new_field_definition['default_value'] != $this->default_value ) {
231                         if(!is_null($this->default_value)){ // add a default value if it is not null - we want to set a default even if default_value is '0', which is not null, but which is empty()
232                                 if(NULL == trim($this->default_value)){
233                                         return " DEFAULT NULL";
234                                 }
235                                 else {
236                                         return " DEFAULT '$this->default_value'";
237                                 }
238                         }else{
239                                 return '';
240                         }
241                 }
242         }
243
244         /*
245          * Return the required clause for this field
246          * Confusingly, when modifying an existing field ($modify=true) there are two exactly opposite cases:
247          * 1. if called by Studio, only $this->required is set. If set, we return "NOT NULL" otherwise we return "NULL"
248          * 2. if not called by Studio, $this->required holds the OLD value of required, and new_field_definition['required'] is the NEW
249          * So if not called by Studio we want to return NULL if required=true (because we are changing FROM this setting)
250          */
251
252         function get_db_required($modify=false){
253                 //              $GLOBALS['log']->debug('get_db_required required='.$this->required." and ".(($modify)?"true":"false")." and ".print_r($this->new_field_definition,true));
254                 $req = "";
255
256                 if ($modify) {
257                         if (!empty($this->new_field_definition['required'])) {
258                                 if ($this->required and $this->new_field_definition['required'] != $this->required) {
259                                         $req = " NULL ";
260                                 }
261                         }
262                         else
263                         {
264                                 $req = ($this->required) ? " NOT NULL " : ''; // bug 17184 tyoung - set required correctly when modifying custom field in Studio
265                         }
266                 }
267                 else
268                 {
269                         if (empty($this->new_field_definition['required']) or $this->new_field_definition['required'] != $this->required ) {
270                                 if(!empty($this->required) && $this->required){
271                                         $req = " NOT NULL";
272                                 }
273                         }
274                 }
275
276                 return $req;
277         }
278
279         /*      function get_db_required($modify=false){
280                 $GLOBALS['log']->debug('get_db_required required='.$this->required." and ".(($modify)?"true":"false")." and ".print_r($this->new_field_definition,true));
281                 if ($modify) {
282                 if (!empty($this->new_field_definition['required'])) {
283                 if ($this->required and $this->new_field_definition['required'] != $this->required) {
284                 return " null ";
285                 }
286                 return "";
287                 }
288                 }
289                 if (empty($this->new_field_definition['required']) or $this->new_field_definition['required'] != $this->required ) {
290                 if(!empty($this->required) && $this->required){
291                 return " NOT NULL";
292                 }
293                 }
294                 return '';
295                 }
296                 */
297         /**
298          * Oracle Support: do not set required constraint if no default value is supplied.
299          * In this case the default value will be handled by the application/sugarbean.
300          */
301         function get_db_add_alter_table($table)
302         {
303                 return $GLOBALS['db']->getHelper()->addColumnSQL($table, $this->get_field_def(), true);
304         }
305
306         function get_db_delete_alter_table($table)
307         {
308                 return $GLOBALS['db']->getHelper()->dropColumnSQL(
309                 $table,
310                 $this->get_field_def()
311                 );
312         }
313
314         /**
315          * mysql requires the datatype caluse in the alter statment.it will be no-op anyway.
316          */
317         function get_db_modify_alter_table($table){
318                 return $GLOBALS['db']->alterColumnSQL($table, $this->get_field_def());
319         }
320
321
322         /*
323          * BEAN FUNCTIONS
324          *
325          */
326         function get_field_def(){
327                 $array =  array(
328                         'required'=>$this->convertBooleanValue($this->required),
329                         'source'=>'custom_fields',
330                         'name'=>$this->name,
331                         'vname'=>$this->vname,
332                         'type'=>$this->type,
333                         'massupdate'=>$this->massupdate,
334                         'default'=>$this->default,
335                         'comments'=> (isset($this->comments)) ? $this->comments : '',
336                     'help'=> (isset($this->help)) ?  $this->help : '',
337                     'importable'=>$this->importable,
338                         'duplicate_merge'=>$this->duplicate_merge,
339                         'duplicate_merge_dom_value'=> isset($this->duplicate_merge_dom_value) ? $this->duplicate_merge_dom_value : $this->duplicate_merge,
340                         'audited'=>$this->convertBooleanValue($this->audited),
341                         'reportable'=>$this->convertBooleanValue($this->reportable),
342             'unified_search'=>$this->convertBooleanValue($this->unified_search)
343                 );
344         if (isset($this->full_text_search)) {
345             $array['full_text_search'] = $this->full_text_search;
346         }
347                 if(!empty($this->len)){
348                         $array['len'] = $this->len;
349                 }
350                 if(!empty($this->size)){
351                         $array['size'] = $this->size;
352                 }
353                 $this->get_dup_merge_def($array);
354
355                 return $array;
356         }
357
358         protected function convertBooleanValue($value)
359         {
360                 if ($value === 'true' || $value === '1' || $value === 1)
361                 return  true;
362                 else if ($value === 'false' || $value === '0' || $value === 0)
363                 return  false;
364                 else
365                 return $value;
366         }
367
368
369         /* if the field is duplicate merge enabled this function will return the vardef entry for the same.
370          */
371         function get_dup_merge_def(&$def) {
372
373                 switch ($def['duplicate_merge_dom_value']) {
374                         case 0:
375                                 $def['duplicate_merge']='disabled';
376                                 break;
377                         case 1:
378                                 $def['duplicate_merge']='enabled';
379                                 break;
380                         case 2:
381                                 $def['merge_filter']='enabled';
382                                 $def['duplicate_merge']='enabled';
383                                 break;
384                         case 3:
385                                 $def['merge_filter']='selected';
386                                 $def['duplicate_merge']='enabled';
387                                 break;
388                         case 4:
389                                 $def['merge_filter']='enabled';
390                                 $def['duplicate_merge']='disabled';
391                                 break;
392                 }
393
394         }
395
396         /*
397                 HELPER FUNCTIONS
398                 */
399
400
401         function prepare(){
402                 if(empty($this->id)){
403                         $this->id = $this->name;
404                 }
405         }
406
407         /**
408          * populateFromRow
409          * This function supports setting the values of all TemplateField instances.
410          * @param $row The Array key/value pairs from fields_meta_data table
411          */
412         function populateFromRow($row=array()) {
413                 $fmd_to_dyn_map = array('comments' => 'comment', 'require_option' => 'required', 'label' => 'vname',
414                                                             'mass_update' => 'massupdate', 'max_size' => 'len', 'default_value' => 'default', 'id_name' => 'ext3');
415                 if(!is_array($row)) {
416                         $GLOBALS['log']->error("Error: TemplateField->populateFromRow expecting Array");
417                 }
418                 //Bug 24189: Copy fields from FMD format to Field objects
419                 foreach ($fmd_to_dyn_map as $fmd_key => $dyn_key) {
420                         if (isset($row[$fmd_key])) {
421                                 $this->$dyn_key = $row[$fmd_key];
422                         }
423                 }
424                 foreach($row as $key=>$value) {
425                         $this->$key = $value;
426                 }
427         }
428
429         function populateFromPost(){
430                 foreach($this->vardef_map as $vardef=>$field){
431                         if(isset($_REQUEST[$vardef])){
432                                 $this->$vardef = $_REQUEST[$vardef];
433
434                                 // Bug 49774, 49775: Strip html tags from 'formula' and 'dependency'.
435                                 // Add to the list below if we need to do the same for other fields.
436                                 if (!empty($this->$vardef) && in_array($vardef, array('formula', 'dependency'))){
437                                     $this->$vardef = to_html(strip_tags(from_html($this->$vardef)));
438                                 }
439
440                 //Remove potential xss code from help field
441                 if($field == 'help' && !empty($this->$vardef))
442                 {
443                     $help = htmlspecialchars_decode($this->$vardef, ENT_QUOTES);
444                     $this->$vardef = htmlentities(remove_xss($help));
445                 }
446
447                                 if($vardef != $field){
448                                         $this->$field = $this->$vardef;
449                                 }
450                         }
451                 }
452                 $this->applyVardefRules();
453                 $GLOBALS['log']->debug('populate: '.print_r($this,true));
454
455         }
456
457         protected function applyVardefRules()
458         {
459         }
460
461         function get_additional_defs(){
462                 return array();
463         }
464
465         function delete($df){
466                 $df->deleteField($this);
467         }
468
469     /**
470      * get_field_name
471      *
472      * This is a helper function to return a field's proper name.  It checks to see if an instance of the module can
473      * be created and then attempts to retrieve the field's name based on the name lookup skey supplied to the method.
474      *
475      * @param String $module The name of the module
476      * @param String $name The field name key
477      * @return The field name for the module
478      */
479     protected function get_field_name($module, $name)
480     {
481        $bean = loadBean($module);
482        if(empty($bean) || is_null($bean))
483        {
484           return $name;
485        }
486
487        $field_defs = $bean->field_defs;
488        return isset($field_defs[$name]['name']) ? $field_defs[$name]['name'] : $name;
489     }
490
491     /**
492      * save
493      *
494      * This function says the field template by calling the DynamicField addFieldObject function.  It then
495      * checks to see if updates are needed for the SearchFields.php file.  In the event that the unified_search
496      * member variable is set to true, a search field definition is updated/created to the SearchFields.php file.
497      *
498      * @param $df Instance of DynamicField
499      */
500         function save($df){
501                 //          $GLOBALS['log']->debug('saving field: '.print_r($this,true));
502                 $df->addFieldObject($this);
503
504         require_once('modules/ModuleBuilder/parsers/parser.searchfields.php');
505         $searchFieldParser = new ParserSearchFields( $df->getModuleName() , $df->getPackageName() ) ;
506             //If unified_search is enabled for this field, then create the SearchFields entry
507             $fieldName = $this->get_field_name($df->getModuleName(), $this->name);
508         if($this->unified_search && !isset($searchFieldParser->searchFields[$df->getModuleName()][$fieldName]))
509         {
510            $searchFieldParser->addSearchField($fieldName, array('query_type'=>'default'));
511            $searchFieldParser->saveSearchFields($searchFieldParser->searchFields);
512         }
513         }
514
515 }
516
517
518 ?>