Merged changes from main branch up to 2.1.3.
[fa-stable.git] / includes / ui / db_pager_view.inc
1 <?php
2 /**********************************************************************
3     Copyright (C) FrontAccounting, LLC.
4         Released under the terms of the GNU General Public License, GPL, 
5         as published by the Free Software Foundation, either version 3 
6         of the License, or (at your option) any later version.
7     This program is distributed in the hope that it will be useful,
8     but WITHOUT ANY WARRANTY; without even the implied warranty of
9     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  
10     See the License here <http://www.gnu.org/licenses/gpl-3.0.html>.
11 ***********************************************************************/
12 //--------------------------------------------------------------------------------------------------
13 function pager_link($link_text, $url, $icon=false)
14 {
15         global $path_to_root;
16         
17         if (user_graphic_links() && $icon)
18                 $link_text = set_icon($icon, $link_text);
19
20         $href = $path_to_root . $url;
21         return "<a href='$href'>" . $link_text . "</a>";
22 }
23
24 function navi_button($name, $value, $enabled=true, $icon = false) {
25         global $path_to_root;
26         return "<button ". ($enabled ? '':'disabled')
27                 ." class=\"navibutton\" type=\"submit\""
28             ." name=\"$name\"  id=\"$name\" value=\"$value\">"
29                 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/".$icon."'>":'')
30                 ."<span>$value</span></button>\n";
31 }
32
33 function navi_button_cell($name, $value, $enabled=true, $align='left') {
34         label_cell(navi_button($name, $value, $enabled), "align='$align'");
35 }
36 //-----------------------------------------------------------------------------
37 //
38 //    Sql paged table view. Call this function inside form.
39 //
40 function display_db_pager(&$pager) {
41     global      $table_style, $use_popup_windows, $use_date_picker, $path_to_root;
42
43         $pager->select_records();
44
45         div_start("_{$pager->name}_span");
46     $headers = array();
47
48         foreach($pager->columns as $num_col=>$col) {
49         if (isset($col['head'])) {
50                         if (!isset($col['ord'])) 
51                                 $headers[] = $col['head'];
52                         else {
53                                 $icon = (($col['ord'] == 'desc') ? 'sort_desc.gif' : 
54                                         ($col['ord'] == 'asc' ? 'sort_asc.gif' : 'sort_none.gif'));
55                                 $headers[] = navi_button($pager->name.'_sort_'.$num_col, 
56                                         $col['head'], true, $icon);
57                         }
58                 }
59         }
60     /* show a table of records returned by the sql */
61     start_table("$table_style width=$pager->width");
62     table_header($headers, 'nowrap');
63
64         if($pager->header_fun) {        // if set header handler
65                 start_row("class='{$pager->header_class}'");
66                 $fun = $pager->header_fun;
67                 if (method_exists($pager, $fun)) { 
68                         $h = $pager->$fun($pager);
69                 } elseif (function_exists($fun)) {
70                         $h = $fun($pager);
71                 }
72                 
73                 foreach($h as $c) {      // draw header columns
74                         $pars = isset($c[1]) ? $c[1] : '';
75                         label_cell($c[0], $pars);
76                 }
77                 end_row();
78         }
79
80         $cc = 0; //row colour counter
81         foreach($pager->data as $line_no => $row) {     
82
83                 $marker = $pager->marker;
84             if ($marker && $marker($row)) 
85                 start_row("class='$pager->marker_class'");
86             else        
87                         alt_table_row_color($cc);
88             foreach ($pager->columns as $k=>$col) {
89                    $coltype = $col['type'];
90                    $cell = isset($col['name']) ? $row[$col['name']] : '';
91
92                    if (isset($col['fun'])) { // use data input function if defined
93                     $fun = $col['fun']; 
94                     if (method_exists($pager, $fun)) { 
95                                 $cell = $pager->$fun($row, $cell);
96                         } elseif (function_exists($fun)) {
97                                 $cell = $fun($row, $cell);
98                         } else
99                                 $cell = '';
100                    }
101
102                    switch($coltype) { // format column
103                     case 'time':
104                           label_cell($cell, "width=40"); break;
105                     case 'date':
106                           label_cell(sql2date($cell), "align='center' nowrap"); break;
107                     case 'dstamp':      // time stamp displayed as date
108                           label_cell(sql2date(substr($cell, 0, 10)), "align='center' nowrap"); break;
109                     case 'tstamp':      // time stamp - FIX user format
110                           label_cell(sql2date(substr($cell, 0, 10)).
111                           ' '. substr($cell, 10), "align='center'"); break;
112                     case 'percent':
113                           percent_cell($cell); break;
114                     case 'amount':
115                           if ($cell=='')
116                                 label_cell('');
117                           else
118                                 amount_cell($cell, false); break;
119                     case 'qty':
120                           if ($cell=='')
121                                 label_cell('');
122                           else
123                                 qty_cell($cell, false, isset($col['dec']) ? $col['dec'] : null); break;
124                     case 'rate':
125                                 label_cell(number_format2($cell, user_exrate_dec()), "align=center"); break;
126                     default:
127 //                  case 'text':
128                           if (isset( $col['align']))
129                                   label_cell($cell, "align='" . $col['align'] . "'");
130                           else
131                                   label_cell($cell);
132                     case 'skip':        // column not displayed
133                   }
134           }
135             end_row();
136         }
137         //end of while loop
138
139         if($pager->footer_fun) {        // if set footer handler
140                 start_row("class='{$pager->footer_class}'");
141                 $fun = $pager->footer_fun;
142                 if (method_exists($pager, $fun)) { 
143                         $h = $pager->$fun($pager);
144                 } elseif (function_exists($fun)) {
145                         $h = $fun($pager);
146                 }
147                 
148                 foreach($h as $c) {      // draw footer columns
149                         $pars = isset($c[1]) ? $c[1] : '';
150                         label_cell($c[0], $pars);
151                 }
152                 end_row();
153         }
154
155         start_row("class='navibar'");
156         $colspan = count($pager->columns);
157          if($pager->rec_count) {
158                 echo "<td colspan=$colspan class='navibar' style='border:none;padding:3px;'>";
159                 echo "<div style='float:right;'>";
160                 $but_pref = $pager->name.'_page_';
161             start_table();
162                 start_row();
163                 echo navi_button_cell($but_pref.'first', _('First'), $pager->first_page, 'right');
164                 echo navi_button_cell($but_pref.'prev', _('Prev'), $pager->prev_page,'right');
165                 echo navi_button_cell($but_pref.'next', _('Next'), $pager->next_page,'right');
166                 echo navi_button_cell($but_pref.'last', _('Last'), $pager->last_page, 'right');
167                 end_row(); 
168                 end_table();
169                 echo "</div>";
170                 $from = ($pager->curr_page-1)*$pager->page_len+1;
171                 $to = $from + $pager->page_len - 1;
172                 if ($to > $pager->rec_count)
173                   $to = $pager->rec_count;
174                 $all = $pager->rec_count;
175                 echo sprintf( _('Records %d-%d of %d'), $from, $to, $all);
176                 echo "</td>";
177         } else {
178           label_cell( _('No records'), "colspan=$colspan class='navibar'");
179         }
180         end_row();
181
182         end_table();
183
184    if (isset($pager->marker_txt))
185                 display_note($pager->marker_txt, 0, 1, "class='$pager->notice_class'");
186
187   div_end();
188   return true;
189 }
190
191
192 ?>