]> CyberLeo.Net >> Repos - Github/sugarcrm.git/blob - include/ListView/ListView.php
Release 6.4.4
[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 nvigation 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 $odd_bg;
239     global $even_bg;
240     global $hilite_bg;
241     global $click_bg;
242
243     $this->xTemplate->assign("BG_HILITE", $hilite_bg);
244     $this->xTemplate->assign('CHECKALL', SugarThemeRegistry::current()->getImage('blank', '', 1, 1, ".gif", ''));
245     //$this->xTemplate->assign("BG_CLICK", $click_bg);
246     $oddRow = true;
247     $count = 0;
248     reset($data);
249
250     //GETTING OFFSET
251     $offset = $this->getOffset($html_varName);
252     //$totaltime = 0;
253     $processed_ids = array();
254
255     $fill_additional_fields = array();
256     //Either retrieve the is_fill_in_additional_fields property from the lone
257     //subpanel or visit each subpanel's subpanels to retreive the is_fill_in_addition_fields
258     //property
259     $subpanel_list=array();
260     if($subpanel_def->isCollection()) {
261         $subpanel_list=$subpanel_def->sub_subpanels;
262     } else {
263         $subpanel_list[]= $subpanel_def;
264     }
265
266     foreach($subpanel_list as $this_subpanel)
267     {
268         if($this_subpanel->is_fill_in_additional_fields())
269         {
270             $fill_additional_fields[] = $this_subpanel->bean_name;
271             $fill_additional_fields[$this_subpanel->bean_name] = true;
272         }
273     }
274
275     if ( empty($data) ) {
276         $this->xTemplate->assign("ROW_COLOR", 'oddListRow');
277         $thepanel=$subpanel_def;
278         if($subpanel_def->isCollection())
279             $thepanel=$subpanel_def->get_header_panel_def();
280         $this->xTemplate->assign("COL_COUNT", count($thepanel->get_list_fields()));
281         $this->xTemplate->parse($xtemplateSection.".nodata");
282     }
283
284     while(list($aVal, $aItem) = each($data))
285     {
286         $aItem->check_date_relationships_load();
287         // TODO: expensive and needs to be removed and done better elsewhere
288
289         if(!empty($fill_additional_fields[$aItem->object_name])
290         || ($aItem->object_name == 'Case' && !empty($fill_additional_fields['aCase']))
291         )
292         {
293             $aItem->fill_in_additional_list_fields();
294             //$aItem->fill_in_additional_detail_fields();
295         }
296         //rrs bug: 25343
297         $aItem->call_custom_logic("process_record");
298
299         if(isset($parent_data[$aItem->id])) {
300
301             $aItem->parent_name = $parent_data[$aItem->id]['parent_name'];
302             if(!empty($parent_data[$aItem->id]['parent_name_owner'])) {
303             $aItem->parent_name_owner =  $parent_data[$aItem->id]['parent_name_owner'];
304             $aItem->parent_name_mod =  $parent_data[$aItem->id]['parent_name_mod'];
305         }}
306
307         $fields = $aItem->get_list_view_data();
308         if(isset($processed_ids[$aItem->id])) {
309             continue;
310
311         } else {
312             $processed_ids[$aItem->id] = 1;
313         }
314
315
316         //ADD OFFSET TO ARRAY
317         $fields['OFFSET'] = ($offset + $count + 1);
318
319         if($this->shouldProcess) {
320             if($aItem->ACLAccess('EditView')) {
321             $this->xTemplate->assign('PREROW', "<input type='checkbox' class='checkbox' name='mass[]' value='". $fields['ID']. "' />");
322             } else {
323                 $this->xTemplate->assign('PREROW', '');
324
325             }
326             if($aItem->ACLAccess('DetailView')) {
327                 $this->xTemplate->assign('TAG_NAME','a');
328             } else {
329                 $this->xTemplate->assign('TAG_NAME','span');
330             }
331             $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);' />");
332         }
333
334         if($oddRow)
335         {
336             $ROW_COLOR = 'oddListRow';
337             $BG_COLOR =  $odd_bg;
338         }
339         else
340         {
341             $ROW_COLOR = 'evenListRow';
342             $BG_COLOR =  $even_bg;
343         }
344         $oddRow = !$oddRow;
345
346         $this->xTemplate->assign("ROW_COLOR", $ROW_COLOR);
347         $this->xTemplate->assign("BG_COLOR", $BG_COLOR);
348         $layout_manager = $this->getLayoutManager();
349         $layout_manager->setAttribute('context','List');
350         $layout_manager->setAttribute('image_path',$this->local_image_path);
351         $layout_manager->setAttribute('module_name', $subpanel_def->_instance_properties['module']);
352         if(!empty($this->child_focus))
353             $layout_manager->setAttribute('related_module_name',$this->child_focus->module_dir);
354
355         //AG$subpanel_data = $this->list_field_defs;
356         //$bla = array_pop($subpanel_data);
357         //select which sub-panel to display here, the decision will be made based on the type of
358         //the sub-panel and panel in the bean being processed.
359         if($subpanel_def->isCollection()) {
360             $thepanel=$subpanel_def->sub_subpanels[$aItem->panel_name];
361         } else {
362             $thepanel=$subpanel_def;
363         }
364         //get data source name
365         $linked_field=$thepanel->get_data_source_name();
366         $linked_field_set=$thepanel->get_data_source_name(true);
367         foreach($thepanel->get_list_fields() as $field_name=>$list_field)
368         {
369             //add linked field attribute to the array.
370             $list_field['linked_field']=$linked_field;
371             $list_field['linked_field_set']=$linked_field_set;
372
373             $usage = empty($list_field['usage']) ? '' : $list_field['usage'];
374             if($usage != 'query_only')
375             {
376                 $list_field['name']=$field_name;
377
378                 $module_field = $field_name.'_mod';
379                 $owner_field = $field_name.'_owner';
380                 if(!empty($aItem->$module_field)) {
381
382                     $list_field['owner_id'] = $aItem->$owner_field;
383                     $list_field['owner_module'] = $aItem->$module_field;
384
385                 } else {
386                     $list_field['owner_id'] = false;
387                     $list_field['owner_module'] = false;
388                 }
389                 if(isset($list_field['alias'])) $list_field['name'] = $list_field['alias'];
390                 else $list_field['name']=$field_name;
391                 $list_field['fields'] = $fields;
392                 $list_field['module'] = $aItem->module_dir;
393                 $list_field['start_link_wrapper'] = $this->start_link_wrapper;
394                 $list_field['end_link_wrapper'] = $this->end_link_wrapper;
395                 $list_field['subpanel_id'] = $this->subpanel_id;
396                 $list_field['DetailView'] = $aItem->ACLAccess('DetailView');
397                 $list_field['ListView'] = $aItem->ACLAccess('ListView');
398                 $list_field['EditView'] = $aItem->ACLAccess('EditView');
399                 $list_field['Delete'] = $aItem->ACLAccess('Delete');
400                 if ( isset($aItem->field_defs[strtolower($list_field['name'])])) {
401                     require_once('include/SugarFields/SugarFieldHandler.php');
402                     // We need to see if a sugar field exists for this field type first,
403                     // if it doesn't, toss it at the old sugarWidgets. This is for
404                     // backwards compatibilty and will be removed in a future release
405                     $vardef = $aItem->field_defs[strtolower($list_field['name'])];
406                     if ( isset($vardef['type']) ) {
407                         $fieldType = isset($vardef['custom_type'])?$vardef['custom_type']:$vardef['type'];
408                         $tmpField = SugarFieldHandler::getSugarField($fieldType,true);
409                     } else {
410                         $tmpField = NULL;
411                     }
412
413                     if ( $tmpField != NULL ) {
414                         $widget_contents = SugarFieldHandler::displaySmarty($list_field['fields'],$vardef,'ListView',$list_field);
415                     } else {
416                         // No SugarField for this particular type
417                         // Use the old, icky, SugarWidget for now
418                         $widget_contents = $layout_manager->widgetDisplay($list_field);
419                     }
420
421                     if ( isset($list_field['widget_class']) && $list_field['widget_class'] == 'SubPanelDetailViewLink' ) {
422                         // 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
423                         // So we'll populate the field data with the pre-rendered display for the field
424                         $list_field['fields'][$field_name] = $widget_contents;
425                         if('full_name' == $field_name){//bug #32465
426                            $list_field['fields'][strtoupper($field_name)] = $widget_contents;
427                         }
428                         $widget_contents = $layout_manager->widgetDisplay($list_field);
429                     } else if(isset($list_field['widget_class']) && $list_field['widget_class'] == 'SubPanelEmailLink' ) {
430                         $widget_contents = $layout_manager->widgetDisplay($list_field);
431                     }
432                 } else {
433                     // This handles the edit and remove buttons
434                 $widget_contents = $layout_manager->widgetDisplay($list_field);
435                 }
436                 static $count;
437                 if(!isset($count))$count = 0; else $count++;
438                 $this->xTemplate->assign('CELL_COUNT', $count);
439                 if ( empty($widget_contents) ) $widget_contents = '&nbsp;';
440                 $this->xTemplate->assign('CELL', $widget_contents);
441                 $this->xTemplate->parse($xtemplateSection.".row.cell");
442             }
443         }
444
445         $aItem->setupCustomFields($aItem->module_dir);
446         $aItem->custom_fields->populateAllXTPL($this->xTemplate, 'detail', $html_varName, $fields);
447
448         $count++;
449
450         $this->xTemplate->parse($xtemplateSection.".row");
451     }
452
453     $this->xTemplate->parse($xtemplateSection);
454 }
455
456 /**sets whether or not to display the xtemplate header and footer
457  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
458  * All Rights Reserved.
459  * Contributor(s): ______________________________________.
460 */
461 function setDisplayHeaderAndFooter($bool) {
462         $this->display_header_and_footer = $bool;
463 }
464
465 /**initializes ListView
466  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
467  * All Rights Reserved.
468  * Contributor(s): ______________________________________.
469 */
470  function ListView() {
471
472
473     if(!$this->initialized) {
474         global $sugar_config;
475         $this->records_per_page = $sugar_config['list_max_entries_per_page'] + 0;
476         $this->initialized = true;
477         global $app_strings, $currentModule;
478         $this->local_theme = SugarThemeRegistry::current()->__toString();
479         $this->local_app_strings =$app_strings;
480         $this->local_image_path = SugarThemeRegistry::current()->getImagePath();
481         $this->local_current_module = $currentModule;
482     }
483 }
484 /**sets how many records should be displayed per page in the list view
485  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
486  * All Rights Reserved.
487  * Contributor(s): ______________________________________.
488 */
489  function setRecordsPerPage($count) {
490     $this->records_per_page = $count;
491 }
492 /**sets the header title */
493  function setHeaderTitle($value) {
494     $this->header_title = $value;
495 }
496 /**sets the header text this is text thats appended to the header table and is usually used for the creation of buttons
497  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
498  * All Rights Reserved.
499  * Contributor(s): ______________________________________.
500 */
501  function setHeaderText($value) {
502     $this->header_text = $value;
503 }
504 /**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
505  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
506  * All Rights Reserved.
507  * Contributor(s): ______________________________________.
508 */
509  function setXTemplatePath($value) {
510     $this->xTemplatePath= $value;
511 }
512
513 /**this is a helper function for allowing ListView to create a new XTemplate it groups parameters that should be set into a single function
514  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
515  * All Rights Reserved.
516  * Contributor(s): ______________________________________.
517 */
518  function initNewXTemplate($XTemplatePath, $modString, $imagePath = null) {
519     $this->setXTemplatePath($XTemplatePath);
520     if(isset($modString))
521         $this->setModStrings($modString);
522     if(isset($imagePath))
523         $this->setImagePath($imagePath);
524 }
525
526
527 function getOrderBy($varName, $defaultOrderBy='', $force_sortorder='') {
528     $sortBy = $this->getSessionVariable($varName, "ORDER_BY") ;
529
530     $orderByDirection = $this->getSessionVariableName($varName, "order_by_direction");
531     $orderByColumn = $this->getSessionVariableName($varName, "ORDER_BY");
532     $lastEqualsSortBy = false;
533     $defaultOrder = false; //ascending
534     
535     if(empty($sortBy)) {
536         $this->setUserVariable($varName, "ORDER_BY", $defaultOrderBy);
537         $sortBy = $defaultOrderBy;
538     } else {
539         $this->setUserVariable($varName, "ORDER_BY", $sortBy);
540     }
541     if($sortBy == 'amount') {
542         $sortBy = 'amount*1';
543     }
544     if($sortBy == 'amount_usdollar') {
545         $sortBy = 'amount_usdollar*1';
546     }
547
548     $desc = $this->getSessionVariable($varName, $sortBy."S");
549
550     if (empty($desc))
551         {
552             $desc = $defaultOrder;
553         }
554         $defaultOrder = $desc ? 'desc' : 'asc';
555         $orderByValue = $defaultOrder;
556         if (isset($_REQUEST[$orderByDirection]))
557         {
558             $possibleRequestOrderBy = $_REQUEST[$orderByDirection];
559             if ($possibleRequestOrderBy == 'asc' || $possibleRequestOrderBy == 'desc')
560             {
561                 $orderByValue = $possibleRequestOrderBy;
562             }
563         }
564
565         if (isset($_REQUEST[$orderByColumn]))
566         {
567             $last = $this->getSessionVariable($varName, "OBL");
568         }
569         if (!empty($last) && $last == $sortBy)
570         {
571             $lastEqualsSortBy = true;
572         } else
573         {
574             $orderByValue = $defaultOrder;
575             $this->setSessionVariable($varName, "OBL", $sortBy);
576         }
577         $desc = $orderByValue == 'desc';
578         $orderByDirectionValue = false;
579         $this->setSessionVariable($varName, $sortBy . "S", $desc);
580         if (!empty($sortBy))
581         {
582             if (empty($force_sortorder))
583             {
584                 if (substr_count(strtolower($sortBy), ' desc') == 0 && substr_count(strtolower($sortBy), ' asc') == 0)
585                 {
586                     if ($sortBy)
587                     {
588                         $orderByDirectionValue = $desc ? 'asc' : 'desc';
589                     }
590                     $this->query_orderby = $sortBy . ' ' . $orderByValue;
591                 }
592             } else
593             {
594                 $this->query_orderby = $sortBy . ' ' . $force_sortorder;
595             }
596             if (!isset($this->appendToBaseUrl))
597             {
598                 $this->appendToBaseUrl = array();
599             }
600             if ($orderByDirectionValue)
601             {
602                 $this->appendToBaseUrl[$orderByDirection] = $orderByDirectionValue;
603             }
604             $offsetVar = $this->getSessionVariableName($varName, "offset");
605             if (isset($_REQUEST[$offsetVar]))
606             {
607                 $this->appendToBaseUrl[$offsetVar] = $_REQUEST[$offsetVar];
608             }
609             //Just clear from url...
610             $this->appendToBaseUrl[$orderByColumn] = false;
611     }else {
612         $this->query_orderby = "";
613     }
614     $this->sortby = $sortBy;
615     return $this->query_orderby;
616
617 }
618
619
620 /**sets the parameters dealing with the db
621  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
622  * All Rights Reserved.
623  * Contributor(s): ______________________________________.
624 */
625  function setQuery($where, $limit, $orderBy, $varName, $allowOrderByOveride=true) {
626     $this->query_where = $where;
627     if($this->getSessionVariable("query", "where") != $where) {
628         $this->query_where_has_changed = true;
629         $this->setSessionVariable("query", "where", $where);
630     }
631
632     $this->query_limit = $limit;
633     if(!$allowOrderByOveride) {
634         $this->query_orderby = $orderBy;
635         return;
636     }
637     $this->getOrderBy($varName, $orderBy);
638
639     $this->setLocalSessionVariable($varName, "QUERY_WHERE", $where);
640
641     //SETTING ORDER_BY FOR USE IN DETAILVIEW
642     $this->setLocalSessionVariable($varName, "ORDER_BY_DETAIL", $this->query_orderby);
643 }
644
645 function displayArrow() {
646
647 }
648
649 /**sets the theme used only use if it is different from the global
650  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
651  * All Rights Reserved.
652  * Contributor(s): ______________________________________.
653 */
654  function setTheme($theme) {
655     $this->local_theme = $theme;
656     if(isset($this->xTemplate))$this->xTemplate->assign("THEME", $this->local_theme);
657 }
658
659 /**sets the AppStrings used only use if it is different from the global
660  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
661  * All Rights Reserved.
662  * Contributor(s): ______________________________________.
663 */
664  function setAppStrings($app_strings) {
665     unset($this->local_app_strings);
666     $this->local_app_strings = $app_strings;
667     if(isset($this->xTemplate))$this->xTemplate->assign("APP", $this->local_app_strings);
668 }
669
670 /**sets the ModStrings used
671  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
672  * All Rights Reserved.
673  * Contributor(s): ______________________________________.
674 */
675  function setModStrings($mod_strings) {
676     unset($this->local_module_strings);
677     $this->local_mod_strings = $mod_strings;
678     if(isset($this->xTemplate))$this->xTemplate->assign("MOD", $this->local_mod_strings);
679 }
680
681 /**sets the ImagePath used
682  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
683  * All Rights Reserved.
684  * Contributor(s): ______________________________________.
685 */
686  function setImagePath($image_path) {
687     $this->local_image_path = $image_path;
688     if(empty($this->local_image_path)) {
689         $this->local_image_path = SugarThemeRegistry::get($this->local_theme)->getImagePath();
690     }
691     if(isset($this->xTemplate))$this->xTemplate->assign("IMAGE_PATH", $this->local_image_path);
692 }
693
694 /**sets the currentModule only use if this is different from the global
695  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
696  * All Rights Reserved.
697  * Contributor(s): ______________________________________.
698 */
699  function setCurrentModule($currentModule) {
700     unset($this->local_current_module);
701     $this->local_current_module = $currentModule;
702     if(isset($this->xTemplate))$this->xTemplate->assign("MODULE_NAME", $this->local_current_module);
703 }
704
705 /**INTERNAL FUNCTION creates an XTemplate DO NOT CALL THIS THIS IS AN INTERNAL FUNCTION
706  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
707  * All Rights Reserved.
708  * Contributor(s): ______________________________________.
709 */
710  function createXTemplate() {
711     if(!isset($this->xTemplate)) {
712         if(isset($this->xTemplatePath)) {
713
714             $this->xTemplate = new XTemplate($this->xTemplatePath);
715             $this->xTemplate->assign("APP", $this->local_app_strings);
716             if(isset($this->local_mod_strings))$this->xTemplate->assign("MOD", $this->local_mod_strings);
717             $this->xTemplate->assign("THEME", $this->local_theme);
718             $this->xTemplate->assign("IMAGE_PATH", $this->local_image_path);
719             $this->xTemplate->assign("MODULE_NAME", $this->local_current_module);
720         } else {
721             $GLOBALS['log']->error("NO XTEMPLATEPATH DEFINED CANNOT CREATE XTEMPLATE");
722         }
723     }
724 }
725
726 /**sets the XTemplate telling ListView to use newXTemplate as its current XTemplate
727  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
728  * All Rights Reserved.
729  * Contributor(s): ______________________________________.
730 */
731  function setXTemplate($newXTemplate) {
732     $this->xTemplate = $newXTemplate;
733 }
734
735 /**returns the XTemplate
736  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
737  * All Rights Reserved.
738  * Contributor(s): ______________________________________.
739 */
740  function getXTemplate() {
741     return $this->xTemplate;
742 }
743
744 /**assigns a name value pair to the XTemplate
745  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
746  * All Rights Reserved.
747  * Contributor(s): ______________________________________.
748 */
749  function xTemplateAssign($name, $value) {
750
751         if(!isset($this->xTemplate)) {
752             $this->createXTemplate();
753         }
754         $this->xTemplate->assign($name, $value);
755
756 }
757
758 /**INTERNAL FUNCTION returns the offset first checking the querey then checking the session if the where clause has changed from the last time it returns 0
759  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
760  * All Rights Reserved.
761  * Contributor(s): ______________________________________.
762 */
763  function getOffset($localVarName) {
764         if($this->query_where_has_changed || isset($GLOBALS['record_has_changed'])) {
765                 $this->setSessionVariable($localVarName,"offset", 0);
766         }
767         $offset = $this->getSessionVariable($localVarName,"offset");
768         if(isset($offset)) {
769                 return $offset;
770         }
771         return 0;
772 }
773
774 /**INTERNAL FUNCTION sets the offset in the session
775  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
776  * All Rights Reserved.
777  * Contributor(s): ______________________________________.
778 */
779  function setOffset($localVarName, $value) {
780         $this->setSessionVariable($localVarName, "offset", $value);
781 }
782
783 /**INTERNAL FUNCTION sets a session variable
784  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
785  * All Rights Reserved.
786  * Contributor(s): ______________________________________.
787 */
788  function setSessionVariable($localVarName,$varName, $value) {
789     $_SESSION[$this->local_current_module."_".$localVarName."_".$varName] = $value;
790 }
791
792 function setUserVariable($localVarName,$varName, $value) {
793         if($this->is_dynamic ||  $localVarName == 'CELL')return;
794         global $current_user;
795         $current_user->setPreference($this->local_current_module."_".$localVarName."_".$varName, $value);
796 }
797
798 /**INTERNAL FUNCTION returns a session variable first checking the querey for it then checking the session
799  * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
800  * All Rights Reserved.
801  * Contributor(s): ______________________________________.
802 */
803  function getSessionVariable($localVarName,$varName) {
804     //Set any variables pass in through request first
805     if(isset($_REQUEST[$this->getSessionVariableName($localVarName, $varName)])) {
806         $this->setSessionVariable($localVarName,$varName,$_REQUEST[$this->getSessionVariableName($localVarName, $varName)]);
807     }
808
809     if(isset($_SESSION[$this->getSessionVariableName($localVarName, $varName)])) {
810         return $_SESSION[$this->getSessionVariableName($localVarName, $varName)];
811     }
812     return "";
813 }
814
815 function getUserVariable($localVarName, $varName) {
816     global $current_user;
817     if($this->is_dynamic ||  $localVarName == 'CELL')return;
818     if(isset($_REQUEST[$this->getSessionVariableName($localVarName, $varName)])) {
819
820             $this->setUserVariable($localVarName,$varName,$_REQUEST[$this->getSessionVariableName($localVarName, $varName)]);
821     }
822     return $current_user->getPreference($this->getSessionVariableName($localVarName, $varName));
823 }
824
825
826     /**
827      * helper method to determine sort order by priority of source
828      * 1. explicit in request object
829      * 2. in session variable
830      * 3. subpaneldefs metadata
831      * 4. default 'asc'
832      * @param array $sortOrderList - contains options
833      * @return string 'asc' | 'desc'
834      */
835     function calculateSortOrder($sortOrderList)
836     {
837         $priority_map = array(
838           'request',
839           'session',
840           'subpaneldefs',
841           'default',
842         );
843
844         foreach($priority_map as $p) {
845             if (key_exists($p, $sortOrderList)) {
846                 $order = strtolower($sortOrderList[$p]);
847                 if (in_array($order, array('asc', 'desc'))) {
848                     return $order;
849                 }
850             }
851         }
852         return 'asc';
853     }
854
855
856     /**
857
858     * @return void
859     * @param unknown $localVarName
860     * @param unknown $varName
861     * @desc INTERNAL FUNCTION returns the session/query variable name
862     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
863     * All Rights Reserved.
864     * Contributor(s): ______________________________________..
865     */
866     function getSessionVariableName($localVarName,$varName) {
867         return $this->local_current_module."_".$localVarName."_".$varName;
868     }
869
870     /**
871
872     * @return void
873     * @param unknown $seed
874     * @param unknown $xTemplateSection
875     * @param unknown $html_varName
876     * @desc INTERNAL FUNCTION Handles List Views using seeds that extend SugarBean
877         $XTemplateSection is the section in the XTemplate file that should be parsed usually main
878         $html_VarName is the variable name used in the XTemplateFile e.g. TASK
879         $seed is a seed that extends SugarBean
880         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
881         * All Rights Reserved..
882         * Contributor(s): ______________________________________..
883     */
884     function processSugarBean($xtemplateSection, $html_varName, $seed) {
885         global $list_view_row_count;
886
887         $current_offset = $this->getOffset($html_varName);
888         $response = array();
889
890         //ADDING VCR CONTROL
891         SugarVCR::erase($seed->module_dir);
892         $params = array();
893         //$filter = array('id', 'full_name');
894         $filter=array();
895         $ret_array = $seed->create_new_list_query($this->query_orderby, $this->query_where, $filter, $params, 0, '', true, $seed, true);
896         if(!is_array($params)) $params = array();
897         if(!isset($params['custom_select'])) $params['custom_select'] = '';
898         if(!isset($params['custom_from'])) $params['custom_from'] = '';
899         if(!isset($params['custom_where'])) $params['custom_where'] = '';
900         if(!isset($params['custom_order_by'])) $params['custom_order_by'] = '';
901         $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'];
902         SugarVCR::store($seed->module_dir,  $main_query);
903         //ADDING VCR CONTROL
904
905         if(empty($this->related_field_name)) {
906             $response = $seed->get_list($this->query_orderby, $this->query_where, $current_offset, $this->query_limit);
907         } else {
908             $related_field_name = $this->related_field_name;
909             $response = $seed->get_related_list($this->child_focus,$related_field_name, $this->query_orderby,
910             $this->query_where, $current_offset, $this->query_limit);
911         }
912
913         $list = $response['list'];
914         $row_count = $response['row_count'];
915         $next_offset = $response['next_offset'];
916         $previous_offset = $response['previous_offset'];
917
918         if(!empty($response['current_offset'])) {
919             $current_offset = $response['current_offset'];
920         }
921
922         $list_view_row_count = $row_count;
923         $this->processListNavigation($xtemplateSection,$html_varName, $current_offset, $next_offset, $previous_offset, $row_count, null, null, empty($seed->column_fields) ? null : count($seed->column_fields));
924
925         return $list;
926     }
927
928
929
930     function processUnionBeans($sugarbean, $subpanel_def, $html_var = 'CELL') {
931
932                 $last_detailview_record = $this->getSessionVariable("detailview", "record");
933                 if(!empty($last_detailview_record) && $last_detailview_record != $sugarbean->id){
934                         $GLOBALS['record_has_changed'] = true;
935                 }
936                 $this->setSessionVariable("detailview", "record", $sugarbean->id);
937
938                 $current_offset = $this->getOffset($html_var);
939                 $module = isset($_REQUEST['module']) ? $_REQUEST['module'] : '';
940                 $response = array();
941
942         // choose sort order
943         $sort_order = array();
944         $sort_order['default'] = 'asc';
945
946         // explicit request parameter gets priority over all
947         $sort_order['request'] = isset($_REQUEST['sort_order']) ? $_REQUEST['sort_order'] : null;
948
949         // see if the session data has a sort order
950         if (isset($_SESSION['last_sub' . $this->subpanel_module . '_order']))
951         {
952             $sort_order['session'] = $_SESSION['last_sub' . $this->subpanel_module . '_order'];
953
954             // We swap the order when the request contains an offset (indicating a column sort issued);
955             // otherwise we do not sort.  If we don't make this check, then the subpanel listview will
956             // swap ordering each time a new record is entered via quick create forms
957             if (isset($_REQUEST[$module . '_' . $html_var . '_offset']))
958             {
959                 $sort_order['session'] = $sort_order['session'] == 'asc' ? 'desc' : 'asc';
960             }
961         }
962         else
963         {
964             $sort_order['session'] = null;
965         }
966
967         // does the metadata have a default sort order?
968         $sort_order['subpaneldefs'] = isset($subpanel_def->_instance_properties['sort_order']) ?
969             $subpanel_def->_instance_properties['sort_order'] : null;
970
971         $this->sort_order = $this->calculateSortOrder($sort_order);
972
973
974         if (isset($subpanel_def->_instance_properties['sort_by'])) {
975             $this->query_orderby = $subpanel_def->_instance_properties['sort_by'];
976         } else {
977             $this->query_orderby = 'id';
978         }
979
980         $this->getOrderBy($html_var,$this->query_orderby, $this->sort_order);
981
982         $_SESSION['last_sub' .$this->subpanel_module. '_order'] = $this->sort_order;
983         $_SESSION['last_sub' .$this->subpanel_module. '_url'] = $this->getBaseURL($html_var);
984
985                 // Bug 8139 - Correct Subpanel sorting on 'name', when subpanel sorting default is 'last_name, first_name'
986                 if (($this->sortby == 'name' || $this->sortby == 'last_name') &&
987                         str_replace(' ', '', trim($subpanel_def->_instance_properties['sort_by'])) == 'last_name,first_name') {
988                         $this->sortby = 'last_name '.$this->sort_order.', first_name ';
989                 }
990
991         if(!empty($this->response)){
992             $response =& $this->response;
993             echo 'cached';
994         }else{
995             $response = SugarBean::get_union_related_list($sugarbean,$this->sortby, $this->sort_order, $this->query_where, $current_offset, -1,-1,$this->query_limit,$subpanel_def);
996             $this->response =& $response;
997         }
998         $list = $response['list'];
999         $row_count = $response['row_count'];
1000         $next_offset = $response['next_offset'];
1001         $previous_offset = $response['previous_offset'];
1002         if(!empty($response['current_offset']))$current_offset = $response['current_offset'];
1003         global $list_view_row_count;
1004         $list_view_row_count = $row_count;
1005         $this->processListNavigation('dyn_list_view', $html_var, $current_offset, $next_offset, $previous_offset, $row_count, $sugarbean,$subpanel_def);
1006
1007         return array('list'=>$list, 'parent_data'=>$response['parent_data'], 'query'=>$response['query']);
1008     }
1009
1010     function getBaseURL($html_varName) {
1011         static $cache = array();
1012
1013         if(!empty($cache[$html_varName]))return $cache[$html_varName];
1014         $blockVariables = array('mass', 'uid', 'massupdate', 'delete', 'merge', 'selectCount','current_query_by_page');
1015         if(!empty($this->base_URL)) {
1016             return $this->base_URL;
1017         }
1018
1019             $baseurl = $_SERVER['PHP_SELF'];
1020             if(empty($baseurl)) {
1021                 $baseurl = 'index.php';
1022             }
1023
1024             /*fixes an issue with deletes when doing a search*/
1025             foreach(array_merge($_GET, $_POST) as $name=>$value) {
1026                 //echo ("$name = $value <br/>");
1027                 if(!empty($value) && $name != 'sort_order' //&& $name != ListView::getSessionVariableName($html_varName,"ORDER_BY")
1028                         && $name != ListView::getSessionVariableName($html_varName,"offset")
1029                         /*&& substr_count($name, "ORDER_BY")==0*/ && !in_array($name, $blockVariables))
1030                 {
1031                     if(is_array($value)) {
1032                         foreach($value as $valuename=>$valuevalue) {
1033                             if(substr_count($baseurl, '?') > 0)
1034                                 $baseurl        .= "&{$name}[]=".$valuevalue;
1035                             else
1036                                 $baseurl        .= "?{$name}[]=".$valuevalue;
1037                         }
1038                     } else {
1039                         $value = urlencode($value);
1040                         if(substr_count($baseurl, '?') > 0) {
1041                             $baseurl    .= "&$name=$value";
1042                         } else {
1043                             $baseurl    .= "?$name=$value";
1044                         }
1045                     }
1046                 }
1047             }
1048
1049
1050             if($_SERVER['REQUEST_METHOD'] == 'POST') {
1051                 // at this point it is possible that the above foreach already executed resulting in double ?'s in the url
1052                 if(substr_count($baseurl, '?') == 0) {
1053                     $baseurl .= '?';
1054                 }
1055                 if(isset($_REQUEST['action'])) $baseurl.= '&action='.$_REQUEST['action'];
1056                 if(isset($_REQUEST['record'])) $baseurl .= '&record='.$_REQUEST['record'];
1057                 if(isset($_REQUEST['module'])) $baseurl .= '&module='.$_REQUEST['module'];
1058             }
1059
1060             $baseurl .= "&".ListView::getSessionVariableName($html_varName,"offset")."=";
1061             $cache[$html_varName] = $baseurl;
1062             return $baseurl;
1063     }
1064     /**
1065     * @return void
1066     * @param unknown $data
1067     * @param unknown $xTemplateSection
1068     * @param unknown $html_varName
1069     * @desc INTERNAL FUNCTION process the List Navigation
1070     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1071     * All Rights Reserved.
1072     * Contributor(s): ______________________________________..
1073     */
1074     function processListNavigation($xtemplateSection, $html_varName, $current_offset, $next_offset, $previous_offset, $row_count, $sugarbean=null, $subpanel_def=null, $col_count = 20) {
1075
1076         global $export_module;
1077         global $sugar_config;
1078         global $current_user;
1079         global $currentModule;
1080         global $app_strings;
1081
1082         $start_record = $current_offset + 1;
1083
1084         if(!is_numeric($col_count))
1085             $col_count = 20;
1086
1087         if($row_count == 0)
1088             $start_record = 0;
1089
1090         $end_record = $start_record + $this->records_per_page;
1091         // back up the the last page.
1092         if($end_record > $row_count+1) {
1093             $end_record = $row_count+1;
1094         }
1095         // Deterime the start location of the last page
1096         if($row_count == 0)
1097             $number_pages = 0;
1098         else
1099             $number_pages = floor(($row_count - 1) / $this->records_per_page);
1100
1101         $last_offset = $number_pages * $this->records_per_page;
1102
1103         if(empty($this->query_limit)  || $this->query_limit > $this->records_per_page) {
1104             $this->base_URL = $this->getBaseURL($html_varName);
1105             $dynamic_url = '';
1106
1107             if($this->is_dynamic) {
1108                 $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;
1109             }
1110
1111             $current_URL = htmlentities($this->base_URL.$current_offset.$dynamic_url);
1112             $start_URL = htmlentities($this->base_URL."0".$dynamic_url);
1113             $previous_URL  = htmlentities($this->base_URL.$previous_offset.$dynamic_url);
1114             $next_URL  = htmlentities($this->base_URL.$next_offset.$dynamic_url);
1115             $end_URL  = htmlentities($this->base_URL.'end'.$dynamic_url);
1116
1117             if(!empty($this->start_link_wrapper)) {
1118                 $current_URL = $this->start_link_wrapper.$current_URL.$this->end_link_wrapper;
1119                 $start_URL = $this->start_link_wrapper.$start_URL.$this->end_link_wrapper;
1120                 $previous_URL = $this->start_link_wrapper.$previous_URL.$this->end_link_wrapper;
1121                 $next_URL = $this->start_link_wrapper.$next_URL.$this->end_link_wrapper;
1122                 $end_URL = $this->start_link_wrapper.$end_URL.$this->end_link_wrapper;
1123             }
1124
1125             $moduleString = "{$currentModule}_{$html_varName}_offset";
1126             $moduleStringOrder = "{$currentModule}_{$html_varName}_ORDER_BY";
1127             if($this->shouldProcess && !$this->multi_select_popup) {
1128                 // check the checkboxes onload
1129                 echo "<script>YAHOO.util.Event.addListener(window, \"load\", sListView.check_boxes);</script>\n";
1130
1131                 $massUpdateRun = isset($_REQUEST['massupdate']) && $_REQUEST['massupdate'] == 'true';
1132                 $uids = empty($_REQUEST['uid']) || $massUpdateRun ? '' : $_REQUEST['uid'];
1133                 $select_entire_list = isset($_REQUEST['select_entire_list']) && !$massUpdateRun ? $_REQUEST['select_entire_list'] : 0;
1134
1135                 echo "<textarea style='display: none' name='uid'>{$uids}</textarea>\n" .
1136                     "<input type='hidden' name='select_entire_list' value='{$select_entire_list}'>\n".
1137                     "<input type='hidden' name='{$moduleString}' value='0'>\n".
1138                     "<input type='hidden' name='{$moduleStringOrder}' value='0'>\n";
1139
1140             }
1141
1142
1143             $GLOBALS['log']->debug("Offsets: (start, previous, next, last)(0, $previous_offset, $next_offset, $last_offset)");
1144
1145             if(0 == $current_offset) {
1146                 $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>";
1147                 $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>";
1148             } else {
1149                 if($this->multi_select_popup) {// nav links for multiselect popup, submit form to save checks.
1150                     $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>";
1151                     $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>";
1152                 } elseif($this->shouldProcess) {
1153                     $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>";
1154                     $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>";
1155                 } else {
1156                     $onClick = '';
1157                     if(0 != preg_match('/javascript.*/', $start_URL)){
1158                         $onClick = "\"$start_URL;\"";
1159                     }else{
1160                         $onClick ="'location.href=\"$start_URL\";'";
1161                     }
1162                     $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>";
1163
1164                     $onClick = '';
1165                     if(0 != preg_match('/javascript.*/', $previous_URL)){
1166                         $onClick = "\"$previous_URL;\"";
1167                     }else{
1168                         $onClick = "'location.href=\"$previous_URL\";'";
1169                     }
1170                     $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>";
1171                 }
1172             }
1173
1174             if($last_offset <= $current_offset) {
1175                 $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>";
1176                 $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>";
1177             } else {
1178                 if($this->multi_select_popup) { // nav links for multiselect popup, submit form to save checks.
1179                     $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>";
1180                     if(!empty($sugar_config['disable_count_query'])) {
1181                         $end_link = '';
1182                     }
1183                     $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>";
1184                 } elseif($this->shouldProcess) {
1185                     $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>";
1186                     $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>";
1187                 } else {
1188                     $onClick = '';
1189                     if(0 != preg_match('/javascript.*/', $next_URL)){
1190                         $onClick = "\"$next_URL;\"";
1191                     }else{
1192                         $onClick ="'location.href=\"$next_URL\";'";
1193                     }
1194                     $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>";
1195
1196                     $onClick = '';
1197                     if(0 != preg_match('/javascript.*/', $end_URL)){
1198                         $onClick = "\"$end_URL;\"";
1199                     }else{
1200                         $onClick = "'location.href=\"$end_URL\";'";
1201                     }
1202                     $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>";
1203
1204                 }
1205             }
1206
1207             $GLOBALS['log']->info("Offset (next, current, prev)($next_offset, $current_offset, $previous_offset)");
1208             $GLOBALS['log']->info("Start/end records ($start_record, $end_record)");
1209
1210             $end_record = $end_record-1;
1211
1212 $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>"
1213  . "<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>"
1214  . "<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>";
1215
1216 $close_inline_img = SugarThemeRegistry::current()->getImage('close_inline', 'border=0', null, null, ".gif", $app_strings['LBL_CLOSEINLINE']);
1217
1218             echo "<script>
1219                 function select_overlib() {
1220                     return overlib('<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>"
1221                 . "<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>"
1222                 . "<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>"
1223                 . "', CENTER, '"
1224                 . "', STICKY, MOUSEOFF, 3000, CLOSETEXT, '" . SugarThemeRegistry::current()->getImage('close_inline', 'border=0', null, null, ".gif", $this->local_app_strings['LBL_CLOSEINLINE']) . "'"
1225                 . ", WIDTH, 150, CLOSETITLE, '" . $this->local_app_strings['LBL_ADDITIONAL_DETAILS_CLOSE_TITLE'] . "', CLOSECLICK, FGCLASS, 'olOptionsFgClass', "
1226                 . "CGCLASS, 'olOptionsCgClass', BGCLASS, 'olBgClass', TEXTFONTCLASS, 'olFontClass', CAPTIONFONTCLASS, 'olOptionsCapFontClass', CLOSEFONTCLASS, 'olOptionsCloseFontClass');
1227                 }
1228                 </script>";
1229
1230             if($this->show_select_menu)
1231             {
1232                 $select_link = "<a id='select_link' onclick='return select_overlib();' 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>";
1233             } else {
1234                 $select_link = "&nbsp;";
1235             }
1236
1237             // put overlib strings into functions to avoid backslash plague!
1238             $export_link = '<input class="button" type="button" value="'.$this->local_app_strings['LBL_EXPORT'].'" ' .
1239                     'onclick="return sListView.send_form(true, \''.$_REQUEST['module'].'\', \'index.php?entryPoint=export\',\''.$this->local_app_strings['LBL_LISTVIEW_NO_SELECTED'].'\')">';
1240
1241             if($this->show_delete_button) {
1242                 $delete_link = '<input class="button" type="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)">';
1243             } else {
1244                 $delete_link = '&nbsp;';
1245             }
1246
1247             $admin = new Administration();
1248             $admin->retrieveSettings('system');
1249
1250             $user_merge = $current_user->getPreference('mailmerge_on');
1251             if($user_merge == 'on' && isset($admin->settings['system_mailmerge_on']) && $admin->settings['system_mailmerge_on']) {
1252                 echo "<script>
1253                 function mailmerge_overlib() {
1254                     return overlib('<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>"
1255                         . "<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>"
1256                         . "<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>"
1257                         . "', CAPTION, '" . $this->local_app_strings['LBL_MAILMERGE']
1258                         . "', STICKY, MOUSEOFF, 3000, CLOSETEXT, '<!--not_in_theme!--><img border=0 style=\'margin-left:2px; margin-right: 2px;\' src=" . $this->local_image_path
1259                         . "close.gif>', WIDTH, 150, CLOSETITLE, '" . $this->local_app_strings['LBL_ADDITIONAL_DETAILS_CLOSE_TITLE'] . "', CLOSECLICK, FGCLASS, 'olOptionsFgClass', "
1260                         . "CGCLASS, 'olOptionsCgClass', BGCLASS, 'olBgClass', TEXTFONTCLASS, 'olFontClass', CAPTIONFONTCLASS, 'olOptionsCapFontClass', CLOSEFONTCLASS, 'olCloseFontClass');
1261                 }
1262             </script>";
1263                 $merge_link = "&nbsp;|&nbsp;<a id='mailmerge_link' onclick='return mailmerge_overlib()'; href=\"javascript:void(0)\">".$this->local_app_strings['LBL_MAILMERGE']."</a>";
1264             } else {
1265                 $merge_link = "&nbsp;";
1266             }
1267
1268             $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='" . (!empty($select_entire_list) ? $row_count : 0) . "' />";
1269
1270             if($_REQUEST['module'] == 'Home' || $this->local_current_module == 'Import'
1271                 || $this->show_export_button == false
1272                 || (!empty($sugar_config['disable_export']))
1273                 || (!empty($sugar_config['admin_export_only'])
1274                 && !(
1275                         is_admin($current_user)
1276                         || (ACLController::moduleSupportsACL($_REQUEST['module'])
1277                             && ACLAction::getUserAccessLevel($current_user->id,$_REQUEST['module'], 'access') == ACL_ALLOW_ENABLED
1278                             && (ACLAction::getUserAccessLevel($current_user->id, $_REQUEST['module'], 'admin') == ACL_ALLOW_ADMIN ||
1279                                 ACLAction::getUserAccessLevel($current_user->id, $_REQUEST['module'], 'admin') == ACL_ALLOW_ADMIN_DEV)))))
1280             {
1281                 if ($_REQUEST['module'] != 'InboundEmail' && $_REQUEST['module'] != 'EmailMan' && $_REQUEST['module'] != 'iFrames') {
1282                     $selected_objects_span = '';
1283                 }
1284                 $export_link = "&nbsp;";
1285                 $merge_link = "&nbsp;";
1286             } elseif($_REQUEST['module'] != "Accounts" && $_REQUEST['module'] != "Cases" && $_REQUEST['module'] != "Contacts" && $_REQUEST['module'] != "Leads" && $_REQUEST['module'] != "Opportunities") {
1287                 $merge_link = "&nbsp;";
1288             }
1289
1290             if($this->show_paging == true) {
1291                 if(!empty($sugar_config['disable_count_query'])) {
1292                     if($row_count > $end_record) {
1293                         $row_count .= '+';
1294                     }
1295                 }
1296
1297                 $html_text = '';
1298                 $html_text .= "<tr class='pagination' role='presentation'>\n";
1299                 $html_text .= "<td COLSPAN=\"$col_count\" align=\"right\">\n";
1300                 //$html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  >$export_link$merge_link$selected_objects_span</td>\n";
1301                 //$html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  >";
1302                 if ($subpanel_def != null) {
1303                     include_once('include/SubPanel/SubPanelTiles.php');
1304                     $subpanelTiles = new SubPanelTiles($sugarbean);
1305                     $html_text .= "<table border=\"0\" cellpadding=\"0\" cellspacing=\"0\" width=\"100%\"><tr><td align=\"left\"  >";
1306
1307                     //attempt to get the query to recreate this subpanel
1308                     if(!empty($this->response)){
1309                         $response =& $this->response;
1310                     }else{
1311                         $response = SugarBean::get_union_related_list($sugarbean,$this->sortby, $this->sort_order, $this->query_where, $current_offset, -1,-1,$this->query_limit,$subpanel_def);
1312                         $this->response = $response;
1313                     }
1314                     //if query is present, then pass it in as parameter
1315                     if (isset($response['query']) && !empty($response['query'])){
1316                         $html_text .= $subpanelTiles->get_buttons($subpanel_def, $response['query']);
1317                     }else{
1318                         $html_text .= $subpanelTiles->get_buttons($subpanel_def);
1319                     }
1320                 }
1321                 else {
1322                     $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";
1323                 }
1324                 $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";
1325                 $html_text .= "</td>\n";
1326                 $html_text .= "</tr>\n";
1327                 $this->xTemplate->assign("PAGINATION",$html_text);
1328             }
1329
1330             //C.L. - Fix for 23461
1331             if(empty($_REQUEST['action']) || $_REQUEST['action'] != 'Popup') {
1332                 $_SESSION['export_where'] = $this->query_where;
1333             }
1334             $this->xTemplate->parse($xtemplateSection.".list_nav_row");
1335         }
1336     } // end processListNavigation
1337
1338     function processOrderBy($html_varName) {
1339
1340         if(!isset($this->base_URL)) {
1341             $this->base_URL = $_SERVER['PHP_SELF'];
1342
1343             if(isset($_SERVER['QUERY_STRING'])) {
1344                 $this->base_URL = preg_replace("/\&".$this->getSessionVariableName($html_varName,"ORDER_BY")."=[0-9a-zA-Z\_\.]*/","",$this->base_URL .'?'.$_SERVER['QUERY_STRING']);
1345                 $this->base_URL = preg_replace("/\&".$this->getSessionVariableName($html_varName,"offset")."=[0-9]*/","",$this->base_URL);
1346             }
1347             if($_SERVER['REQUEST_METHOD'] == 'POST') {
1348                 $this->base_URL .= '?';
1349                 if(isset($_REQUEST['action'])) $this->base_URL .= '&action='.$_REQUEST['action'];
1350                 if(isset($_REQUEST['record'])) $this->base_URL .= '&record='.$_REQUEST['record'];
1351                 if(isset($_REQUEST['module'])) $this->base_URL .= '&module='.$_REQUEST['module'];
1352             }
1353             $this->base_URL .= "&".$this->getSessionVariableName($html_varName,"offset")."=";
1354         }
1355
1356         if($this->is_dynamic) {
1357             $this->base_URL.='&to_pdf=true&action=SubPanelViewer&subpanel=' . $this->source_module;
1358         }
1359
1360         //bug43465 start
1361         if (isset($this->appendToBaseUrl) && is_array($this->appendToBaseUrl))
1362         {
1363             foreach ($this->appendToBaseUrl as $key => $value)
1364             {
1365                 $fullRequestString = $key . '=' . $value;
1366
1367                 if ($this->base_URL == "/index.php")
1368                 {
1369                     $this->base_URL .= "?";
1370                 } else
1371                 {
1372                     if ($fullRequestString == substr($this->baseURL, '-' . strlen($fullRequestString)))
1373                     {
1374                         $this->base_URL = preg_replace("/&" . $key . "\=.*/", "", $this->base_URL);
1375                     } else
1376                     {
1377                         $this->base_URL = preg_replace("/&" . $key . "\=.*?&/", "&", $this->base_URL);
1378                     }
1379                     $this->base_URL .= "&";
1380                 }
1381                 if (!empty($value))
1382                 {
1383                     $this->base_URL .= "{$key}={$value}";
1384                 }
1385             }
1386         }
1387         //bug43465 end
1388         
1389         $sort_URL_base = $this->base_URL. "&".$this->getSessionVariableName($html_varName,"ORDER_BY")."=";
1390
1391         if($sort_URL_base !== "")
1392         {
1393             $this->xTemplate->assign("ORDER_BY", $sort_URL_base);
1394             return $sort_URL_base;
1395         } else {
1396             return '';
1397         }
1398     }
1399
1400
1401     function getAdditionalHeader() {
1402
1403     }
1404
1405
1406     /**
1407     * @return void
1408     * @param unknown $data
1409     * @param unknown $xTemplateSection
1410     * @param unknown $html_varName
1411     * @desc INTERNAL FUNCTION handles the rows
1412     * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1413     * All Rights Reserved.
1414     * Contributor(s): ______________________________________..
1415     */
1416     function processListRows($data, $xtemplateSection, $html_varName)
1417     {
1418         global $odd_bg;
1419         global $even_bg;
1420         global $hilite_bg;
1421         global $app_strings, $sugar_version, $sugar_config;
1422         global $currentModule;
1423
1424         static $overlib_included;
1425         if(!$overlib_included) {
1426             echo getVersionedScript('cache/include/javascript/sugar_grp_overlib.js').
1427                 '<div id="overDiv" style="position:absolute; visibility:hidden; z-index:1000;"></div>';
1428             $overlib_included = true;
1429         }
1430
1431
1432         $this->xTemplate->assign('BG_HILITE', $hilite_bg);
1433         $this->xTemplate->assign('CHECKALL', SugarThemeRegistry::current()->getImage('blank', '', 1, 1, ".gif", ''));
1434     //$this->xTemplate->assign("BG_CLICK", $click_bg);
1435         $oddRow = true;
1436         $count = 0;
1437         reset($data);
1438
1439         //GETTING OFFSET
1440         $offset = $this->getOffset($html_varName);
1441         $timeStamp = $this->unique_id();
1442         $_SESSION[$html_varName."_FROM_LIST_VIEW"] = $timeStamp;
1443
1444         $associated_row_data = array();
1445
1446         //mail merge list
1447         $mergeList = array();
1448         $module = '';
1449         //todo what is this?  It is using an array as a boolean
1450         while(list($aVal, $aItem) = each($data))
1451         {
1452             if(isset($this->data_array)) {
1453                 $fields = $this->data_array;
1454             } else {
1455                 $aItem->check_date_relationships_load();
1456                 $fields = $aItem->get_list_view_data();
1457             }
1458
1459             if(is_object($aItem)) { // cn: bug 5349
1460                 //add item id to merge list, if the button is clicked
1461                 $mergeList[] = $aItem->id;
1462                 if(empty($module)) {
1463                     $module = $aItem->module_dir;
1464                 }
1465             }
1466             //ADD OFFSET TO ARRAY
1467
1468                 $fields['OFFSET'] = ($offset + $count + 1);
1469
1470             $fields['STAMP'] = $timeStamp;
1471             if($this->shouldProcess) {
1472
1473             $prerow = '';
1474             if(!isset($this->data_array)) {
1475                 $prerow .= "<input onclick='sListView.check_item(this, document.MassUpdate)' type='checkbox' class='checkbox' name='mass[]' value='". $fields['ID']. "'>";
1476             }
1477             $this->xTemplate->assign('PREROW', $prerow);
1478
1479             $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)'>");
1480             }
1481             if(!isset($this->data_array)) {
1482                 $tag = $aItem->listviewACLHelper();
1483                 $this->xTemplate->assign('TAG',$tag) ;
1484             }
1485
1486             if($oddRow)
1487             {
1488                 $ROW_COLOR = 'oddListRow';
1489                 $BG_COLOR =  $odd_bg;
1490             }
1491             else
1492             {
1493                 $ROW_COLOR = 'evenListRow';
1494                 $BG_COLOR =  $even_bg;
1495             }
1496             $oddRow = !$oddRow;
1497
1498             $this->xTemplate->assign('ROW_COLOR', $ROW_COLOR);
1499             $this->xTemplate->assign('BG_COLOR', $BG_COLOR);
1500
1501             if(isset($this->data_array))
1502             {
1503                 $this->xTemplate->assign('KEY', $aVal);
1504                 $this->xTemplate->assign('VALUE', $aItem);
1505                 $this->xTemplate->assign('INDEX', $count);
1506
1507             }
1508             else
1509             {
1510     //AED -- some modules do not have their additionalDetails.php established. Add a check to ensure require_once does not fail
1511     // Bug #2786
1512                 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'))) {
1513
1514                     $additionalDetailsFile = 'modules/' . $aItem->module_dir . '/metadata/additionalDetails.php';
1515                     if(file_exists('custom/modules/' . $aItem->module_dir . '/metadata/additionalDetails.php')){
1516                         $additionalDetailsFile = 'custom/modules/' . $aItem->module_dir . '/metadata/additionalDetails.php';
1517                     }
1518
1519                     require_once($additionalDetailsFile);
1520                     $ad_function = (empty($this->additionalDetailsFunction) ? 'additionalDetails' : $this->additionalDetailsFunction) . $aItem->object_name;
1521                     $results = $ad_function($fields);
1522                     $results['string'] = str_replace(array("&#039", "'"), '\&#039', $results['string']); // no xss!
1523
1524                     if(trim($results['string']) == '') $results['string'] = $app_strings['LBL_NONE'];
1525                     $fields[$results['fieldToAddTo']] = $fields[$results['fieldToAddTo']].'</a>';
1526                 }
1527                 $this->xTemplate->assign($html_varName, $fields);
1528                 $aItem->setupCustomFields($aItem->module_dir);
1529                 $aItem->custom_fields->populateAllXTPL($this->xTemplate, 'detail', $html_varName, $fields);
1530             }
1531             if(!isset($this->data_array) && $aItem->ACLAccess('DetailView')) {
1532                 $count++;
1533             }
1534             if(isset($this->data_array)) {
1535                 $count++;
1536             }
1537             if(!isset($this->data_array)) {
1538                 $aItem->list_view_parse_additional_sections($this->xTemplate, $xtemplateSection);
1539
1540                 if($this->xTemplate->exists($xtemplateSection.'.row.pro')) {
1541                     $this->xTemplate->parse($xtemplateSection.'.row.pro');
1542                 }
1543             }
1544             $this->xTemplate->parse($xtemplateSection . '.row');
1545
1546             if(isset($fields['ID'])) {
1547                 $associated_row_data[$fields['ID']] = $fields;
1548                 // Bug 38908: cleanup data for JS to avoid having &nbsp; shuffled around
1549                 foreach($fields as $key => $value) {
1550                     if($value == '&nbsp;') {
1551                         $associated_row_data[$fields['ID']][$key] = '';
1552                     }
1553                 }
1554             }
1555         }
1556
1557         $_SESSION['MAILMERGE_RECORDS'] = $mergeList;
1558         $_SESSION['MAILMERGE_MODULE_FROM_LISTVIEW'] = $module;
1559         if(empty($_REQUEST['action']) || $_REQUEST['action'] != 'Popup') {
1560             $_SESSION['MAILMERGE_MODULE'] = $module;
1561         }
1562
1563         if($this->process_for_popups)
1564         {
1565             $json = getJSONobj();
1566             $is_show_fullname = showFullName() ? 1 : 0;
1567             $associated_javascript_data = '<script type="text/javascript">' . "\n"
1568                 //. '<!-- // associated javascript data generated by ListView' . "\n"
1569                 . 'var associated_javascript_data = '
1570                 . $json->encode($associated_row_data) . ";\n"
1571                 //. '-->' . "\n"
1572                 . 'var is_show_fullname = '
1573                 . $is_show_fullname . ";\n"
1574                 . '</script>';
1575             $this->xTemplate->assign('ASSOCIATED_JAVASCRIPT_DATA', $associated_javascript_data);
1576         }
1577
1578         $this->xTemplate->parse($xtemplateSection);
1579     }
1580
1581
1582     function getLayoutManager()
1583     {
1584         require_once('include/generic/LayoutManager.php');
1585         if($this->layout_manager == null)
1586         {
1587             $this->layout_manager = new LayoutManager();
1588         }
1589         return $this->layout_manager;
1590     }
1591
1592
1593     function process_dynamic_listview_header($source_module, $subpanel_def, $html_var = 'CELL')
1594     {
1595
1596
1597         $layout_manager = $this->getLayoutManager();
1598         $layout_manager->setAttribute('order_by_link',$this->processOrderBy($html_var));
1599         $layout_manager->setAttribute('context','HeaderCell');
1600         $layout_manager->setAttribute('image_path',$this->local_image_path);
1601         $layout_manager->setAttribute('html_varName',$html_var);
1602         $layout_manager->setAttribute('module_name', $source_module);
1603         list($orderBy,$desc) = $this->getOrderByInfo($html_var);
1604
1605         if($orderBy == 'amount*1')
1606         {
1607             $orderBy=  'amount';
1608         }
1609
1610         foreach($subpanel_def->get_list_fields() as $column_name=>$widget_args)
1611         {
1612             $usage = empty($widget_args['usage']) ? '' : $widget_args['usage'];
1613             if($usage != 'query_only')
1614             {
1615                 $imgArrow = '';
1616
1617                 if($orderBy == $column_name || (isset($widget_args['sort_by']) && str_replace('.','_',$widget_args['sort_by']) == $orderBy))
1618                 {
1619                     $imgArrow = "_down";
1620                     if($this->sort_order == 'asc') {
1621                         $imgArrow = "_up";
1622                     }
1623                 }
1624                 $widget_args['name']=$column_name;
1625                 $widget_args['sort'] = $imgArrow;
1626                 $widget_args['start_link_wrapper'] = $this->start_link_wrapper;
1627                 $widget_args['end_link_wrapper'] = $this->end_link_wrapper;
1628                 $widget_args['subpanel_module'] = $this->subpanel_module;
1629
1630                 $widget_contents = $layout_manager->widgetDisplay($widget_args);
1631                 $cell_width = empty($widget_args['width']) ? '' : $widget_args['width'];
1632                 $this->xTemplate->assign('HEADER_CELL', $widget_contents);
1633                 static $count;
1634             if(!isset($count))$count = 0; else $count++;
1635                 $this->xTemplate->assign('CELL_COUNT', $count);
1636                 $this->xTemplate->assign('CELL_WIDTH', $cell_width);
1637                 $this->xTemplate->parse('dyn_list_view.header_cell');
1638             }
1639         }
1640
1641     }
1642
1643
1644     /**
1645     * @return void
1646     * @param unknown $seed
1647     * @param unknown $xTemplateSection
1648     * @param unknown $html_varName
1649     * @desc PUBLIC FUNCTION Handles List Views using seeds that extend SugarBean
1650         $XTemplateSection is the section in the XTemplate file that should be parsed usually main
1651         $html_VarName is the variable name used in the XTemplateFile e.g. TASK
1652         $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
1653         if no XTemplate is set it will create  a new XTemplate
1654         * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc..
1655         * All Rights Reserved..
1656         * Contributor(s): ______________________________________..
1657     */
1658
1659     function processListViewTwo($seed, $xTemplateSection, $html_varName) {
1660         global $current_user;
1661         if(!isset($this->xTemplate)) {
1662             $this->createXTemplate();
1663         }
1664
1665         $isSugarBean = is_subclass_of($seed, "SugarBean");
1666         $list = null;
1667
1668         if($isSugarBean) {
1669             $list = $this->processSugarBean($xTemplateSection, $html_varName, $seed);
1670         } else {
1671             $list = $seed;
1672         }
1673
1674         if (is_object($seed) && isset($seed->object_name) && $seed->object_name == 'WorkFlow') {
1675             $tab=array();
1676             $access = get_workflow_admin_modules_for_user($current_user);
1677             for ($i = 0; $i < count($list); $i++) {
1678                 if(!empty($access[$list[$i]->base_module])){
1679                     $tab[]=$list[$i];
1680                 }
1681             }
1682             $list = $tab;
1683         }
1684
1685         if($this->is_dynamic) {
1686             $this->processHeaderDynamic($xTemplateSection,$html_varName);
1687             $this->processListRows($list,$xTemplateSection, $html_varName);
1688         } else {
1689             $this->processSortArrows($html_varName);
1690
1691             if($isSugarBean) {
1692                 $seed->parse_additional_headers($this->xTemplate, $xTemplateSection);
1693             }
1694             $this->xTemplateAssign('CHECKALL', SugarThemeRegistry::current()->getImage('blank', '', 1, 1, ".gif", ''));
1695
1696             // Process the  order by before processing the pro_nav.  The pro_nav requires the order by values to be set
1697             $this->processOrderBy($html_varName);
1698
1699
1700             $this->processListRows($list,$xTemplateSection, $html_varName);
1701         }
1702
1703         if($this->display_header_and_footer) {
1704             $this->getAdditionalHeader();
1705             if(!empty($this->header_title)) {
1706                 echo get_form_header($this->header_title, $this->header_text, false);
1707             }
1708         }
1709
1710         $this->xTemplate->out($xTemplateSection);
1711
1712         if(isset($_SESSION['validation'])) {
1713             print base64_decode('PGEgaHJlZj0naHR0cDovL3d3dy5zdWdhcmNybS5jb20nPlBPV0VSRUQmbmJzcDtCWSZuYnNwO1NVR0FSQ1JNPC9hPg==');
1714         }
1715     }
1716
1717     function getArrowStart() {
1718         $imgFileParts = pathinfo(SugarThemeRegistry::current()->getImageURL("arrow.gif"));
1719
1720         return "&nbsp;<!--not_in_theme!--><img border='0' src='".$imgFileParts['dirname']."/".$imgFileParts['filename']."";
1721     }
1722
1723     function getArrowUpDownStart($upDown) {
1724         $ext = ( SugarThemeRegistry::current()->pngSupport ? "png" : "gif" );
1725
1726         if (!isset($upDown) || empty($upDown)) {
1727             $upDown = "";
1728         }
1729         return "&nbsp;<img border='0' src='".SugarThemeRegistry::current()->getImageURL("arrow{$upDown}.{$ext}")."' ";
1730     }
1731
1732         function getArrowEnd() {
1733                 $imgFileParts = pathinfo(SugarThemeRegistry::current()->getImageURL("arrow.gif"));
1734
1735         list($width,$height) = ListView::getArrowImageSize();
1736
1737                 return '.'.$imgFileParts['extension']."' width='$width' height='$height' align='absmiddle' alt=".translate('LBL_SORT').">";
1738     }
1739
1740     function getArrowUpDownEnd($upDown) {
1741         if (!isset($upDown) || empty($upDown)) {
1742             $upDown = "";
1743         }
1744         $imgFileParts = pathinfo(SugarThemeRegistry::current()->getImageURL("arrow{$upDown}.gif"));
1745
1746         list($width,$height) = ListView::getArrowUpDownImageSize($upDown);
1747
1748         //get the right alt tag for the sort
1749         $sortStr = translate('LBL_ALT_SORT');
1750         if($upDown == '_down'){
1751             $sortStr = translate('LBL_ALT_SORT_DESC');
1752         }elseif($upDown == '_up'){
1753             $sortStr = translate('LBL_ALT_SORT_ASC');
1754         }
1755         return " width='$width' height='$height' align='absmiddle' alt='$sortStr'>";
1756     }
1757
1758         function getArrowImageSize() {
1759             // jbasicChartDashletsExpColust get the non-sort image's size.. the up and down have be the same.
1760                 $image = SugarThemeRegistry::current()->getImageURL("arrow.gif",false);
1761
1762         $cache_key = 'arrow_size.'.$image;
1763
1764         // Check the cache
1765         $result = sugar_cache_retrieve($cache_key);
1766         if(!empty($result))
1767         return $result;
1768
1769         // No cache hit.  Calculate the value and return.
1770         $result = getimagesize($image);
1771         sugar_cache_put($cache_key, $result);
1772         return $result;
1773     }
1774
1775     function getArrowUpDownImageSize($upDown) {
1776         // just get the non-sort image's size.. the up and down have be the same.
1777         $image = SugarThemeRegistry::current()->getImageURL("arrow{$upDown}.gif",false);
1778
1779         $cache_key = 'arrowupdown_size.'.$image;
1780
1781         // Check the cache
1782         $result = sugar_cache_retrieve($cache_key);
1783         if(!empty($result))
1784         return $result;
1785
1786         // No cache hit.  Calculate the value and return.
1787         $result = getimagesize($image);
1788         sugar_cache_put($cache_key, $result);
1789         return $result;
1790     }
1791
1792         function getOrderByInfo($html_varName)
1793         {
1794                 $orderBy = $this->getSessionVariable($html_varName, "OBL");
1795                 $desc = $this->getSessionVariable($html_varName, $orderBy.'S');
1796                 $orderBy = str_replace('.', '_', $orderBy);
1797                 return array($orderBy,$desc);
1798         }
1799
1800     function processSortArrows($html_varName)
1801     {
1802
1803         $this->xTemplateAssign("arrow_start", $this->getArrowStart());
1804
1805         list($orderBy,$desc) = $this->getOrderByInfo($html_varName);
1806
1807                 $imgArrow = "_up";
1808                 if($desc) {
1809                         $imgArrow = "_down";
1810                 }
1811                 /**
1812                  * @deprecated only used by legacy opportunites listview, nothing current. Leaving for BC
1813                  */
1814                 if($orderBy == 'amount*1')
1815                 {
1816                         $this->xTemplateAssign('amount_arrow', $imgArrow);
1817                 }
1818                 else if($orderBy == 'amount_usdollar*1')
1819                 {
1820                         $this->xTemplateAssign('amount_usdollar_arrow', $imgArrow);
1821                 }
1822                 else
1823                 {
1824                         $this->xTemplateAssign($orderBy.'_arrow', $imgArrow);
1825                 }
1826
1827         $this->xTemplateAssign('arrow_end', $this->getArrowEnd());
1828     }
1829
1830     // this is where translation happens for dynamic list views
1831     function loadListFieldDefs(&$subpanel_fields,&$child_focus)
1832     {
1833         $this->list_field_defs = $subpanel_fields;
1834
1835         for($i=0;$i < count($this->list_field_defs);$i++)
1836         {
1837             $list_field = $this->list_field_defs[$i];
1838             $field_def = null;
1839             $key = '';
1840             if(!empty($list_field['vname']))
1841             {
1842                 $key = $list_field['vname'];
1843             } else if(isset($list_field['name']) &&  isset($child_focus->field_defs[$list_field['name']]))
1844             {
1845                     $field_def = $child_focus->field_defs[$list_field['name']];
1846                     $key = $field_def['vname'];
1847             }
1848             if(!empty($key))
1849             {
1850                 $list_field['label'] = translate($key,$child_focus->module_dir);
1851                 $this->list_field_defs[$i]['label'] = preg_replace('/:$/','',$list_field['label']);
1852             }
1853             else
1854             {
1855                 $this->list_field_defs[$i]['label'] ='&nbsp;';
1856             }
1857         }
1858     }
1859
1860     function unique_id() {
1861         return sugar_microtime();
1862     }
1863
1864      /**INTERNAL FUNCTION sets a session variable keeping it local to the listview
1865      not the current_module
1866      * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1867      * All Rights Reserved.
1868      * Contributor(s): ______________________________________.
1869      */
1870      function setLocalSessionVariable($localVarName,$varName, $value) {
1871         $_SESSION[$localVarName."_".$varName] = $value;
1872      }
1873
1874      /**INTERNAL FUNCTION returns a session variable that is local to the listview,
1875      not the current_module
1876      * Portions created by SugarCRM are Copyright (C) SugarCRM, Inc.
1877      * All Rights Reserved.
1878      * Contributor(s): ______________________________________.
1879      */
1880  function getLocalSessionVariable($localVarName,$varName) {
1881     if(isset($_SESSION[$localVarName."_".$varName])) {
1882         return $_SESSION[$localVarName."_".$varName];
1883     }
1884     else{
1885         return "";
1886     }
1887  }
1888
1889  /* Set to true if you want Additional Details to appear in the listview
1890   */
1891  function setAdditionalDetails($value = true, $function = '') {
1892     if(!empty($function)) $this->additionalDetailsFunction = $function;
1893     $this->_additionalDetails = $value;
1894  }
1895
1896 }
1897 ?>