rtgx

Pokročilý zoznam stránok

Zadanie

  • zoznam stránok (určený počet na stranu)
  • položky: názov, dátum, autor, anotácia
  • navigácia v zozname (prvé, pred., ďalšie, posledné)
  • použitie šablóny pre rendering výstupu
  • nastavovanie vybraných premenných v TS setup

Riešenie

Použil som vložený PHP skritp vo forme objektu USER_INT.
Pozrite si výsledok >>

PHP skript (teaser.inc)
<?php

require_once(PATH_tslib.'class.tslib_pibase.php');

/**
* Pages teaser
*
* @author Robert Gonda <robert.gonda@gmail.com>
* @author_company BSP Magnetica, s.r.o.
*/
class user_teaser extends tslib_pibase {

var $prefixId = 'pageteaser';
var $conf;
var $id;
var $GPvars;
var $local_cObj;
var $out = '';
var $totalTemplate;
var $counter = 0;
var $pages = 0;
var $start = 0;

/**
* Configuration, setup vars
*
* @return void
*/
function config() {

// Create conf, classes
$this->conf['limit'] = intval( $this->conf['limit'] );
$this->conf['orderby'] = $this->conf['orderby'] ? $this->conf['orderby'] : 'pages.sorting, tt_content.sorting';
$this->id = intval( $GLOBALS['TSFE']->id );
$this->GPvars = t3lib_div::GPvar( $this->prefixId );
$this->local_cObj = t3lib_div::makeInstance( 'tslib_cObj' );
$this->totalTemplate = $this->cObj->fileResource( $this->conf['template'] );

if( $this->conf['level'] == 1 )
$this->pids = $this->getPagesUids();
elseif ($this->conf['level'] == 100)
$this->pids = $this->getPagesContentUids();
else
$this->pids = $this->id;
}

/**
* Pages teaser
*
* @param string $content:
* @param array $conf: TS configuration
* @return string return: HTML output
*/
function teaser( $content, $conf ) {

// Conf base
$this->conf = &$conf;
$this->config();

// SQL typo3 conditions
$this->where = $this->local_cObj->enableFields( 'pages' );
if ($this->conf['level'] == 100)
$this->where .= " AND pages.abstract <> '' ";
$this->whereContent = $this->local_cObj->enableFields( 'tt_content' );

// Get/set navigation variable
$this->countPages();
$this->GPvars['page'] = $this->GPvars['page'] > 0 ? intval( $this->GPvars['page'] ) : 0;
$this->pages = ceil( $this->counter / $this->conf['limit'] );
if( $this->GPvars['page'] > 0 && $this->GPvars['page'] < $this->pages )
$this->start = $this->GPvars['page'];
else
$this->GPvars['page'] = 0;

// Set limitation
if( $deflimit != '' )
$limit = 'LIMIT '.$deflimit;
elseif( $this->start > 0 )
$limit = 'LIMIT '.( $this->start * $this->conf['limit'] ).', '.$this->conf['limit'];
else
$limit = 'LIMIT 0, '.$this->conf['limit'];

// SQL query prepare
$query = '
SELECT pages.uid, pages.title, pages.author, pages.crdate, pages.starttime, pages.abstract,
tt_content.bodytext AS bodytext
FROM pages
LEFT JOIN tt_content ON ( tt_content.pid = pages.uid '.$this->whereContent.' )
WHERE pages.pid IN ( '.$this->pids.' ) '.$this->where.'
GROUP BY pages.uid
ORDER BY '.$this->conf['orderby'].'
'.$limit.'
';

// Get pages with content
$res = $GLOBALS['TYPO3_DB']->sql_query( $query );
if( $res ) {
$numrows = $GLOBALS['TYPO3_DB']->sql_num_rows( $res );
if( $numrows > 0 ) {

// Template, subpart, fields list
$template['total'] = $this->local_cObj->getSubpart( $this->totalTemplate, '###TEASER###' );
$template['item'] = $this->local_cObj->getSubpart( $template['total'], '###ITEM###' );

// Parse pages list
while( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {

// Page, content marks
$url = str_replace( '&', '&', $this->pi_getPageLink( $row['uid'], '_top', array() ) );
$submarks['###TITLE###'] = '<a href="'.$url.'" title="'.$row['title'].'">'.$row['title'].'</a>';
$submarks['###CRDATE###'] = date( 'd.m.Y', $row['crdate'] );
$submarks['###STARTTIME###'] = date( 'd.m.Y', $row['starttime'] );
$submarks['###AUTHOR###'] = $row['author'];
$submarks['###BODYTEXT###'] = $this->cObj->stdWrap( ( $row['abstract'] != '' ? $row['abstract'] : $row['bodytext'] ), $this->conf['bodytext.'] );

// Complete subpart
$rows .= $this->cObj->substituteMarkerArrayCached( $template['item'], $submarks, array(), $wrappedSubSubpart );
}

// Navigation subpart
if( $this->conf['navigation'] ) {
$template['pages'] = $this->local_cObj->getSubpart( $template['total'], '###PAGE_LISTING###' );
$template['pages_list'] = $this->local_cObj->getSubpart( $template['pages'], '###PAGES_LIST###' );
$navsubparts = array();
$navmarks = array();
$navwrappedSubpart = array();
if( $this->parseNavigation( $template['pages_list'], $navsubparts, $navmarks, $navwrappedSubpart ) )
$subparts['###PAGE_LISTING###'] = $this->cObj->substituteMarkerArrayCached( $template['pages'], $navmarks, $navsubparts, $navwrappedSubpart );
else
$subparts['###PAGE_LISTING###'] = '';
}
else {
$subparts['###PAGE_LISTING###'] = '';
}

// Complete all subparts
$subparts['###ITEM###'] = $rows;
$template = $this->cObj->substituteMarkerArrayCached( $template['total'], $marks, $subparts, array() );
}
}
return $template;
}

/**
* Pages counter
*
* @return int return: count of pages
*/
function countPages() {

// SQL query for count items witout limit
$query = '
SELECT COUNT(*) AS counter
FROM pages
WHERE pages.pid IN ( '.$this->pids.' ) '.$this->where.'
';

// Get pages with content
$res = $GLOBALS['TYPO3_DB']->sql_query( $query );
if( $res ) {
$tmp = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res );
$this->counter = intval( $tmp['counter'] );
}
return $this->counter;
}

/**
* Pages uids list from actual page
*
* @return string return: pages.uids comma separated list
*/
function getPagesUids() {

$query = '
SELECT uid FROM pages
WHERE pid = '.$this->id.' '.$this->where.' AND doktype < 250
';

// Get pages with content
$res = $GLOBALS['TYPO3_DB']->sql_query( $query );
if( $res ) {
while( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
$tmp[] = $row['uid'];
}
if( is_array( $tmp ) ) {
$uids = implode( ',', $tmp );
}
}
return $uids;
}

/**
* Pages uids from pages with content
*
* @return string return: pages.uids comma separated list
*/
function getPagesContentUids() {

$query = '
SELECT DISTINCT p.pid FROM pages p
WHERE p.abstract<>\'\' '.$this->where.' AND p.doktype < 250
';
// Get pages with content
$res = $GLOBALS['TYPO3_DB']->sql_query( $query );
if( $res ) {
while( $row = $GLOBALS['TYPO3_DB']->sql_fetch_assoc( $res ) ) {
$tmp[] = $row['pid'];
}
if( is_array( $tmp ) ) {
$uids = implode( ',', $tmp );
}
}
return $uids;
}

/**
* Navigation
*
* @param array $template: template subpart for navigation pages list
* @param array &$subparts:
* @param array &$marks:
* @param array &$wrappedSubpart:
* @return void
*/
function parseNavigation( $template, &$subparts, &$marks, &$wrappedSubpart ) {

// Search params
$params = array();
$searchParams = array();

// More than one pages
if( $this->counter > $this->conf['limit'] ) {

// Prev marks, link
if( $this->start > 0 ) {
$params = $searchParams;
// Link to first page
$marks['###PAGE_START###'] = '<<';
$params[$this->prefixId.'[page]'] = 0;
$link = '<a href="'.str_replace( '&', '&', $this->pi_getPageLink( $this->id, '_self', $params ) ).'" target="_self" title="'.$marks['###PAGE_START###'].'">';
$wrappedSubpart['###LINK_START###'] = array( $link, '</a>' );
// Link to previous page
$marks['###PAGE_PREV###'] = '<';
$params[$this->prefixId.'[page]'] = $this->start - 1;
$link = '<a href="'.str_replace( '&', '&', $this->pi_getPageLink( $this->id, '_self', $params ) ).'" target="_self" title="'.$marks['###PAGE_PREV###'].'">';
$wrappedSubpart['###LINK_PREV###'] = array( $link, '</a>' );
}
else {
$marks['###PAGE_START###'] = '<<';
$wrappedSubpart['###LINK_START###'] = '';
$marks['###PAGE_PREV###'] = '<';
$wrappedSubpart['###LINK_PREV###'] = '';
}

// Next marks, link
if( $this->start < ( $this->pages - 1 ) ) {
$params = $searchParams;
// Link to last page
$marks['###PAGE_END###'] = '>>';
$params[$this->prefixId.'[page]'] = $this->pages - 1;
$link = '<a href="'.str_replace( '&', '&', $this->pi_getPageLink( $this->id, '_self', $params ) ).'" target="_self" title="'.$marks['###PAGE_END###'].'">';
$wrappedSubpart['###LINK_END###'] = array( $link, '</a>' );
// Link to next page
$marks['###PAGE_NEXT###'] = '>';
$params[$this->prefixId.'[page]'] = $this->start + 1;
$link = '<a href="'.str_replace( '&', '&', $this->pi_getPageLink( $this->id, '_self', $params ) ).'" target="_self" title="'.$marks['###PAGE_NEXT###'].'">';
$wrappedSubpart['###LINK_NEXT###'] = array( $link, '</a>' );
}
else {
$marks['###PAGE_END###'] = '>>';
$wrappedSubpart['###LINK_END###'] = '';
$marks['###PAGE_NEXT###'] = '>';
$wrappedSubpart['###LINK_NEXT###'] = '';
}

// Pages list
$p = intval( $this->GPvars['page'] / 10 ) * 10;
for( $i = 0; $p < $this->pages && $i < 10; $p++, $i++ ) {
$pageNumber = $p;
$marksP['###PAGE###'] = $pageNumber + 1;
// Link to page
if( $pageNumber != $this->GPvars['page'] ) {
$params = $searchParams;
$params[$this->prefixId.'[page]'] = $pageNumber;
$link = '<a href="'.str_replace( '&', '&', $this->pi_getPageLink( $this->id, '_self', $params ) ).'" target="_self" title="'.$marksP['###PAGE###'].'">';
$wrappedSubpartP['###LINK_PAGE###'] = array( $link, '</a>' );
}
else {
$wrappedSubpartP['###LINK_PAGE###'] = array( '<strong>', '</strong>' );
}
$rows .= $this->cObj->substituteMarkerArrayCached( $template, $marksP, array(), $wrappedSubpartP );
}

// Complete all subparts
$subparts['###PAGES_LIST###'] = $rows;
return true;
}
return false;
}
}

?>
HTML šablóna (teaser.html)
<!-- ###TEASER### -->

<!-- ###ITEM### -->
<p class="date">###STARTTIME###</p>
<h2 class="header2">###TITLE###</h2>
<p class="author">###AUTHOR###</p>
<p>###BODYTEXT###</p>
<!-- ###ITEM### -->

<!-- ###PAGE_LISTING### begin -->
<table class="navigation noBorder">
<tbody><tr>
<td><!-- ###LINK_START### -->###PAGE_START###<!-- ###LINK_START### --></td>
<td><!-- ###LINK_PREV### -->###PAGE_PREV###<!-- ###LINK_PREV### --></td>
<!-- ###PAGES_LIST### -->
<td><!-- ###LINK_PAGE### -->###PAGE###<!-- ###LINK_PAGE### --></td>
<!-- ###PAGES_LIST### -->
<td><!-- ###LINK_NEXT### -->###PAGE_NEXT###<!-- ###LINK_NEXT### --></td>
<td><!-- ###LINK_END### -->###PAGE_END###<!-- ###LINK_END### --></td>
</tr></tbody>
</table>
<!-- ###PAGE_LISTING### end -->

<!-- ###TEASER### -->
TS setup
includeLibs.teaser = fileadmin/sitename/inc/teaser.inc

temp.teaser = USER_INT
temp.teaser {
userFunc = user_teaser->teaser
template = fileadmin/sitename/tmpl/teaser.html
orderby = pages.sorting, tt_content.sorting
limit = 10
level = 0
navigation = 1
bodytext {
wrap = |
stripHtml = 1
crop = 330|...|1
}
}

[globalVar = TSFE:id=1]
temp.teaser {
orderby = pages.starttime DESC, tt_content.sorting
limit = 5
level = 1
navigation = 0
}
[global]

...
page.10.marks {
CONTENT = COA
CONTENT {
10 < styles.content.get
20 < temp.teaser
}

Výsledok (screenshot)

Zoznam stránok s navigáciou
 
Žiadne dokumenty ani odkazy k tejto stránke.