New files after 2.1RC merge
[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         $link = access_string($link_text);
18         if (user_graphic_links() && $icon)
19                 $link[0] = set_icon($icon, $link[0]);
20
21         $href = $path_to_root . $url;
22         return "<a href='$href'$link[1]>" . $link[0] . "</a>";
23 }
24
25 function navi_button($name, $value, $enabled=true, $icon = false) {
26         global $path_to_root;
27         return "<button ". ($enabled ? '':'disabled')
28                 ." class=\"navibutton\" type=\"submit\""
29             ." name=\"$name\"  id=\"$name\" value=\"$value\">"
30                 .($icon ? "<img src='$path_to_root/themes/".user_theme()."/images/".$icon."'>":'')
31                 ."<span>$value</span></button>\n";
32 }
33
34 function navi_button_cell($name, $value, $enabled=true, $align='left') {
35         label_cell(navi_button($name, $value, $enabled), "align='$align'");
36 }
37 //-----------------------------------------------------------------------------
38 //
39 //    Sql paged table view. Call this function inside form.
40 //
41 function display_db_pager(&$pager) {
42     global      $table_style, $use_popup_windows, $use_date_picker, $path_to_root;
43
44         $pager->select_records();
45
46         div_start("_{$pager->name}_span");
47     $headers = array();
48
49         foreach($pager->columns as $num_col=>$col) {
50         if (isset($col['head'])) {
51                         if (!isset($col['ord'])) 
52                                 $headers[] = $col['head'];
53                         else {
54                                 $icon = (($col['ord'] == 'desc') ? 'sort_desc.gif' : 
55                                         ($col['ord'] == 'asc' ? 'sort_asc.gif' : 'sort_none.gif'));
56                                 $headers[] = navi_button($pager->name.'_sort_'.$num_col, 
57                                         $col['head'], true, $icon);
58                         }
59                 }
60         }
61     /* show a table of records returned by the sql */
62     start_table("$table_style width=$pager->width");
63     table_header($headers, 'nowrap');
64
65         if($pager->header_fun) {        // if set header handler
66                 start_row("class='{$pager->header_class}'");
67                 $fun = $pager->header_fun;
68                 if (method_exists($pager, $fun)) { 
69                         $h = $pager->$fun($pager);
70                 } elseif (function_exists($fun)) {
71                         $h = $fun($pager);
72                 }
73                 
74                 foreach($h as $c) {      // draw header columns
75                         $pars = isset($c[1]) ? $c[1] : '';
76                         label_cell($c[0], $pars);
77                 }
78                 end_row();
79         }
80
81         $cc = 0; //row colour counter
82         foreach($pager->data as $line_no => $row) {     
83
84                 $marker = $pager->marker;
85             if ($marker && $marker($row)) 
86                 start_row("class='$pager->marker_class'");
87             else        
88                         alt_table_row_color($cc);
89             foreach ($pager->columns as $k=>$col) {
90                    $coltype = $col['type'];
91                    $cell = isset($col['name']) ? $row[$col['name']] : '';
92
93                    if (isset($col['fun'])) { // use data input function if defined
94                     $fun = $col['fun']; 
95                     if (method_exists($pager, $fun)) { 
96                                 $cell = $pager->$fun($row, $cell);
97                         } elseif (function_exists($fun)) {
98                                 $cell = $fun($row, $cell);
99                         } else
100                                 $cell = '';
101                    }
102
103                    switch($coltype) { // format column
104                     case 'time':
105                           label_cell($cell, "width=40"); break;
106                     case 'date':
107                           label_cell(sql2date($cell), "align='center' nowrap"); break;
108                     case 'dstamp':      // time stamp displayed as date
109                           label_cell(sql2date(substr($cell, 0, 10)), "align='center' nowrap"); break;
110                     case 'tstamp':      // time stamp - FIX user format
111                           label_cell(sql2date(substr($cell, 0, 10)).
112                           ' '. substr($cell, 10), "align='center'"); break;
113                     case 'percent':
114                           percent_cell($cell); break;
115                     case 'amount':
116                           if ($cell=='')
117                                 label_cell('');
118                           else
119                                 amount_cell($cell, false); break;
120                     case 'qty':
121                           if ($cell=='')
122                                 label_cell('');
123                           else
124                                 qty_cell($cell, false, isset($col['dec']) ? $col['dec'] : null); break;
125                     case 'rate':
126                                 label_cell(number_format2($cell, user_exrate_dec()), "align=center"); break;
127                     default:
128 //                  case 'text':
129                           if (isset( $col['align']))
130                                   label_cell($cell, "align='" . $col['align'] . "'");
131                           else
132                                   label_cell($cell);
133                     case 'skip':        // column not displayed
134                   }
135           }
136             end_row();
137         }
138         //end of while loop
139
140         if($pager->footer_fun) {        // if set footer handler
141                 start_row("class='{$pager->footer_class}'");
142                 $fun = $pager->footer_fun;
143                 if (method_exists($pager, $fun)) { 
144                         $h = $pager->$fun($pager);
145                 } elseif (function_exists($fun)) {
146                         $h = $fun($pager);
147                 }
148                 
149                 foreach($h as $c) {      // draw footer columns
150                         $pars = isset($c[1]) ? $c[1] : '';
151                         label_cell($c[0], $pars);
152                 }
153                 end_row();
154         }
155
156         start_row("class='navibar'");
157         $colspan = count($pager->columns);
158          if($pager->rec_count) {
159                 echo "<td colspan=$colspan class='navibar' style='border:none;padding:3px;'>";
160                 echo "<div style='float:right;'>";
161                 $but_pref = $pager->name.'_page_';
162             start_table();
163                 start_row();
164                 echo navi_button_cell($but_pref.'first', _('First'), $pager->first_page, 'right');
165                 echo navi_button_cell($but_pref.'prev', _('Prev'), $pager->prev_page,'right');
166                 echo navi_button_cell($but_pref.'next', _('Next'), $pager->next_page,'right');
167                 echo navi_button_cell($but_pref.'last', _('Last'), $pager->last_page, 'right');
168                 end_row(); 
169                 end_table();
170                 echo "</div>";
171                 $from = ($pager->curr_page-1)*$pager->page_len+1;
172                 $to = $from + $pager->page_len - 1;
173                 if ($to > $pager->rec_count)
174                   $to = $pager->rec_count;
175                 $all = $pager->rec_count;
176                 echo sprintf( _('Records %d-%d of %d'), $from, $to, $all);
177                 echo "</td>";
178         } else {
179           label_cell( _('No records'), "colspan=$colspan class='navibar'");
180         }
181         end_row();
182
183         end_table();
184
185    if (isset($pager->marker_txt))
186                 display_note($pager->marker_txt, 0, 1, "class='$pager->notice_class'");
187
188   div_end();
189   return true;
190 }
191
192
193 ?>