]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Base/SugarFieldBase.php
Release 6.4.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-2011 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                 $parentFieldArray = $this->setupFieldArray($parentFieldArray, $vardef);
129         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex, false);
130
131         $this->ss->left_delimiter = '{';
132         $this->ss->right_delimiter = '}';
133         $this->ss->assign('col',$vardef['name']);
134
135         return $this->fetch($this->findTemplate('ListView'));
136     }
137
138     /**
139      * Returns a smarty template for the DetailViews
140      *
141      * @param parentFieldArray string name of the variable in the parent template for the bean's data
142      * @param vardef vardef field defintion
143      * @param displayParam parameters for display
144      *      available paramters are:
145      *      * labelSpan - column span for the label
146      *      * fieldSpan - column span for the field
147      */
148     function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
149         return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'DetailView');
150     }
151
152         // 99% of all fields will just format like a listview, but just in case, it's here to override
153     function getChangeLogSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
154         return $this->formatField($parentFieldArray[$vardef['name']],$vardef);
155     }
156
157
158     function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
159         if(!empty($vardef['function']['returns']) && $vardef['function']['returns'] == 'html'){
160                 $type = $this->type;
161                 $this->type = 'Base';
162                 $result= $this->getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
163                 $this->type = $type;
164                 return $result;
165         }
166        return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
167     }
168
169     function getImportViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex)
170     {
171         return $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
172     }
173
174
175
176     function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
177                 if(!empty($vardef['auto_increment']))$vardef['len']=255;
178         return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
179     }
180
181     function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
182          if (is_array($displayParams) && !isset($displayParams['formName']))
183                      $displayParams['formName'] = 'popup_query_form';
184              else if (empty($displayParams))
185                      $displayParams = array('formName' => 'popup_query_form');
186                  return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
187     }
188
189     public function getEmailTemplateValue($inputField, $vardef, $context = null){
190         // This does not return a smarty section, instead it returns a direct value
191         return $this->formatField($inputField,$vardef);
192     }
193
194     function displayFromFunc( $displayType, $parentFieldArray, $vardef, $displayParams, $tabindex = 0 ) {
195
196         if ( ! is_array($vardef['function']) ) {
197             $funcName = $vardef['function'];
198             $includeFile = '';
199             $onListView = false;
200             $returnsHtml = false;
201         } else {
202             $funcName = $vardef['function']['name'];
203             $includeFile = '';
204             if ( isset($vardef['function']['include']) ) {
205                 $includeFile = $vardef['function']['include'];
206             }
207             if ( isset($vardef['function']['onListView']) && $vardef['function']['onListView'] == true ) {
208                 $onListView = true;
209             } else {
210                 $onListView = false;
211             }
212             if ( isset($vardef['function']['returns']) && $vardef['function']['returns'] == 'html' ) {
213                 $returnsHtml = true;
214             } else {
215                 $returnsHtml = false;
216             }
217         }
218
219         if ( $displayType == 'ListView'
220                 || $displayType == 'popupView'
221                 || $displayType == 'searchView'
222                 || $displayType == 'wirelessEditView'
223                 || $displayType == 'wirelessDetailView'
224                 || $displayType == 'wirelessListView'
225                 ) {
226             // Traditionally, before 6.0, additional functions were never called, so this code doesn't get called unless the vardef forces it
227             if ( $onListView ) {
228                 if ( !empty($includeFile) ) {
229                     require_once($includeFile);
230                 }
231
232                 return $funcName($parentFieldArray, $vardef['name'], $parentFieldArray[strtoupper($vardef['name'])], $displayType);
233             } else {
234                 $displayTypeFunc = 'get'.$displayType.'Smarty';
235                 return $this->$displayTypeFunc($parentFieldArray, $vardef, $displayParams, $tabindex);
236             }
237         } else {
238             if ( !empty($displayParams['idName']) ) {
239                 $fieldName = $displayParams['idName'];
240             } else {
241                 $fieldName = $vardef['name'];
242             }
243             if ( $returnsHtml ) {
244                 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
245                 $tpl = $this->findTemplate($displayType.'Function');
246                 if ( $tpl == '' ) {
247                     // Can't find a function template, just use the base
248                     $tpl = $this->findTemplate('DetailViewFunction');
249                 }
250                 return "<span id='{$vardef['name']}_span'>" . $this->fetch($tpl) . '</span>';
251             } else {
252                 return '{sugar_run_helper include="'.$includeFile.'" func="'.$funcName.'" bean=$bean field="'.$fieldName.'" value=$fields.'.$fieldName.'.value displayType="'.$displayType.'"}';
253             }
254         }
255     }
256
257     function getEditView() {
258     }
259
260     /**
261      * getSearchWhereValue
262      *
263      * Checks and returns a sane value based on the field type that can be used when building the where clause in a
264      * search form.
265      *
266      * @param $value Mixed value being searched on
267      * @return Mixed the value for the where clause used in search
268      */
269     function getSearchWhereValue($value) {
270         return $value;
271     }
272
273     /**
274      * getSearchInput
275      *
276      * This function allows the SugarFields to handle returning the search input value given arguments (typically from $_REQUEST/$_POST)
277      * and a search string.
278      *
279      * @param $key String value of key to search for
280      * @param $args Mixed value containing haystack to search for value in
281      * @return $value Mixed value that the SugarField should return
282      */
283     function getSearchInput($key='', $args=array())
284     {
285         //Nothing specified return empty string
286         if(empty($key) || empty($args))
287         {
288                 return '';
289         }
290
291         return isset($args[$key]) ? $args[$key] : '';
292     }
293
294     function getQueryLike() {
295
296     }
297
298     function getQueryIn() {
299     }
300
301     /**
302      * Setup function to assign values to the smarty template, should be called before every display function
303      */
304     function setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass=true) {
305         $this->button = '';
306         $this->buttons = '';
307         $this->image = '';
308         if ($twopass){
309                 $this->ss->left_delimiter = '{{';
310                 $this->ss->right_delimiter = '}}';
311         }
312         $this->ss->assign('parentFieldArray', $parentFieldArray);
313         $this->ss->assign('vardef', $vardef);
314         $this->ss->assign('tabindex', $tabindex);
315
316         //for adding attributes to the field
317
318         if(!empty($displayParams['field'])){
319                 $plusField = '';
320                 foreach($displayParams['field'] as $key=>$value){
321                         $plusField .= ' ' . $key . '="' . $value . '"';//bug 27381
322                 }
323                 $displayParams['field'] = $plusField;
324         }
325         //for adding attributes to the button
326         if(!empty($displayParams['button'])){
327                 $plusField = '';
328                 foreach($displayParams['button'] as $key=>$value){
329                         $plusField .= ' ' . $key . '="' . $value . '"';
330                 }
331                 $displayParams['button'] = $plusField;
332                 $this->button = $displayParams['button'];
333         }
334         if(!empty($displayParams['buttons'])){
335             $plusField = '';
336             foreach($displayParams['buttons'] as $keys=>$values){
337                 foreach($values as $key=>$value){
338                     $plusField[$keys] .= ' ' . $key . '="' . $value . '"';
339                 }
340             }
341             $displayParams['buttons'] = $plusField;
342             $this->buttons = $displayParams['buttons'];
343         }
344         if(!empty($displayParams['image'])){
345             $plusField = '';
346             foreach($displayParams['image'] as $key=>$value){
347                 $plusField .= ' ' . $key . '="' . $value . '"';
348             }
349             $displayParams['image'] = $plusField;
350             $this->image = $displayParams['image'];
351         }
352         $this->ss->assign('displayParams', $displayParams);
353
354
355     }
356
357     protected function getAccessKey($vardef, $fieldType = null, $module = null) {
358         global $app_strings;
359
360         $labelList = array(
361             'accessKey' => array(),
362             'accessKeySelect' => array(),
363             'accessKeyClear' => array(),
364         );
365
366         // Labels are always in uppercase
367         if ( isset($fieldType) ) {
368             $fieldType = strtoupper($fieldType);
369         }
370
371         if ( isset($module) ) {
372             $module = strtoupper($module);
373         }
374
375         // The vardef is the most specific, then the module + fieldType, then the module, then the fieldType
376         if ( isset($vardef['accessKey']) ) {
377             $labelList['accessKey'][] = $vardef['accessKey'];
378         }
379         if ( isset($vardef['accessKeySelect']) ) {
380             $labelList['accessKeySelect'][] = $vardef['accessKeySelect'];
381         }
382         if ( isset($vardef['accessKeyClear']) ) {
383             $labelList['accessKeyClear'][] = $vardef['accessKeyClear'];
384         }
385
386         if ( isset($fieldType) && isset($module) ) {
387             $labelList['accessKey'][] = 'LBL_ACCESSKEY_'.$fieldType.'_'.$module;
388             $labelList['accessKeySelect'][] = 'LBL_ACCESSKEY_SELECT_'.$fieldType.'_'.$module;
389             $labelList['accessKeyClear'][] = 'LBL_ACCESSKEY_CLEAR_'.$fieldType.'_'.$module;
390         }
391
392         if ( isset($module) ) {
393             $labelList['accessKey'][] = 'LBL_ACCESSKEY_'.$module;
394             $labelList['accessKeySelect'][] = 'LBL_ACCESSKEY_SELECT_'.$module;
395             $labelList['accessKeyClear'][] = 'LBL_ACCESSKEY_CLEAR_'.$module;
396         }
397
398         if ( isset($fieldType) ) {
399             $labelList['accessKey'][] = 'LBL_ACCESSKEY_'.$fieldType;
400             $labelList['accessKeySelect'][] = 'LBL_ACCESSKEY_SELECT_'.$fieldType;
401             $labelList['accessKeyClear'][] = 'LBL_ACCESSKEY_CLEAR_'.$fieldType;
402         }
403
404         // Attach the defaults to the ends
405         $labelList['accessKey'][] = 'LBL_ACCESSKEY';
406         $labelList['accessKeySelect'][] = 'LBL_SELECT_BUTTON';
407         $labelList['accessKeyClear'][] = 'LBL_CLEAR_BUTTON';
408
409         // Figure out the label and the key for the button.
410         // 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.
411         $keyTypes = array('accessKey','accessKeySelect','accessKeyClear');
412         $accessKeyList = array(
413             'accessKey' => '',
414             'accessKeyLabel' => '',
415             'accessKeyTitle' => '',
416             'accessKeySelect' => '',
417             'accessKeySelectLabel' => '',
418             'accessKeySelectTitle' => '',
419             'accessKeyClear' => '',
420             'accessKeyClearLabel' => '',
421             'accessKeyClearTitle' => '',
422         );
423         foreach( $keyTypes as $type ) {
424             foreach ( $labelList[$type] as $tryThis ) {
425                 if ( isset($app_strings[$tryThis.'_KEY']) && isset($app_strings[$tryThis.'_TITLE']) && isset($app_strings[$tryThis.'_LABEL']) ) {
426                     $accessKeyList[$type] = $tryThis.'_KEY';
427                     $accessKeyList[$type.'Title'] = $tryThis.'_TITLE';
428                     $accessKeyList[$type.'Label'] = $tryThis.'_LABEL';
429                     break;
430                 }
431             }
432         }
433
434         return $accessKeyList;
435     }
436
437         /**
438      * This should be called when the bean is saved. The bean itself will be passed by reference
439      * @param SugarBean bean - the bean performing the save
440      * @param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
441      */
442     public function save($bean, $params, $field, $properties, $prefix = '') {
443          if ( isset($params[$prefix.$field]) ) {
444              if(isset($properties['len']) && isset($properties['type']) && $this->isTrimmable($properties['type'])){
445                  $bean->$field = trim($this->unformatField($params[$prefix.$field], $properties));
446              }
447              else {
448                  $bean->$field = $this->unformatField($params[$prefix.$field], $properties);
449                  }
450          }
451      }
452
453      /**
454       * Check if the field is allowed to be trimmed
455       *
456       * @param string $type
457       * @return boolean
458       */
459      protected function isTrimmable($type) {
460          return in_array($type, array('varchar', 'name'));
461      }
462
463     /**
464      * Handles import field sanitizing for an field type
465      *
466      * @param  $value    string value to be sanitized
467      * @param  $vardefs  array
468      * @param  $focus    SugarBean object
469      * @param  $settings ImportFieldSanitize object
470      * @return string sanitized value or boolean false if there's a problem with the value
471      */
472     public function importSanitize(
473         $value,
474         $vardef,
475         $focus,
476         ImportFieldSanitize $settings
477         )
478     {
479         if( isset($vardef['len']) ) {
480             // check for field length
481             $value = sugar_substr($value, $vardef['len']);
482         }
483
484         return $value;
485     }
486
487     /**
488      * isRangeSearchView
489      * This method helps determine whether or not to display the range search view code for the sugar field
490      * @param array $vardef entry representing the sugar field's definition
491      * @return boolean true if range search view should be displayed, false otherwise
492      */
493     protected function isRangeSearchView($vardef)
494     {
495         return !empty($vardef['enable_range_search']) && !empty($_REQUEST['action']) && $_REQUEST['action']!='Popup';
496     }
497
498     /**
499      * setupFieldArray
500      * This method takes the $parentFieldArray mixed variable which may be an Array or object and attempts
501      * to call any custom fieldSpecific formatting to the value depending on the field type.
502      *
503      * @see SugarFieldEnum.php, SugarFieldInt.php, SugarFieldFloat.php, SugarFieldRelate.php
504      * @param   mixed   $parentFieldArray Array or Object of data where the field's value comes from
505      * @param   array   $vardef The vardef entry linked to the SugarField instance
506      * @return  array   $parentFieldArray The formatted $parentFieldArray with the formatField method possibly applied
507      */
508     protected function setupFieldArray($parentFieldArray, $vardef)
509     {
510         $fieldName = $vardef['name'];
511         if ( is_array($parentFieldArray) )
512         {
513             $fieldNameUpper = strtoupper($fieldName);
514             if ( isset($parentFieldArray[$fieldNameUpper]))
515             {
516                 $parentFieldArray[$fieldName] = $this->formatField($parentFieldArray[$fieldNameUpper],$vardef);
517             } else {
518                 $parentFieldArray[$fieldName] = '';
519             }
520         } elseif (is_object($parentFieldArray)) {
521             if ( isset($parentFieldArray->$fieldName) )
522             {
523                 $parentFieldArray->$fieldName = $this->formatField($parentFieldArray->$fieldName,$vardef);
524             } else {
525                 $parentFieldArray->$fieldName = '';
526             }
527         }
528         return $parentFieldArray;
529     }
530
531 }
532 ?>