]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Base/SugarFieldBase.php
Release 6.5.0
[Github/sugarcrm.git] / include / SugarFields / Fields / Base / SugarFieldBase.php
1 <?php
2 /*********************************************************************************
3  * SugarCRM Community Edition is a customer relationship management program developed by
4  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
5  * 
6  * This program is free software; you can redistribute it and/or modify it under
7  * the terms of the GNU Affero General Public License version 3 as published by the
8  * Free Software Foundation with the addition of the following permission added
9  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
10  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
11  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
12  * 
13  * This program is distributed in the hope that it will be useful, but WITHOUT
14  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
15  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
16  * details.
17  * 
18  * You should have received a copy of the GNU Affero General Public License along with
19  * this program; if not, see http://www.gnu.org/licenses or write to the Free
20  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
21  * 02110-1301 USA.
22  * 
23  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
24  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
25  * 
26  * The interactive user interfaces in modified source and object code versions
27  * of this program must display Appropriate Legal Notices, as required under
28  * Section 5 of the GNU Affero General Public License version 3.
29  * 
30  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
31  * these Appropriate Legal Notices must retain the display of the "Powered by
32  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
33  * technical reasons, the Appropriate Legal Notices must display the words
34  * "Powered by SugarCRM".
35  ********************************************************************************/
36
37 /**
38  * SugarFieldBase translates and displays fields from a vardef definition into different formats
39  * including DetailView, ListView, EditView. It also provides Search Inputs and database queries
40  * to handle searching
41  *
42  */
43 class SugarFieldBase {
44     var $ss; // Sugar Smarty Object
45     var $hasButton = false;
46     function SugarFieldBase($type) {
47         $this->type = $type;
48         $this->ss = new Sugar_Smarty();
49     }
50     function fetch($path){
51         $additional = '';
52         if(!$this->hasButton && !empty($this->button)){
53                 $additional .= '<input type="button" class="button" ' . $this->button . '>';
54         }
55         if(!empty($this->buttons)){
56             foreach($this->buttons as $v){
57                 $additional .= ' <input type="button" class="button" ' . $v . '>';
58             }
59
60         }
61         if(!empty($this->image)){
62             $additional .= ' <img ' . $this->image . '>';
63         }
64         return $this->ss->fetch($path) . $additional;
65     }
66
67     function findTemplate($view){
68         static $tplCache = array();
69
70         if ( isset($tplCache[$this->type][$view]) ) {
71             return $tplCache[$this->type][$view];
72         }
73
74         $lastClass = get_class($this);
75         $classList = array($this->type,str_replace('SugarField','',$lastClass));
76         while ( $lastClass = get_parent_class($lastClass) ) {
77             $classList[] = str_replace('SugarField','',$lastClass);
78         }
79
80         $tplName = '';
81         foreach ( $classList as $className ) {
82             global $current_language;
83             if(isset($current_language)) {
84                 $tplName = 'include/SugarFields/Fields/'. $className .'/'. $current_language . '.' . $view .'.tpl';
85                 if ( file_exists('custom/'.$tplName) ) {
86                     $tplName = 'custom/'.$tplName;
87                     break;
88                 }
89                 if ( file_exists($tplName) ) {
90                     break;
91                 }
92             }
93             $tplName = 'include/SugarFields/Fields/'. $className .'/'. $view .'.tpl';
94             if ( file_exists('custom/'.$tplName) ) {
95                 $tplName = 'custom/'.$tplName;
96                 break;
97             }
98             if ( file_exists($tplName) ) {
99                 break;
100             }
101         }
102
103         $tplCache[$this->type][$view] = $tplName;
104
105         return $tplName;
106     }
107
108     public function formatField($rawField, $vardef){
109         // The base field doesn't do any formatting, so override it in subclasses for more specific actions
110         return $rawField;
111     }
112
113
114     public function unformatField($formattedField, $vardef){
115         // The base field doesn't do any formatting, so override it in subclasses for more specific actions
116         return $formattedField;
117     }
118
119     function getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex = -1, $view){
120         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
121
122
123         return $this->fetch($this->findTemplate($view));
124     }
125
126     function getListViewSmarty($parentFieldArray, $vardef, $displayParams, $col) {
127         $tabindex = 1;
128         //fixing bug #46666: don't need to format enum and radioenum fields 
129         //because they are already formated in SugarBean.php in the function get_list_view_array() as fix of bug #21672
130         if ($this->type != 'Enum' && $this->type != 'Radioenum')
131         {
132             $parentFieldArray = $this->setupFieldArray($parentFieldArray, $vardef);
133         }
134                 else
135         {
136                 $vardef['name'] = strtoupper($vardef['name']);
137         }
138         
139         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex, false);
140
141         $this->ss->left_delimiter = '{';
142         $this->ss->right_delimiter = '}';
143         $this->ss->assign('col',$vardef['name']);
144
145         return $this->fetch($this->findTemplate('ListView'));
146     }
147
148     /**
149      * Returns a smarty template for the DetailViews
150      *
151      * @param parentFieldArray string name of the variable in the parent template for the bean's data
152      * @param vardef vardef field defintion
153      * @param displayParam parameters for display
154      *      available paramters are:
155      *      * labelSpan - column span for the label
156      *      * fieldSpan - column span for the field
157      */
158     function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
159         return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'DetailView');
160     }
161
162         // 99% of all fields will just format like a listview, but just in case, it's here to override
163     function getChangeLogSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
164         return $this->formatField($parentFieldArray[$vardef['name']],$vardef);
165     }
166
167
168     function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
169         if(!empty($vardef['function']['returns']) && $vardef['function']['returns'] == 'html'){
170                 $type = $this->type;
171                 $this->type = 'Base';
172                 $result= $this->getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
173                 $this->type = $type;
174                 return $result;
175         }
176         // jpereira@dri - #Bug49513 - Readonly type not working as expected
177         // If readonly is set in displayParams, the vardef will be displayed as in DetailView.
178         if (isset($displayParams['readonly']) && $displayParams['readonly']) {
179                 return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'DetailView');
180         }       
181         // ~ jpereira@dri - #Bug49513 - Readonly type not working as expected
182        return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
183     }
184
185     function getImportViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex)
186     {
187         return $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
188     }
189
190
191
192     function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
193                 if(!empty($vardef['auto_increment']))$vardef['len']=255;
194         return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
195     }
196
197     function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
198          if (is_array($displayParams) && !isset($displayParams['formName']))
199                      $displayParams['formName'] = 'popup_query_form';
200              else if (empty($displayParams))
201                      $displayParams = array('formName' => 'popup_query_form');
202                  return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
203     }
204
205     public function getEmailTemplateValue($inputField, $vardef, $context = null){
206         // This does not return a smarty section, instead it returns a direct value
207         return $this->formatField($inputField,$vardef);
208     }
209
210     function displayFromFunc( $displayType, $parentFieldArray, $vardef, $displayParams, $tabindex = 0 ) {
211
212         if ( ! is_array($vardef['function']) ) {
213             $funcName = $vardef['function'];
214             $includeFile = '';
215             $onListView = false;
216             $returnsHtml = false;
217         } else {
218             $funcName = $vardef['function']['name'];
219             $includeFile = '';
220             if ( isset($vardef['function']['include']) ) {
221                 $includeFile = $vardef['function']['include'];
222             }
223             if ( isset($vardef['function']['onListView']) && $vardef['function']['onListView'] == true ) {
224                 $onListView = true;
225             } else {
226                 $onListView = false;
227             }
228             if ( isset($vardef['function']['returns']) && $vardef['function']['returns'] == 'html' ) {
229                 $returnsHtml = true;
230             } else {
231                 $returnsHtml = false;
232             }
233         }
234
235         if ( $displayType == 'ListView'
236                 || $displayType == 'popupView'
237                 || $displayType == 'searchView'
238                 || $displayType == 'wirelessEditView'
239                 || $displayType == 'wirelessDetailView'
240                 || $displayType == 'wirelessListView'
241                 ) {
242             // Traditionally, before 6.0, additional functions were never called, so this code doesn't get called unless the vardef forces it
243             if ( $onListView ) {
244                 if ( !empty($includeFile) ) {
245                     require_once($includeFile);
246                 }
247
248                 return $funcName($parentFieldArray, $vardef['name'], $parentFieldArray[strtoupper($vardef['name'])], $displayType);
249             } else {
250                 $displayTypeFunc = 'get'.$displayType.'Smarty';
251                 return $this->$displayTypeFunc($parentFieldArray, $vardef, $displayParams, $tabindex);
252             }
253         } else {
254             if ( !empty($displayParams['idName']) ) {
255                 $fieldName = $displayParams['idName'];
256             } else {
257                 $fieldName = $vardef['name'];
258             }
259             if ( $returnsHtml ) {
260                 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
261                 $tpl = $this->findTemplate($displayType.'Function');
262                 if ( $tpl == '' ) {
263                     // Can't find a function template, just use the base
264                     $tpl = $this->findTemplate('DetailViewFunction');
265                 }
266                 return "<span id='{$vardef['name']}_span'>" . $this->fetch($tpl) . '</span>';
267             } else {
268                 return '{sugar_run_helper include="'.$includeFile.'" func="'.$funcName.'" bean=$bean field="'.$fieldName.'" value=$fields.'.$fieldName.'.value displayType="'.$displayType.'"}';
269             }
270         }
271     }
272
273     function getEditView() {
274     }
275
276     /**
277      * getSearchWhereValue
278      *
279      * Checks and returns a sane value based on the field type that can be used when building the where clause in a
280      * search form.
281      *
282      * @param $value Mixed value being searched on
283      * @return Mixed the value for the where clause used in search
284      */
285     function getSearchWhereValue($value) {
286         return $value;
287     }
288
289     /**
290      * getSearchInput
291      *
292      * This function allows the SugarFields to handle returning the search input value given arguments (typically from $_REQUEST/$_POST)
293      * and a search string.
294      *
295      * @param $key String value of key to search for
296      * @param $args Mixed value containing haystack to search for value in
297      * @return $value Mixed value that the SugarField should return
298      */
299     function getSearchInput($key='', $args=array())
300     {
301         //Nothing specified return empty string
302         if(empty($key) || empty($args))
303         {
304                 return '';
305         }
306
307         return isset($args[$key]) ? $args[$key] : '';
308     }
309
310     function getQueryLike() {
311
312     }
313
314     function getQueryIn() {
315     }
316
317     /**
318      * Setup function to assign values to the smarty template, should be called before every display function
319      */
320     function setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass=true) {
321         $this->button = '';
322         $this->buttons = '';
323         $this->image = '';
324         if ($twopass){
325                 $this->ss->left_delimiter = '{{';
326                 $this->ss->right_delimiter = '}}';
327         }
328         $this->ss->assign('parentFieldArray', $parentFieldArray);
329         $this->ss->assign('vardef', $vardef);
330         $this->ss->assign('tabindex', $tabindex);
331
332         //for adding attributes to the field
333
334         if(!empty($displayParams['field'])){
335                 $plusField = '';
336                 foreach($displayParams['field'] as $key=>$value){
337                         $plusField .= ' ' . $key . '="' . $value . '"';//bug 27381
338                 }
339                 $displayParams['field'] = $plusField;
340         }
341         //for adding attributes to the button
342         if(!empty($displayParams['button'])){
343                 $plusField = '';
344                 foreach($displayParams['button'] as $key=>$value){
345                         $plusField .= ' ' . $key . '="' . $value . '"';
346                 }
347                 $displayParams['button'] = $plusField;
348                 $this->button = $displayParams['button'];
349         }
350         if(!empty($displayParams['buttons'])){
351             $plusField = '';
352             foreach($displayParams['buttons'] as $keys=>$values){
353                 foreach($values as $key=>$value){
354                     $plusField[$keys] .= ' ' . $key . '="' . $value . '"';
355                 }
356             }
357             $displayParams['buttons'] = $plusField;
358             $this->buttons = $displayParams['buttons'];
359         }
360         if(!empty($displayParams['image'])){
361             $plusField = '';
362             foreach($displayParams['image'] as $key=>$value){
363                 $plusField .= ' ' . $key . '="' . $value . '"';
364             }
365             $displayParams['image'] = $plusField;
366             $this->image = $displayParams['image'];
367         }
368         $this->ss->assign('displayParams', $displayParams);
369
370
371     }
372
373     protected function getAccessKey($vardef, $fieldType = null, $module = null) {
374         global $app_strings;
375
376         $labelList = array(
377             'accessKey' => array(),
378             'accessKeySelect' => array(),
379             'accessKeyClear' => array(),
380         );
381
382         // Labels are always in uppercase
383         if ( isset($fieldType) ) {
384             $fieldType = strtoupper($fieldType);
385         }
386
387         if ( isset($module) ) {
388             $module = strtoupper($module);
389         }
390
391         // The vardef is the most specific, then the module + fieldType, then the module, then the fieldType
392         if ( isset($vardef['accessKey']) ) {
393             $labelList['accessKey'][] = $vardef['accessKey'];
394         }
395         if ( isset($vardef['accessKeySelect']) ) {
396             $labelList['accessKeySelect'][] = $vardef['accessKeySelect'];
397         }
398         if ( isset($vardef['accessKeyClear']) ) {
399             $labelList['accessKeyClear'][] = $vardef['accessKeyClear'];
400         }
401
402         if ( isset($fieldType) && isset($module) ) {
403             $labelList['accessKey'][] = 'LBL_ACCESSKEY_'.$fieldType.'_'.$module;
404             $labelList['accessKeySelect'][] = 'LBL_ACCESSKEY_SELECT_'.$fieldType.'_'.$module;
405             $labelList['accessKeyClear'][] = 'LBL_ACCESSKEY_CLEAR_'.$fieldType.'_'.$module;
406         }
407
408         if ( isset($module) ) {
409             $labelList['accessKey'][] = 'LBL_ACCESSKEY_'.$module;
410             $labelList['accessKeySelect'][] = 'LBL_ACCESSKEY_SELECT_'.$module;
411             $labelList['accessKeyClear'][] = 'LBL_ACCESSKEY_CLEAR_'.$module;
412         }
413
414         if ( isset($fieldType) ) {
415             $labelList['accessKey'][] = 'LBL_ACCESSKEY_'.$fieldType;
416             $labelList['accessKeySelect'][] = 'LBL_ACCESSKEY_SELECT_'.$fieldType;
417             $labelList['accessKeyClear'][] = 'LBL_ACCESSKEY_CLEAR_'.$fieldType;
418         }
419
420         // Attach the defaults to the ends
421         $labelList['accessKey'][] = 'LBL_ACCESSKEY';
422         $labelList['accessKeySelect'][] = 'LBL_SELECT_BUTTON';
423         $labelList['accessKeyClear'][] = 'LBL_CLEAR_BUTTON';
424
425         // Figure out the label and the key for the button.
426         // Later on we may attempt to make sure there are no two buttons with the same keys, but for now we will just use whatever is specified.
427         $keyTypes = array('accessKey','accessKeySelect','accessKeyClear');
428         $accessKeyList = array(
429             'accessKey' => '',
430             'accessKeyLabel' => '',
431             'accessKeyTitle' => '',
432             'accessKeySelect' => '',
433             'accessKeySelectLabel' => '',
434             'accessKeySelectTitle' => '',
435             'accessKeyClear' => '',
436             'accessKeyClearLabel' => '',
437             'accessKeyClearTitle' => '',
438         );
439         foreach( $keyTypes as $type ) {
440             foreach ( $labelList[$type] as $tryThis ) {
441                 if ( isset($app_strings[$tryThis.'_KEY']) && isset($app_strings[$tryThis.'_TITLE']) && isset($app_strings[$tryThis.'_LABEL']) ) {
442                     $accessKeyList[$type] = $tryThis.'_KEY';
443                     $accessKeyList[$type.'Title'] = $tryThis.'_TITLE';
444                     $accessKeyList[$type.'Label'] = $tryThis.'_LABEL';
445                     break;
446                 }
447             }
448         }
449
450         return $accessKeyList;
451     }
452
453         /**
454      * This should be called when the bean is saved. The bean itself will be passed by reference
455      * @param SugarBean bean - the bean performing the save
456      * @param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
457      */
458     public function save($bean, $params, $field, $properties, $prefix = '') {
459          if ( isset($params[$prefix.$field]) ) {
460              if(isset($properties['len']) && isset($properties['type']) && $this->isTrimmable($properties['type'])){
461                  $bean->$field = trim($this->unformatField($params[$prefix.$field], $properties));
462              }
463              else {
464                  $bean->$field = $this->unformatField($params[$prefix.$field], $properties);
465                  }
466          }
467      }
468
469      /**
470       * Check if the field is allowed to be trimmed
471       *
472       * @param string $type
473       * @return boolean
474       */
475      protected function isTrimmable($type) {
476          return in_array($type, array('varchar', 'name'));
477      }
478
479     /**
480      * Handles import field sanitizing for an field type
481      *
482      * @param  $value    string value to be sanitized
483      * @param  $vardefs  array
484      * @param  $focus    SugarBean object
485      * @param  $settings ImportFieldSanitize object
486      * @return string sanitized value or boolean false if there's a problem with the value
487      */
488     public function importSanitize(
489         $value,
490         $vardef,
491         $focus,
492         ImportFieldSanitize $settings
493         )
494     {
495         if( isset($vardef['len']) ) {
496             // check for field length
497             $value = sugar_substr($value, $vardef['len']);
498         }
499
500         return $value;
501     }
502
503     /**
504      * isRangeSearchView
505      * This method helps determine whether or not to display the range search view code for the sugar field
506      * @param array $vardef entry representing the sugar field's definition
507      * @return boolean true if range search view should be displayed, false otherwise
508      */
509     protected function isRangeSearchView($vardef)
510     {
511         return !empty($vardef['enable_range_search']) && !empty($_REQUEST['action']) && $_REQUEST['action']!='Popup';
512     }
513
514     /**
515      * setupFieldArray
516      * This method takes the $parentFieldArray mixed variable which may be an Array or object and attempts
517      * to call any custom fieldSpecific formatting to the value depending on the field type.
518      *
519      * @see SugarFieldEnum.php, SugarFieldInt.php, SugarFieldFloat.php, SugarFieldRelate.php
520      * @param   mixed   $parentFieldArray Array or Object of data where the field's value comes from
521      * @param   array   $vardef The vardef entry linked to the SugarField instance
522      * @return  array   $parentFieldArray The formatted $parentFieldArray with the formatField method possibly applied
523      */
524     protected function setupFieldArray($parentFieldArray, $vardef)
525     {
526         $fieldName = $vardef['name'];
527         if ( is_array($parentFieldArray) )
528         {
529             $fieldNameUpper = strtoupper($fieldName);
530             if ( isset($parentFieldArray[$fieldNameUpper]))
531             {
532                 $parentFieldArray[$fieldName] = $this->formatField($parentFieldArray[$fieldNameUpper],$vardef);
533             } else {
534                 $parentFieldArray[$fieldName] = '';
535             }
536         } elseif (is_object($parentFieldArray)) {
537             if ( isset($parentFieldArray->$fieldName) )
538             {
539                 $parentFieldArray->$fieldName = $this->formatField($parentFieldArray->$fieldName,$vardef);
540             } else {
541                 $parentFieldArray->$fieldName = '';
542             }
543         }
544         return $parentFieldArray;
545     }
546
547 }
548 ?>