1<?php
2/*
3 * CHeMS (Content HElper Management System)
4 * Copyright (C) 2007-2009  Claudio M. Alessi
5 *
6 * This program is free software: you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation, either version 3 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
18 *
19*/
20
21defined("_CEXEC") or die ("Restricted access!");
22
23/*
24 * Common routines
25*/
26
27/*
28 * Organize the given values informations into the given object
29 * skipping all not needed fields. Since the function is called
30 * directly from an user file (not the core) it have to use the
31 * global objects in order to get the needed informations.
32*/
33function insert(&$object, $values)
34{
35   // Get the object ID
36   $id = $values['id'];
37
38   // Generate the object informations
39   foreach($values as $key => $val) {
40      if( $key !== "id" )
41         $object[$id][$key] = $val;
42   }
43
44} /* eof insert() */
45
46/*
47 * Set the inf and sup extremes for the elements list
48 * computed with the number of totals elements and the
49 * number of elements for each listing page.
50*/
51function xtrset($elements, $elements_per_page, &$inf, &$sup, $sort="desc")
52{
53
54   // No pages to show: don't load any page (show an error)
55   if( ! $elements_per_page ) {
56      $inf = $sup = 0;
57      return;
58   }
59
60   // If negative then show all pages
61   if( $elements_per_page < 0 )
62      $elements_per_page = $elements;
63
64   // Calculate the number of navigation pages
65   $pages_total = (int)($elements / $elements_per_page +
66                       ($elements % $elements_per_page ? 1 : 0));
67
68   // Get the current page
69   $page_current = (isset($_GET['list']) ? $_GET['list'] : 1);
70
71   /*
72    * If the page is not betwen the extremes of the valid range
73    * then the extremes (sup/inf) are set to zero.
74   */
75
76   // Check the extremes
77   if( $page_current < 1 || $page_current > $elements )
78      $sup = $inf = 0;
79   else {
80
81      /*
82       * Inf/Sup extremes
83      */
84      if( $sort === "asc" ) {
85         $sup = $elements_per_page * $page_current;
86	 $inf = $sup - $elements_per_page + 1;
87	 while( $sup > $elements ) --$sup;
88      }
89      else { // descending (default)
90         $sup = $elements - $elements_per_page * ($page_current - 1);
91         $inf = ($sup > $elements_per_page ? $sup - $elements_per_page + 1 : 1);
92      }
93  }
94
95} /* eof xtrset() */
96
97?>
98