29 import(
'Dataface/QueryBuilder.php');
30 import(
'Dataface/Table.php');
31 import(
'Dataface/LinkTool.php');
33 $GLOBALS[
'Dataface_ResultController_limit'] = 20;
34 $GLOBALS[
'Dataface_ResultController_skip'] = 0;
72 $this->_baseUrl = $baseUrl ? $baseUrl :
$_SERVER[
'PHP_SELF'];
76 $this->_fields =& $this->_table->fields();
79 $this->_query =&
$app->getQuery();
82 $this->_resultSet =&
$app->getResultSet();
86 if ( !is_array($query) ){
87 $query = array(
"-mode"=>
$app->_conf[
'default_mode']);
95 if ( !isset( $query[
'-limit']) ){
96 $query[
'-limit'] =
$GLOBALS[
'Dataface_ResultController_limit'];
98 if ( !isset( $query[
'-skip']) ){
99 $query[
'-skip'] =
$GLOBALS[
'Dataface_ResultController_skip'];
101 if ( !isset( $query[
'-cursor'] ) ){
102 $query[
'-cursor'] = $query[
'-skip'];
104 if ( !isset( $query[
'-mode'] ) ){
105 $query[
'-mode'] =
$app->_conf[
'default_mode'];
109 $this->_query =& $this->_queryBuilder->_query;
113 foreach ($this->_fields as
$field){
114 if ( preg_match(
'/char/i', $field[
'Type']) ){
115 $this->_titleColumn = $field[
'name'];
120 if ( !isset($this->_titleColumn) ){
121 reset($this->_fields);
122 $field = current($this->_fields);
123 $this->_titleColumn = $field[
'name'];
127 $this->_pos = $query[
'-cursor'];
130 $this->_displayedRecords = $query[
'-limit'];
137 if ( !isset( $this->_records) ){
138 $this->_records = array();
140 $sql = $this->_queryBuilder->select();
142 $sqlStats = $this->_queryBuilder->select_num_rows();
144 $res = $db->query($sqlStats, $this->_table->db, null,
true);
146 $this->_totalRecords = $res[0][
'num'];
151 $res = $db->query($sql, $this->_table->db, null,
true);
153 if ( !$res and !is_array($res) ){
154 throw new Exception(
"An error occurred attempting to retrieve records from the database.: ".mysql_error($this->
db), E_USER_ERROR);
156 $this->_displayedRecords = count($res);
159 foreach ($res as $row){
160 $this->_records[] = $row;
176 $this->_baseUrl = $url;
180 if ( !isset($this->_basUrl) ){
181 $this->_baseUrl =
$_SERVER[
'PHP_SELF'];
190 if (!isset( $this->_totalRecords) ){
192 $res = $db->query($this->_queryBuilder->select_num_rows(), $this->_table->db, null,
true);
193 $this->_totalRecords = $res[0][
'num'];
216 for ( $i=0; $i<$totalRecords; $i+=$this->_query[
'-limit']){
217 $query = array(
"-skip"=>$i,
"-action"=>
"list");
219 $contents[$link] = $pageNumber++;
221 if ( $this->_resultSet->start() >= $i and $this->_resultSet->start() < ($i+$this->_query[
'-limit'])){
222 $selected_url = $link;
237 if (! isset( $this->_contentsList) ){
239 $this->_contentsList = array();
267 $titles = $this->_resultSet->getTitles(
false,
true,
false);
270 $len = count($titles);
274 $query = array(
"-cursor"=>
$index,
"-action"=>
'browse');
275 $query = array_merge( $this->_query, $query);
276 foreach ($query as $key=>$value) {
277 if ( $value === null ){
284 $this->_contentsList[ $link ] = $titles[
$index];
287 if (
$index == $this->_query[
'-cursor'] ){
288 $selected_url = $link;
318 $html =
'<select name="controller" class="jumpMenu" onchange="javascript:window.location=this.options[this.selectedIndex].value;">
323 $currentLink = preg_replace(
'/&?-limit=\d+/',
'', $currentLink);
326 foreach ($contents as $key=>$value){
328 $selected = $key==$selected_url ?
"selected" :
'';
330 <option value="'.$key.
'" '.$selected.
'>'.$value.
'</option>';
332 $html .=
'</select>';
343 $limitval = $this->_resultSet->limit();
344 }
else if (isset($_GET[
'-'.$prefix.
'limit'])){
345 $limitval = $_GET[
'-'.$prefix.
'limit'];
349 return '(Display <input type="text" value="'.$limitval.
'" onchange="window.location = \''.$currentLink.
'&-'.$prefix.
'limit=\'+this.value" size="3"/> Records per page)';
355 $query =&
$app->getQuery();
356 if ( !isset($this->type) ) $this->type = $query[
'-mode'];
358 switch ( $this->type ){
371 $start = $rs->start();
373 $limit = max($rs->limit(),1);
374 $found = $rs->found();
377 $pages_before = ceil(floatval($start)/floatval($limit));
378 $pages_after = ceil(floatval($found-$end-1)/floatval($limit));
379 $curr_page = $pages_before + 1;
380 $total_pages = $pages_before+$pages_after+1;
384 for ( $i = $curr_page; $i>max(0,$curr_page-5); $i-- ){
385 $pages[$i] =
$app->url(
'-'.$prefix.
'limit='.$limit.
'&-'.$prefix.
'skip='.max($i_start,0));
386 if ( $this->_baseUrl ) $pages[$i] = $this->_baseUrl.
'?'.substr($pages[$i], strpos($pages[$i],
'?')+1);
390 $i_start = $start+$limit;
391 for ($i = $curr_page+1; $i<=min($total_pages,$curr_page+5); $i++){
392 $pages[$i] =
$app->url(
'-'.$prefix.
'limit='.$limit.
'&-'.$prefix.
'skip='.$i_start);
393 if ( $this->_baseUrl ) $pages[$i] = $this->_baseUrl.
'?'.substr($pages[$i], strpos($pages[$i],
'?')+1);
399 if ( $curr_page > 1 ){
400 $pages2[df_translate(
'scripts.GLOBAL.LABEL_PREV',
'Prev')] = $pages[$curr_page-1];
403 foreach ( $pages as $pageno=>$pageval){
404 $pages2[$pageno] = $pageval;
407 if ( $curr_page < $total_pages ){
409 $pages2[df_translate(
'scripts.GLOBAL.LABEL_NEXT',
'Next')] = $pages[$curr_page+1];
411 $out = array(
'<ul class="resultController">');
412 $out[] =
'<li class="rs-description">'.df_translate(
'scripts.GLOBAL.MESSAGE_FOUND',
'Found '.$found.
' records', array(
'found'=>$found)).
' </li>';
413 foreach ($pages2 as $pageno=>$link){
414 if ( $pageno == $curr_page ) $selected =
' selected';
416 $out[] =
'<li class="'.$selected.
'"><a href="'.htmlspecialchars($link).
'">'.$pageno.
'</a></li>';
418 $appurl =
$app->url(
'');
419 $appurl = preg_replace(
'/[&\?]'.preg_quote(
'-'.$prefix.
'limit=').
'[^&]*/',
'', $appurl);
420 $appurl = preg_replace(
'/[&\?]'.preg_quote(
'-'.$prefix.
'skip=').
'[^&]*/',
'', $appurl);
421 $urlprefix = ( $this->_baseUrl ? $this->_baseUrl.
'?'.substr($appurl,strpos($appurl,
'?')+1) : $appurl);
422 $out[] =
'<li class="results-per-page"> '.df_translate(
'scripts.GLOBAL.LABEL_SHOWING',
'Showing').
' <input type="text" value="'.$limit.
'" onchange="window.location = \''.$urlprefix.
'&-'.$prefix.
'limit=\'+this.value" size="3"/>'.df_translate(
'scripts.GLOBAL.MESSAGE_RESULTS_PER_PAGE',
'Results per page');
425 return implode(
"\n",
$out);
433 <div class="resultController">
435 <div class="container"><div class="controllerJumpMenu">
442 <table class="forwardBackTable" width="100%" border="0" cellpadding="0" cellspacing="5"><tr><td width="33%" valign="top" align="left" bgcolor="#eaeaea">
444 </td><td width="34%" valign="top" align="center">
448 if ( $this->_query[
'-mode'] ==
'list' ) {
449 $html .=
'<br/>'.$this->limitField();
455 </td><td width="33%" valign="top" align="right" bgcolor="#eaeaea">
466 function getLinkHtml($pos, $linkId=null,$imgURL = null, $imgAlt=
"Go", $mode=null){
467 $mode = $mode ? $mode : $this->_query[
'-mode'];
468 if ( $pos<0 )
return '';
471 array(
'-cursor'=>$pos)
474 if ( $mode ==
'browse' ){
477 }
else if ( $mode ==
'list' ){
479 $to = min( $pos + $this->_resultSet->limit(), $this->_resultSet->found() );
480 $title =
"Records ".($from+1).
" to ".($to);
482 array(
'-skip'=>$from)
486 if ( $linkId !== null ){
487 $id =
" id=\"$linkId\"";
491 if ( isset($imgURL) ){
492 return '<a href="'.$url.
'"'.$id.
' title="'.$title.
'"><img src="'.$imgURL.
'" alt="'.$imgAlt.
'" /></a>';
494 return '<a href="'.$url.
'"'.$id.
' title="'.$title.
'">'.$imgAlt.
'</a>';
500 $img = DATAFACE_URL.
'/images/go-previous.png';
502 $mode = $mode ? $mode : $this->_query[
'-mode'];
504 if ( $mode ==
'browse'){
505 if ( $this->_resultSet->cursor()<=0 )
return '';
507 $this->_resultSet->cursor()-1,
512 }
else if ( $mode ==
'list') {
513 if ( $this->_resultSet->start()<=0 )
return '';
516 $this->_resultSet->start()-$this->_resultSet->limit(),
529 if ( !isset($img) ) $img = DATAFACE_URL.
'/images/go-next.png';
530 $mode = $mode ? $mode : $this->_query[
'-mode'];
531 if ( $mode==
'browse' && $this->_resultSet->cursor()+1 < $this->_resultSet->found()){
533 $this->_resultSet->cursor()+1,
539 }
else if ( $mode==
'list' and $this->_resultSet->end()+1 < $this->_resultSet->found() ) {
541 $this->_resultSet->end()+1,
554 if ( $this->_query[
'-mode'] ==
'list' ){
555 return "<b>Now Showing:</b> ".$this->getLinkHtml($this->_resultSet->start(),
'currentLink');
556 }
else if ($this->_query[
'-mode'] ==
'browse' ) {
558 return "<b>This Record: </b>".$this->getLinkHtml($this->_resultSet->cursor(),
'currentLink');
565 if ( $this->_query[
'-mode'] ==
'list' ){
569 echo
'<ul class="page-index">';
571 foreach ( $pages as $link=>$title ){
576 }
else if ( $link == $selected ){
577 echo
'<li class="selected-page">'.$title.
'</li>
580 echo
'<li><a href="'.$link.
'">'.$title.
'</a></li>
585 $out = ob_get_contents();