573cae2897889b91b600abcf6f09e164cfe71155
[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 Affero General Public License,
5         AGPL, as published by the Free Software Foundation, either version 
6         3 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/agpl-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' width=50"); break;
108                     case 'dstamp':      // time stamp displayed as date
109                           label_cell(sql2date(substr($cell, 0, 10)), "align='center'"); 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         //end_table();
157         //start_table("$table_style align='center' class='navibar' width=100%");
158         /*
159         start_row("class='navibar'");
160         $colspan = count($pager->columns);
161          if($pager->rec_count) {
162                 $from = ($pager->curr_page-1)*$pager->page_len+1;
163                 $to = $from + $pager->page_len - 1;
164                 if ($to > $pager->rec_count)
165                   $to = $pager->rec_count;
166                 $all = $pager->rec_count;
167                 $lcols = ($colspan > 4 ? 3 : 2);
168                 label_cell(sprintf( _('Records %d-%d of %d'), $from, $to, $all),
169                         "colspan=$lcols class='navibar' style='border:none'");
170                 //echo "<td colspan=".($pager->columns -.$lcols)." style='border:none'>";
171                 echo "<td colspan=".($colspan-3)." style='border:none'>";
172                 $but_pref = $pager->name.'_page_';
173             start_table("align='right'");
174                 start_row();
175                 echo navi_button_cell($but_pref.'first', _('First'), $pager->first_page, 'right');
176                 echo navi_button_cell($but_pref.'prev', _('Prev'), $pager->prev_page,'right');
177                 echo navi_button_cell($but_pref.'next', _('Next'), $pager->next_page,'right');
178                 echo navi_button_cell($but_pref.'last', _('Last'), $pager->last_page, 'right');
179                 end_row(); 
180                 end_table();    
181                 echo "</td>";
182         } else {
183           label_cell( _('No records'), "colspan=$colspan class='navibar'");
184         }
185         end_row();
186         */
187         start_row("class='navibar'");
188         $colspan = count($pager->columns);
189          if($pager->rec_count) {
190                 echo "<td colspan=$colspan class='navibar' style='border:none;padding:3px;'>";
191                 echo "<div style='float:right;'>";
192                 $but_pref = $pager->name.'_page_';
193             start_table();
194                 start_row();
195                 echo navi_button_cell($but_pref.'first', _('First'), $pager->first_page, 'right');
196                 echo navi_button_cell($but_pref.'prev', _('Prev'), $pager->prev_page,'right');
197                 echo navi_button_cell($but_pref.'next', _('Next'), $pager->next_page,'right');
198                 echo navi_button_cell($but_pref.'last', _('Last'), $pager->last_page, 'right');
199                 end_row(); 
200                 end_table();
201                 echo "</div>";
202                 $from = ($pager->curr_page-1)*$pager->page_len+1;
203                 $to = $from + $pager->page_len - 1;
204                 if ($to > $pager->rec_count)
205                   $to = $pager->rec_count;
206                 $all = $pager->rec_count;
207                 echo sprintf( _('Records %d-%d of %d'), $from, $to, $all);
208                 echo "</td>";
209         } else {
210           label_cell( _('No records'), "colspan=$colspan class='navibar'");
211         }
212         end_row();
213
214         end_table();
215
216    if (isset($pager->marker_txt))
217                 display_note($pager->marker_txt, 0, 1, "class='$pager->notice_class'");
218
219   div_end();
220   return true;
221 }
222
223
224 ?>