1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377
<?php
/*-----8<--------------------------------------------------------------------
*
* BEdita - a semantic content management framework
*
* Copyright 2008 ChannelWeb Srl, Chialab Srl
*
* This file is part of BEdita: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published
* by the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* BEdita is distributed WITHOUT ANY WARRANTY; without even the implied
* warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU Lesser General Public License for more details.
* You should have received a copy of the GNU Lesser General Public License
* version 3 along with BEdita (see LICENSE.LGPL).
* If not, see <http://gnu.org/licenses/lgpl-3.0.html>.
*
*------------------------------------------------------------------->8-----
*/
/**
* Table toolbar for pagination and search helper
*
* @version $Revision$
* @modifiedby $LastChangedBy$
* @lastmodified $LastChangedDate$
*
* $Id$
*/
class BeToolbarHelper extends AppHelper {
/**
* Included helpers.
*
* @var array
*/
var $helpers = array('Form', 'Html');
var $tags = array(
'with_text' => '<span %s >%s</span>',
'without_text' => '<span %s />'
) ;
/**
* initialize toolbar in params['toolbar']
*
* @param array $toolbar
*/
public function init(&$toolbar) {
$this->params['toolbar'] = $toolbar;
}
/**
* Return the link (html anchor tag) for the next page
*
* @param string $title Label link
* @param array $option HTML attributes for link
* @param string $disabledTitle Label link disabled
* @param array $disabledOption HTML attributes for link disabled
* (if present, insert a tag SPAN)
* @return string
*/
public function next($title = ' > ', $options = array(), $disabledTitle = ' > ', $disabledOption = array()) {
return $this->_scroll('next', $title, $options, $disabledTitle, $disabledOption) ;
}
/**
* Return the link (html anchor tag) for the previous page
*
* @param string $title Label link
* @param array $option HTML attributes for link
* @param string $disabledTitle Label link disabled
* @param array $disabledOption HTML attributes for link disabled
* (if present, insert a tag SPAN)
* @return string
*/
public function prev($title = ' < ', $options = array(), $disabledTitle = ' < ', $disabledOption = array()) {
return $this->_scroll('prev', $title, $options, $disabledTitle, $disabledOption) ;
}
/**
* Return the link (html anchor tag) for the first page
*
* @param string $title Label link
* @param array $option HTML attributes for link
* @param string $disabledTitle Label link disabled
* @param array $disabledOption HTML attributes for link disabled
* (if present, insert a tag SPAN)
* @return string
*/
public function first($title = ' |< ', $options = array(), $disabledTitle = ' |< ', $disabledOption = array()) {
return $this->_scroll('first', $title, $options, $disabledTitle, $disabledOption) ;
}
/**
* Return the link (html anchor tag) for the last page
*
* @param string $title Label link
* @param array $option HTML attributes for link
* @param string $disabledTitle Label link disabled
* @param array $disabledOption HTML attributes for link disabled
* (if present, insert a tag SPAN)
* @return string
*/
public function last($title = ' >| ', $options = array(), $disabledTitle = ' >| ', $disabledOption = array()) {
return $this->_scroll('last', $title, $options, $disabledTitle, $disabledOption) ;
}
/**
* Return number of records found
*
* @return mixed int|string
*/
public function size() {
return (isset($this->params['toolbar']['size'])?$this->params['toolbar']['size']:"" ) ;
}
/**
* Return current page
*
* @return mixed int|string
*/
public function current() {
return (isset($this->params['toolbar']['page'])?$this->params['toolbar']['page']:"" ) ;
}
/**
* Return total number of pages
*
* @return mixed int|string
*/
public function pages() {
return (isset($this->params['toolbar']['pages'])?$this->params['toolbar']['pages']:"" ) ;
}
/**
* View page size html select tag
*
* @param array $htmlAttributes associative Array with HTML attributes
* @param array $options Array. Default: 1, 5, 10,20, 50, 100
* @return string
*/
public function changeDim($htmlAttributes = array(), $options = array(1, 5, 10, 20, 50, 100)) {
if(!isset($this->params['toolbar']['dim'])) return "" ;
// Define script for page change
$data = $this->getPassedArgs();
unset($data["page"]);
unset($data["dim"]);
$url = $this->getUrl($data);
$htmlAttributes['onchange'] = "document.location = '{$url}'+'/dim:'+ this[this.selectedIndex].value +'/page:1'" ;
$tmp = array() ;
foreach ($options as $k) $tmp[$k] = $k ;
$options = $tmp ;
return $this->Form->select("", $options, $this->params['toolbar']['dim'], $htmlAttributes, false) ;
}
/**
* return dropdown list with changed dimension
*
* @param string $selectId
* @param array $htmlAttributes
* @param array $options
* @return string
*/
public function changeDimSelect($selectId, $htmlAttributes = array(), $options = array(1 => 1, 5 => 5, 10 => 10, 20 => 20, 50 => 50, 100 => 100)) {
if(!isset($this->params['toolbar']['dim'])) return "" ;
// Define script for page change
$data = $this->getPassedArgs();
unset($data["page"]);
unset($data["dim"]);
$url = $this->getUrl($data);
if($this->params["action"] == "index" && !preg_match("/\/(index$|index\/)/i", $url )) {
$url .= "/".$this->params["action"];
}
$htmlAttributes['onchange'] = "document.location = '{$url}'+'/dim:'+ this[this.selectedIndex].value" ;
return $this->Form->select($selectId, $options, $this->params['toolbar']['dim'], $htmlAttributes, false) ;
}
/**
* Change selected page
*
* @param array $htmlAttributes associative Array with HTML attributes
* @param array $items number of available pages, before and after current. Default: 5
* @return string
*/
public function changePage($htmlAttributes = array(), $items = 5) {
if(!isset($this->params['toolbar']['page'])) return "" ;
// Define script for page change
$data = $this->getPassedArgs();
unset($data["page"]);
$url = $this->getUrl($data);
$htmlAttributes['onchange'] = "document.location = '{$url}'+'/page:'+ this[this.selectedIndex].value" ;
// Define the number of pages available
$pages = array() ;
for($i = $this->params['toolbar']['page']; $i >= 1 ; $i--) {
$pages[] = $i ;
}
for($i = $this->params['toolbar']['page']; $i <= $this->params['toolbar']['pages'] ; $i++) {
$pages[] = $i ;
}
sort($pages) ;
// View select
$tmp = array() ;
foreach ($pages as $k) $tmp[$k] = $k ;
$pages = $tmp ;
return $this->Form->select("", $pages, $this->params['toolbar']['page'], $htmlAttributes, false) ;
}
/**
* Change page in a dropdown list of results
*
* @param string $selectId
* @param array $htmlAttributes
* @param array $items
* @return string
*/
public function changePageSelect($selectId, $htmlAttributes = array(), $items = 5) {
if(!isset($this->params['toolbar']['page'])) return "" ;
// Define script for page change
$data = $this->getPassedArgs();
unset($data["page"]);
$url = $this->getUrl($data);
if($this->params["action"] == "index" && !preg_match("/\/(index$|index\/)/i", $url)) {
$url .= "/".$this->params["action"];
}
$htmlAttributes['onchange'] = "document.location = '{$url}'+'/page:'+ this[this.selectedIndex].value" ;
// Define the number of pages available
$pages = array() ;
for($i = $this->params['toolbar']['page']; $i >= 1 ; $i--) {
$pages[] = $i ;
}
for($i = $this->params['toolbar']['page']; $i <= $this->params['toolbar']['pages'] ; $i++) {
$pages[] = $i ;
}
sort($pages) ;
// View select
$tmp = array() ;
foreach ($pages as $k) $tmp[$k] = $k ;
$pages = $tmp ;
return $this->Form->select($selectId, $pages, $this->params['toolbar']['page'], $htmlAttributes, false) ;
}
/**
* Change list order
*
* @param string $field Field for the "order by"
* @param string $title Title for the link. Default: field name
* @param array $htmlAttributes associative Array with HTML attributes
* @param boolean $dir Se presente impone la direzione. 1: ascending, 0: descending
* otherwise, !(<current value>)
* @return string
*/
public function order($field, $title="", $image="", $htmlAttributes = array(), $dir=null) {
if(!isset($this->params['toolbar'])) return "" ;
$data = $this->getPassedArgs();
if(isset($data['order']) && $data['order'] == $field) {
if(!isset($dir)) $dir = (isset($data['dir'])) ? (!$data['dir']) : true ;
if ($dir == 1) {
$class = "SortUp desc";
}
else {
$class = "SortUp asc";
}
} else {
if(!isset($dir)) $dir = true ;
$class = "";
}
// Crea l'url
$data['order'] = $field ;
$data['dir'] = (integer)$dir ;
$url = $this->getUrl($data);
if (!empty($image)) {
$htmlAttributes["alt"] = __($htmlAttributes["alt"], true);
$title = $this->Html->image($image, $htmlAttributes);
} else {
$title = __($title, true);
}
return '<a class="'.$class.'" href="' . htmlentities($url) . '">' . $title . '</a>';
}
/**
* Return the link (html anchor tag) for the page $where
*
* @param string $where page target (next, prev, first, last)
* @param string $title Label link
* @param array $option HTML attributes for link
* @param string $disabledTitle Label link disabled
* @param array $disabledOption HTML attributes for link disabled
* (if present, insert a tag SPAN)
* @return string
*/
private function _scroll($where, $title, $options, $disabledTitle, $disabledOption) {
$page = (isset($this->params['toolbar'][$where]))? $this->params['toolbar'][$where] : false ;
// Next page not found or toolbar not found, link disabled
if(!$page) {
return $this->_output($disabledTitle, $disabledOption) ;
}
// Create url
$data = $this->getPassedArgs();
$data['page'] = $page ;
$url = $this->getUrl($data);
return '<a title="go to '.$where.' page" href="' . $url . '">' . __($title, true) . '</a>';
}
/**
* return output for text
*
* @param string $text
* @param array $options
* @return string
*/
private function _output($text, $options) {
return $this->output(
sprintf(
(($text)?$this->tags['with_text']:$this->tags['without_text']),
$this->_parseAttributes($options, null, ' ', ''), __($text,true)
)
);
}
/**
* get array arguments
*
* @param array $otherParams
* @return array
*/
public function getPassedArgs($otherParams=array()) {
if (empty($otherParams))
return array_merge($this->params["pass"], $this->params["named"]);
else
return array_merge($this->params["pass"], $this->params["named"], $otherParams);
}
/**
*
* Fix the parameter $data to correct work with plugin modules.
* It must be manually removed from the data array
*
* Enter description here ...
* @param unknown_type $data
*/
private function getUrl ($data) {
$data['plugin'] = "";
return Router::url($data) ;
}
}
?>