2013-02-01 01:32:43 -08:00
< ? php
/*
* e107 website system
*
* Copyright ( C ) 2008 - 2013 e107 Inc ( e107 . org )
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
*/
if ( ! defined ( 'e107_INIT' )) { exit ; }
class page_shortcodes extends e_shortcode
{
2013-05-29 15:33:39 -07:00
protected $request ;
function __construct ()
2013-02-01 01:32:43 -08:00
{
2017-01-25 13:08:53 -08:00
2016-02-28 20:50:09 -08:00
$this -> request = e107 :: getRegistry ( 'core/page/request' );
2013-05-29 15:33:39 -07:00
2013-05-30 14:16:49 -07:00
$action = varset ( $this -> request [ 'action' ]);
if (( $action == 'listPages' || $action == 'listChapters' ) && vartrue ( $this -> request [ 'id' ]))
2013-05-29 15:33:39 -07:00
{
2021-02-17 20:23:18 +01:00
$this -> var = e107 :: getDb () -> retrieve ( 'page_chapters' , 'chapter_id, chapter_name, chapter_meta_description, chapter_sef' , 'chapter_id = ' . intval ( $this -> request [ 'id' ]) . ' LIMIT 1' );
2013-06-01 14:03:18 -07:00
}
if ( $action == 'showPage' && vartrue ( $this -> request [ 'id' ])) // get chapter and description from current.
{
$query = " SELECT p.page_id,c.chapter_name,c.chapter_meta_description FROM #page AS p LEFT JOIN #page_chapters AS c ON p.page_chapter = c.chapter_id WHERE p.page_id = " . intval ( $this -> request [ 'id' ]) . " LIMIT 1 " ;
$rows = e107 :: getDb () -> retrieve ( $query , true );
$this -> var = $rows [ 0 ];
}
2013-05-29 15:33:39 -07:00
}
/**
* Page Navigation
2019-06-06 14:25:58 -07:00
* @ example { PAGE_NAVIGATION : template = navdoc & auto = 1 } in your Theme template .
* @ example { PAGE_NAVIGATION : chapter = 4 }
2022-03-11 18:01:56 -08:00
* @ example { PAGE_NAVIGATION : book = 3 & chapters = true & pages = true }
2013-05-29 15:33:39 -07:00
*/
2017-10-17 16:22:56 -07:00
function sc_page_navigation ( $parm = null ) // TODO when No $parm provided, auto-detect based on URL which book/chapters to display.
2013-05-29 15:33:39 -07:00
{
// $parm = eHelper::scParams($parm);
2013-05-30 14:16:49 -07:00
2013-05-29 23:00:55 -07:00
$tmpl = e107 :: getCoreTemplate ( 'chapter' , vartrue ( $parm [ 'template' ], 'nav' ), true , true ); // always merge
$template = $tmpl [ 'showPage' ];
2013-05-29 15:33:39 -07:00
$request = $this -> request ;
if ( $request && is_array ( $request ))
{
switch ( $request [ 'action' ])
{
2013-05-30 14:16:49 -07:00
case 'listBooks' :
$parm [ 'cbook' ] = 'all' ;
$template = $tmpl [ 'listBooks' ];
if ( e107 :: getPref ( 'listBooks' , false ) == false ) // List Books has been disabled.
{
return false ;
}
break ;
2013-05-29 15:33:39 -07:00
case 'listChapters' :
$parm [ 'cbook' ] = $request [ 'id' ];
2013-05-29 23:00:55 -07:00
$template = $tmpl [ 'listChapters' ];
2013-05-29 15:33:39 -07:00
break ;
case 'listPages' :
$parm [ 'cchapter' ] = $request [ 'id' ];
2013-05-29 23:00:55 -07:00
$template = $tmpl [ 'listPages' ];
2013-05-30 14:16:49 -07:00
2013-05-29 15:33:39 -07:00
break ;
case 'showPage' :
$parm [ 'cpage' ] = $request [ 'id' ];
break ;
}
}
if ( $parm )
{
2021-09-04 15:06:19 +02:00
$parm = http_build_query ( $parm );
2013-05-29 15:33:39 -07:00
}
else
{
$parm = '' ;
}
2019-06-06 14:25:58 -07:00
/** @var page_sitelink $links */
2013-05-29 15:33:39 -07:00
$links = e107 :: getAddon ( 'page' , 'e_sitelink' );
2022-03-11 18:01:56 -08:00
$data = $links -> pageNav ( $parm , true );
2013-05-29 15:33:39 -07:00
2013-02-14 15:37:42 +02:00
if ( isset ( $data [ 'title' ]) && ! vartrue ( $template [ 'noAutoTitle' ]))
{
2013-05-29 15:33:39 -07:00
// use chapter title
$template [ 'caption' ] = $data [ 'title' ];
2013-02-14 15:37:42 +02:00
$data = $data [ 'body' ];
2013-05-29 15:33:39 -07:00
}
2017-10-17 16:22:56 -07:00
if ( empty ( $data )){ return null ; }
2013-05-29 15:33:39 -07:00
2013-02-01 01:32:43 -08:00
return e107 :: getNav () -> render ( $data , $template ) ;
2013-05-29 15:33:39 -07:00
2013-02-01 01:32:43 -08:00
}
2013-03-11 05:12:45 -07:00
2013-05-29 15:33:39 -07:00
function sc_page_chapter_name ( $parm = '' )
2013-06-01 14:03:18 -07:00
{
2019-02-26 12:22:36 -08:00
return e107 :: getParser () -> toHTML ( $this -> var [ 'chapter_name' ]);
2013-05-29 15:33:39 -07:00
}
function sc_page_chapter_description ( $parm = '' )
{
2019-02-26 12:22:36 -08:00
return e107 :: getParser () -> toHTML ( $this -> var [ 'chapter_meta_description' ], true );
2013-05-29 15:33:39 -07:00
}
2013-03-11 05:12:45 -07:00
/**
* New in v2 . x . eg . { CMENU = feature - 1 } Renders a menu called 'feature-1' as found in the e107_page table See admin Pages / Menus .
*/
2021-01-08 11:46:30 -08:00
function sc_cmenu ( $parm = null )
2013-03-11 05:12:45 -07:00
{
2021-01-08 11:46:30 -08:00
if ( empty ( $parm ))
{
return null ;
}
2014-01-15 17:11:09 +02:00
return e107 :: getMenu () -> renderMenu ( $parm , false , false , true );
2013-03-11 05:12:45 -07:00
}
2017-01-25 13:08:53 -08:00
/**
* Render All visible Menus from a specific chapter .
* @ param null $parm
2017-01-25 17:57:38 -08:00
* @ example { CHAPTER_MENUS : name = chapter - sef - url }
2017-10-26 11:21:18 -07:00
* @ example { CHAPTER_MENUS : name = chapter - sef - url & template = xxxxx }
2017-01-25 13:08:53 -08:00
* @ return string
*/
function sc_chapter_menus ( $parm = null )
{
2021-01-08 11:46:30 -08:00
if ( empty ( $parm [ 'name' ]))
{
if ( ADMIN )
{
return " <div class='alert alert-danger'>Missing 'name'. eg: { CHAPTER_MENUS: name=chapter-sef-url}</div> " ;
}
return null ;
}
2017-01-25 13:08:53 -08:00
$tp = e107 :: getParser ();
$text = '' ;
2017-10-26 11:21:18 -07:00
$start = '' ;
2017-10-27 13:16:26 -07:00
$classCount = 0 ;
2017-01-25 13:08:53 -08:00
2017-10-26 11:21:18 -07:00
$sef = $tp -> filter ( $parm [ 'name' ], 'str' );
$registry = 'e_shortcode/sc_chapter_menus/' . $sef ;
if ( ! $pageArray = e107 :: getRegistry ( $registry ))
2017-01-25 13:08:53 -08:00
{
2017-10-26 11:21:18 -07:00
$query = " SELECT * FROM #page AS p LEFT JOIN #page_chapters as ch ON p.page_chapter=ch.chapter_id WHERE ch.chapter_visibility IN ( " . USERCLASS_LIST . " ) AND p.menu_class IN ( " . USERCLASS_LIST . " ) AND ch.chapter_sef = ' " . $sef . " ' ORDER BY p.page_order ASC " ;
2017-10-27 13:16:26 -07:00
e107 :: getDebug () -> log ( " Loading Page Chapters ( " . $sef . " ) " );
2017-10-26 11:21:18 -07:00
if ( ! $pageArray = e107 :: getDb () -> retrieve ( $query , true ))
{
e107 :: getDebug () -> log ( '{CHAPTER_MENUS: name=' . $parm [ 'name' ] . '} failed.<br />Query: ' . $query );
return null ;
}
e107 :: setRegistry ( $registry , $pageArray );
2017-01-25 13:08:53 -08:00
}
$template = e107 :: getCoreTemplate ( 'menu' , null , true , true );
$sc = e107 :: getScBatch ( 'page' , null , 'cpage' );
2017-11-10 09:03:04 -08:00
$editable = array (
'table' => 'page' ,
'pid' => 'page_id' ,
'perms' => '5' ,
'shortcodes' => array (
'cpagetitle' => array ( 'field' => 'page_subtitle' , 'type' => 'text' , 'container' => 'span' ),
'cpagebody' => array ( 'field' => 'page_text' , 'type' => 'html' , 'container' => 'div' ),
'cmenubody' => array ( 'field' => 'menu_text' , 'type' => 'html' , 'container' => 'div' ),
)
);
2017-01-25 17:57:38 -08:00
$sc -> setVars ( $pageArray [ 0 ]);
2017-11-10 09:03:04 -08:00
$sc -> editable ( $editable );
2017-01-25 17:57:38 -08:00
$tpl = varset ( $pageArray [ 0 ][ 'menu_template' ], 'default' ); // use start template from first row.
2017-10-26 11:21:18 -07:00
if ( ! empty ( $parm [ 'template' ]))
{
2017-10-26 12:01:27 -07:00
e107 :: getDebug () -> log ( '{CHAPTER_MENUS CUSTOM TEMPLATE}' );
2017-10-26 11:21:18 -07:00
$tpl = $parm [ 'template' ];
$start .= " <!-- CHAPTER_MENUS Start Template: " . $tpl . " --> " ;
if ( empty ( $template [ $tpl ]))
{
e107 :: getDebug () -> log ( '{CHAPTER_MENUS: ' . http_build_query ( $parm ) . '} has an empty template.' );
}
2017-01-25 13:08:53 -08:00
2017-10-26 11:21:18 -07:00
}
2017-10-27 13:16:26 -07:00
if ( ! empty ( $parm [ 'class' ]) && is_array ( $parm [ 'class' ]))
{
$classArray = $parm [ 'class' ];
$classCount = count ( $parm [ 'class' ]);
}
2017-10-26 11:21:18 -07:00
$active = varset ( $parm [ 'active' ], 1 );
$start .= $tp -> parseTemplate ( $template [ $tpl ][ 'start' ], true , $sc );
$c = 1 ;
2017-10-27 13:16:26 -07:00
$i = 0 ;
2017-01-25 13:08:53 -08:00
foreach ( $pageArray as $row )
{
2017-11-17 14:02:16 -08:00
if ( ! empty ( $parm [ 'limit' ]) && $c > $parm [ 'limit' ])
{
break ;
}
2017-10-26 12:01:27 -07:00
$row [ 'cmenu_tab_active' ] = ( $c === ( int ) $active ) ? true : false ;
2017-10-26 11:21:18 -07:00
if ( empty ( $parm [ 'template' ]))
{
$tpl = varset ( $row [ 'menu_template' ], 'default' );
}
2017-10-27 13:16:26 -07:00
$itemTemplate = $template [ $tpl ][ 'body' ];
2017-01-25 13:08:53 -08:00
$sc -> setVars ( $row );
2017-01-25 17:57:38 -08:00
2017-10-27 13:16:26 -07:00
if ( ! empty ( $classArray ))
{
$itemTemplate = str_replace ( '{CLASS}' , $classArray [ $i ], $itemTemplate );
$i ++ ;
if ( $classCount === $i )
{
$i = 0 ;
}
}
$text .= $tp -> parseTemplate ( $itemTemplate , true , $sc );
2017-10-26 11:21:18 -07:00
$c ++ ;
2017-01-25 17:57:38 -08:00
}
$end = $tp -> parseTemplate ( $template [ $tpl ][ 'end' ], true , $sc );
2017-10-26 11:21:18 -07:00
$end .= " <!-- " . http_build_query ( $parm ) . " --> " ;
if ( ! empty ( $parm [ 'template' ]))
{
$end .= " <!-- CHAPTER_MENUS end template: " . $parm [ 'template' ] . " --> " ;
}
2017-01-25 17:57:38 -08:00
if ( ! empty ( $text ))
{
return $start . $text . $end ;
2017-01-25 13:08:53 -08:00
}
2017-01-25 17:57:38 -08:00
2017-01-25 13:08:53 -08:00
}
2017-11-10 15:37:23 -08:00
/**
* Render All visible Chapters from a specific Book .
* Uses " listChapter " template key . ie . $CHAPTER_TEMPLATE [ --- TEMPLATE -- ][ 'listChapters' ]
* @ param null $parm
* @ example { BOOK_CHAPTERS : name = book - sef - url }
* @ example { BOOK_CHAPTERS : name = book - sef - url & template = xxxxx & limit = 3 }
* @ return string
*/
2021-01-08 11:46:30 -08:00
function sc_book_chapters ( $parm = null )
2017-11-10 15:37:23 -08:00
{
2021-01-08 11:46:30 -08:00
if ( empty ( $parm [ 'name' ]))
{
if ( ADMIN )
{
return " <div class='alert alert-danger'>Missing 'name'. eg: { BOOK_CHAPTER: name=book-sef-url}</div> " ;
}
2017-11-10 15:37:23 -08:00
2021-01-08 11:46:30 -08:00
return null ;
}
2017-11-10 15:37:23 -08:00
2021-01-08 11:46:30 -08:00
$tp = e107 :: getParser ();
2017-11-10 15:37:23 -08:00
2021-01-08 11:46:30 -08:00
$sef = $tp -> filter ( $parm [ 'name' ], 'str' );
2017-11-10 15:37:23 -08:00
$tmplKey = varset ( $parm [ 'template' ], 'panel' );
$limit = ( int ) varset ( $parm [ 'limit' ], 3 );
$registry = 'e_shortcode/sc_book_chapters/' . $sef . '/' . $limit ;
if ( ! $chapArray = e107 :: getRegistry ( $registry ))
{
$bookID = e107 :: getDb () -> retrieve ( 'page_chapters' , 'chapter_id' , " chapter_sef = ' " . $sef . " ' LIMIT 1 " );
if ( empty ( $bookID ))
{
return null ;
}
$query = " SELECT * FROM #page_chapters WHERE chapter_visibility IN ( " . USERCLASS_LIST . " ) AND chapter_parent = " . $bookID . " ORDER BY chapter_order ASC LIMIT " . $limit ;
e107 :: getDebug () -> log ( " Loading sc_book_chapters( " . $sef . " ) " );
if ( ! $chapArray = e107 :: getDb () -> retrieve ( $query , true ))
{
e107 :: getDebug () -> log ( '{BOOK_CHAPTERS: name=' . $parm [ 'name' ] . '} failed.<br />Query: ' . $query );
return null ;
}
e107 :: setRegistry ( $registry , $chapArray );
}
$temp = e107 :: getCoreTemplate ( 'chapter' , $tmplKey , true , true );
$template = $temp [ 'listChapters' ];
$sc = e107 :: getScBatch ( 'page' , null , 'cpage' );
$start = " <!-- sc_book_chapters Start Template: " . $tmplKey . " --> " ;
$start .= $tp -> parseTemplate ( $template [ 'start' ], true , $sc );
$text = '' ;
foreach ( $chapArray as $row )
{
$sc -> setVars ( $row );
$sc -> setChapter ( $row [ 'chapter_id' ]);
$text .= $tp -> parseTemplate ( $template [ 'item' ], true , $sc );
}
2021-02-24 20:33:45 +01:00
$end = $tp -> parseTemplate ( $template [ 'end' ], true , $sc );
2017-11-10 15:37:23 -08:00
$end .= " <!-- sc_book_chapters end template: " . $tmplKey . " --> " ;
if ( ! empty ( $text ))
{
return $start . $text . $end ;
}
return null ;
}
2013-02-01 01:32:43 -08:00
}