Dokeos PHP Cross Reference Learning Management Systems

Source: /main/inc/lib/fckeditor/editor/plugins/ajaxfilemanager/inc/class.pagination.php - 584 lines - 12226 bytes - Summary - Text - Print

   1  <?php
   2  /**
   3   * Pagination Class
   4   * @author Logan Cai  (cailongqun [at] yahoo [dot] com [dot] cn)
   5   * @since 27/Nov/20006
   6   *
   7   */
   8  class pagination
   9  {
  10      var $totalItems = 0;
  11      var $itemsPerPage = 30;
  12      var $currentPage = 1;
  13      var $friendlyUrl = false;
  14      var $baseUrl = "";
  15      var $pageIndex = "page";
  16      var $groupLimit = 5;
  17      var $excludedQueryStrings = array();
  18      var $totalPages = 0;
  19      var $url = "";
  20      var $previousText = "Previous";
  21      var $nextText = "Next";
  22      var $lastText = "Last";
  23      var $firstText = "First";
  24      var $limitIndex ='limit';
  25      var $limits = array(5, 10, 20, 30, 50, 80, 150, 999);
  26  
  27  
  28      /**
  29       * Contructor
  30       *
  31       * @param boolean $friendlyUrl set the returned url
  32       * as search engine friendly or Normal Url
  33       */
  34  	function pagination($friendlyUrl=false)
  35      {
  36          $this->friendlyUrl = $friendlyUrl;
  37          $this->__resetCurrentPage();
  38      }
  39  
  40      /**
  41       * set maximum number of items per page
  42       *
  43       * @param integer $value maximum number of items per page
  44       */
  45  	function setLimit($value)
  46      {
  47          $this->itemsPerPage = intval($value);
  48      }
  49      /**
  50       * get maximum number of items per page
  51       *
  52       * @return integer
  53       */
  54  	function getLimit()
  55      {
  56          return $this->itemsPerPage;
  57      }
  58  
  59      /**
  60       * set the total number of items
  61       *
  62       * @param integer $value the total number of items
  63       */
  64  	function setTotal($value)
  65      {
  66          $this->totalItems = intval($value);
  67      }
  68      /**
  69       * get the total number of items
  70       *
  71       * @return integer total number of items
  72       */
  73  	function getTotal()
  74      {
  75          return $this->totalItems;
  76      }
  77      /**
  78       * get total pages will be used to display all records
  79       *
  80       */
  81  	function getTotalPages()
  82      {
  83  
  84          $output = floor(($this->totalItems / $this->itemsPerPage ));
  85          if($this->totalItems % $this->itemsPerPage)
  86          {
  87              $output++;
  88          }
  89          return $output;
  90      }
  91  
  92      /**
  93       * Set the index of URL Query String
  94       *
  95       * @param string $value e.g. page
  96       */
  97  	function setPageIndex($value)
  98      {
  99          $this->pageIndex = $value;
 100          $this->__resetCurrentPage();
 101      }
 102  
 103  
 104  	function getPageIndex()
 105      {
 106          return $this->pageIndex;
 107      }
 108      /**
 109       * initiate or reset the current page number
 110       *
 111       */
 112  	function __resetCurrentPage()
 113      {
 114          $this->currentPage = ((isset($_GET[$this->pageIndex]) && intval($_GET[$this->pageIndex]) > 0)?intval($_GET[$this->pageIndex]):1);
 115      }
 116  
 117      /**
 118       * set the base url used in the links, default is $PHP_SELF
 119       *
 120       * @param string $value the base url
 121       */
 122  	function setUrl($value="")
 123      {
 124          if(empty($value))
 125          {
 126              if($this->friendlyUrl)
 127              {
 128                  $this->url = "http://" . $_SERVER['HTTP_HOST'] . "/";
 129              }else
 130              {
 131                  $this->url = $_SERVER['PHP_SELF'];
 132              }
 133          }else
 134          {
 135              $this->url = $value;
 136          }
 137  
 138      }
 139  
 140      /**
 141       * get the base url variable
 142       *
 143       * @return string the base url
 144       */
 145  	function getUrl()
 146      {
 147  
 148          if(empty($this->url))
 149          {
 150              $this->setUrl();
 151  
 152          }
 153          return $this->url;
 154      }
 155  
 156      /**
 157       * set base url for pagination links after exculed those keys
 158       * identified on excluded query strings
 159       */
 160  	function __setBaseUrl()
 161      {
 162  
 163          if(empty($this->url))
 164          {
 165              $this->getUrl();
 166          }
 167  
 168          if($this->friendlyUrl)
 169          {
 170              $this->baseUrl = $this->getUrl();
 171          }else
 172          {
 173  
 174              $appendingQueryStrings = array();
 175              $this->excludedQueryStrings[$this->pageIndex] =$this->pageIndex;
 176              foreach($_GET as $k=>$v)
 177              {
 178                  if((array_search($k, $this->excludedQueryStrings) === false ))
 179                  {
 180                      $appendingQueryStrings[$k] = $k . "=" . $v;
 181                  }
 182              }
 183              if(sizeof($appendingQueryStrings))
 184              {
 185                  $this->baseUrl = $this->__appendQueryString($this->url, implode("&", $appendingQueryStrings));
 186              }else
 187              {
 188                  $this->baseUrl = $this->getUrl();
 189              }
 190  
 191          }
 192  
 193  
 194      }
 195      /**
 196       * get base url for pagination links aftr excluded those key
 197       * identified on excluded query strings
 198       *
 199       */
 200  	function __getBaseUrl()
 201      {
 202  
 203          if(empty($this->baseUrl))
 204          {
 205  
 206              $this->__setBaseUrl();
 207          }
 208          return $this->baseUrl;
 209      }
 210  
 211  
 212      /**
 213       * get the first item number
 214       *
 215       * @return interger the first item number displayed within current page
 216       */
 217  	function getFirstItem()
 218      {
 219          $output = 0;
 220          $temStartItemNumber = (($this->currentPage - 1) * $this->itemsPerPage + 1);
 221          if($this->totalItems && $temStartItemNumber <= $this->totalItems )
 222          {
 223  
 224              $output = $temStartItemNumber;
 225          }
 226          return $output;
 227      }
 228      /**
 229       * get the last item number displayed within current page
 230       *
 231       * @return interger the last item number
 232       */
 233  	function getLastItem()
 234      {
 235          $output = 0;
 236          $temEndItemNumber = (($this->currentPage) * $this->itemsPerPage);
 237          if($this->totalItems)
 238          {
 239              if($temEndItemNumber <= $this->totalItems)
 240              {
 241                  $output = $temEndItemNumber;
 242              }else
 243              {
 244                  $output = $this->totalItems;
 245              }
 246  
 247          }
 248          return $output;
 249      }
 250      /**
 251       * set  page groupings limit
 252       * used for previous 1 2 3 4 5 next
 253       *
 254       * @param unknown_type $value
 255       */
 256  	function setGroupLimit($value)
 257      {
 258          $this->groupLimit = intval($value);
 259      }
 260      /**
 261       * get page grouping limit
 262       *
 263       * @return integer the page grouping limit
 264       */
 265  	function getGroupLimit()
 266      {
 267          return $this->groupLimit;
 268      }
 269      /**
 270       * get the page offset number
 271       * used for Query . e.g SELECT SQL_CALC_FOUND_ROWS *
 272       *                         FROM mytable LIMIT getPageOffset(), getItemsPerPage()
 273       *
 274       * @return iner
 275       */
 276  	function getPageOffset()
 277      {
 278          return (($this->currentPage - 1)  * $this->itemsPerPage);
 279      }
 280      /**
 281       * get the last url if any
 282       * @return  string the last url
 283       */
 284  	function getLastUrl()
 285      {
 286  
 287          $url = "";
 288          $totalPages = $this->getTotalPages();
 289          if($this->currentPage < $totalPages)
 290          {
 291              $url = $this->__getBaseUrl();
 292  
 293              if($this->friendlyUrl)
 294              {
 295                  $url .= $this->pageIndex . $totalPages . "/";
 296              }else
 297              {
 298                  $url = $this->__appendQueryString($url, $this->pageIndex . "=" . $totalPages);
 299              }
 300              $url = sprintf('<a href="%s" class="pagination_last"><span>%s</span></a>',
 301              $url,
 302              $this->lastText);
 303          }
 304          return $url;
 305      }
 306  
 307  
 308  
 309  
 310      /**
 311       * get the first url if any
 312       * @return string the first url
 313       */
 314  
 315  	function getFirstUrl()
 316      {
 317          $url = "";
 318          if($this->currentPage > 1)
 319          {
 320              $url = $this->__getBaseUrl();
 321              if($this->friendlyUrl)
 322              {
 323                  $url .= $this->pageIndex .  "1/";
 324              }else
 325              {
 326                  $url = $this->__appendQueryString($url, $this->pageIndex . "=1");
 327              }
 328              $url = sprintf('<a href="%s" class="pagination_first"><span>%s</span></a>',
 329              $url,
 330              $this->firstText);
 331  
 332          }
 333          return $url;
 334      }
 335  
 336      /**
 337       * get the previous page url if anywhere
 338       *
 339       * @param array $excludedQueryStrings excluded the value from $_GET
 340       * @return string the previous page url
 341       */
 342  	function getPreviousUrl()
 343      {
 344          $url = "";
 345          if($this->currentPage > 1 && $this->totalItems > 0 )
 346          {
 347              $url = $this->__getBaseUrl();
 348              if($this->friendlyUrl)
 349              {
 350                  $url .= $this->pageIndex . ($this->currentPage - 1) . "/";
 351              }else
 352              {
 353                  $url = $this->__appendQueryString($url, $this->pageIndex . "=" . ($this->currentPage -1));
 354              }
 355              $url = sprintf('<a href="%s" class="pagination_previous"><span>%s</span></a>',
 356              $url,
 357              $this->previousText);
 358  
 359          }
 360  
 361          return $url;
 362      }
 363      /**
 364       * get the next page url if anywhere
 365       *
 366       * @param array $excludedQueryStrings excluded the value from $_GET
 367       * @return string the next page url
 368       */
 369  	function getNextUrl()
 370      {
 371          $url = "";
 372          if($this->totalItems > ($this->currentPage * $this->itemsPerPage))
 373          {
 374              $url = $this->__getBaseUrl();
 375              if($this->friendlyUrl)
 376              {
 377                  $url .= $this->pageIndex . ($this->currentPage + 1) . "/";
 378              }else
 379              {
 380                  $url = $this->__appendQueryString($url, $this->pageIndex . "=" . ($this->currentPage + 1));
 381              }
 382              $url = sprintf('<a href="%s" class="pagination_next"><span>%s</span></a>',
 383              $url,
 384              $this->nextText);
 385          }
 386          return $url;
 387  
 388      }
 389  
 390      /**
 391       * get the group page links  e.g. 1,2,3,4,5
 392       * return format
 393       * <a class="pagination_group" href='yoururl'>1</a>
 394       * <a class="pagination_group active" href='#'>2</a>
 395       * <a class="pagination_group" href='yoururl'>3</a>
 396       */
 397  	function getGroupUrls()
 398      {
 399          $output = "";
 400          if($this->totalItems > 0)
 401          {
 402              $displayedPages = 0;
 403              $url = $this->__getBaseUrl();
 404              $totalPages = $this->getTotalPages();
 405              // find halfway point
 406              $groupLimitHalf = floor($this->groupLimit / 2);
 407              // determine what item/page we start with
 408              $itemStart = $this->currentPage - $groupLimitHalf;
 409              $itemStart = ($itemStart > 0 && $itemStart <= $totalPages)?$itemStart:1;
 410              $itemEnd = $itemStart;
 411  
 412              while($itemEnd < ($itemStart + $this->groupLimit - 1) && $itemEnd < $totalPages)
 413              {
 414                  $itemEnd++;
 415              }
 416  
 417  
 418              if($totalPages > ($itemEnd - $itemStart))
 419              {
 420                  for($i = $itemStart; $i > 1 && ($itemEnd - $itemStart + 1) < $this->groupLimit; $i--)
 421                  {
 422                      $itemStart--;
 423                  }
 424              }
 425  
 426              for($item = $itemStart; $item <= $itemEnd; $item++)
 427              {
 428                  if($item != $this->currentPage)
 429                  {//it is not the active link
 430                      if($this->friendlyUrl)
 431                      {
 432                          $temUrl = $url . $this->pageIndex . $item .   "/";
 433                      }else
 434                      {
 435                          $temUrl  = $this->__appendQueryString($url, $this->pageIndex . "=" . $item);
 436                      }
 437                      $output .= sprintf(' <a class="pagination_group" href="%s"><span>%d</span></a> ', $temUrl, $item);
 438                  }else
 439                  {//active link
 440                      $output .= sprintf(' <a class="pagination_group pagination_active" href="#"><span>%d</span></a> ', $item);
 441                  }
 442              }
 443          }
 444          return $output;
 445      }
 446      /**
 447       * set the text of previous page link
 448       *
 449       * @param string $value
 450       */
 451  	function setPreviousText($value)
 452      {
 453          $this->previousText = $value;
 454      }
 455      /**
 456       * set the text of first page link
 457       *
 458       * @param string $value
 459       */
 460  	function setFirstText($value)
 461      {
 462          $this->firstText = $value;
 463      }
 464      /**
 465       * set the text of next page link
 466       *
 467       * @param string $value
 468       */
 469  
 470  	function setNextText($value)
 471      {
 472          $this->nextText = $value;
 473      }
 474      /**
 475       * set the text of last page link
 476       *
 477       * @param string $value
 478       */
 479  	function setLastText($value)
 480      {
 481          $this->lastText = $value;
 482      }
 483  
 484      /**
 485       * set the excluded query string from $_GET;
 486       *
 487       * @param array the lists of the query string keys
 488       */
 489  
 490  	function setExcludedQueryString($values = array())
 491      {
 492          $this->excludedQueryStrings = $values;
 493      }
 494  
 495  	function getExcludedQueryString()
 496      {
 497          return $this->excludedQueryStrings;
 498      }
 499  
 500  
 501      /**
 502       * add extra query stiring to a url
 503       * @param string $baseUrl
 504       * @param string $extra the query string added to the base url
 505       */
 506  	function __appendQueryString($baseUrl, $extra)
 507      {
 508          $output = trim($baseUrl);
 509          if(strpos($baseUrl, "?") !== false)
 510          {
 511              $output .= "&" . $extra;
 512          }else
 513          {
 514              $output .= "?" . $extra;
 515          }
 516          return $output;
 517      }
 518      /**
 519       * return the html
 520       *
 521       * @param integer $type
 522       */
 523  	function getPaginationHTML($type=1, $cssClass="pagination")
 524      {
 525          $output = '';
 526          $output .= "<div class=\"pagination_content\"><p class=\"$cssClass\">\n";
 527          switch($type)
 528          {
 529              case "2":
 530                  $output .= "<span class=\"pagination_summany\">" . $this->getFirstItem() . " to " . $this->getLastItem() . " of " . $this->getTotal() . " results.</span> ";
 531              if($previousUrl = $this->getPreviousUrl())
 532              {
 533                  $output .= " " . $previousUrl;
 534              }
 535  
 536              if($nextUrl = $this->getNextUrl())
 537              {
 538                  $output .= " " . $nextUrl;
 539              }
 540                  break;
 541              case 1:
 542                  //get full summary pagination
 543              default:
 544                  $output .= "<span class=\"pagination_summany\">" . $this->getFirstItem() . "/" . $this->getLastItem() . " (" . $this->getTotal() . ")</span> ";
 545              if($firstUrl = $this->getFirstUrl())
 546              {
 547                  $output .= " " . $firstUrl;
 548              }
 549              if($previousUrl = $this->getPreviousUrl())
 550              {
 551                  $output .= " " . $previousUrl;
 552              }
 553  
 554              if($groupUrls = $this->getGroupUrls())
 555              {
 556                  $output .= " " . $groupUrls;
 557              }
 558              if($nextUrl = $this->getNextUrl())
 559              {
 560                  $output .= " " . $nextUrl;
 561              }
 562              if($lastUrl = $this->getLastUrl())
 563              {
 564                  $output .= " " . $lastUrl;
 565              }
 566              $itemPerPage = '';
 567              $itemPerPage .= "<select name=\"" . $this->limitIndex . "\" id=\"limit\" class=\"input inputLimit\" onchange=\"changePaginationLimit();\">\n";
 568              foreach ($this->limits as $v)
 569              {
 570                  $itemPerPage .= "<option value=\"" . $v . "\" " . ($v==$this->itemsPerPage?'selected="selected"':'') . ">" . $v . "</option>\n";
 571              }
 572              $itemPerPage .= "</select>\n";
 573              $output .= "<span class=\"pagination_items_per_page\">";
 574              $output .= sprintf(PAGINATION_ITEMS_PER_PAGE, $itemPerPage);
 575              $output .= "</span>";
 576              $output .= "<span class=\"pagination_parent\"><a href=\"javascript: void(0);\" onclick=\"javascript: goParentFolder();\" id=\"pagination_parent_link\" title=\"" . PAGINATION_GO_PARENT . "\">&nbsp;</a></span>";
 577          }
 578  
 579          $output .= "</p></div>";
 580          return $output;
 581      }
 582  
 583  }
 584  ?>

title

Description

title

Description

title

Description

title

title

Body