]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/ListView/ListView.php
Release 6.5.1
[Github/sugarcrm.git] / include / ListView / ListView.php
1 <?php
2 if(!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3 /*********************************************************************************
4  * SugarCRM Community Edition is a customer relationship management program developed by
5  * SugarCRM, Inc. Copyright (C) 2004-2012 SugarCRM Inc.
6  * 
7  * This program is free software; you can redistribute it and/or modify it under
8  * the terms of the GNU Affero General Public License version 3 as published by the
9  * Free Software Foundation with the addition of the following permission added
10  * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
11  * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
12  * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
13  * 
14  * This program is distributed in the hope that it will be useful, but WITHOUT
15  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
16  * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
17  * details.
18  * 
19  * You should have received a copy of the GNU Affero General Public License along with
20  * this program; if not, see http://www.gnu.org/licenses or write to the Free
21  * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
22  * 02110-1301 USA.
23  * 
24  * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
25  * SW2-130, Cupertino, CA 95014, USA. or at email address contact@sugarcrm.com.
26  * 
27  * The interactive user interfaces in modified source and object code versions
28  * of this program must display Appropriate Legal Notices, as required under
29  * Section 5 of the GNU Affero General Public License version 3.
30  * 
31  * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
32  * these Appropriate Legal Notices must retain the display of the "Powered by
33  * SugarCRM" logo. If the display of the logo is not reasonably feasible for
34  * technical reasons, the Appropriate Legal Notices must display the words
35  * "Powered by SugarCRM".
36  ********************************************************************************/
37
38 require_once('include/EditView/SugarVCR.php');
39 /**
40  * ListView - list of many objects
41  * @api
42  */
43 class ListView
44 {
45     var $local_theme= null;
46     var $local_app_strings= null;
47     var $local_image_path = null;
48     var $local_current_module = null;
49     var $local_mod_strings = null;
50     var $records_per_page = 20;
51     var $xTemplate = null;
52     var $xTemplatePath = null;
53     var $seed_data = null;
54     var $query_where = null;
55     var $query_limit = -1;
56     var $query_orderby = null;
57     var $header_title = '';
58     var $header_text = '';
59     var $initialized = false;
60     var $show_export_button = true;
61     var $show_delete_button = true;
62     var $show_select_menu = true;
63     var $show_paging = true;
64     var $show_mass_update = true;
65     var $show_mass_update_form = true;
66     var $query_where_has_changed = false;
67     var $display_header_and_footer = true;
68     var $baseURL = '';
69     var $is_dynamic = false;
70     var $inline = false;
71     var $start_link_wrapper = '';
72     var $end_link_wrapper = '';
73     var $list_field_defs = array();
74
75     var $shouldProcess = false;
76     var $data_array;
77     var $related_field_name = '';
78     var $child_focus = '';
79     var $layout_manager = null;
80     var $process_for_popups = false;
81     var $multi_select_popup=false;
82     var $_additionalDetails = false;
83     var $additionalDetailsFunction = null;
84     var $sort_order = '';
85     var $force_mass_update=false;
86     var $keep_mass_update_form_open=false;
87     var $ignorePopulateOnly = false;
88
89 function setDataArray($value) {
90     $this->data_array = $value;
91 }
92
93 function processListViewMulti($seed, $xTemplateSection, $html_varName) {
94
95     $this->shouldProcess = true;
96
97     echo "<form name='MassUpdate' method='post' action='index.php'>";
98     $this->processListViewTwo($seed, $xTemplateSection, $html_varName);
99
100     echo "<a href='javascript:" . ((!$this->multi_select_popup) ? 'sListView.' : ''). "check_all(document.MassUpdate, \"mass[]\", true)'>".translate('LBL_CHECKALL')."</a> - <a href='javascript:sListView.check_all(document.MassUpdate, \"mass[]\", false);'>".translate('LBL_CLEARALL')."</a>";
101     echo '<br><br>';
102 }
103
104
105 function processListView($seed, $xTemplateSection, $html_varName)
106 {
107     global $sugar_config;
108
109     $populateOnly = $this->ignorePopulateOnly ? FALSE : (!empty($sugar_config['save_query']) && $sugar_config['save_query'] == 'populate_only');
110     if(isset($seed->module_dir) && $populateOnly) {
111         if(empty($GLOBALS['displayListView']) && strcmp(strtolower($_REQUEST['action']), 'popup') != 0 && (!empty($_REQUEST['clear_query']) || $_REQUEST['module'] == $seed->module_dir && ((empty($_REQUEST['query']) || $_REQUEST['query'] == 'MSI')&& (empty($_SESSION['last_search_mod']) || $_SESSION['last_search_mod'] != $seed->module_dir)))) {
112             $_SESSION['last_search_mod'] = $_REQUEST['module'] ;
113             return;
114         }
115     }
116     if(strcmp(strtolower($_REQUEST['action']), 'popup') != 0){
117         $_SESSION['last_search_mod'] = $_REQUEST['module'] ;
118     }
119     //following session variable will track the detail view navigation history.
120     //needs to the reset after each search.
121     $this->setLocalSessionVariable($html_varName,"DETAIL_NAV_HISTORY",false);
122
123     require_once('include/MassUpdate.php');
124     $mass = new MassUpdate();
125     $add_acl_javascript = false;
126     if(!isset($_REQUEST['action'])) {
127         $this->shouldProcess=false;
128     } else {
129     $this->shouldProcess = is_subclass_of($seed, "SugarBean")
130         && (($_REQUEST['action'] == 'index') || ('ListView' == substr($_REQUEST['action'],0,8)) /* cn: to include all ListViewXXX.php type views */)
131         && ($_REQUEST['module'] == $seed->module_dir);
132     }
133
134     //when processing a multi-select popup.
135     if($this->process_for_popups && $this->multi_select_popup)  $this->shouldProcess =true;
136     //mass update turned off?
137     if(!$this->show_mass_update) $this->shouldProcess = false;
138     if(is_subclass_of($seed, "SugarBean")) {
139         if($seed->bean_implements('ACL')) {
140             if(!ACLController::checkAccess($seed->module_dir,'list',true)) {
141                 if($_REQUEST['module'] != 'Home') {
142                     ACLController::displayNoAccess();
143                 }
144                 return;
145             }
146             if(!ACLController::checkAccess($seed->module_dir,'export',true)) {
147                 $sugar_config['disable_export']= true;
148             }
149
150         }
151     }
152
153     //force mass update form if requested.
154     if($this->force_mass_update) {
155         $this->shouldProcess = true;
156     }
157
158     if($this->shouldProcess) {
159         echo $mass->getDisplayMassUpdateForm(true, $this->multi_select_popup);
160         echo $mass->getMassUpdateFormHeader($this->multi_select_popup);
161         $mass->setSugarBean($seed);
162
163         //C.L. Fix for 10048, do not process handleMassUpdate for multi select popups
164         if(!$this->multi_select_popup) {
165             $mass->handleMassUpdate();
166         }
167     }
168
169     $this->processListViewTwo($seed,$xTemplateSection, $html_varName);
170
171     if($this->shouldProcess && empty($this->process_for_popups)) {
172         //echo "<a href='javascript:sListView.clear_all(document.MassUpdate, \"mass[]\");'>".translate('LBL_CLEARALL')."</a>";
173         // cn: preserves current functionality, exception is InboundEmail
174         if($this->show_mass_update_form) {
175             echo $mass->getMassUpdateForm();
176         }
177         if(!$this->keep_mass_update_form_open) {
178             echo $mass->endMassUpdateForm();
179         }
180     }
181 }
182
183
184 function process_dynamic_listview($source_module, $sugarbean,$subpanel_def)
185 {
186         $this->source_module = $source_module;
187         $this->subpanel_module = $subpanel_def->name;
188         if(!isset($this->xTemplate))
189             $this->createXTemplate();
190
191         $html_var = $this->subpanel_module . "_CELL";
192
193         $list_data = $this->processUnionBeans($sugarbean,$subpanel_def, $html_var);
194
195         $list = $list_data['list'];
196         $parent_data = $list_data['parent_data'];
197
198         if($subpanel_def->isCollection()) {
199             $thepanel=$subpanel_def->get_header_panel_def();
200         } else {
201             $thepanel=$subpanel_def;
202         }
203
204
205
206         $this->process_dynamic_listview_header($thepanel->get_module_name(), $thepanel, $html_var);
207         $this->process_dynamic_listview_rows($list,$parent_data, 'dyn_list_view', $html_var,$subpanel_def);
208
209         if($this->display_header_and_footer)
210         {
211             $this->getAdditionalHeader();
212             if(!empty($this->header_title))
213             {
214                 echo get_form_header($this->header_title, $this->header_text, false);
215             }
216         }
217
218         $this->xTemplate->out('dyn_list_view');
219
220         if(isset($_SESSION['validation']))
221         {
222             print base64_decode('PGEgaHJlZj0naHR0cDovL3d3dy5zdWdhcmNybS5jb20nPlBPV0VSRUQmbmJzcDtCWSZuYnNwO1NVR0FSQ1JNPC9hPg==');
223         }
224         if(isset($list_data['query'])) {
225             return ($list_data['query']);
226         }
227     }
228
229 /**
230  * @return void
231  * @param unknown $data
232  * @param unknown $xTemplateSection
233  * @param unknown $html_varName
234  * @desc INTERNAL FUNCTION handles the rows
235  */
236  function process_dynamic_listview_rows($data,$parent_data, $xtemplateSection, $html_varName, $subpanel_def)
237  {
238     global $subpanel_item_count;
239     global $odd_bg;
240     global $even_bg;
241     global $hilite_bg;
242     global $click_bg;
243
244     $this->xTemplate->assign("BG_HILITE", $hilite_bg);
245     $this->xTemplate->assign('CHECKALL', SugarThemeRegistry::current()->getImage('blank', '', 1, 1, ".gif", ''));
246     //$this->xTemplate->assign("BG_CLICK", $click_bg);
247     $subpanel_item_count = 0;
248     $oddRow = true;
249     $count = 0;
250     reset($data);
251
252     //GETTING OFFSET
253     $offset = $this->getOffset($html_varName);
254     //$totaltime = 0;
255     $processed_ids = array();
256
257     $fill_additional_fields = array();
258     //Either retrieve the is_fill_in_additional_fields property from the lone
259     //subpanel or visit each subpanel's subpanels to retrieve the is_fill_in_addition_fields
260     //property
261     $subpanel_list=array();
262     if($subpanel_def->isCollection()) {
263         $subpanel_list=$subpanel_def->sub_subpanels;
264     } else {
265         $subpanel_list[]= $subpanel_def;
266     }
267
268     foreach($subpanel_list as $this_subpanel)
269     {
270         if($this_subpanel->is_fill_in_additional_fields())
271         {
272             $fill_additional_fields[] = $this_subpanel->bean_name;
273             $fill_additional_fields[$this_subpanel->bean_name] = true;
274         }
275     }
276
277     if ( empty($data) ) {
278         $this->xTemplate->assign("ROW_COLOR", 'oddListRow');
279         $thepanel=$subpanel_def;
280         if($subpanel_def->isCollection())
281             $thepanel=$subpanel_def->get_header_panel_def();
282         $this->xTemplate->assign("COL_COUNT", count($thepanel->get_list_fields()));
283         $this->xTemplate->parse($xtemplateSection.".nodata");
284     }
285
286     while(list($aVal, $aItem) = each($data))
287     {
288         $subpanel_item_count++;
289         $aItem->check_date_relationships_load();
290         // TODO: expensive and needs to be removed and done better elsewhere
291
292         if(!empty($fill_additional_fields[$aItem->object_name])
293         || ($aItem->object_name == 'Case' && !empty($fill_additional_fields['aCase']))
294         )
295         {
296             $aItem->fill_in_additional_list_fields();
297             //$aItem->fill_in_additional_detail_fields();
298         }
299         //rrs bug: 25343
300         $aItem->call_custom_logic("process_record");
301
302         if(isset($parent_data[$aItem->id])) {
303
304             $aItem->parent_name = $parent_data[$aItem->id]['parent_name'];
305             if(!empty($parent_data[$aItem->id]['parent_name_owner'])) {
306             $aItem->parent_name_owner =  $parent_data[$aItem->id]['parent_name_owner'];
307             $aItem->parent_name_mod =  $parent_data[$aItem->id]['parent_name_mod'];
308         }}
309
310         $fields = $aItem->get_list_view_data();
311         if(isset($processed_ids[$aItem->id])) {
312             continue;
313
314         } else {
315             $processed_ids[$aItem->id] = 1;
316         }
317
318
319         //ADD OFFSET TO ARRAY
320         $fields['OFFSET'] = ($offset + $count + 1);
321
322         if($this->shouldProcess) {
323             if($aItem->ACLAccess('EditView')) {
324             $this->xTemplate->assign('PREROW', "<input type='checkbox' class='checkbox' name='mass[]' value='". $fields['ID']. "' />");
325             } else {
326                 $this->xTemplate->assign('PREROW', '');
327
328             }
329             if($aItem->ACLAccess('DetailView')) {
330                 $this->xTemplate->assign('TAG_NAME','a');
331             } else {
332                 $this->xTemplate->assign('TAG_NAME','span');
333             }
334             $this->xTemplate->assign('CHECKALL', "<input type='checkbox'  title='".$GLOBALS['app_strings']['LBL_SELECT_ALL_TITLE']."' class='checkbox' name='massall' id='massall' value='' onclick='sListView.check_all(document.MassUpdate, \"mass[]\", this.checked);' />");
335         }
336
337         if($oddRow)
338         {
339             $ROW_COLOR = 'oddListRow';
340             $BG_COLOR =  $odd_bg;
341         }
342         else
343         {
344             $ROW_COLOR = 'evenListRow';
345             $BG_COLOR =  $even_bg;
346         }
347         $oddRow = !$oddRow;
348                 $button_contents = array();
349         $this->xTemplate->assign("ROW_COLOR", $ROW_COLOR);
350         $this->xTemplate->assign("BG_COLOR", $BG_COLOR);
351         $layout_manager = $this->getLayoutManager();
352         $layout_manager->setAttribute('context','List');
353         $layout_manager->setAttribute('image_path',$this->local_image_path);
354         $layout_manager->setAttribute('module_name', $subpanel_def->_instance_properties['module']);
355         if(!empty($this->child_focus))
356             $layout_manager->setAttribute('related_module_name',$this->child_focus->module_dir);
357
358         //AG$subpanel_data = $this->list_field_defs;
359         //$bla = array_pop($subpanel_data);
360         //select which sub-panel to display here, the decision will be made based on the type of
361         //the sub-panel and panel in the bean being processed.
362         if($subpanel_def->isCollection()) {
363             $thepanel=$subpanel_def->sub_subpanels[$aItem->panel_name];
364         } else {
365             $thepanel=$subpanel_def;
366         }
367         //get data source name
368         $linked_field=$thepanel->get_data_source_name();
369         $linked_field_set=$thepanel->get_data_source_name(true);
370         static $count;
371         if(!isset($count))$count = 0;
372
373         $field_acl['DetailView'] = $aItem->ACLAccess('DetailView');
374         $field_acl['ListView'] = $aItem->ACLAccess('ListView');
375         $field_acl['EditView'] = $aItem->ACLAccess('EditView');
376         $field_acl['Delete'] = $aItem->ACLAccess('Delete');
377         foreach($thepanel->get_list_fields() as $field_name=>$list_field)
378         {
379             //add linked field attribute to the array.
380             $list_field['linked_field']=$linked_field;
381             $list_field['linked_field_set']=$linked_field_set;
382
383             $usage = empty($list_field['usage']) ? '' : $list_field['usage'];
384             if($usage != 'query_only')
385             {
386                 $list_field['name']=$field_name;
387
388                 $module_field = $field_name.'_mod';
389                 $owner_field = $field_name.'_owner';
390                 if(!empty($aItem->$module_field)) {
391
392                     $list_field['owner_id'] = $aItem->$owner_field;
393                     $list_field['owner_module'] = $aItem->$module_field;
394
395                 } else {
396                     $list_field['owner_id'] = false;
397                     $list_field['owner_module'] = false;
398                 }
399                 if(isset($list_field['alias'])) $list_field['name'] = $list_field['alias'];
400                 else $list_field['name']=$field_name;
401                 $list_field['fields'] = $fields;
402                 $list_field['module'] = $aItem->module_dir;
403                 $list_field['start_link_wrapper'] = $this->start_link_wrapper;
404                 $list_field['end_link_wrapper'] = $this->end_link_wrapper;
405                 $list_field['subpanel_id'] = $this->subpanel_id;
406                 $list_field += $field_acl;
407                 if ( isset($aItem->field_defs[strtolower($list_field['name'])])) {
408                     require_once('include/SugarFields/SugarFieldHandler.php');
409                     // We need to see if a sugar field exists for this field type first,
410                     // if it doesn't, toss it at the old sugarWidgets. This is for
411                     // backwards compatibility and will be removed in a future release
412                     $vardef = $aItem->field_defs[strtolower($list_field['name'])];
413                     if ( isset($vardef['type']) ) {
414                         $fieldType = isset($vardef['custom_type'])?$vardef['custom_type']:$vardef['type'];
415                         $tmpField = SugarFieldHandler::getSugarField($fieldType,true);
416                     } else {
417                         $tmpField = NULL;
418                     }
419
420                     if ( $tmpField != NULL ) {
421                         $widget_contents = SugarFieldHandler::displaySmarty($list_field['fields'],$vardef,'ListView',$list_field);
422                     } else {
423                         // No SugarField for this particular type
424                         // Use the old, icky, SugarWidget for now
425                         $widget_contents = $layout_manager->widgetDisplay($list_field);
426                     }
427
428                     if ( isset($list_field['widget_class']) && $list_field['widget_class'] == 'SubPanelDetailViewLink' ) {
429                         // We need to call into the old SugarWidgets for the time being, so it can generate a proper link with all the various corner-cases handled
430                         // So we'll populate the field data with the pre-rendered display for the field
431                         $list_field['fields'][$field_name] = $widget_contents;
432                         if('full_name' == $field_name){//bug #32465
433                            $list_field['fields'][strtoupper($field_name)] = $widget_contents;
434                         }
435
436                         //vardef source is non db, assign the field name to varname for processing of column.
437                         if(!empty($vardef['source']) && $vardef['source']=='non-db'){
438                             $list_field['varname'] = $field_name;
439
440                         }
441                         $widget_contents = $layout_manager->widgetDisplay($list_field);
442                     } else if(isset($list_field['widget_class']) && $list_field['widget_class'] == 'SubPanelEmailLink' ) {
443                         $widget_contents = $layout_manager->widgetDisplay($list_field);
444                     }
445
446                  $count++;
447                 $this->xTemplate->assign('CELL_COUNT', $count);
448                 $this->xTemplate->assign('CLASS', "");
449                 if ( empty($widget_contents) ) $widget_contents = '&nbsp;';
450                 $this->xTemplate->assign('CELL', $widget_contents);
451                 $this->xTemplate->parse($xtemplateSection.".row.cell");
452                 } else {
453                     // This handles the edit and remove buttons and icon widget
454                         if( isset($list_field['widget_class']) && $list_field['widget_class'] == "SubPanelIcon") {
455                                 $count++;
456                                 $widget_contents = $layout_manager->widgetDisplay($list_field);
457                                 $this->xTemplate->assign('CELL_COUNT', $count);
458                                 $this->xTemplate->assign('CLASS', "");
459                                 if ( empty($widget_contents) ) $widget_contents = '&nbsp;';
460                                 $this->xTemplate->assign('CELL', $widget_contents);
461                                 $this->xTemplate->parse($xtemplateSection.".row.cell");
462                         } elseif (preg_match("/button/i", $list_field['name'])) {
463                                 if($layout_manager->widgetDisplay($list_field) != "")
464                                 $button_contents[] = $layout_manager->widgetDisplay($list_field);
465                         } else {
466                                 $count++;
467                                 $this->xTemplate->assign('CLASS', "");
468                                 $widget_contents = $layout_manager->widgetDisplay($list_field);
469                                 $this->xTemplate->assign('CELL_COUNT', $count);
470                                 if ( empty($widget_contents) ) $widget_contents = '&nbsp;';
471                                 $this->xTemplate->assign('CELL', $widget_contents);
472                                 $this->xTemplate->parse($xtemplateSection.".row.cell");
473                         }
474                 }
475
476             }
477         }
478
479
480         // Make sure we have at least one button before rendering a column for
481         // the action buttons in a list view. Relevant bugs: #51647 and #51640.
482         if(isset($button_contents[0])) {
483             // this is for inline buttons on listviews
484             // bug#51275: smarty widget to help provide the action menu functionality as it is currently sprinkled throughout the app with html
485             require_once('include/Smarty/plugins/function.sugar_action_menu.php');
486             $tempid = create_guid();
487             $button_contents[0] = "<div style='display: inline' id='$tempid'>".$button_contents[0]."</div>";
488             $action_button = smarty_function_sugar_action_menu(array(
489                 'id' => $tempid,
490                 'buttons' => $button_contents,
491                 'class' => 'clickMenu subpanel records fancymenu button',
492                 'flat' => false //assign flat value as false to display dropdown menu at any other preferences.
493             ), $this->xTemplate);
494             $this->xTemplate->assign('CLASS', "inlineButtons");
495             $this->xTemplate->assign('CELL_COUNT', ++$count);
496             //Bug#51275 for beta3 pre_script is not required any more
497             $this->xTemplate->assign('CELL', $action_button);
498             $this->xTemplate->parse($xtemplateSection.".row.cell");
499         }
500
501
502         $aItem->setupCustomFields($aItem->module_dir);
503         $aItem->custom_fields->populateAllXTPL($this->xTemplate, 'detail', $html_varName, $fields);
504
505         $count++;
506
507         $this->xTemplate->parse($xtemplateSection.".row");
508     }
509
510     $this->xTemplate->parse($xtemplateSection);
511 }
512
513 /**sets whether or not to display the xtemplate header and footer
514  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
515  * All Rights Reserved.
516  * Contributor(s): ______________________________________.
517 */
518 function setDisplayHeaderAndFooter($bool) {
519         $this->display_header_and_footer = $bool;
520 }
521
522 /**initializes ListView
523  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
524  * All Rights Reserved.
525  * Contributor(s): ______________________________________.
526 */
527  function ListView() {
528
529
530     if(!$this->initialized) {
531         global $sugar_config;
532         $this->records_per_page = $sugar_config['list_max_entries_per_page'] + 0;
533         $this->initialized = true;
534         global $app_strings, $currentModule;
535         $this->local_theme = SugarThemeRegistry::current()->__toString();
536         $this->local_app_strings =$app_strings;
537         $this->local_image_path = SugarThemeRegistry::current()->getImagePath();
538         $this->local_current_module = $currentModule;
539     }
540 }
541 /**sets how many records should be displayed per page in the list view
542  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
543  * All Rights Reserved.
544  * Contributor(s): ______________________________________.
545 */
546  function setRecordsPerPage($count) {
547     $this->records_per_page = $count;
548 }
549 /**sets the header title */
550  function setHeaderTitle($value) {
551     $this->header_title = $value;
552 }
553 /**sets the header text this is text that's appended to the header table and is usually used for the creation of buttons
554  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
555  * All Rights Reserved.
556  * Contributor(s): ______________________________________.
557 */
558  function setHeaderText($value) {
559     $this->header_text = $value;
560 }
561 /**sets the path for the XTemplate HTML file to be used this is only needed to be set if you are allowing ListView to create the XTemplate
562  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
563  * All Rights Reserved.
564  * Contributor(s): ______________________________________.
565 */
566  function setXTemplatePath($value) {
567     $this->xTemplatePath= $value;
568 }
569
570 /**this is a helper function for allowing ListView to create a new XTemplate it groups parameters that should be set into a single function
571  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
572  * All Rights Reserved.
573  * Contributor(s): ______________________________________.
574 */
575  function initNewXTemplate($XTemplatePath, $modString, $imagePath = null) {
576     $this->setXTemplatePath($XTemplatePath);
577     if(isset($modString))
578         $this->setModStrings($modString);
579     if(isset($imagePath))
580         $this->setImagePath($imagePath);
581 }
582
583
584 function getOrderBy($varName, $defaultOrderBy='', $force_sortorder='') {
585     $sortBy = $this->getSessionVariable($varName, "ORDER_BY") ;
586
587     $orderByDirection = $this->getSessionVariableName($varName, "order_by_direction");
588     $orderByColumn = $this->getSessionVariableName($varName, "ORDER_BY");
589     $lastEqualsSortBy = false;
590     $defaultOrder = false; //ascending
591
592     if(empty($sortBy)) {
593         $this->setUserVariable($varName, "ORDER_BY", $defaultOrderBy);
594         $sortBy = $defaultOrderBy;
595     } else {
596         $this->setUserVariable($varName, "ORDER_BY", $sortBy);
597     }
598
599     $desc = $this->getSessionVariable($varName, $sortBy."S");
600
601     if (empty($desc))
602         {
603             $desc = $defaultOrder;
604         }
605         $defaultOrder = $desc ? 'desc' : 'asc';
606         $orderByValue = $defaultOrder;
607         if (isset($_REQUEST[$orderByDirection]))
608         {
609             $possibleRequestOrderBy = $_REQUEST[$orderByDirection];
610             if ($possibleRequestOrderBy == 'asc' || $possibleRequestOrderBy == 'desc')
611             {
612                 $orderByValue = $possibleRequestOrderBy;
613             }
614         }
615
616         if (isset($_REQUEST[$orderByColumn]))
617         {
618             $last = $this->getSessionVariable($varName, "OBL");
619         }
620         if (!empty($last) && $last == $sortBy)
621         {
622             $lastEqualsSortBy = true;
623         } else
624         {
625             $orderByValue = $defaultOrder;
626             $this->setSessionVariable($varName, "OBL", $sortBy);
627         }
628         $desc = $orderByValue == 'desc';
629         $orderByDirectionValue = false;
630         $this->setSessionVariable($varName, $sortBy . "S", $desc);
631         if (!empty($sortBy))
632         {
633             if (empty($force_sortorder))
634             {
635                 if (substr_count(strtolower($sortBy), ' desc') == 0 && substr_count(strtolower($sortBy), ' asc') == 0)
636                 {
637                     if ($sortBy)
638                     {
639                         $orderByDirectionValue = $desc ? 'asc' : 'desc';
640                     }
641                     $this->query_orderby = $sortBy . ' ' . $orderByValue;
642                 }
643             } else
644             {
645                 $this->query_orderby = $sortBy . ' ' . $force_sortorder;
646             }
647             if (!isset($this->appendToBaseUrl))
648             {
649                 $this->appendToBaseUrl = array();
650             }
651             if ($orderByDirectionValue)
652             {
653                 $this->appendToBaseUrl[$orderByDirection] = $orderByDirectionValue;
654             }
655             $offsetVar = $this->getSessionVariableName($varName, "offset");
656             if (isset($_REQUEST[$offsetVar]))
657             {
658                 $this->appendToBaseUrl[$offsetVar] = $_REQUEST[$offsetVar];
659             }
660             //Just clear from url...
661             $this->appendToBaseUrl[$orderByColumn] = false;
662     }else {
663         $this->query_orderby = "";
664     }
665     $this->sortby = $sortBy;
666     return $this->query_orderby;
667
668 }
669
670
671 /**sets the parameters dealing with the db
672  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
673  * All Rights Reserved.
674  * Contributor(s): ______________________________________.
675 */
676  function setQuery($where, $limit, $orderBy, $varName, $allowOrderByOveride=true) {
677     $this->query_where = $where;
678     if($this->getSessionVariable("query", "where") != $where) {
679         $this->query_where_has_changed = true;
680         $this->setSessionVariable("query", "where", $where);
681     }
682
683     $this->query_limit = $limit;
684     if(!$allowOrderByOveride) {
685         $this->query_orderby = $orderBy;
686         return;
687     }
688     $this->getOrderBy($varName, $orderBy);
689
690     $this->setLocalSessionVariable($varName, "QUERY_WHERE", $where);
691
692     //SETTING ORDER_BY FOR USE IN DETAILVIEW
693     $this->setLocalSessionVariable($varName, "ORDER_BY_DETAIL", $this->query_orderby);
694 }
695
696 function displayArrow() {
697
698 }
699
700 /**sets the theme used only use if it is different from the global
701  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
702  * All Rights Reserved.
703  * Contributor(s): ______________________________________.
704 */
705  function setTheme($theme) {
706     $this->local_theme = $theme;
707     if(isset($this->xTemplate))$this->xTemplate->assign("THEME", $this->local_theme);
708 }
709
710 /**sets the AppStrings used only use if it is different from the global
711  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
712  * All Rights Reserved.
713  * Contributor(s): ______________________________________.
714 */
715  function setAppStrings($app_strings) {
716     unset($this->local_app_strings);
717     $this->local_app_strings = $app_strings;
718     if(isset($this->xTemplate))$this->xTemplate->assign("APP", $this->local_app_strings);
719 }
720
721 /**sets the ModStrings used
722  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
723  * All Rights Reserved.
724  * Contributor(s): ______________________________________.
725 */
726  function setModStrings($mod_strings) {
727     unset($this->local_module_strings);
728     $this->local_mod_strings = $mod_strings;
729     if(isset($this->xTemplate))$this->xTemplate->assign("MOD", $this->local_mod_strings);
730 }
731
732 /**sets the ImagePath used
733  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
734  * All Rights Reserved.
735  * Contributor(s): ______________________________________.
736 */
737  function setImagePath($image_path) {
738     $this->local_image_path = $image_path;
739     if(empty($this->local_image_path)) {
740         $this->local_image_path = SugarThemeRegistry::get($this->local_theme)->getImagePath();
741     }
742     if(isset($this->xTemplate))$this->xTemplate->assign("IMAGE_PATH", $this->local_image_path);
743 }
744
745 /**sets the currentModule only use if this is different from the global
746  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
747  * All Rights Reserved.
748  * Contributor(s): ______________________________________.
749 */
750  function setCurrentModule($currentModule) {
751     unset($this->local_current_module);
752     $this->local_current_module = $currentModule;
753     if(isset($this->xTemplate))$this->xTemplate->assign("MODULE_NAME", $this->local_current_module);
754 }
755
756 /**INTERNAL FUNCTION creates an XTemplate DO NOT CALL THIS THIS IS AN INTERNAL FUNCTION
757  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
758  * All Rights Reserved.
759  * Contributor(s): ______________________________________.
760 */
761  function createXTemplate() {
762     if(!isset($this->xTemplate)) {
763         if(isset($this->xTemplatePath)) {
764
765             $this->xTemplate = new XTemplate($this->xTemplatePath);
766             $this->xTemplate->assign("APP", $this->local_app_strings);
767             if(isset($this->local_mod_strings))$this->xTemplate->assign("MOD", $this->local_mod_strings);
768             $this->xTemplate->assign("THEME", $this->local_theme);
769             $this->xTemplate->assign("IMAGE_PATH", $this->local_image_path);
770             $this->xTemplate->assign("MODULE_NAME", $this->local_current_module);
771         } else {
772             $GLOBALS['log']->error("NO XTEMPLATEPATH DEFINED CANNOT CREATE XTEMPLATE");
773         }
774     }
775 }
776
777 /**sets the XTemplate telling ListView to use newXTemplate as its current XTemplate
778  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
779  * All Rights Reserved.
780  * Contributor(s): ______________________________________.
781 */
782  function setXTemplate($newXTemplate) {
783     $this->xTemplate = $newXTemplate;
784 }
785
786 /**returns the XTemplate
787  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
788  * All Rights Reserved.
789  * Contributor(s): ______________________________________.
790 */
791  function getXTemplate() {
792     return $this->xTemplate;
793 }
794
795 /**assigns a name value pair to the XTemplate
796  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
797  * All Rights Reserved.
798  * Contributor(s): ______________________________________.
799 */
800  function xTemplateAssign($name, $value) {
801
802         if(!isset($this->xTemplate)) {
803             $this->createXTemplate();
804         }
805         $this->xTemplate->assign($name, $value);
806
807 }
808
809 /**INTERNAL FUNCTION returns the offset first checking the query then checking the session if the where clause has changed from the last time it returns 0
810  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
811  * All Rights Reserved.
812  * Contributor(s): ______________________________________.
813 */
814  function getOffset($localVarName) {
815         if($this->query_where_has_changed || isset($GLOBALS['record_has_changed'])) {
816                 $this->setSessionVariable($localVarName,"offset", 0);
817         }
818         $offset = $this->getSessionVariable($localVarName,"offset");
819         if(isset($offset)) {
820                 return $offset;
821         }
822         return 0;
823 }
824
825 /**INTERNAL FUNCTION sets the offset in the session
826  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
827  * All Rights Reserved.
828  * Contributor(s): ______________________________________.
829 */
830  function setOffset($localVarName, $value) {
831         $this->setSessionVariable($localVarName, "offset", $value);
832 }
833
834 /**INTERNAL FUNCTION sets a session variable
835  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
836  * All Rights Reserved.
837  * Contributor(s): ______________________________________.
838 */
839  function setSessionVariable($localVarName,$varName, $value) {
840     $_SESSION[$this->local_current_module."_".$localVarName."_".$varName] = $value;
841 }
842
843 function setUserVariable($localVarName,$varName, $value) {
844         if($this->is_dynamic ||  $localVarName == 'CELL')return;
845         global $current_user;
846         $current_user->setPreference($this->local_current_module."_".$localVarName."_".$varName, $value);
847 }
848
849 /**INTERNAL FUNCTION returns a session variable first checking the query for it then checking the session
850  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
851  * All Rights Reserved.
852  * Contributor(s): ______________________________________.
853 */
854  function getSessionVariable($localVarName,$varName) {
855     //Set any variables pass in through request first
856     if(isset($_REQUEST[$this->getSessionVariableName($localVarName, $varName)])) {
857         $this->setSessionVariable($localVarName,$varName,$_REQUEST[$this->getSessionVariableName($localVarName, $varName)]);
858     }
859
860     if(isset($_SESSION[$this->getSessionVariableName($localVarName, $varName)])) {
861         return $_SESSION[$this->getSessionVariableName($localVarName, $varName)];
862     }
863     return "";
864 }
865
866 function getUserVariable($localVarName, $varName) {
867     global $current_user;
868     if($this->is_dynamic ||  $localVarName == 'CELL')return;
869     if(isset($_REQUEST[$this->getSessionVariableName($localVarName, $varName)])) {
870
871             $this->setUserVariable($localVarName,$varName,$_REQUEST[$this->getSessionVariableName($localVarName, $varName)]);
872     }
873     return $current_user->getPreference($this->getSessionVariableName($localVarName, $varName));
874 }
875
876
877     /**
878      * helper method to determine sort order by priority of source
879      * 1. explicit in request object
880      * 2. in session variable
881      * 3. subpaneldefs metadata
882      * 4. default 'asc'
883      * @param array $sortOrderList - contains options
884      * @return string 'asc' | 'desc'
885      */
886     function calculateSortOrder($sortOrderList)
887     {
888         $priority_map = array(
889           'request',
890           'session',
891           'subpaneldefs',
892           'default',
893         );
894
895         foreach($priority_map as $p) {
896             if (key_exists($p, $sortOrderList)) {
897                 $order = strtolower($sortOrderList[$p]);
898                 if (in_array($order, array('asc', 'desc'))) {
899                     return $order;
900                 }
901             }
902         }
903         return 'asc';
904     }
905
906
907     /**
908
909     * @return void
910     * @param unknown $localVarName
911     * @param unknown $varName
912     * @desc INTERNAL FUNCTION returns the session/query variable name
913     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
914     * All Rights Reserved.
915     * Contributor(s): ______________________________________..
916     */
917     function getSessionVariableName($localVarName,$varName) {
918         return $this->local_current_module."_".$localVarName."_".$varName;
919     }
920
921     /**
922
923     * @return void
924     * @param unknown $seed
925     * @param unknown $xTemplateSection
926     * @param unknown $html_varName
927     * @desc INTERNAL FUNCTION Handles List Views using seeds that extend SugarBean
928         $XTemplateSection is the section in the XTemplate file that should be parsed usually main
929         $html_VarName is the variable name used in the XTemplateFile e.g. TASK
930         $seed is a seed that extends SugarBean
931         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
932         * All Rights Reserved..
933         * Contributor(s): ______________________________________..
934     */
935     function processSugarBean($xtemplateSection, $html_varName, $seed) {
936         global $list_view_row_count;
937
938         $current_offset = $this->getOffset($html_varName);
939         $response = array();
940
941         //ADDING VCR CONTROL
942         SugarVCR::erase($seed->module_dir);
943         $params = array();
944         //$filter = array('id', 'full_name');
945         $filter=array();
946         $ret_array = $seed->create_new_list_query($this->query_orderby, $this->query_where, $filter, $params, 0, '', true, $seed, true);
947         if(!is_array($params)) $params = array();
948         if(!isset($params['custom_select'])) $params['custom_select'] = '';
949         if(!isset($params['custom_from'])) $params['custom_from'] = '';
950         if(!isset($params['custom_where'])) $params['custom_where'] = '';
951         if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
952         $main_query = $ret_array['select'] . $params['custom_select'] . $ret_array['from'] . $params['custom_from'] . $ret_array['where'] . $params['custom_where'] . $ret_array['order_by'] . $params['custom_order_by'];
953         SugarVCR::store($seed->module_dir,  $main_query);
954         //ADDING VCR CONTROL
955
956         if(empty($this->related_field_name)) {
957             $response = $seed->get_list($this->query_orderby, $this->query_where, $current_offset, $this->query_limit);
958         } else {
959             $related_field_name = $this->related_field_name;
960             $response = $seed->get_related_list($this->child_focus,$related_field_name, $this->query_orderby,
961             $this->query_where, $current_offset, $this->query_limit);
962         }
963
964         $list = $response['list'];
965         $row_count = $response['row_count'];
966         $next_offset = $response['next_offset'];
967         $previous_offset = $response['previous_offset'];
968
969         if(!empty($response['current_offset'])) {
970             $current_offset = $response['current_offset'];
971         }
972
973         $list_view_row_count = $row_count;
974         $this->processListNavigation($xtemplateSection,$html_varName, $current_offset, $next_offset, $previous_offset, $row_count, null, null, empty($seed->column_fields) ? null : count($seed->column_fields));
975
976         return $list;
977     }
978
979
980
981     function processUnionBeans($sugarbean, $subpanel_def, $html_var = 'CELL') {
982
983                 $last_detailview_record = $this->getSessionVariable("detailview", "record");
984                 if(!empty($last_detailview_record) && $last_detailview_record != $sugarbean->id){
985                         $GLOBALS['record_has_changed'] = true;
986                 }
987                 $this->setSessionVariable("detailview", "record", $sugarbean->id);
988
989                 $current_offset = $this->getOffset($html_var);
990                 $module = isset($_REQUEST['module']) ? $_REQUEST['module'] : '';
991                 $response = array();
992
993         // choose sort order
994         $sort_order = array();
995         $sort_order['default'] = 'asc';
996
997         // explicit request parameter gets priority over all
998         $sort_order['request'] = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : null;
999
1000         // see if the session data has a sort order
1001         if (isset($_SESSION['last_sub' . $this->subpanel_module . '_order']))
1002         {
1003             $sort_order['session'] = $_SESSION['last_sub' . $this->subpanel_module . '_order'];
1004
1005             // We swap the order when the request contains an offset (indicating a column sort issued);
1006             // otherwise we do not sort.  If we don't make this check, then the subpanel listview will
1007             // swap ordering each time a new record is entered via quick create forms
1008             if (isset($_REQUEST[$module . '_' . $html_var . '_offset']))
1009             {
1010                 $sort_order['session'] = $sort_order['session'] == 'asc' ? 'desc' : 'asc';
1011             }
1012         }
1013         else
1014         {
1015             $sort_order['session'] = null;
1016         }
1017
1018         // does the metadata have a default sort order?
1019         $sort_order['subpaneldefs'] = isset($subpanel_def->_instance_properties['sort_order']) ?
1020             $subpanel_def->_instance_properties['sort_order'] : null;
1021
1022         $this->sort_order = $this->calculateSortOrder($sort_order);
1023
1024
1025         if (isset($subpanel_def->_instance_properties['sort_by'])) {
1026             $this->query_orderby = $subpanel_def->_instance_properties['sort_by'];
1027         } else {
1028             $this->query_orderby = 'id';
1029         }
1030
1031         $this->getOrderBy($html_var,$this->query_orderby, $this->sort_order);
1032
1033         $_SESSION['last_sub' .$this->subpanel_module. '_order'] = $this->sort_order;
1034         $_SESSION['last_sub' .$this->subpanel_module. '_url'] = $this->getBaseURL($html_var);
1035
1036                 // Bug 8139 - Correct Subpanel sorting on 'name', when subpanel sorting default is 'last_name, first_name'
1037                 if (($this->sortby == 'name' || $this->sortby == 'last_name') &&
1038                         str_replace(' ', '', trim($subpanel_def->_instance_properties['sort_by'])) == 'last_name,first_name') {
1039                         $this->sortby = 'last_name '.$this->sort_order.', first_name ';
1040                 }
1041
1042         if(!empty($this->response)){
1043             $response =& $this->response;
1044             echo 'cached';
1045         }else{
1046             $response = SugarBean::get_union_related_list($sugarbean,$this->sortby, $this->sort_order, $this->query_where, $current_offset, -1,-1,$this->query_limit,$subpanel_def);
1047             $this->response =& $response;
1048         }
1049         $list = $response['list'];
1050         $row_count = $response['row_count'];
1051         $next_offset = $response['next_offset'];
1052         $previous_offset = $response['previous_offset'];
1053         if(!empty($response['current_offset']))$current_offset = $response['current_offset'];
1054         global $list_view_row_count;
1055         $list_view_row_count = $row_count;
1056         $this->processListNavigation('dyn_list_view', $html_var, $current_offset, $next_offset, $previous_offset, $row_count, $sugarbean,$subpanel_def);
1057
1058         return array('list'=>$list, 'parent_data'=>$response['parent_data'], 'query'=>$response['query']);
1059     }
1060
1061     function getBaseURL($html_varName) {
1062         static $cache = array();
1063
1064         if(!empty($cache[$html_varName]))return $cache[$html_varName];
1065         $blockVariables = array('mass', 'uid', 'massupdate', 'delete', 'merge', 'selectCount','current_query_by_page');
1066         if(!empty($this->base_URL)) {
1067             return $this->base_URL;
1068         }
1069
1070             $baseurl = $_SERVER['PHP_SELF'];
1071             if(empty($baseurl)) {
1072                 $baseurl = 'index.php';
1073             }
1074
1075             /*fixes an issue with deletes when doing a search*/
1076             foreach(array_merge($_GET, $_POST) as $name=>$value) {
1077                 //echo ("$name = $value <br/>");
1078                 if(!empty($value) && $name != 'sort_order' //&& $name != ListView::getSessionVariableName($html_varName,"ORDER_BY")
1079                         && $name != ListView::getSessionVariableName($html_varName,"offset")
1080                         /*&& substr_count($name, "ORDER_BY")==0*/ && !in_array($name, $blockVariables))
1081                 {
1082                     if(is_array($value)) {
1083                         foreach($value as $valuename=>$valuevalue) {
1084                             if(substr_count($baseurl, '?') > 0)
1085                                 $baseurl        .= "&{$name}[]=".$valuevalue;
1086                             else
1087                                 $baseurl        .= "?{$name}[]=".$valuevalue;
1088                         }
1089                     } else {
1090                         $value = urlencode($value);
1091                         if(substr_count($baseurl, '?') > 0) {
1092                             $baseurl    .= "&$name=$value";
1093                         } else {
1094                             $baseurl    .= "?$name=$value";
1095                         }
1096                     }
1097                 }
1098             }
1099
1100
1101             if($_SERVER['REQUEST_METHOD'] == 'POST') {
1102                 // at this point it is possible that the above foreach already executed resulting in double ?'s in the url
1103                 if(substr_count($baseurl, '?') == 0) {
1104                     $baseurl .= '?';
1105                 }
1106                 if(isset($_REQUEST['action'])) $baseurl.= '&action='.$_REQUEST['action'];
1107                 if(isset($_REQUEST['record'])) $baseurl .= '&record='.$_REQUEST['record'];
1108                 if(isset($_REQUEST['module'])) $baseurl .= '&module='.$_REQUEST['module'];
1109             }
1110
1111             $baseurl .= "&".ListView::getSessionVariableName($html_varName,"offset")."=";
1112             $cache[$html_varName] = $baseurl;
1113             return $baseurl;
1114     }
1115     /**
1116     * @return void
1117     * @param unknown $data
1118     * @param unknown $xTemplateSection
1119     * @param unknown $html_varName
1120     * @desc INTERNAL FUNCTION process the List Navigation
1121     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1122     * All Rights Reserved.
1123     * Contributor(s): ______________________________________..
1124     */
1125     function processListNavigation($xtemplateSection, $html_varName, $current_offset, $next_offset, $previous_offset, $row_count, $sugarbean=null, $subpanel_def=null, $col_count = 20) {
1126
1127         global $export_module;
1128         global $sugar_config;
1129         global $current_user;
1130         global $currentModule;
1131         global $app_strings;
1132
1133         $start_record = $current_offset + 1;
1134
1135         if(!is_numeric($col_count))
1136             $col_count = 20;
1137
1138         if($row_count == 0)
1139             $start_record = 0;
1140
1141         $end_record = $start_record + $this->records_per_page;
1142         // back up the the last page.
1143         if($end_record > $row_count+1) {
1144             $end_record = $row_count+1;
1145         }
1146         // Determine the start location of the last page
1147         if($row_count == 0)
1148             $number_pages = 0;
1149         else
1150             $number_pages = floor(($row_count - 1) / $this->records_per_page);
1151
1152         $last_offset = $number_pages * $this->records_per_page;
1153
1154         if(empty($this->query_limit)  || $this->query_limit > $this->records_per_page) {
1155             $this->base_URL = $this->getBaseURL($html_varName);
1156             $dynamic_url = '';
1157
1158             if($this->is_dynamic) {
1159                 $dynamic_url .='&'. $this->getSessionVariableName($html_varName,'ORDER_BY') . '='. $this->getSessionVariable($html_varName,'ORDER_BY').'&sort_order='.$this->sort_order.'&to_pdf=true&action=SubPanelViewer&subpanel=' . $this->subpanel_module;
1160             }
1161
1162             $current_URL = htmlentities($this->base_URL.$current_offset.$dynamic_url);
1163             $start_URL = htmlentities($this->base_URL."0".$dynamic_url);
1164             $previous_URL  = htmlentities($this->base_URL.$previous_offset.$dynamic_url);
1165             $next_URL  = htmlentities($this->base_URL.$next_offset.$dynamic_url);
1166             $end_URL  = htmlentities($this->base_URL.'end'.$dynamic_url);
1167
1168             if(!empty($this->start_link_wrapper)) {
1169                 $current_URL = $this->start_link_wrapper.$current_URL.$this->end_link_wrapper;
1170                 $start_URL = $this->start_link_wrapper.$start_URL.$this->end_link_wrapper;
1171                 $previous_URL = $this->start_link_wrapper.$previous_URL.$this->end_link_wrapper;
1172                 $next_URL = $this->start_link_wrapper.$next_URL.$this->end_link_wrapper;
1173                 $end_URL = $this->start_link_wrapper.$end_URL.$this->end_link_wrapper;
1174             }
1175
1176             $moduleString = "{$currentModule}_{$html_varName}_offset";
1177             $moduleStringOrder = "{$currentModule}_{$html_varName}_ORDER_BY";
1178             if($this->shouldProcess && !$this->multi_select_popup) {
1179                 // check the checkboxes onload
1180                 echo "<script>YAHOO.util.Event.addListener(window, \"load\", sListView.check_boxes);</script>\n";
1181
1182                 $massUpdateRun = isset($_REQUEST['massupdate']) && $_REQUEST['massupdate'] == 'true';
1183                 $uids = empty($_REQUEST['uid']) || $massUpdateRun ? '' : $_REQUEST['uid'];
1184                 $select_entire_list = ($massUpdateRun) ? 0 : (isset($_POST['select_entire_list']) ? $_POST['select_entire_list'] : (isset($_REQUEST['select_entire_list']) ? $_REQUEST['select_entire_list'] : 0));
1185
1186                 echo "<textarea style='display: none' name='uid'>{$uids}</textarea>\n" .
1187                     "<input type='hidden' name='select_entire_list' value='{$select_entire_list}'>\n".
1188                     "<input type='hidden' name='{$moduleString}' value='0'>\n".
1189                     "<input type='hidden' name='{$moduleStringOrder}' value='0'>\n";
1190
1191             }
1192
1193
1194             $GLOBALS['log']->debug("Offsets: (start, previous, next, last)(0, $previous_offset, $next_offset, $last_offset)");
1195
1196             if(0 == $current_offset) {
1197                 $start_link = "<button type='button' name='listViewStartButton' title='{$this->local_app_strings['LNK_LIST_START']}' class='button' disabled>".SugarThemeRegistry::current()->getImage("start_off","aborder='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_START'])."</button>";
1198                 $previous_link = "<button type='button' name='listViewPrevButton' title='{$this->local_app_strings['LNK_LIST_PREVIOUS']}' class='button' disabled>".SugarThemeRegistry::current()->getImage("previous_off","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_PREVIOUS'])."</button>";
1199             } else {
1200                 if($this->multi_select_popup) {// nav links for multiselect popup, submit form to save checks.
1201                     $start_link = "<button type='button' class='button' name='listViewStartButton' title='{$this->local_app_strings['LNK_LIST_START']}' onClick='javascript:save_checks(0, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("start","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_START'])."</button>";
1202                     $previous_link = "<button type='button' class='button' name='listViewPrevButton' title='{$this->local_app_strings['LNK_LIST_PREVIOUS']}' onClick='javascript:save_checks($previous_offset, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("previous","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_PREVIOUS'])."</button>";
1203                 } elseif($this->shouldProcess) {
1204                     $start_link = "<button type='button' class='button' name='listViewStartButton' title='{$this->local_app_strings['LNK_LIST_START']}' onClick='location.href=\"$start_URL\"; sListView.save_checks(0, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("start","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_START'])."</button>";
1205                     $previous_link = "<button type='button' class='button' name='listViewPrevButton' title='{$this->local_app_strings['LNK_LIST_PREVIOUS']}' onClick='location.href=\"$previous_URL\"; sListView.save_checks($previous_offset, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("previous","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_PREVIOUS'])."</button>";
1206                 } else {
1207                     $onClick = '';
1208                     if(0 != preg_match('/javascript.*/', $start_URL)){
1209                         $onClick = "\"$start_URL;\"";
1210                     }else{
1211                         $onClick ="'location.href=\"$start_URL\";'";
1212                     }
1213                     $start_link = "<button type='button' class='button' name='listViewStartButton' title='{$this->local_app_strings['LNK_LIST_START']}' onClick=".$onClick.">".SugarThemeRegistry::current()->getImage("start","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_START'])."</button>";
1214
1215                     $onClick = '';
1216                     if(0 != preg_match('/javascript.*/', $previous_URL)){
1217                         $onClick = "\"$previous_URL;\"";
1218                     }else{
1219                         $onClick = "'location.href=\"$previous_URL\";'";
1220                     }
1221                     $previous_link = "<button type='button' class='button' name='listViewPrevButton' title='{$this->local_app_strings['LNK_LIST_PREVIOUS']}' onClick=".$onClick.">".SugarThemeRegistry::current()->getImage("previous","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_PREVIOUS'])."</button>";
1222                 }
1223             }
1224
1225             if($last_offset <= $current_offset) {
1226                 $end_link = "<button type='button' name='listViewEndButton' title='{$this->local_app_strings['LNK_LIST_END']}' class='button' disabled>".SugarThemeRegistry::current()->getImage("end_off","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_END'])."</button>";
1227                 $next_link = "<button type='button' name='listViewNextButton' title='{$this->local_app_strings['LNK_LIST_NEXT']}' class='button' disabled>".SugarThemeRegistry::current()->getImage("next_off","aborder='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_NEXT'])."</button>";
1228             } else {
1229                 if($this->multi_select_popup) { // nav links for multiselect popup, submit form to save checks.
1230                     $end_link = "<button type='button' name='listViewEndButton' class='button' title='{$this->local_app_strings['LNK_LIST_END']}' onClick='javascript:save_checks($last_offset, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("end","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_END'])."</button>";
1231                     if(!empty($sugar_config['disable_count_query'])) {
1232                         $end_link = '';
1233                     }
1234                     $next_link = "<button type='button' name='listViewNextButton' title='{$this->local_app_strings['LNK_LIST_NEXT']}' class='button' onClick='javascript:save_checks($next_offset, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("next","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_NEXT'])."</button>";
1235                 } elseif($this->shouldProcess) {
1236                     $end_link = "<button type='button' name='listViewEndButton' class='button' title='{$this->local_app_strings['LNK_LIST_END']}' onClick='location.href=\"$end_URL\"; sListView.save_checks(\"end\", \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("end","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_END'])."</button>";
1237                     $next_link = "<button type='button' name='listViewNextButton' class='button' title='{$this->local_app_strings['LNK_LIST_NEXT']}' onClick='location.href=\"$next_URL\"; sListView.save_checks($next_offset, \"{$moduleString}\");'>".SugarThemeRegistry::current()->getImage("next","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_NEXT'])."</button>";
1238                 } else {
1239                     $onClick = '';
1240                     if(0 != preg_match('/javascript.*/', $next_URL)){
1241                         $onClick = "\"$next_URL;\"";
1242                     }else{
1243                         $onClick ="'location.href=\"$next_URL\";'";
1244                     }
1245                     $next_link = "<button type='button' name='listViewNextButton' class='button' title='{$this->local_app_strings['LNK_LIST_NEXT']}' onClick=".$onClick.">".SugarThemeRegistry::current()->getImage("next","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_NEXT'])."</button>";
1246
1247                     $onClick = '';
1248                     if(0 != preg_match('/javascript.*/', $end_URL)){
1249                         $onClick = "\"$end_URL;\"";
1250                     }else{
1251                         $onClick = "'location.href=\"$end_URL\";'";
1252                     }
1253                     $end_link = "<button type='button' name='listViewEndButton' class='button' title='{$this->local_app_strings['LNK_LIST_END']}' onClick=".$onClick.">".SugarThemeRegistry::current()->getImage("end","border='0' align='absmiddle'",null,null,'.gif',$this->local_app_strings['LNK_LIST_END'])."</button>";
1254
1255                 }
1256             }
1257
1258             $GLOBALS['log']->info("Offset (next, current, prev)($next_offset, $current_offset, $previous_offset)");
1259             $GLOBALS['log']->info("Start/end records ($start_record, $end_record)");
1260
1261             $end_record = $end_record-1;
1262
1263 $script_href = "<a style=\'width: 150px\' name=\"thispage\" class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'if (document.MassUpdate.select_entire_list.value==1){document.MassUpdate.select_entire_list.value=0;sListView.check_all(document.MassUpdate, \"mass[]\", true, $this->records_per_page)}else {sListView.check_all(document.MassUpdate, \"mass[]\", true)};\' href=\'#\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_CURRENT']}&nbsp;&#x28;{$this->records_per_page}&#x29;&#x200E;</a>"
1264  . "<a style=\'width: 150px\' name=\"selectall\" class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'sListView.check_entire_list(document.MassUpdate, \"mass[]\",true,{$row_count});\' href=\'#\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_ENTIRE']}&nbsp;&#x28;{$row_count}&#x29;&#x200E;</a>"
1265  . "<a style=\'width: 150px\' name=\"deselect\" class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'sListView.clear_all(document.MassUpdate, \"mass[]\", false);\' href=\'#\'>{$this->local_app_strings['LBL_LISTVIEW_NONE']}</a>";
1266
1267 $close_inline_img = SugarThemeRegistry::current()->getImage('close_inline', 'border=0', null, null, ".gif", $app_strings['LBL_CLOSEINLINE']);
1268
1269             echo "<script>
1270                 function select_dialog() {
1271                         var \$dialog = \$('<div></div>')
1272                                         .html('<a style=\'width: 150px\' name=\"thispage\" class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'if (document.MassUpdate.select_entire_list.value==1){document.MassUpdate.select_entire_list.value=0;sListView.check_all(document.MassUpdate, \"mass[]\", true, $this->records_per_page)}else {sListView.check_all(document.MassUpdate, \"mass[]\", true)};\' href=\'javascript:void(0)\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_CURRENT']}&nbsp;&#x28;{$this->records_per_page}&#x29;&#x200E;</a>"
1273                 . "<a style=\'width: 150px\' name=\"selectall\" class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'sListView.check_entire_list(document.MassUpdate, \"mass[]\",true,{$row_count});\' href=\'javascript:void(0)\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_ENTIRE']}&nbsp;&#x28;{$row_count}&#x29;&#x200E;</a>"
1274                 . "<a style=\'width: 150px\' name=\"deselect\" class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'sListView.clear_all(document.MassUpdate, \"mass[]\", false);\' href=\'javascript:void(0)\'>{$this->local_app_strings['LBL_LISTVIEW_NONE']}</a>')
1275                                         .dialog({
1276                                                 autoOpen: false,
1277                                                 width: 150
1278                                         });
1279                                         \$dialog.dialog('open');
1280
1281                 }
1282                 </script>";
1283
1284             if($this->show_select_menu)
1285             {
1286                 $total_label = "";
1287                 $total = $row_count;
1288                 $pageTotal = ($row_count > 0) ? $end_record - $start_record + 1 : 0;
1289                 if (!empty($GLOBALS['sugar_config']['disable_count_query']) && $GLOBALS['sugar_config']['disable_count_query'] === true && $total > $pageTotal) {
1290                     $this->show_plus = true;
1291                     $total =  $pageTotal;
1292                     $total_label = $total.'+';
1293                 } else {
1294                     $this->show_plus = false;
1295                     $total_label = $total;
1296                 }
1297                 echo "<input type='hidden' name='show_plus' value='{$this->show_plus}'>\n";
1298
1299                 //Bug#52931: Replace with actionMenu
1300                 //$select_link = "<a id='select_link' onclick='return select_dialog();' href=\"javascript:void(0)\">".$this->local_app_strings['LBL_LINK_SELECT']."&nbsp;".SugarThemeRegistry::current()->getImage('MoreDetail', 'border=0', 11, 7, '.png', $app_strings['LBL_MOREDETAIL'])."</a>";
1301                 $menuItems = array(
1302                     "<input title=\"".$app_strings['LBL_SELECT_ALL_TITLE']."\" type='checkbox' class='checkbox massall' name='massall' id='massall' value='' onclick='sListView.check_all(document.MassUpdate, \"mass[]\", this.checked);' /><a href='javascript: void(0);'></a>",
1303                     "<a  name='thispage' id='button_select_this_page' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' onclick='if (document.MassUpdate.select_entire_list.value==1){document.MassUpdate.select_entire_list.value=0;sListView.check_all(document.MassUpdate, \"mass[]\", true, $pageTotal)}else {sListView.check_all(document.MassUpdate, \"mass[]\", true)};' href='#'>{$app_strings['LBL_LISTVIEW_OPTION_CURRENT']}&nbsp;&#x28;{$pageTotal}&#x29;&#x200E;</a>",
1304                     "<a  name='selectall' id='button_select_all' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' onclick='sListView.check_entire_list(document.MassUpdate, \"mass[]\",true,{$total});' href='#'>{$app_strings['LBL_LISTVIEW_OPTION_ENTIRE']}&nbsp;&#x28;{$total_label}&#x29;&#x200E;</a>",
1305                     "<a name='deselect' id='button_deselect' class='menuItem' onmouseover='hiliteItem(this,\"yes\");' onmouseout='unhiliteItem(this);' onclick='sListView.clear_all(document.MassUpdate, \"mass[]\", false);' href='#'>{$app_strings['LBL_LISTVIEW_NONE']}</a>",
1306                 );
1307                 require_once('include/Smarty/plugins/function.sugar_action_menu.php');
1308                 $select_link = smarty_function_sugar_action_menu(array(
1309                     'class' => 'clickMenu selectmenu',
1310                     'id' => 'selectLink',
1311                     'buttons' => $menuItems,
1312                     'flat' => false,
1313                 ),$this->xTemplate);
1314
1315             } else {
1316                 $select_link = "&nbsp;";
1317             }
1318
1319             $export_link = '<input class="button" type="button" value="'.$this->local_app_strings['LBL_EXPORT'].'" ' .
1320                     'onclick="return sListView.send_form(true, \''.$_REQUEST['module'].'\', \'index.php?entryPoint=export\',\''.$this->local_app_strings['LBL_LISTVIEW_NO_SELECTED'].'\')">';
1321
1322             if($this->show_delete_button) {
1323                 $delete_link = '<input class="button" type="button" id="delete_button" name="Delete" value="'.$this->local_app_strings['LBL_DELETE_BUTTON_LABEL'].'" onclick="return sListView.send_mass_update(\'selected\',\''.$this->local_app_strings['LBL_LISTVIEW_NO_SELECTED'].'\', 1)">';
1324             } else {
1325                 $delete_link = '&nbsp;';
1326             }
1327
1328             $admin = new Administration();
1329             $admin->retrieveSettings('system');
1330
1331             $user_merge = $current_user->getPreference('mailmerge_on');
1332             if($user_merge == 'on' && isset($admin->settings['system_mailmerge_on']) && $admin->settings['system_mailmerge_on']) {
1333                 echo "<script>
1334                 function mailmerge_dialog(el) {
1335                         var \$dialog = \$('<div></div>')
1336                                         .html('<a style=\'width: 150px\' class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' onclick=\'return sListView.send_form(true, \"MailMerge\", \"index.php\", \"{$this->local_app_strings['LBL_LISTVIEW_NO_SELECTED']}\")\' href=\'javascript:void(0)\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_SELECTED']}</a>"
1337                         . "<a style=\'width: 150px\' class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' href=\'index.php?action=index&module=MailMerge\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_CURRENT']}</a>"
1338                         . "<a style=\'width: 150px\' class=\'menuItem\' onmouseover=\'hiliteItem(this,\"yes\");\' onmouseout=\'unhiliteItem(this);\' href=\'index.php?action=index&module=MailMerge&entire=true\'>{$this->local_app_strings['LBL_LISTVIEW_OPTION_ENTIRE']}</a>')
1339                                         .dialog({
1340                                                 autoOpen: false,
1341                                                 title: '". $this->local_app_strings['LBL_MAILMERGE']."',
1342                                                 width: 150,
1343                                                 position: {
1344                                                     my: myPos,
1345                                                     at: atPos,
1346                                                     of: \$(el)
1347                                                 }
1348                                         });
1349
1350                 }
1351             </script>";
1352                 $merge_link = "&nbsp;|&nbsp;<a id='mailmerge_link' onclick='return mailmerge_dialog(this)'; href=\"javascript:void(0)\">".$this->local_app_strings['LBL_MAILMERGE']."</a>";
1353             } else {
1354                 $merge_link = "&nbsp;";
1355             }
1356
1357             $selected_objects_span = "&nbsp;|&nbsp;{$this->local_app_strings['LBL_LISTVIEW_SELECTED_OBJECTS']}<input  style='border: 0px; background: transparent; font-size: inherit; color: inherit' type='text' readonly name='selectCount[]' value='" . ((isset($_POST['mass'])) ? count($_POST['mass']): 0) . "' />";
1358
1359             if($_REQUEST['module'] == 'Home' || $this->local_current_module == 'Import'
1360                 || $this->show_export_button == false
1361                 || (!empty($sugar_config['disable_export']))
1362                 || (!empty($sugar_config['admin_export_only'])
1363                 && !(
1364                         is_admin($current_user)
1365                         || (ACLController::moduleSupportsACL($_REQUEST['module'])
1366                             && ACLAction::getUserAccessLevel($current_user->id,$_REQUEST['module'], 'access') == ACL_ALLOW_ENABLED
1367                             && (ACLAction::getUserAccessLevel($current_user->id, $_REQUEST['module'], 'admin') == ACL_ALLOW_ADMIN ||
1368                                 ACLAction::getUserAccessLevel($current_user->id, $_REQUEST['module'], 'admin') == ACL_ALLOW_ADMIN_DEV)))))
1369             {
1370                 if ($_REQUEST['module'] != 'InboundEmail' && $_REQUEST['module'] != 'EmailMan' && $_REQUEST['module'] != 'iFrames') {
1371                     $selected_objects_span = '';
1372                 }
1373                 $export_link = "&nbsp;";
1374                 $merge_link = "&nbsp;";
1375             } elseif($_REQUEST['module'] != "Accounts" && $_REQUEST['module'] != "Cases" && $_REQUEST['module'] != "Contacts" && $_REQUEST['module'] != "Leads" && $_REQUEST['module'] != "Opportunities") {
1376                 $merge_link = "&nbsp;";
1377             }
1378
1379             if($this->show_paging == true) {
1380                 if(!empty($sugar_config['disable_count_query'])) {
1381                     if($row_count > $end_record) {
1382                         $row_count .= '+';
1383                     }
1384                 }
1385
1386                 $html_text = '';
1387                 $html_text .= "<tr class='pagination' role='presentation'>\n";
1388                 $html_text .= "<td COLSPAN=\"$col_count\" align=\"right\">\n";
1389                 //$html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  >$export_link$merge_link$selected_objects_span</td>\n";
1390                 //$html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  >";
1391                 if ($subpanel_def != null) {
1392                     include_once('include/SubPanel/SubPanelTiles.php');
1393                     $subpanelTiles = new SubPanelTiles($sugarbean);
1394                     $html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  >";
1395
1396                     //attempt to get the query to recreate this subpanel
1397                     if(!empty($this->response)){
1398                         $response =& $this->response;
1399                     }else{
1400                         $response = SugarBean::get_union_related_list($sugarbean,$this->sortby, $this->sort_order, $this->query_where, $current_offset, -1,-1,$this->query_limit,$subpanel_def);
1401                         $this->response = $response;
1402                     }
1403                     //if query is present, then pass it in as parameter
1404                     if (isset($response['query']) && !empty($response['query'])){
1405                         $html_text .= $subpanelTiles->get_buttons($subpanel_def, $response['query']);
1406                     }else{
1407                         $html_text .= $subpanelTiles->get_buttons($subpanel_def);
1408                     }
1409                 }
1410                 else {
1411                     $html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  nowrap>$select_link&nbsp;$export_link&nbsp;$delete_link&nbsp;$selected_objects_span";
1412                 }
1413                 $html_text .= "</td>\n<td nowrap align=\"right\">".$start_link."&nbsp;&nbsp;".$previous_link."&nbsp;&nbsp;<span class='pageNumbers'>(".$start_record." - ".$end_record." ".$this->local_app_strings['LBL_LIST_OF']." ".$row_count.")</span>&nbsp;&nbsp;".$next_link."&nbsp;&nbsp;".$end_link."</td></tr></table>\n";
1414                 $html_text .= "</td>\n";
1415                 $html_text .= "</tr>\n";
1416                 $this->xTemplate->assign("PAGINATION",$html_text);
1417             }
1418
1419             //C.L. - Fix for 23461
1420             if(empty($_REQUEST['action']) || $_REQUEST['action'] != 'Popup') {
1421                 $_SESSION['export_where'] = $this->query_where;
1422             }
1423             $this->xTemplate->parse($xtemplateSection.".list_nav_row");
1424         }
1425     } // end processListNavigation
1426
1427     function processOrderBy($html_varName) {
1428
1429         if(!isset($this->base_URL)) {
1430             $this->base_URL = $_SERVER['PHP_SELF'];
1431
1432             if(isset($_SERVER['QUERY_STRING'])) {
1433                 $this->base_URL = preg_replace("/\&".$this->getSessionVariableName($html_varName,"ORDER_BY")."=[0-9a-zA-Z\_\.]*/","",$this->base_URL .'?'.$_SERVER['QUERY_STRING']);
1434                 $this->base_URL = preg_replace("/\&".$this->getSessionVariableName($html_varName,"offset")."=[0-9]*/","",$this->base_URL);
1435             }
1436             if($_SERVER['REQUEST_METHOD'] == 'POST') {
1437                 $this->base_URL .= '?';
1438                 if(isset($_REQUEST['action'])) $this->base_URL .= '&action='.$_REQUEST['action'];
1439                 if(isset($_REQUEST['record'])) $this->base_URL .= '&record='.$_REQUEST['record'];
1440                 if(isset($_REQUEST['module'])) $this->base_URL .= '&module='.$_REQUEST['module'];
1441             }
1442             $this->base_URL .= "&".$this->getSessionVariableName($html_varName,"offset")."=";
1443         }
1444
1445         if($this->is_dynamic) {
1446             $this->base_URL.='&to_pdf=true&action=SubPanelViewer&subpanel=' . $this->source_module;
1447         }
1448
1449         //bug43465 start
1450         if (isset($this->appendToBaseUrl) && is_array($this->appendToBaseUrl))
1451         {
1452             foreach ($this->appendToBaseUrl as $key => $value)
1453             {
1454                 $fullRequestString = $key . '=' . $value;
1455
1456                 if ($this->base_URL == "/index.php")
1457                 {
1458                     $this->base_URL .= "?";
1459                 } else
1460                 {
1461                     if ($fullRequestString == substr($this->baseURL, '-' . strlen($fullRequestString)))
1462                     {
1463                         $this->base_URL = preg_replace("/&" . $key . "\=.*/", "", $this->base_URL);
1464                     } else
1465                     {
1466                         $this->base_URL = preg_replace("/&" . $key . "\=.*?&/", "&", $this->base_URL);
1467                     }
1468                     $this->base_URL .= "&";
1469                 }
1470                 if (!empty($value))
1471                 {
1472                     $this->base_URL .= "{$key}={$value}";
1473                 }
1474             }
1475         }
1476         //bug43465 end
1477
1478         $sort_URL_base = $this->base_URL. "&".$this->getSessionVariableName($html_varName,"ORDER_BY")."=";
1479
1480         if($sort_URL_base !== "")
1481         {
1482             $this->xTemplate->assign("ORDER_BY", $sort_URL_base);
1483             return $sort_URL_base;
1484         } else {
1485             return '';
1486         }
1487     }
1488
1489
1490     function getAdditionalHeader() {
1491
1492     }
1493
1494
1495     /**
1496     * @return void
1497     * @param unknown $data
1498     * @param unknown $xTemplateSection
1499     * @param unknown $html_varName
1500     * @desc INTERNAL FUNCTION handles the rows
1501     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1502     * All Rights Reserved.
1503     * Contributor(s): ______________________________________..
1504     */
1505     function processListRows($data, $xtemplateSection, $html_varName)
1506     {
1507         global $odd_bg;
1508         global $even_bg;
1509         global $hilite_bg;
1510         global $app_strings, $sugar_version, $sugar_config;
1511         global $currentModule;
1512
1513         $this->xTemplate->assign('BG_HILITE', $hilite_bg);
1514         $this->xTemplate->assign('CHECKALL', SugarThemeRegistry::current()->getImage('blank', '', 1, 1, ".gif", ''));
1515     //$this->xTemplate->assign("BG_CLICK", $click_bg);
1516         $oddRow = true;
1517         $count = 0;
1518         reset($data);
1519
1520         //GETTING OFFSET
1521         $offset = $this->getOffset($html_varName);
1522         $timeStamp = $this->unique_id();
1523         $_SESSION[$html_varName."_FROM_LIST_VIEW"] = $timeStamp;
1524
1525         $associated_row_data = array();
1526
1527         //mail merge list
1528         $mergeList = array();
1529         $module = '';
1530         //todo what is this?  It is using an array as a boolean
1531         while(list($aVal, $aItem) = each($data))
1532         {
1533             if(isset($this->data_array)) {
1534                 $fields = $this->data_array;
1535             } else {
1536                 $aItem->check_date_relationships_load();
1537                 $fields = $aItem->get_list_view_data();
1538             }
1539
1540             if(is_object($aItem)) { // cn: bug 5349
1541                 //add item id to merge list, if the button is clicked
1542                 $mergeList[] = $aItem->id;
1543                 if(empty($module)) {
1544                     $module = $aItem->module_dir;
1545                 }
1546             }
1547             //ADD OFFSET TO ARRAY
1548
1549                 $fields['OFFSET'] = ($offset + $count + 1);
1550
1551             $fields['STAMP'] = $timeStamp;
1552             if($this->shouldProcess) {
1553
1554             $prerow = '';
1555             if(!isset($this->data_array)) {
1556                 $prerow .= "<input onclick='sListView.check_item(this, document.MassUpdate)' type='checkbox' class='checkbox' name='mass[]' value='". $fields['ID']. "'>";
1557             }
1558             $this->xTemplate->assign('PREROW', $prerow);
1559
1560             $this->xTemplate->assign('CHECKALL', "<input type='checkbox' class='checkbox'  title='".$GLOBALS['app_strings']['LBL_SELECT_ALL_TITLE']."'  name='massall' id='massall' value='' onclick='sListView.check_all(document.MassUpdate, \"mass[]\", this.checked)'>");
1561             }
1562             if(!isset($this->data_array)) {
1563                 $tag = $aItem->listviewACLHelper();
1564                 $this->xTemplate->assign('TAG',$tag) ;
1565             }
1566
1567             if($oddRow)
1568             {
1569                 $ROW_COLOR = 'oddListRow';
1570                 $BG_COLOR =  $odd_bg;
1571             }
1572             else
1573             {
1574                 $ROW_COLOR = 'evenListRow';
1575                 $BG_COLOR =  $even_bg;
1576             }
1577             $oddRow = !$oddRow;
1578
1579             $this->xTemplate->assign('ROW_COLOR', $ROW_COLOR);
1580             $this->xTemplate->assign('BG_COLOR', $BG_COLOR);
1581
1582             if(isset($this->data_array))
1583             {
1584                 $this->xTemplate->assign('KEY', $aVal);
1585                 $this->xTemplate->assign('VALUE', $aItem);
1586                 $this->xTemplate->assign('INDEX', $count);
1587
1588             }
1589             else
1590             {
1591     //AED -- some modules do not have their additionalDetails.php established. Add a check to ensure require_once does not fail
1592     // Bug #2786
1593                 if($this->_additionalDetails && $aItem->ACLAccess('DetailView') && (file_exists('modules/' . $aItem->module_dir . '/metadata/additionalDetails.php') || file_exists('custom/modules/' . $aItem->module_dir . '/metadata/additionalDetails.php'))) {
1594
1595                     $additionalDetailsFile = 'modules/' . $aItem->module_dir . '/metadata/additionalDetails.php';
1596                     if(file_exists('custom/modules/' . $aItem->module_dir . '/metadata/additionalDetails.php')){
1597                         $additionalDetailsFile = 'custom/modules/' . $aItem->module_dir . '/metadata/additionalDetails.php';
1598                     }
1599
1600                     require_once($additionalDetailsFile);
1601                     $ad_function = (empty($this->additionalDetailsFunction) ? 'additionalDetails' : $this->additionalDetailsFunction) . $aItem->object_name;
1602                     $results = $ad_function($fields);
1603                     $results['string'] = str_replace(array("&#039", "'"), '\&#039', $results['string']); // no xss!
1604
1605                     if(trim($results['string']) == '') $results['string'] = $app_strings['LBL_NONE'];
1606                     $fields[$results['fieldToAddTo']] = $fields[$results['fieldToAddTo']].'</a>';
1607                 }
1608
1609                 if($aItem->ACLAccess('Delete')) {
1610                     $delete = '<a class="listViewTdToolsS1" onclick="return confirm(\''.$this->local_app_strings['NTC_DELETE_CONFIRMATION'].'\')" href="'.'index.php?action=Delete&module='.$aItem->module_dir.'&record='.$fields['ID'].'&return_module='.$aItem->module_dir.'&return_action=index&return_id=">'.$this->local_app_strings['LBL_DELETE_INLINE'].'</a>';
1611                     require_once('include/Smarty/plugins/function.sugar_action_menu.php');
1612                     $fields['DELETE_BUTTON'] = smarty_function_sugar_action_menu(array(
1613                         'id' => $aItem->module_dir.'_'.$fields['ID'].'_create_button',
1614                         'buttons' => array($delete),
1615                     ), $this);
1616
1617                 }
1618
1619                 $this->xTemplate->assign($html_varName, $fields);
1620                 $aItem->setupCustomFields($aItem->module_dir);
1621                 $aItem->custom_fields->populateAllXTPL($this->xTemplate, 'detail', $html_varName, $fields);
1622             }
1623             if(!isset($this->data_array) && $aItem->ACLAccess('DetailView')) {
1624                 $count++;
1625             }
1626             if(isset($this->data_array)) {
1627                 $count++;
1628             }
1629             if(!isset($this->data_array)) {
1630                 $aItem->list_view_parse_additional_sections($this->xTemplate, $xtemplateSection);
1631
1632                 if($this->xTemplate->exists($xtemplateSection.'.row.pro')) {
1633                     $this->xTemplate->parse($xtemplateSection.'.row.pro');
1634                 }
1635             }
1636             $this->xTemplate->parse($xtemplateSection . '.row');
1637
1638             if(isset($fields['ID'])) {
1639                 $associated_row_data[$fields['ID']] = $fields;
1640                 // Bug 38908: cleanup data for JS to avoid having &nbsp; shuffled around
1641                 foreach($fields as $key => $value) {
1642                     if($value == '&nbsp;') {
1643                         $associated_row_data[$fields['ID']][$key] = '';
1644                     }
1645                 }
1646             }
1647         }
1648
1649         $_SESSION['MAILMERGE_RECORDS'] = $mergeList;
1650         $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'] = $module;
1651         if(empty($_REQUEST['action']) || $_REQUEST['action'] != 'Popup') {
1652             $_SESSION['MAILMERGE_MODULE'] = $module;
1653         }
1654
1655         if($this->process_for_popups)
1656         {
1657             $json = getJSONobj();
1658             $is_show_fullname = showFullName() ? 1 : 0;
1659             $associated_javascript_data = '<script type="text/javascript">' . "\n"
1660                 //. '<!-- // associated javascript data generated by ListView' . "\n"
1661                 . 'var associated_javascript_data = '
1662                 . $json->encode($associated_row_data) . ";\n"
1663                 //. '-->' . "\n"
1664                 . 'var is_show_fullname = '
1665                 . $is_show_fullname . ";\n"
1666                 . '</script>';
1667             $this->xTemplate->assign('ASSOCIATED_JAVASCRIPT_DATA', $associated_javascript_data);
1668         }
1669
1670         $this->xTemplate->parse($xtemplateSection);
1671     }
1672
1673
1674     function getLayoutManager()
1675     {
1676         require_once('include/generic/LayoutManager.php');
1677         if($this->layout_manager == null)
1678         {
1679             $this->layout_manager = new LayoutManager();
1680         }
1681         return $this->layout_manager;
1682     }
1683
1684
1685     function process_dynamic_listview_header($source_module, $subpanel_def, $html_var = 'CELL')
1686     {
1687
1688
1689         $layout_manager = $this->getLayoutManager();
1690         $layout_manager->setAttribute('order_by_link',$this->processOrderBy($html_var));
1691         $layout_manager->setAttribute('context','HeaderCell');
1692         $layout_manager->setAttribute('image_path',$this->local_image_path);
1693         $layout_manager->setAttribute('html_varName',$html_var);
1694         $layout_manager->setAttribute('module_name', $source_module);
1695         list($orderBy,$desc) = $this->getOrderByInfo($html_var);
1696
1697         if($orderBy == 'amount*1')
1698         {
1699             $orderBy=  'amount';
1700         }
1701                 $buttons = false;
1702         foreach($subpanel_def->get_list_fields() as $column_name=>$widget_args)
1703         {
1704             $usage = empty($widget_args['usage']) ? '' : $widget_args['usage'];
1705             if($usage != 'query_only')
1706             {
1707                 $imgArrow = '';
1708
1709                 if($orderBy == $column_name || (isset($widget_args['sort_by']) && str_replace('.','_',$widget_args['sort_by']) == $orderBy))
1710                 {
1711                     $imgArrow = "_down";
1712                     if($this->sort_order == 'asc') {
1713                         $imgArrow = "_up";
1714                     }
1715                 }
1716
1717                 if (!preg_match("/_button/i", $column_name)) {
1718                         $widget_args['name']=$column_name;
1719                         $widget_args['sort'] = $imgArrow;
1720                         $widget_args['start_link_wrapper'] = $this->start_link_wrapper;
1721                         $widget_args['end_link_wrapper'] = $this->end_link_wrapper;
1722                         $widget_args['subpanel_module'] = $this->subpanel_module;
1723
1724                         $widget_contents = $layout_manager->widgetDisplay($widget_args);
1725                         $cell_width = empty($widget_args['width']) ? '' : $widget_args['width'];
1726                         $this->xTemplate->assign('HEADER_CELL', $widget_contents);
1727                         static $count;
1728                     if(!isset($count))$count = 0; else $count++;
1729                         $this->xTemplate->assign('CELL_COUNT', $count);
1730                         $this->xTemplate->assign('CELL_WIDTH', $cell_width);
1731                         $this->xTemplate->parse('dyn_list_view.header_cell');
1732                 } else {
1733                         $buttons = true;
1734                 }
1735             }
1736         }
1737
1738         if($buttons) {
1739                                 $this->xTemplate->assign('HEADER_CELL', "&nbsp;");
1740                                 $this->xTemplate->assign('CELL_COUNT', $count);
1741                         $this->xTemplate->assign('CELL_WIDTH', $cell_width);
1742                         $this->xTemplate->parse('dyn_list_view.header_cell');
1743         }
1744
1745     }
1746
1747
1748     /**
1749     * @return void
1750     * @param unknown $seed
1751     * @param unknown $xTemplateSection
1752     * @param unknown $html_varName
1753     * @desc PUBLIC FUNCTION Handles List Views using seeds that extend SugarBean
1754         $XTemplateSection is the section in the XTemplate file that should be parsed usually main
1755         $html_VarName is the variable name used in the XTemplateFile e.g. TASK
1756         $seed is a seed there are two types of seeds one is a subclass of SugarBean, the other is a list usually created from a sugar bean using get_list
1757         if no XTemplate is set it will create  a new XTemplate
1758         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
1759         * All Rights Reserved..
1760         * Contributor(s): ______________________________________..
1761     */
1762
1763     function processListViewTwo($seed, $xTemplateSection, $html_varName) {
1764         global $current_user;
1765         if(!isset($this->xTemplate)) {
1766             $this->createXTemplate();
1767         }
1768
1769         $isSugarBean = is_subclass_of($seed, "SugarBean");
1770         $list = null;
1771
1772         if($isSugarBean) {
1773             $list = $this->processSugarBean($xTemplateSection, $html_varName, $seed);
1774         } else {
1775             $list = $seed;
1776         }
1777
1778         if (is_object($seed) && isset($seed->object_name) && $seed->object_name == 'WorkFlow') {
1779             $tab=array();
1780             $access = get_workflow_admin_modules_for_user($current_user);
1781             for ($i = 0; $i < count($list); $i++) {
1782                 if(!empty($access[$list[$i]->base_module])){
1783                     $tab[]=$list[$i];
1784                 }
1785             }
1786             $list = $tab;
1787         }
1788
1789         if($this->is_dynamic) {
1790             $this->processHeaderDynamic($xTemplateSection,$html_varName);
1791             $this->processListRows($list,$xTemplateSection, $html_varName);
1792         } else {
1793             $this->processSortArrows($html_varName);
1794
1795             if($isSugarBean) {
1796                 $seed->parse_additional_headers($this->xTemplate, $xTemplateSection);
1797             }
1798             $this->xTemplateAssign('CHECKALL', SugarThemeRegistry::current()->getImage('blank', '', 1, 1, ".gif", ''));
1799
1800             // Process the  order by before processing the pro_nav.  The pro_nav requires the order by values to be set
1801             $this->processOrderBy($html_varName);
1802
1803
1804             $this->processListRows($list,$xTemplateSection, $html_varName);
1805         }
1806
1807         if($this->display_header_and_footer) {
1808             $this->getAdditionalHeader();
1809             if(!empty($this->header_title)) {
1810                 echo get_form_header($this->header_title, $this->header_text, false);
1811             }
1812         }
1813
1814         $this->xTemplate->out($xTemplateSection);
1815
1816         if(isset($_SESSION['validation'])) {
1817             print base64_decode('PGEgaHJlZj0naHR0cDovL3d3dy5zdWdhcmNybS5jb20nPlBPV0VSRUQmbmJzcDtCWSZuYnNwO1NVR0FSQ1JNPC9hPg==');
1818         }
1819     }
1820
1821     function getArrowStart() {
1822         $imgFileParts = pathinfo(SugarThemeRegistry::current()->getImageURL("arrow.gif"));
1823
1824         return "&nbsp;<!--not_in_theme!--><img border='0' src='".$imgFileParts['dirname']."/".$imgFileParts['filename']."";
1825     }
1826
1827     function getArrowUpDownStart($upDown) {
1828         $ext = ( SugarThemeRegistry::current()->pngSupport ? "png" : "gif" );
1829
1830         if (!isset($upDown) || empty($upDown)) {
1831             $upDown = "";
1832         }
1833         return "&nbsp;<img border='0' src='".SugarThemeRegistry::current()->getImageURL("arrow{$upDown}.{$ext}")."' ";
1834     }
1835
1836         function getArrowEnd() {
1837                 $imgFileParts = pathinfo(SugarThemeRegistry::current()->getImageURL("arrow.gif"));
1838
1839         list($width,$height) = ListView::getArrowImageSize();
1840
1841                 return '.'.$imgFileParts['extension']."' width='$width' height='$height' align='absmiddle' alt=".translate('LBL_SORT').">";
1842     }
1843
1844     function getArrowUpDownEnd($upDown) {
1845         if (!isset($upDown) || empty($upDown)) {
1846             $upDown = "";
1847         }
1848         $imgFileParts = pathinfo(SugarThemeRegistry::current()->getImageURL("arrow{$upDown}.gif"));
1849
1850         list($width,$height) = ListView::getArrowUpDownImageSize($upDown);
1851
1852         //get the right alt tag for the sort
1853         $sortStr = translate('LBL_ALT_SORT');
1854         if($upDown == '_down'){
1855             $sortStr = translate('LBL_ALT_SORT_DESC');
1856         }elseif($upDown == '_up'){
1857             $sortStr = translate('LBL_ALT_SORT_ASC');
1858         }
1859         return " width='$width' height='$height' align='absmiddle' alt='$sortStr'>";
1860     }
1861
1862         function getArrowImageSize() {
1863             // jbasicChartDashletsExpColust get the non-sort image's size.. the up and down have be the same.
1864                 $image = SugarThemeRegistry::current()->getImageURL("arrow.gif",false);
1865
1866         $cache_key = 'arrow_size.'.$image;
1867
1868         // Check the cache
1869         $result = sugar_cache_retrieve($cache_key);
1870         if(!empty($result))
1871         return $result;
1872
1873         // No cache hit.  Calculate the value and return.
1874         $result = getimagesize($image);
1875         sugar_cache_put($cache_key, $result);
1876         return $result;
1877     }
1878
1879     function getArrowUpDownImageSize($upDown) {
1880         // just get the non-sort image's size.. the up and down have be the same.
1881         $image = SugarThemeRegistry::current()->getImageURL("arrow{$upDown}.gif",false);
1882
1883         $cache_key = 'arrowupdown_size.'.$image;
1884
1885         // Check the cache
1886         $result = sugar_cache_retrieve($cache_key);
1887         if(!empty($result))
1888         return $result;
1889
1890         // No cache hit.  Calculate the value and return.
1891         $result = getimagesize($image);
1892         sugar_cache_put($cache_key, $result);
1893         return $result;
1894     }
1895
1896         function getOrderByInfo($html_varName)
1897         {
1898                 $orderBy = $this->getSessionVariable($html_varName, "OBL");
1899                 $desc = $this->getSessionVariable($html_varName, $orderBy.'S');
1900                 $orderBy = str_replace('.', '_', $orderBy);
1901                 return array($orderBy,$desc);
1902         }
1903
1904     function processSortArrows($html_varName)
1905     {
1906
1907         $this->xTemplateAssign("arrow_start", $this->getArrowStart());
1908
1909         list($orderBy,$desc) = $this->getOrderByInfo($html_varName);
1910
1911                 $imgArrow = "_up";
1912                 if($desc) {
1913                         $imgArrow = "_down";
1914                 }
1915                 /**
1916                  * @deprecated only used by legacy opportunites listview, nothing current. Leaving for BC
1917                  */
1918                 if($orderBy == 'amount')
1919                 {
1920                         $this->xTemplateAssign('amount_arrow', $imgArrow);
1921                 }
1922                 else if($orderBy == 'amount_usdollar')
1923                 {
1924                         $this->xTemplateAssign('amount_usdollar_arrow', $imgArrow);
1925                 }
1926                 else
1927                 {
1928                         $this->xTemplateAssign($orderBy.'_arrow', $imgArrow);
1929                 }
1930
1931         $this->xTemplateAssign('arrow_end', $this->getArrowEnd());
1932     }
1933
1934     // this is where translation happens for dynamic list views
1935     function loadListFieldDefs(&$subpanel_fields,&$child_focus)
1936     {
1937         $this->list_field_defs = $subpanel_fields;
1938
1939         for($i=0;$i < count($this->list_field_defs);$i++)
1940         {
1941             $list_field = $this->list_field_defs[$i];
1942             $field_def = null;
1943             $key = '';
1944             if(!empty($list_field['vname']))
1945             {
1946                 $key = $list_field['vname'];
1947             } else if(isset($list_field['name']) &&  isset($child_focus->field_defs[$list_field['name']]))
1948             {
1949                     $field_def = $child_focus->field_defs[$list_field['name']];
1950                     $key = $field_def['vname'];
1951             }
1952             if(!empty($key))
1953             {
1954                 $list_field['label'] = translate($key,$child_focus->module_dir);
1955                 $this->list_field_defs[$i]['label'] = preg_replace('/:$/','',$list_field['label']);
1956             }
1957             else
1958             {
1959                 $this->list_field_defs[$i]['label'] ='&nbsp;';
1960             }
1961         }
1962     }
1963
1964     function unique_id() {
1965         return sugar_microtime();
1966     }
1967
1968      /**INTERNAL FUNCTION sets a session variable keeping it local to the listview
1969      not the current_module
1970      * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1971      * All Rights Reserved.
1972      * Contributor(s): ______________________________________.
1973      */
1974      function setLocalSessionVariable($localVarName,$varName, $value) {
1975         $_SESSION[$localVarName."_".$varName] = $value;
1976      }
1977
1978      /**INTERNAL FUNCTION returns a session variable that is local to the listview,
1979      not the current_module
1980      * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1981      * All Rights Reserved.
1982      * Contributor(s): ______________________________________.
1983      */
1984  function getLocalSessionVariable($localVarName,$varName) {
1985     if(isset($_SESSION[$localVarName."_".$varName])) {
1986         return $_SESSION[$localVarName."_".$varName];
1987     }
1988     else{
1989         return "";
1990     }
1991  }
1992
1993  /* Set to true if you want Additional Details to appear in the listview
1994   */
1995  function setAdditionalDetails($value = true, $function = '') {
1996     if(!empty($function)) $this->additionalDetailsFunction = $function;
1997     $this->_additionalDetails = $value;
1998  }
1999
2000 }
2001 ?>