]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/SugarFields/Fields/Base/SugarFieldBase.php
Release 6.2.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         // FIXME: Rework the listview to use two-pass rendering like the DetailView
128
129         $tabindex = 1;
130         $isArray = is_array($parentFieldArray);
131         $fieldName = $vardef['name'];
132
133         if ( $isArray ) {
134                 $fieldNameUpper = strtoupper($fieldName);
135             if ( isset($parentFieldArray[$fieldNameUpper])) {
136                 $parentFieldArray[$fieldName] = $this->formatField($parentFieldArray[$fieldNameUpper],$vardef);
137             } else {
138                 $parentFieldArray[$fieldName] = '';
139             }
140         } else {
141             if ( isset($parentFieldArray->$fieldName) ) {
142                 $parentFieldArray->$fieldName = $this->formatField($parentFieldArray->$fieldName,$vardef);
143             } else {
144                 $parentFieldArray->$fieldName = '';
145             }
146         }
147         $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex, false);
148
149         $this->ss->left_delimiter = '{';
150         $this->ss->right_delimiter = '}';
151         $this->ss->assign('col',$vardef['name']);
152
153         return $this->fetch($this->findTemplate('ListView'));
154     }
155
156     /**
157      * Returns a smarty template for the DetailViews
158      *
159      * @param parentFieldArray string name of the variable in the parent template for the bean's data
160      * @param vardef vardef field defintion
161      * @param displayParam parameters for display
162      *      available paramters are:
163      *      * labelSpan - column span for the label
164      *      * fieldSpan - column span for the field
165      */
166     function getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
167         return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'DetailView');
168     }
169
170         // 99% of all fields will just format like a listview, but just in case, it's here to override
171     function getChangeLogSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
172         return $this->formatField($parentFieldArray[$vardef['name']],$vardef);
173     }
174
175
176     function getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
177         if(!empty($vardef['function']['returns']) && $vardef['function']['returns'] == 'html'){
178                 $type = $this->type;
179                 $this->type = 'Base';
180                 $result= $this->getDetailViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
181                 $this->type = $type;
182                 return $result;
183         }
184        return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
185     }
186
187     function getImportViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex)
188     {
189         return $this->getEditViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
190     }
191
192
193
194     function getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex) {
195                 if(!empty($vardef['auto_increment']))$vardef['len']=255;
196         return $this->getSmartyView($parentFieldArray, $vardef, $displayParams, $tabindex, 'EditView');
197     }
198
199     function getPopupViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex){
200          if (is_array($displayParams) && !isset($displayParams['formName']))
201                      $displayParams['formName'] = 'popup_query_form';
202              else if (empty($displayParams))
203                      $displayParams = array('formName' => 'popup_query_form');
204                  return $this->getSearchViewSmarty($parentFieldArray, $vardef, $displayParams, $tabindex);
205     }
206
207     public function getEmailTemplateValue($inputField, $vardef, $context = null){
208         // This does not return a smarty section, instead it returns a direct value
209         return $this->formatField($inputField,$vardef);
210     }
211
212     function displayFromFunc( $displayType, $parentFieldArray, $vardef, $displayParams, $tabindex = 0 ) {
213
214         if ( ! is_array($vardef['function']) ) {
215             $funcName = $vardef['function'];
216             $includeFile = '';
217             $onListView = false;
218             $returnsHtml = false;
219         } else {
220             $funcName = $vardef['function']['name'];
221             $includeFile = '';
222             if ( isset($vardef['function']['include']) ) {
223                 $includeFile = $vardef['function']['include'];
224             }
225             if ( isset($vardef['function']['onListView']) && $vardef['function']['onListView'] == true ) {
226                 $onListView = true;
227             } else {
228                 $onListView = false;
229             }
230             if ( isset($vardef['function']['returns']) && $vardef['function']['returns'] == 'html' ) {
231                 $returnsHtml = true;
232             } else {
233                 $returnsHtml = false;
234             }
235         }
236
237         if ( $displayType == 'ListView'
238                 || $displayType == 'popupView'
239                 || $displayType == 'searchView'
240                 || $displayType == 'wirelessEditView'
241                 || $displayType == 'wirelessDetailView'
242                 || $displayType == 'wirelessListView'
243                 ) {
244             // Traditionally, before 6.0, additional functions were never called, so this code doesn't get called unless the vardef forces it
245             if ( $onListView ) {
246                 if ( !empty($includeFile) ) {
247                     require_once($includeFile);
248                 }
249
250                 return $funcName($parentFieldArray, $vardef['name'], $parentFieldArray[$vardef['name']], $displayType);
251             } else {
252                 $displayTypeFunc = 'get'.$displayType.'Smarty';
253                 return $this->$displayTypeFunc($parentFieldArray, $vardef, $displayParams, $tabindex);
254             }
255         } else {
256             if ( !empty($displayParams['idName']) ) {
257                 $fieldName = $displayParams['idName'];
258             } else {
259                 $fieldName = $vardef['name'];
260             }
261             if ( $returnsHtml ) {
262                 $this->setup($parentFieldArray, $vardef, $displayParams, $tabindex);
263                 $tpl = $this->findTemplate($displayType.'Function');
264                 if ( $tpl == '' ) {
265                     // Can't find a function template, just use the base
266                     $tpl = $this->findTemplate('DetailViewFunction');
267                 }
268                 return "<span id='{$vardef['name']}'>" . $this->fetch($tpl) . '</span>';
269             } else {
270                 return '{sugar_run_helper include="'.$includeFile.'" func="'.$funcName.'" bean=$bean field="'.$fieldName.'" value=$fields.'.$fieldName.'.value displayType="'.$displayType.'"}';
271             }
272         }
273     }
274
275     function getEditView() {
276     }
277
278     function getSearchInput() {
279     }
280
281     function getQueryLike() {
282     }
283
284     function getQueryIn() {
285     }
286
287     /**
288      * Setup function to assign values to the smarty template, should be called before every display function
289      */
290     function setup($parentFieldArray, $vardef, $displayParams, $tabindex, $twopass=true) {
291         $this->button = '';
292         $this->buttons = '';
293         $this->image = '';
294         if ($twopass){
295                 $this->ss->left_delimiter = '{{';
296                 $this->ss->right_delimiter = '}}';
297         }
298         $this->ss->assign('parentFieldArray', $parentFieldArray);
299         $this->ss->assign('vardef', $vardef);
300         $this->ss->assign('tabindex', $tabindex);
301
302         //for adding attributes to the field
303
304         if(!empty($displayParams['field'])){
305                 $plusField = '';
306                 foreach($displayParams['field'] as $key=>$value){
307                         $plusField .= ' ' . $key . '="' . $value . '"';//bug 27381
308                 }
309                 $displayParams['field'] = $plusField;
310         }
311         //for adding attributes to the button
312         if(!empty($displayParams['button'])){
313                 $plusField = '';
314                 foreach($displayParams['button'] as $key=>$value){
315                         $plusField .= ' ' . $key . '="' . $value . '"';
316                 }
317                 $displayParams['button'] = $plusField;
318                 $this->button = $displayParams['button'];
319         }
320         if(!empty($displayParams['buttons'])){
321             $plusField = '';
322             foreach($displayParams['buttons'] as $keys=>$values){
323                 foreach($values as $key=>$value){
324                     $plusField[$keys] .= ' ' . $key . '="' . $value . '"';
325                 }
326             }
327             $displayParams['buttons'] = $plusField;
328             $this->buttons = $displayParams['buttons'];
329         }
330         if(!empty($displayParams['image'])){
331             $plusField = '';
332             foreach($displayParams['image'] as $key=>$value){
333                 $plusField .= ' ' . $key . '="' . $value . '"';
334             }
335             $displayParams['image'] = $plusField;
336             $this->image = $displayParams['image'];
337         }
338         $this->ss->assign('displayParams', $displayParams);
339
340
341     }
342
343              /**
344      * This should be called when the bean is saved. The bean itself will be passed by reference
345      * @param SugarBean bean - the bean performing the save
346      * @param array params - an array of paramester relevant to the save, most likely will be $_REQUEST
347      */
348         public function save($bean, $params, $field, $properties, $prefix = ''){
349          if ( isset($params[$prefix.$field]) ) {
350              if(isset($properties['len']) && isset($properties['type']) && 'varchar' == $properties['type']){
351                  $bean->$field = trim($this->unformatField($params[$prefix.$field],$properties));
352              }
353                  else {
354                  $bean->$field = $this->unformatField($params[$prefix.$field],$properties);
355                  }
356          }
357      }
358
359     /**
360      * Handles import field sanitizing for an field type
361      *
362      * @param  $value    string value to be sanitized
363      * @param  $vardefs  array
364      * @param  $focus    SugarBean object
365      * @param  $settings ImportFieldSanitize object
366      * @return string sanitized value or boolean false if there's a problem with the value
367      */
368     public function importSanitize(
369         $value,
370         $vardef,
371         $focus,
372         ImportFieldSanitize $settings
373         )
374     {
375         if( isset($vardef['len']) ) {
376             // check for field length
377             $value = sugar_substr($value, $vardef['len']);
378         }
379
380         return $value;
381     }
382
383     /**
384      * isRangeSearchView
385      * This method helps determine whether or not to display the range search view code for the sugar field
386      * @param array $vardef entry representing the sugar field's definition
387      * @return boolean true if range search view should be displayed, false otherwise
388      */
389     protected function isRangeSearchView($vardef)
390     {
391         return !empty($vardef['enable_range_search']) && !empty($_REQUEST['action']) && $_REQUEST['action']!='Popup';
392     }
393 }