00001 <?php
00002
00003
00004
00005
00006
00007
00008 class WidgetPagerCalculator implements IPolicyHolder {
00009 protected $data;
00010 protected $policy;
00011
00012
00013
00014 protected $adapter;
00015
00016
00017
00018
00019
00020
00021
00022
00023 public function get_data_item($key, $default) {
00024 return Arr::get_item($this->data, $key, $default);
00025 }
00026
00027
00028
00029
00030
00031
00032
00033 public function set_data($data) {
00034 $this->data = $data;
00035 $this->adapter = $this->get_data_item('adapter', new PagerDefaultAdapter($data['page_data']));
00036 }
00037
00038
00039
00040
00041
00042
00043 public function get_policy() {
00044 return $this->policy;
00045 }
00046
00047
00048
00049
00050
00051
00052 public function set_policy($policy) {
00053 $this->policy = $policy;
00054 }
00055
00056
00057
00058
00059
00060
00061
00062 public function has_policy($policy) {
00063 return Common::flag_is_set($this->policy, $policy);
00064 }
00065
00066
00067
00068
00069
00070 public function get_current_page() {
00071 return $this->data['page'];
00072 }
00073
00074
00075
00076
00077
00078 public function get_total_pages() {
00079 return $this->data['pages_total'];
00080 }
00081
00082
00083
00084
00085
00086
00087
00088
00089 public function get_previous_link($policy = WidgetPager::NONE, $cls = '') {
00090 $text = tr('< Previous Page', array('app', 'core'));
00091 $link = Arr::get_item($this->data, 'previous_link', '');
00092 return $this->get_next_prev_link($link, $text, $cls, $policy);
00093 }
00094
00095
00096
00097
00098
00099
00100
00101
00102 public function get_next_link($policy = WidgetPager::NONE, $cls = '') {
00103 $text = tr('Next Page >', array('app', 'core'));
00104 $link = Arr::get_item($this->data, 'next_link', '');
00105 return $this->get_next_prev_link($link, $text, $cls, $policy);
00106 }
00107
00108
00109
00110
00111
00112
00113
00114
00115
00116 public function get_page_links_array($total = 0, $policy = WidgetPager::NONE, $gap = '...') {
00117
00118 $pages = $this->strip_pages($this->data['page'], $this->data['pages_total'], $total, $policy);
00119 $ret = array();
00120 foreach ($pages as $page) {
00121 $text = String::escape($page['page']);
00122 if ($page['url']) {
00123 $ret[] = html::a($text, $page['url'], tr('Show page %page', array('app', 'core'), array('%page' => $text)));
00124 } else {
00125 if ($text === '') {
00126 $ret[] = $gap;
00127 }
00128 else {
00129 $ret[] = html::span($text, 'currentpage');
00130 }
00131 }
00132 }
00133 return $ret;
00134 }
00135
00136
00137
00138
00139
00140
00141
00142
00143 public function get_page_x_of_n($policy = WidgetPager::NONE, $cls = '') {
00144 $policy |= $this->policy;
00145 $ret = '';
00146 if (!Common::flag_is_set($policy, WidgetPager::NO_PAGE_X_OF_N)) {
00147 $ret = tr('Page %page of %total', array('app', 'core'), array('%page' => $this->data['page'], '%total' => $this->data['pages_total']));
00148 }
00149 return $ret;
00150 }
00151
00152
00153
00154
00155 public function get_page_url($page) {
00156 if ($page != $this->get_data_item('page', 0)) {
00157 return $this->adapter->get_url_for_page($page);
00158 } else {
00159 return '';
00160 }
00161 }
00162
00163
00164
00165
00166
00167
00168
00169
00170
00171
00172
00173 public function get_page_link($page, $text, $cls = '', $policy = self::NONE) {
00174 $policy |= $this->policy;
00175 $ret = '';
00176 if ($page != $this->get_current_page()) {
00177 $ret .= html::a($text, $this->get_page_url($page), '', array('class' => $cls));
00178 }
00179 else if (!Common::flag_is_set($policy, WidgetPager::HIDE_INACTIVE_LINKS)) {
00180 $ret .= html::span($text, $cls);
00181 }
00182 return $ret;
00183 }
00184
00185
00186
00187
00188
00189
00190
00191
00192
00193 protected function get_next_prev_link($link, $text, $cls, $policy) {
00194 $policy |= $this->policy;
00195 $ret = '';
00196 if ($link !== '') {
00197 $ret .= html::a($text, $link, '', array('class' => $cls));
00198 }
00199 else if (!Common::flag_is_set($policy, WidgetPager::HIDE_INACTIVE_LINKS)) {
00200 $ret .= html::span($text, $cls);
00201 }
00202 return $ret;
00203 }
00204
00205
00206
00207
00208
00209
00210
00211
00212
00213
00214
00215 protected function strip_pages($current_page, $total_pages, $num_extract, $policy) {
00216 $gap = $this->create_page_array_item('');
00217 $policy |= $this->policy;
00218 $ret = array();
00219
00220 $current_page_index = $current_page - 1;
00221 $total_pages_index = $total_pages - 1;
00222
00223 $num_extract -= 1;
00224
00225
00226
00227 $first_page_index = $current_page_index - ceil($num_extract / 2.0);
00228 $last_page_index = $current_page_index + floor($num_extract / 2.0);
00229
00230
00231 if ( !Common::flag_is_set($policy, WidgetPager::NO_FORCE_TOTAL_LINKS)) {
00232 if ($first_page_index < 0) {
00233 $last_page_index -= $first_page_index;
00234 $first_page_index = 0;
00235 }
00236 if ($last_page_index > $total_pages_index) {
00237 $first_page_index -= ($last_page_index - $total_pages_index);
00238 $last_page_index = $total_pages_index;
00239 }
00240 }
00241
00242 if ($first_page_index < 0) { $first_page_index = 0; }
00243 if ($last_page_index > $total_pages_index) { $last_page_index = $total_pages_index; }
00244
00245 $strip = !Common::flag_is_set($policy, WidgetPager::NO_STRIP);
00246 if ($strip) {
00247
00248 if ($first_page_index > 0) {
00249 $ret[] = $this->create_page_link_array_item(1);
00250 }
00251 if ($first_page_index > 1) {
00252 $ret[] = $gap;
00253 }
00254 }
00255
00256
00257 for ($i = $first_page_index; $i < $current_page_index; $i++) {
00258 $ret[] = $this->create_page_link_array_item($i + 1);
00259 }
00260
00261 $ret[] = $this->create_page_array_item($current_page);
00262
00263 for ($i = $current_page_index + 1; $i <= $last_page_index; $i++) {
00264 $ret[] = $this->create_page_link_array_item($i + 1);
00265 }
00266
00267 if ($strip) {
00268
00269 if ($last_page_index < $total_pages_index - 1) {
00270 $ret[] = $gap;
00271 }
00272 if ($last_page_index < $total_pages_index ) {
00273 $ret[] = $this->create_page_link_array_item($total_pages_index + 1);
00274 }
00275 }
00276
00277 return $ret;
00278 }
00279
00280
00281
00282
00283
00284
00285
00286 protected function create_page_array_item($text) {
00287 return array(
00288 'page' => $text,
00289 'url' => ''
00290 );
00291 }
00292
00293
00294 protected function create_page_link_array_item($page) {
00295 return array(
00296 'page' => $page,
00297 'url' => $this->get_page_url($page)
00298 );
00299 }
00300 }
00301
00302
00303
00304
00305
00306
00307
00308 class WidgetPager implements IWidget {
00309 const HIDE_INACTIVE_LINKS = 128;
00310 const NO_STRIP = 256;
00311 const NO_PAGE_X_OF_N = 512;
00312 const NO_FORCE_TOTAL_LINKS = 1024;
00313 const DONT_INDEX_PAGE_2PP = 2048;
00314 const DONT_CHANGE_TITLE = 4096;
00315 const DONT_ADD_BREADCRUMB = 8192;
00316
00317 public $data;
00318
00319 public static function output($data, $policy = self::NONE) {
00320 $w = new WidgetPager($data);
00321 return $w->render($policy);
00322 }
00323
00324 public function __construct($data) {
00325 $this->data = $data;
00326 }
00327
00328 public function render($policy = self::NONE) {
00329 if (Arr::get_item($this->data, 'pages_total', 0) <= 1) {
00330 return '';
00331 }
00332
00333 $policy = $policy | ($this->data['policy'] * 2048);
00334 $calculator = Arr::get_item($this->data, 'calculator', Config::get_value(Config::PAGER_CALCULATOR));
00335 if (!$calculator instanceof WidgetPagerCalculator) {
00336 $calculator = new WidgetPagerCalculator();
00337 }
00338
00339 $calculator->set_data($this->data);
00340 $calculator->set_policy($policy);
00341
00342 $view = ViewFactory::create_view(IViewFactory::MESSAGE, 'widgets/pager.meta');
00343 $view->assign('pager_calculator', $calculator);
00344 $view->assign('page_data', $this->data['page_data']);
00345 $view->render();
00346
00347 $view = ViewFactory::create_view(IViewFactory::MESSAGE, 'widgets/pager.breadcrumb');
00348 $view->assign('pager_calculator', $calculator);
00349 $view->assign('page_data', $this->data['page_data']);
00350 $view->render();
00351
00352 $view = ViewFactory::create_view(IViewFactory::MESSAGE, 'widgets/pager');
00353 $view->assign('pager_calculator', $calculator);
00354 $view->assign('page_data', $this->data['page_data']);
00355 return $view->render();
00356 }
00357 }