Esempio n. 1
0
 /**
  * Cooperate with data class to fetch items for each page.
  * Use sql LIMIT to separate page items.
  *
  * @return array
  */
 protected function __paging()
 {
     // do paging when have total count
     if ($this->totalNum > 0) {
         // for paging field
         $this->totalPage = $this->totalPage ? $this->totalPage : ceil($this->totalNum / $this->each);
         $this->page = $this->page > $this->totalPage ? $this->totalPage : $this->page;
         $this->frNum = $this->each * ($this->page - 1);
         $this->frStr = $this->frNum + 1;
         $this->toNum = $this->page == $this->totalPage ? $this->totalNum : $this->each * $this->page;
         $this->toStr = $this->toNum;
         for ($i = 1; $i <= $this->totalPage; $i++) {
             if ($this->page == $i) {
                 if (isset($this->pattern['PageTag']) && sizeof($this->pattern['PageTag']) == 2) {
                     $pages[$i] = $this->pattern['PageTag'][0] . $i . $this->pattern['PageTag'][1];
                 } else {
                     $pages[$i] = '<b>' . $i . '</b>';
                 }
             } else {
                 if ($this->page == $i && $this->totalPage == $i) {
                     $pages[$i] = $this->totalPage;
                 } else {
                     $pages[$i] = $this->makeURL($i);
                 }
             }
         }
         // provide 3 paging modes
         if (isset($this->pattern['Mode'])) {
             // init array for mode 3
             $pages1 = array();
             $pages2 = array();
             $pages3 = array();
             switch ($this->pattern['Mode']) {
                 // Google Style Paging
                 case '1':
                     $start_pos = $this->page - 10 < 0 ? 0 : $this->page - 10;
                     $end_pos = $this->page + 9 > $this->totalPage ? $this->totalPage : $this->page + 9;
                     $pages = Hush_Util::array_chop($pages, $start_pos, $end_pos);
                     break;
                     // My Style Paging
                 // My Style Paging
                 case '2':
                     // don't split pages array when total page < 10
                     $limit = isset($this->pattern['ModeArg']['LIMIT']) ? $this->pattern['ModeArg']['LIMIT'] : 10;
                     $half_limit = ceil($limit / 2);
                     if ($this->totalPage > $limit) {
                         $start_pos = $this->page - $half_limit < 0 ? 0 : $this->page - $half_limit;
                         $start_pos = $start_pos + $limit > $this->totalPage ? $this->totalPage - $limit : $start_pos;
                         $pages = array_slice($pages, $start_pos, $limit);
                     }
                     break;
                 case '3':
                     $split1 = 5;
                     $split2 = $this->totalPage - 4;
                     if ($this->totalPage <= 10) {
                         $pages1 = $pages;
                     } else {
                         $i = $this->page;
                         if ($i > 1 && $i < $this->totalPage) {
                             $pages2 = array($pages[$i - 1], $pages[$i], $pages[$i + 1]);
                         } else {
                             $pages2 = array($pages[$i]);
                         }
                         $pages1 = array_slice($pages, 0, 3);
                         $pages3 = array_slice($pages, -3, 3);
                         if ($this->page <= $split1) {
                             $pages1 = array_unique(array_merge($pages1, $pages2));
                             $pages2 = $pages3;
                             $pages3 = array();
                         } elseif ($this->page >= $split2) {
                             if ($this->page != $this->totalPage) {
                                 $pages2 = array_unique(array_merge($pages2, $pages3));
                             } else {
                                 $pages2 = array_unique(array_merge($pages3, $pages2));
                             }
                             $pages3 = array();
                         }
                     }
                     // Show All Paging
                 // Show All Paging
                 default:
                     break;
             }
             // build paging string
             $spacing = array_key_exists('Spacing', $this->pattern) ? $this->pattern['Spacing'] : ' ';
             if ($this->pattern['Mode'] == 3) {
                 $this->pageStr = implode($spacing, $pages1);
                 if (sizeof($pages2)) {
                     $this->pageStr .= ' ... ' . implode($spacing, $pages2);
                 }
                 if (sizeof($pages3)) {
                     $this->pageStr .= ' ... ' . implode($spacing, $pages3);
                 }
             } else {
                 $this->pageStr = implode($spacing, $pages);
             }
         }
         // get page array
         if (is_array($this->items)) {
             for ($i = $this->frNum; $i < $this->toNum; $i++) {
                 $this->pageArr[] = $this->items[$i];
             }
         }
     } else {
         $this->frNum = $this->each * ($this->page - 1);
         $this->frStr = $this->frNum + 1;
         $this->toNum = $this->each * $this->page;
         $this->toStr = $this->toNum;
     }
     // for prev/next field
     if (1) {
         $prevStr = array_key_exists('Prev', $this->pattern) ? $this->pattern['Prev'] : $this->prevStr;
         $nextStr = array_key_exists('Next', $this->pattern) ? $this->pattern['Next'] : $this->nextStr;
         if ($this->page != 1) {
             $page = $this->page - 1;
             $this->prevStr = $this->makeURL($page, $prevStr);
         } else {
             if (!array_key_exists('TrimPag', $this->pattern)) {
                 $this->prevStr = $prevStr;
             } else {
                 $this->prevStr = '';
             }
         }
         if ($this->page != $this->totalPage) {
             $page = $this->page + 1;
             $this->nextStr = $this->makeURL($page, $nextStr);
         } else {
             if (!array_key_exists('TrimPag', $this->pattern)) {
                 $this->nextStr = $nextStr;
             } else {
                 $this->nextStr = '';
             }
         }
     }
     // return builded paging array
     return $this->toArray();
 }