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