00001 <?php
00002
00003
00004
00005
00006
00007
00015 class Pagination {
00016
00018 private $view;
00019
00021 private $resultCount;
00022
00024 private $startIdx;
00025
00027 private $endIdx;
00028
00030 private $pageCount;
00031
00033 private $currentPage;
00034
00036 private $maxPages;
00037
00039 private $isMobile = false;
00040
00042 private $sort_order;
00043
00045 private $sort_by;
00046
00054 public function __construct(&$view, $maxPages, $response, $isMobile=false) {
00055
00056 if (gettype($response) != 'object' || get_class($response) != 'SimpleXMLElement') {
00057 $this->template = null;
00058 return;
00059 }
00060
00061 $this->view = $view;
00062 $this->isMobile = $isMobile;
00063 $this->maxPages = $maxPages;
00064
00065
00066
00067
00068 $currentPageElement = $response->xpath('/rss/channel/vc_page');
00069 if (count($currentPageElement)) {
00070 $currentPage = (int)$currentPageElement[0];
00071 } else {
00072 $currentPage = 1;
00073 }
00074
00075
00076
00077
00078 $resultCountElement = $response->xpath('/rss/channel/vc_resultCount');
00079 if (count($resultCountElement)) {
00080 $resultCount = (int)$resultCountElement[0];
00081 } else {
00082 $resultCount = 0;
00083 }
00084
00085
00086
00087
00088 $pageCountElement = $response->xpath('/rss/channel/vc_pageCount');
00089 if (count($pageCountElement)) {
00090 $pageCount = (int)$pageCountElement[0];
00091 } else {
00092 $pageCount = 1;
00093 }
00094
00095
00096
00097
00098 $resultsPerPageElement = $response->xpath('/rss/channel/vc_resultPerPage');
00099 if (count($resultsPerPageElement)) {
00100 $resultsPerPage = (int)$resultsPerPageElement[0];
00101 } else {
00102 $resultsPerPage = 0;
00103 }
00104
00105
00106
00107
00108 $resultSortOrderElement = $response->xpath('/rss/channel/vc_sortOrder');
00109 if (count($resultSortOrderElement)) {
00110 $resultSortOrder = (int)$resultSortOrderElement[0];
00111 } else {
00112 $resultSortOrder = 'asc';
00113 }
00114
00115
00116
00117
00118 $resultSortByElement = $response->xpath('/rss/channel/vc_sortBy');
00119 if (count($resultSortByElement)) {
00120 $resultSortBy = (int)$resultSortByElement[0];
00121 } else {
00122 $resultSortBy = 'minprice';
00123 }
00124
00125
00126
00127
00128 $this->currentPage = $currentPage;
00129 $this->pageCount = $pageCount;
00130 $this->resultCount = $resultCount;
00131 $this->sort_order = $resultSortOrder;
00132 $this->sort_by = $resultSortBy;
00133 $this->startIdx = ($currentPage - 1) * $resultsPerPage + 1;
00134 $this->endIdx = $currentPage * $resultsPerPage;
00135 if ($this->endIdx > $resultCount) {
00136 $this->endIdx = $resultCount;
00137 }
00138 }
00139
00147 private function makeATag($page,$text, $extra = null) {
00148 global $_GET;
00149
00150 $link = '?page=' . (string)$page;
00151
00153 foreach($_GET as $name => $value) {
00154 if ($name == 'page'
00155 || ($extra && array_key_exists($name, $extra))) {
00156 continue;
00157 }
00158
00159 $link .= '&' . $name . '=' . $value;
00160 }
00161
00162 if ($extra) {
00163 foreach ($extra as $name => $value) {
00164 $link .= '&' . $name . '=' . $value;
00165 }
00166 }
00167
00168 return '<a href="' . $link . '">' . $text . '</a> ';
00169 }
00170
00176 private function makeDisabledATag($text) {
00177 return '<a class="disabled">' . $text . '</a>';
00178 }
00179
00187 public function makeJumpToPageControls($pageFrom,$pageTo,$currentPage) {
00188
00189 $controls = '';
00190
00191 for ($page = (int)$pageFrom;$page <= (int)$pageTo;$page++) {
00192 if ($currentPage != $page) {
00193 $controls .= $this->makeATag($page,(string)$page). " ";
00194 } else {
00195 $controls .= (string)$page . " ";
00196 }
00197 }
00198
00199 return $controls;
00200 }
00201
00203 protected function renderControls() {
00204
00205
00206
00207
00208 $controls = "";
00209
00210 if ($this->isMobile) {
00211 if ($this->currentPage != 1) {
00212 $controls .= $this->makeATag($this->currentPage - 1,'前の10件');
00213 } else {
00214 $controls .= $this->makeDisabledATag('前の10件') . ' ';
00215 }
00216
00217 $controls .= '|'.' ';
00218
00219 if ($this->currentPage != $this->pageCount) {
00220 $controls .= $this->makeATag($this->currentPage + 1,'次の10件 ');
00221 } else {
00222 $controls .= $this->makeDisabledATag('次の10件 ') . ' ';
00223 }
00224 } else {
00225 if ($this->currentPage != 1) {
00226 $controls .= $this->makeATag(1,'最初');
00227 } else {
00228 $controls .= $this->makeDisabledATag('最初');
00229 }
00230 $controls .= '|'.' ';
00231
00232 if ($this->currentPage != 1) {
00233 $controls .= $this->makeATag($this->currentPage - 1,'前へ');
00234 } else {
00235 $controls .= $this->makeDisabledATag('前へ');
00236 }
00237
00238 $controls .= '|'. ' ';
00239
00240
00241
00242
00243
00244 if ($this->maxPages >= $this->pageCount) {
00245
00246
00247
00248 $controls .= $this->makeJumpToPageControls(1,$this->pageCount,$this->currentPage);
00249 } else if ($this->currentPage <= $this->maxPages / 2 + 1) {
00250
00251
00252
00253 $controls .= $this->makeJumpToPageControls(1,$this->maxPages,$this->currentPage);
00254 $controls .= '...';
00255 } else if ($this->currentPage > $this->pageCount - $this->maxPages / 2) {
00256
00257
00258
00259 $controls .= '...'. ' ';
00260 $controls .= $this->makeJumpToPageControls(
00261 $this->pageCount - $this->maxPages + 1,
00262 $this->pageCount,
00263 $this->currentPage
00264 );
00265 } else {
00266
00267
00268
00269 $controls .= '...'. ' ';
00270 $startPage = ceil($this->currentPage - $this->maxPages/2);
00271 $controls .= $this->makeJumpToPageControls(
00272 $startPage,
00273 $startPage + $this->maxPages - 1,
00274 $this->currentPage
00275 );
00276 $controls .= '...'. ' ';
00277 }
00278
00279 $controls .= '|'.' ';
00280
00281 if ($this->currentPage != $this->pageCount) {
00282 $controls .= $this->makeATag($this->currentPage + 1,'次へ');
00283 } else {
00284 $controls .= $this->makeDisabledATag('次へ');
00285 }
00286
00287 $controls .= '|'.' ';
00288
00289 if ($this->currentPage != $this->pageCount) {
00290 $controls .= $this->makeATag($this->pageCount,'最後');
00291 } else {
00292 $controls .= $this->makeDisabledATag('最後');
00293 }
00294 }
00295
00296 return $controls;
00297 }
00298
00305 private function renderSortByPrice($html) {
00306
00307 $sortByMinPrice = $this->makeATag($this->currentPage,'料金が安い順',
00308 array('sort_by' => 'minprice', 'sort_order' => 'asc'));
00309 $sortByMaxPrice = $this->makeATag($this->currentPage,'料金が高い順',
00310 array('sort_by' => 'minprice', 'sort_order' => 'desc'));
00311
00312
00313
00314
00315
00316
00317
00318
00319
00320
00321
00322
00323
00324 $html = str_replace('${SORT_BY_MIN_PRICE}', $sortByMinPrice, $html);
00325 $html = str_replace('${SORT_BY_MAX_PRICE}', $sortByMaxPrice, $html);
00326
00327 return $html;
00328 }
00329
00336 private function renderSortByRelevance($html) {
00337 $sortByRelevance = $this->makeATag($this->currentPage,'高い',
00338 array('sort_by' => 'score', 'sort_order' => 'desc'));
00339
00340 $html = str_replace('${SORT_BY_RELEVANCE}', $sortByRelevance, $html);
00341
00342 return $html;
00343 }
00344
00350 public function toString() {
00351 if (!$this->view) {
00352 return '';
00353 }
00354
00355 $html = $this->view;
00356 $html = str_replace('${TOTAL_COUNT}',(string)$this->resultCount, $html);
00357 $html = str_replace('${START}',(string)$this->startIdx, $html);
00358 $html = str_replace('${END}',(string)$this->endIdx, $html);
00359 $html = str_replace('${CONTROLS}',$this->renderControls(), $html);
00360
00361 $html = $this->renderSortByPrice($html);
00362
00363
00364 if ($this->isMobile) {
00365 $html = $this->renderSortByPrice($html);
00366 $html = $this->renderSortByRelevance($html);
00367 }
00368
00369 return $html;
00370 }
00371 }
00372 ?>