]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/EditView/EditView2.php
Release 6.2.0
[Github/sugarcrm.git] / include / EditView / EditView2.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
39 require_once('include/TemplateHandler/TemplateHandler.php');
40 require_once('include/EditView/SugarVCR.php');
41
42 class EditView
43 {
44     var $th;
45     var $tpl;
46     var $notes;
47     var $id;
48     var $metadataFile;
49     var $headerTpl;
50     var $footerTpl;
51     var $returnAction;
52     var $returnModule;
53     var $returnId;
54     var $isDuplicate;
55     var $focus;
56     var $module;
57     var $fieldDefs;
58     var $sectionPanels;
59     var $view = 'EditView';
60     var $formatFields = true;
61     var $showDetailData = true;
62     var $showVCRControl = true;
63     var $showSectionPanelsTitles = true;
64     var $quickSearchCode;
65     var $ss;
66     var $offset = 0;
67     var $populateBean = true;
68     var $moduleTitleKey;
69     var $viewObject = null;
70     var $formName = '';
71
72     /**
73      * EditView constructor
74      * This is the EditView constructor responsible for processing the new
75      * Meta-Data framework
76      *
77      * @param $module String value of module this Edit view is for
78      * @param $focus An empty sugarbean object of module
79      * @param $id The record id to retrieve and populate data for
80      * @param $metadataFile String value of file location to use in overriding default metadata file
81      * @param tpl String value of file location to use in overriding default Smarty template
82      * @param createFocus bool value to tell whether to create a new bean if we do not have one with an id, this is used from ConvertLead
83      *
84      */
85     function setup($module, $focus = null, $metadataFile = null, $tpl = 'include/EditView/EditView.tpl', $createFocus = true)
86     {
87         $this->th = new TemplateHandler();
88         $this->th->ss =& $this->ss;
89         $this->tpl = $tpl;
90         $this->module = $module;
91         $this->focus = $focus;
92
93         //this logic checks if the focus has an id and if it does not then it will create a new instance of the focus bean
94         //but in convert lead we do not want to create a new instance and do not want to populate id.
95         if($createFocus)
96         {
97                 $this->createFocus();
98         }
99         if(empty($GLOBALS['sugar_config']['showDetailData'])) {
100                 $this->showDetailData = false;
101         }
102         $this->metadataFile = $metadataFile;
103
104         if(isset($GLOBALS['sugar_config']['disable_vcr'])) {
105            $this->showVCRControl = !$GLOBALS['sugar_config']['disable_vcr'];
106         }
107         if(!empty($this->metadataFile) && file_exists($this->metadataFile)){
108                 include($this->metadataFile);
109         }else {
110                 //If file doesn't exist we create a best guess
111                 if(!file_exists("modules/$this->module/metadata/editviewdefs.php") &&
112                     file_exists("modules/$this->module/EditView.html")) {
113                     require_once('include/SugarFields/Parsers/EditViewMetaParser.php');
114                 global $dictionary;
115                     $htmlFile = "modules/" . $this->module . "/EditView.html";
116                     $parser = new EditViewMetaParser();
117                     if(!file_exists('modules/'.$this->module.'/metadata')) {
118                        sugar_mkdir('modules/'.$this->module.'/metadata');
119                     }
120                         $fp = sugar_fopen('modules/'.$this->module.'/metadata/editviewdefs.php', 'w');
121                     fwrite($fp, $parser->parse($htmlFile, $dictionary[$focus->object_name]['fields'], $this->module));
122                     fclose($fp);
123                 }
124
125                 //Flag an error... we couldn't create the best guess meta-data file
126                 if(!file_exists("modules/$this->module/metadata/editviewdefs.php")) {
127                    global $app_strings;
128                    $error = str_replace("[file]", "modules/$this->module/metadata/editviewdefs.php", $app_strings['ERR_CANNOT_CREATE_METADATA_FILE']);
129                    $GLOBALS['log']->fatal($error);
130                    echo $error;
131                    die();
132                 }
133             require_once("modules/$this->module/metadata/editviewdefs.php");
134         }
135
136         $this->defs = $viewdefs[$this->module][$this->view];
137         $this->isDuplicate = isset($_REQUEST['isDuplicate']) && $_REQUEST['isDuplicate'] == 'true' && $this->focus->aclAccess('edit');
138     }
139
140     function createFocus()
141     {
142         global $beanList, $beanFiles;
143
144         if(empty($beanList[$this->module]))return;
145         if(!$this->focus ){
146            $bean = $beanList[$this->module];
147             require_once($beanFiles[$bean]);
148            $obj = new $bean();
149            $this->focus = $obj;
150         }
151
152         //If there is no idea, assume we are creating a new instance
153         //and call the fill_in_additional_detail_fields where initialization
154         //code has been moved to
155         if(empty($this->focus->id)) {
156            global $current_user;
157            $this->focus->fill_in_additional_detail_fields();
158            $this->focus->assigned_user_id = $current_user->id;
159         }
160     }
161
162     function populateBean()
163     {
164         if(!empty($_REQUEST['record']) && $this->populateBean) {
165            global $beanList;
166            $bean = $beanList[$this->module];
167            $obj = new $bean();
168            $this->focus = $obj->retrieve($_REQUEST['record']);
169         } else {
170            $GLOBALS['log']->debug("Unable to populate bean, no record parameter found");
171         }
172     }
173
174     /**
175      * enableFormatting
176      * This method is used to manually turn on/off the field formatting
177      * @param $format boolean value to turn on/off field formatting
178      */
179     function enableFormatting(
180         $format = true
181         )
182     {
183         $this->formatFields = $format;
184     }
185
186
187     function requiredFirst()
188     {
189          $panels = array('required'=>array());
190          $reqCol = -1;
191          $reqRow = 0;
192         foreach($this->defs['panels'] as $key=>$p){
193
194                         foreach($p as $row=>$rowDef) {
195                             foreach($rowDef as $col => $colDef) {
196                                 $field = (is_array($p[$row][$col])) ? $p[$row][$col]['name'] : $p[$row][$col];
197                                 if((!empty($this->focus->field_defs[$field])
198                                         && !empty($this->focus->field_defs[$field]['required']))
199                                     || ( !empty($p[$row][$col]['displayParams']['required']) )) {
200                                         $reqCol++;
201                                         if($reqCol == $this->defs['templateMeta']['maxColumns']) {
202                                                 $reqCol = -1;
203                                                 $reqRow++;
204                                         }
205                                     $panels['required'][$reqRow][$reqCol] = $p[$row][$col];
206                                 }else{
207                                         $panels[$key][$row][$col] = $p[$row][$col];
208                                 }
209
210                             } //foreach
211                         } //foreach
212
213
214                 } //foreach
215          $this->defs['panels'] = $panels;
216     }
217
218     function render()
219     {
220         $totalWidth = 0;
221         foreach($this->defs['templateMeta']['widths'] as $col => $def) {
222             foreach($def as $k => $value) $totalWidth += $value;
223         }
224         // calculate widths
225         foreach($this->defs['templateMeta']['widths'] as $col => $def) {
226             foreach($def as $k => $value)
227                 $this->defs['templateMeta']['widths'][$col][$k] = round($value / ($totalWidth / 100), 2);
228         }
229
230         $this->sectionPanels = array();
231         $this->sectionLabels = array();
232         if(!empty($this->defs['panels']) && count($this->defs['panels']) > 0) {
233            $keys = array_keys($this->defs['panels']);
234            if(is_numeric($keys[0])) {
235                   $defaultPanel = $this->defs['panels'];
236                   unset($this->defs['panels']); //blow away current value
237               $this->defs['panels'][''] = $defaultPanel;
238            }
239         }
240         if($this->view == 'EditView' && !empty($GLOBALS['sugar_config']['forms']['requireFirst'])){
241                 $this->requiredFirst();
242         }
243
244         $maxColumns = isset($this->defs['templateMeta']['maxColumns']) ? $this->defs['templateMeta']['maxColumns'] : 2;
245         $panelCount = 0;
246                 static $itemCount = 100; //Start the generated tab indexes at 100 so they don't step on custom ones.
247
248                 /* loop all the panels */
249                 foreach($this->defs['panels'] as $key=>$p)
250                 {
251                         $panel = array();
252
253                 if(!is_array($this->defs['panels'][$key])) {
254                    $this->sectionPanels[strtoupper($key)] = $p;
255                 } else {
256
257                                 foreach($p as $row=>$rowDef) {
258                                     $columnsInRows = count($rowDef);
259                                     $columnsUsed = 0;
260                                     foreach($rowDef as $col => $colDef) {
261                                         $panel[$row][$col] = is_array($p[$row][$col]) ? array('field' => $p[$row][$col]) : array('field' => array('name'=>$p[$row][$col]));
262                             $panel[$row][$col]['field']['tabindex'] = (isset($p[$row][$col]['tabindex']) && is_numeric($p[$row][$col]['tabindex'])) ? $p[$row][$col]['tabindex'] : $itemCount;
263
264                                         if($columnsInRows < $maxColumns) {
265                                             if($col == $columnsInRows - 1) {
266                                                 $panel[$row][$col]['colspan'] = 2 * $maxColumns - ($columnsUsed + 1);
267                                             } else {
268                                                 $panel[$row][$col]['colspan'] = floor(($maxColumns * 2 - $columnsInRows) / $columnsInRows);
269                                                 $columnsUsed = $panel[$row][$col]['colspan'];
270                                             }
271                                         }
272
273                                         //Set address types to have colspan value of 2 if colspan is not already defined
274                                         if(is_array($colDef) && !empty($colDef['hideLabel']) && !isset($panel[$row][$col]['colspan'])) {
275                                             $panel[$row][$col]['colspan'] = 2;
276                                         }
277
278                                         $itemCount++;
279
280                                     } //foreach
281                                 } //foreach
282
283                                 // Panel alignment will be off if the panel doesn't have a row with the max columns
284                                 // It will not be aligned to the other panels so we fill out the columns in the last row
285                                 $addFiller = true;
286                                 foreach($panel as $row) {
287                                         if(count($row) == $this->defs['templateMeta']['maxColumns']) {
288                                            $addFiller = false;
289                                            break;
290                                         }
291                                 }
292
293                                 if($addFiller) {
294                                    $rowCount = count($panel);
295                                    $filler = count($panel[$rowCount-1]);
296                                    while($filler < $this->defs['templateMeta']['maxColumns']) {
297                                       $panel[$rowCount - 1][$filler++] = array('field'=>array('name'=>''));
298                                    } //while
299                                 }
300
301
302                                 $this->sectionPanels[strtoupper($key)] = $panel;
303                         }
304
305
306                 $panelCount++;
307                 } //foreach
308     }
309
310     function process(
311         $checkFormName = false,
312         $formName = ''
313         )
314     {
315         global $mod_strings, $sugar_config, $app_strings, $app_list_strings;
316
317                 //the retrieve already did this work;
318         //$this->focus->fill_in_relationship_fields();
319
320         if(!$this->th->checkTemplate($this->module, $this->view, $checkFormName, $formName)){
321                 $this->render();
322         }
323                 if(isset($_REQUEST['offset'])) {
324                         $this->offset = $_REQUEST['offset'] - 1;
325                 } //if
326         if($this->showVCRControl) {
327                 $this->th->ss->assign('PAGINATION', SugarVCR::menu($this->module, $this->offset, $this->focus->is_AuditEnabled(), ($this->view == 'EditView')));
328         } //if
329         if(isset($_REQUEST['return_module'])) $this->returnModule = $_REQUEST['return_module'];
330                 if(isset($_REQUEST['return_action'])) $this->returnAction = $_REQUEST['return_action'];
331                 if(isset($_REQUEST['return_id'])) $this->returnId = $_REQUEST['return_id'];
332                 if(isset($_REQUEST['return_relationship'])) $this->returnRelationship = $_REQUEST['return_relationship'];
333                 if(isset($_REQUEST['return_name'])) $this->returnName = $this->getValueFromRequest($_REQUEST, 'return_name' ) ;
334
335                 // handle Create $module then Cancel
336                 if (empty($this->returnId)) {
337                         $this->returnAction = 'index';
338                 }
339
340         $is_owner = $this->focus->isOwner($GLOBALS['current_user']->id);
341
342         $this->fieldDefs = array();
343                 if($this->focus){
344
345                         global $current_user;
346
347                         /*if(empty($this->focus->assigned_user_id) ){
348                                 $this->focus->assigned_user_id = $current_user->id;
349                         }*/
350
351                         if(!empty($this->focus->assigned_user_id)) {
352
353                            $this->focus->assigned_user_name = get_assigned_user_name($this->focus->assigned_user_id);
354                         }
355                 foreach($this->focus->toArray() as $name => $value) {
356
357                 $valueFormatted = false;
358                         //if($this->focus->field_defs[$name]['type']=='link')continue;
359
360                         if (!empty($this->fieldDefs[$name]) && !empty($this->fieldDefs[$name]['value']))
361                        $this->fieldDefs[$name] = array_merge($this->focus->field_defs[$name] , $this->fieldDefs[$name] ) ;
362                     else
363                    $this->fieldDefs[$name] = $this->focus->field_defs[$name];
364
365
366                                 foreach(array("formula", "default", "comments", "help") as $toEscape)
367                         {
368                             if (!empty($this->fieldDefs[$name][$toEscape])) {
369                                 $this->fieldDefs[$name][$toEscape] = htmlentities($this->fieldDefs[$name][$toEscape], ENT_QUOTES, 'UTF-8');
370                             }
371                         }
372
373                     if(isset($this->fieldDefs[$name]['options']) && isset($app_list_strings[$this->fieldDefs[$name]['options']])) {
374                         $this->fieldDefs[$name]['options'] = $app_list_strings[$this->fieldDefs[$name]['options']]; // fill in enums
375                     } //if
376
377                         if(isset($this->fieldDefs[$name]['function'])) {
378                                 $function = $this->fieldDefs[$name]['function'];
379                                 if(is_array($function) && isset($function['name'])){
380                                         $function = $this->fieldDefs[$name]['function']['name'];
381                                 }else{
382                                         $function = $this->fieldDefs[$name]['function'];
383                                 }
384                                 if(!empty($this->fieldDefs[$name]['function']['returns']) && $this->fieldDefs[$name]['function']['returns'] == 'html'){
385                                                 if(!empty($this->fieldDefs[$name]['function']['include'])){
386                                                                 require_once($this->fieldDefs[$name]['function']['include']);
387                                                 }
388                                                 $value = $function($this->focus, $name, $value, $this->view);
389                                                 $valueFormatted = true;
390                                         }else{
391                                                 $this->fieldDefs[$name]['options'] = $function($this->focus, $name, $value, $this->view);
392                                         }
393                         }
394
395                         if(isset($this->fieldDefs[$name]['type']) && $this->fieldDefs[$name]['type'] == 'function' && isset($this->fieldDefs[$name]['function_name'])){
396                                 $value = $this->callFunction($this->fieldDefs[$name]);
397                                 $valueFormatted = true;
398                         }
399
400                         if(!$valueFormatted) {
401                     // $this->focus->format_field($this->focus->field_defs[$name]);
402                    $value = isset($this->focus->$name) ? $this->focus->$name : '';
403                         }
404
405                         if (empty($this->fieldDefs[$name]['value']))
406                         {
407                        $this->fieldDefs[$name]['value'] = $value;
408                         }
409
410
411                     //This code is used for QuickCreates that go to Full Form view
412                         if($this->populateBean && empty($this->focus->id) && (isset($this->fieldDefs[$name]['function']['returns']) ? $this->fieldDefs[$name]['function']['returns'] != 'html' : true) && isset($_REQUEST[$name])) {
413                        $this->fieldDefs[$name]['value'] = $this->getValueFromRequest($_REQUEST, $name);
414                     } //if
415
416                     /*
417                 * Populate any relate fields that are linked by a relationship to the calling module.
418                 * Clicking the create button on a subpanel for example will populate three values in the $_REQUEST:
419                 * 1. return_module => the name of the calling module
420                 * 2. return_id => the id of the record in the calling module that the user was viewing and that should be associated with this new record
421                 * 3. return_name => the display value of the return_id record - the value to show in any relate field in this EditView
422                 * Only do if this fieldDef does not already have a value; if it does it will have been explicitly set, and that should overrule this less specific mechanism
423                 */
424                     if (isset($this->returnModule) && isset($this->returnName) && empty($this->focus->id) && empty($this->fieldDefs['name']['value']) )
425                 {
426                        if ( ($this->focus->field_defs[$name]['type'] == 'relate') && isset($this->focus->field_defs[$name][ 'module' ]) && $this->focus->field_defs[$name][ 'module' ] == $this->returnModule )
427                        {
428                        if (isset( $this->fieldDefs[$name]['id_name'])
429                            && !empty($this->returnRelationship)
430                            && isset($this->focus->field_defs[$this->fieldDefs[$name]['id_name']]['relationship'])
431                            && ($this->returnRelationship == $this->focus->field_defs[$this->fieldDefs[$name]['id_name']]['relationship']))
432                            {
433                                $this->fieldDefs[$name]['value'] =  $this->returnName ;
434                                // set the hidden id field for this relate field to the correct value i.e., return_id
435                                $this->fieldDefs[$this->fieldDefs[$name]['id_name']]['value'] = $this->returnId ;
436                            }
437                        }
438                 }
439
440                 } //foreach
441
442                 } //if
443
444                 if(isset($this->focus->additional_meta_fields)) {
445                     $this->fieldDefs = array_merge($this->fieldDefs, $this->focus->additional_meta_fields);
446                 }
447
448         if($this->isDuplicate) {
449                    foreach($this->fieldDefs as $name=>$defs) {
450                                 if(!empty($defs['auto_increment'])) {
451                                    $this->fieldDefs[$name]['value'] = '';
452                                 }
453                    }
454         }
455     }
456
457     /**
458      * display
459      * This method makes the Smarty variable assignments and then displays the
460      * generated view.
461      * @param $showTitle boolean value indicating whether or not to show a title on the resulting page
462      * @param $ajaxSave boolean value indicating whether or not the operation is an Ajax save request
463      * @return HTML display for view as String
464      */
465     function display(
466         $showTitle = true,
467         $ajaxSave = false
468         )
469     {
470         global $mod_strings, $sugar_config, $app_strings, $app_list_strings, $theme, $current_user;
471
472
473         if(isset($this->defs['templateMeta']['javascript'])) {
474            if(is_array($this->defs['templateMeta']['javascript'])) {
475                  $this->th->ss->assign('externalJSFile', 'modules/' . $this->module . '/metadata/editvewdefs.js');
476            } else {
477              $this->th->ss->assign('scriptBlocks', $this->defs['templateMeta']['javascript']);
478            }
479         }
480
481         $this->th->ss->assign('id', $this->fieldDefs['id']['value']);
482         $this->th->ss->assign('offset', $this->offset + 1);
483         $this->th->ss->assign('APP', $app_strings);
484         $this->th->ss->assign('MOD', $mod_strings);
485         $this->th->ss->assign('fields', $this->fieldDefs);
486         $this->th->ss->assign('sectionPanels', $this->sectionPanels);
487         $this->th->ss->assign('returnModule', $this->returnModule);
488         $this->th->ss->assign('returnAction', $this->returnAction);
489         $this->th->ss->assign('returnId', $this->returnId);
490         $this->th->ss->assign('isDuplicate', $this->isDuplicate);
491         $this->th->ss->assign('def', $this->defs);
492         $this->th->ss->assign('useTabs', isset($this->defs['templateMeta']['useTabs']) ? $this->defs['templateMeta']['useTabs'] : false);
493         $this->th->ss->assign('maxColumns', isset($this->defs['templateMeta']['maxColumns']) ? $this->defs['templateMeta']['maxColumns'] : 2);
494         $this->th->ss->assign('module', $this->module);
495         $this->th->ss->assign('headerTpl', isset($this->defs['templateMeta']['form']['headerTpl']) ? $this->defs['templateMeta']['form']['headerTpl'] : 'include/' . $this->view . '/header.tpl');
496         $this->th->ss->assign('footerTpl', isset($this->defs['templateMeta']['form']['footerTpl']) ? $this->defs['templateMeta']['form']['footerTpl'] : 'include/' . $this->view . '/footer.tpl');
497         $this->th->ss->assign('current_user', $current_user);
498         $this->th->ss->assign('bean', $this->focus);
499         $this->th->ss->assign('isAuditEnabled', $this->focus->is_AuditEnabled());
500         $this->th->ss->assign('gridline',$current_user->getPreference('gridline') == 'on' ? '1' : '0');
501
502         global $js_custom_version;
503         global $sugar_version;
504         $this->th->ss->assign('SUGAR_VERSION', $sugar_version);
505         $this->th->ss->assign('JS_CUSTOM_VERSION', $js_custom_version);
506
507         //this is used for multiple forms on one page
508         if ( !empty($this->formName) ) {
509             $form_id = $this->formName;
510             $form_name = $this->formName;
511         }
512         else {
513             $form_id = $this->view;
514             $form_name = $this->view;
515         }
516         if($ajaxSave && empty($this->formName)){
517                 $form_id = 'form_'.$this->view .'_'.$this->module;
518                 $form_name = $form_id;
519                 $this->view = $form_name;
520                 //$this->defs['templateMeta']['form']['buttons'] = array();
521                 //$this->defs['templateMeta']['form']['buttons']['ajax_save'] = array('id' => 'AjaxSave', 'customCode'=>'<input type="button" class="button" value="Save" onclick="this.form.action.value=\'AjaxFormSave\';return saveForm(\''.$form_name.'\', \'multiedit_form_{$module}\', \'Saving {$module}...\');"/>');
522         }
523
524                 $form_name = $form_name == "QuickCreate" ? "QuickCreate_{$this->module}" : $form_name;
525         $form_id = $form_id == "QuickCreate" ? "QuickCreate_{$this->module}" : $form_id;
526
527         if(isset($this->defs['templateMeta']['preForm'])) {
528           $this->th->ss->assign('preForm', $this->defs['templateMeta']['preForm']);
529         } //if
530         if(isset($this->defs['templateMeta']['form']['closeFormBeforeCustomButtons'])) {
531           $this->th->ss->assign('closeFormBeforeCustomButtons', $this->defs['templateMeta']['form']['closeFormBeforeCustomButtons']);
532         }
533         if(isset($this->defs['templateMeta']['form']['enctype'])) {
534           $this->th->ss->assign('enctype', 'enctype="'.$this->defs['templateMeta']['form']['enctype'].'"');
535         }
536         //for SugarFieldImage, we must set form enctype to "multipart/form-data"
537         foreach($this->fieldDefs as $field){
538                 if(isset($field['type']) && $field['type'] == 'image'){
539                         $this->th->ss->assign('enctype', 'enctype="multipart/form-data"');
540                         break;
541                 }
542         }
543         $this->th->ss->assign('showDetailData', $this->showDetailData);
544         $this->th->ss->assign('showSectionPanelsTitles', $this->showSectionPanelsTitles);
545         $this->th->ss->assign('form_id', $form_id);
546         $this->th->ss->assign('form_name', $form_name);
547                 $this->th->ss->assign('set_focus_block', get_set_focus_js());
548
549         $this->th->ss->assign('form', isset($this->defs['templateMeta']['form']) ? $this->defs['templateMeta']['form'] : null);
550         $this->th->ss->assign('includes', isset($this->defs['templateMeta']['includes']) ? $this->defs['templateMeta']['includes'] : null);
551                 $this->th->ss->assign('view', $this->view);
552
553         //Calculate time & date formatting (may need to calculate this depending on a setting)
554         global $timedate;
555         $this->th->ss->assign('CALENDAR_DATEFORMAT', $timedate->get_cal_date_format());
556         $this->th->ss->assign('USER_DATEFORMAT', $timedate->get_user_date_format());
557         $time_format = $timedate->get_user_time_format();
558         $this->th->ss->assign('TIME_FORMAT', $time_format);
559
560         $date_format = $timedate->get_cal_date_format();
561         $time_separator = ":";
562         if(preg_match('/\d+([^\d])\d+([^\d]*)/s', $time_format, $match)) {
563            $time_separator = $match[1];
564         }
565
566         // Create Smarty variables for the Calendar picker widget
567         $t23 = strpos($time_format, '23') !== false ? '%H' : '%I';
568         if(!isset($match[2]) || $match[2] == '') {
569           $this->th->ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M");
570         } else {
571           $pm = $match[2] == "pm" ? "%P" : "%p";
572           $this->th->ss->assign('CALENDAR_FORMAT', $date_format . ' ' . $t23 . $time_separator . "%M" . $pm);
573         }
574
575         $this->th->ss->assign('TIME_SEPARATOR', $time_separator);
576
577                 $seps = get_number_seperators();
578                 $this->th->ss->assign('NUM_GRP_SEP', $seps[0]);
579                 $this->th->ss->assign('DEC_SEP', $seps[1]);
580
581                 if($this->view == 'EditView') {
582                         $height = $current_user->getPreference('text_editor_height');
583                         $width = $current_user->getPreference('text_editor_width');
584                         $height = isset($height) ? $height : '300px';
585                 $width = isset($width) ? $width : '95%';
586                         $this->th->ss->assign('RICH_TEXT_EDITOR_HEIGHT', $height);
587                         $this->th->ss->assign('RICH_TEXT_EDITOR_WIDTH', $width);
588                 } else {
589                         $this->th->ss->assign('RICH_TEXT_EDITOR_HEIGHT', '100px');
590                         $this->th->ss->assign('RICH_TEXT_EDITOR_WIDTH', '95%');
591                 }
592
593
594                 $this->th->ss->assign('SHOW_VCR_CONTROL', $this->showVCRControl);
595
596         //$str='';
597
598         $str = $this->showTitle($showTitle);
599
600         //Use the output filter to trim the whitespace
601         $this->th->ss->load_filter('output', 'trimwhitespace');
602         $str .= $this->th->displayTemplate($this->module, $form_name, $this->tpl, $ajaxSave, $this->defs);
603                 return $str;
604     }
605
606     function insertJavascript(
607         $javascript
608         )
609     {
610         $this->ss->assign('javascript', $javascript);
611     }
612
613     function callFunction(
614         $vardef
615         )
616     {
617                 $can_execute = true;
618                 $execute_function = array();
619                 $execute_params = array();
620                 if(!empty($vardef['function_class'])){
621                         $execute_function[] =   $vardef['function_class'];
622                         $execute_function[] =   $vardef['function_name'];
623                 }else{
624                         $execute_function       = $vardef['function_name'];
625                 }
626                 foreach($vardef['function_params'] as $param ){
627                         if (empty($vardef['function_params_source']) or $vardef['function_params_source']=='parent'){
628                                 if(empty($this->focus->$param)){
629                                         $can_execute = false;
630                                 }else{
631                                         $execute_params[] = $this->focus->$param;
632                                 }
633                         }else if ($vardef['function_params_source']=='this'){
634                                 if(empty($this->focus->$param)){
635                                         $can_execute = false;
636                                 }else{
637                                         $execute_params[] = $this->focus->$param;
638                                 }
639                         }else{
640                                 $can_execute = false;
641                         }
642
643                 }
644                 $value = '';
645                 if($can_execute){
646                         if(!empty($vardef['function_require'])){
647                                 require_once($vardef['function_require']);
648                         }
649                         $value = call_user_func_array($execute_function, $execute_params);
650                 }
651                 return $value;
652     }
653
654     /**
655      * getValueFromRequest
656      * This is a helper method to extract a value from the request
657      * Array.  We do some special processing for fields that start
658      * with 'date_' by checking to see if they also include time
659      * and meridiem values
660      *
661      * @param request The request Array
662      * @param name The field name to extract value for
663      * @return String value for given name
664      */
665     function getValueFromRequest(
666         $request,
667         $name
668         )
669     {
670         //Special processing for date values (combine to one field)
671         if(preg_match('/^date_(.*)$/s', $name, $matches)) {
672            $d = $request[$name];
673
674            if(isset($request['time_' . $matches[1]])) {
675                    $d .= ' ' . $request['time_' . $matches[1]];
676                    if(isset($request[$matches[1] . '_meridiem'])) {
677                           $d .= $request[$matches[1] . '_meridiem'];
678                    }
679            } else {
680                    if(isset($request['time_hour_' . $matches[1]]) &&
681                       isset($request['time_minute_' . $matches[1]])) {
682                           $d .= ' ' . $request['time_hour_' . $matches[1]] . ':' . $request['time_minute_' . $matches[1]];
683                    }
684                    if(isset($request['meridiem'])) {
685                           $d .= $request['meridiem'];
686                    }
687            }
688            return $d;
689         }
690
691         return $request[$name];
692     }
693
694         /**
695          * Allow Subviews to overwrite this method to show custom titles.
696          * Examples: Projects & Project Templates.
697          * params: $showTitle: boolean for backwards compatibility.
698          */
699     public function showTitle(
700         $showTitle = false
701         )
702     {
703         global $mod_strings, $app_strings;
704
705         if (is_null($this->viewObject))
706                 if (!empty($GLOBALS['current_view']))
707                         $this->viewObject = $GLOBALS['current_view'];
708                 else
709                         $this->viewObject = new SugarView();
710
711         if ($showTitle)
712                 return $this->viewObject->getModuleTitle();
713
714         return '';
715     }
716 }