2009-11-20 05:01:51 +00:00
< ? php
/*
* e107 website system
*
2012-12-18 01:32:33 -08:00
* Copyright ( C ) 2008 - 2013 e107 Inc ( e107 . org )
2009-11-20 05:01:51 +00:00
* Released under the terms and conditions of the
* GNU General Public License ( http :// www . gnu . org / licenses / gpl . txt )
*
*/
2012-12-18 01:32:33 -08:00
if ( ! defined ( 'e107_INIT' )) { exit ; }
2009-11-20 05:01:51 +00:00
2013-02-26 03:43:52 -08:00
class page_sitelink // include plugin-folder in the name.
2009-11-20 05:01:51 +00:00
{
2014-01-10 18:27:42 -08:00
private $chapterSef = array ();
2014-01-12 05:33:10 -08:00
private $chapterParent = array ();
2017-10-17 16:22:56 -07:00
private $chapterName = array ();
2014-01-10 18:27:42 -08:00
function __construct ()
{
$sql = e107 :: getDb ();
2017-10-17 16:22:56 -07:00
$books = $sql -> retrieve ( " SELECT chapter_id,chapter_name,chapter_sef,chapter_parent FROM #page_chapters ORDER BY chapter_id ASC " , true );
2014-01-10 18:27:42 -08:00
foreach ( $books as $row )
{
$id = $row [ 'chapter_id' ];
$this -> chapterSef [ $id ] = $row [ 'chapter_sef' ];
2014-01-12 05:33:10 -08:00
$this -> chapterParent [ $id ] = $row [ 'chapter_parent' ];
2017-10-17 16:22:56 -07:00
$this -> chapterName [ $id ] = $row [ 'chapter_name' ];
2014-01-10 18:27:42 -08:00
}
}
2017-10-17 16:22:56 -07:00
private function getName ( $chapter )
{
return varset ( $this -> chapterName [ $chapter ], null );
}
2014-01-12 05:33:10 -08:00
private function getSef ( $chapter )
2014-01-10 18:27:42 -08:00
{
2014-01-12 12:52:16 -08:00
return vartrue ( $this -> chapterSef [ $chapter ], '--sef-not-assigned--' );
2014-01-10 18:27:42 -08:00
}
2014-01-12 05:33:10 -08:00
private function getParent ( $chapter )
{
return varset ( $this -> chapterParent [ $chapter ], false );
}
2014-01-10 18:27:42 -08:00
2009-11-20 05:01:51 +00:00
function config ()
{
$links = array ();
2013-10-28 21:30:24 -07:00
$sql = e107 :: getDb ();
$links [] = array (
'name' => " All Books " ,
'function' => " bookNav " ,
'description' => " A list of all books "
);
2017-10-17 16:22:56 -07:00
/* // TODO - get these working.
$links [] = array (
'name' => " All Books & chapters " ,
'function' => " bookNavChapters " ,
'description' => " A list of all books and their chapters "
);
$links [] = array (
'name' => " All Books, Chapters & Pages " ,
'function' => " bookNavChaptersPages " ,
'description' => " A list of all books, chapters and pages "
);
*/
2013-10-28 21:30:24 -07:00
$books = $sql -> retrieve ( " SELECT * FROM #page_chapters WHERE chapter_parent =0 ORDER BY chapter_order ASC " , true );
foreach ( $books as $row )
{
$links [] = array (
'name' => " All Chapters from " . $row [ 'chapter_name' ],
'function' => " chapterNav " ,
'parm' => $row [ 'chapter_id' ],
'description' => " A list of all chapters from the book " . $row [ 'chapter_name' ]
);
2017-10-17 16:22:56 -07:00
$links [] = array (
'name' => " All Chapters & Pages from " . $row [ 'chapter_name' ],
'function' => " chapterNavPages " ,
'parm' => $row [ 'chapter_id' ],
'description' => " A list of all chapters and pages from the book " . $row [ 'chapter_name' ]
);
2013-10-28 21:30:24 -07:00
}
2016-07-08 17:37:41 -07:00
$chaps = $sql -> retrieve ( " SELECT * FROM #page_chapters WHERE chapter_parent !=0 ORDER BY chapter_order ASC " , true );
foreach ( $chaps as $row )
{
$links [] = array (
'name' => " All Pages from " . $row [ 'chapter_name' ],
'function' => " pagesFromChapter " ,
'parm' => $row [ 'chapter_id' ],
'description' => " A list of all pages from the chapter " . $row [ 'chapter_name' ]
);
}
2009-11-20 05:01:51 +00:00
$links [] = array (
'name' => " All Pages " ,
2016-07-08 17:37:41 -07:00
'function' => " pageList " ,
2013-10-28 21:30:24 -07:00
'parm' => " " ,
'description' => " A list of all pages "
2009-11-20 05:01:51 +00:00
);
return $links ;
}
2013-10-28 21:30:24 -07:00
/**
* Return a tree of all books and their chapters .
*/
2017-10-17 16:22:56 -07:00
public function bookNav ( $book = 0 )
2013-10-28 21:30:24 -07:00
{
2017-10-17 16:22:56 -07:00
$parm = array ( 'book' => $book );
return $this -> pageNav ( $parm );
2013-10-28 21:30:24 -07:00
}
2016-07-08 17:37:41 -07:00
2017-10-17 16:22:56 -07:00
//TODO
public function bookNavChapters ( $book = 0 )
{
$parm = array ( 'book' => $book , 'chapters' => true );
return $this -> pageNav ( $parm );
}
//TODO
public function bookNavChaptersPages ( $book = 0 )
2016-07-08 17:37:41 -07:00
{
2017-10-17 16:22:56 -07:00
$parm = array ( 'book' => $book , 'chapters' => true , 'pages' => true );
return $this -> pageNav ( $parm );
}
2016-07-08 17:37:41 -07:00
2017-10-17 16:22:56 -07:00
public function chapterNavPages ( $id )
{
return $this -> chapterNav ( $id , true );
}
public function pagesFromChapter ( $id )
{
2016-07-08 17:37:41 -07:00
return $this -> pageList ( $id );
}
2017-10-03 11:11:01 -07:00
public function pageList ( $parm )
2016-07-08 17:37:41 -07:00
{
$sql = e107 :: getDb ();
$arr = array ();
if ( ! empty ( $parm ))
{
$query = " SELECT * FROM `#page` WHERE page_class IN ( " . USERCLASS_LIST . " ) AND page_chapter = " . intval ( $parm ) . " ORDER BY page_order ASC " ;
}
else
{
$query = " SELECT * FROM `#page` WHERE page_class IN ( " . USERCLASS_LIST . " ) ORDER BY page_order ASC " ;
}
$pages = $sql -> retrieve ( $query , true );
foreach ( $pages as $row )
{
$chapter_parent = $this -> getParent ( $row [ 'page_chapter' ]);
$row [ 'book_sef' ] = $this -> getSef ( $chapter_parent );
$row [ 'chapter_sef' ] = $this -> getSef ( $row [ 'page_chapter' ]);
if ( ! vartrue ( $row [ 'chapter_sef' ]))
{
// $row['chapter_sef'] = '--sef-not-assigned--';
}
2017-10-17 16:22:56 -07:00
$arr [] = $this -> pageArray ( $row );
}
return $arr ;
}
private function pageArray ( $row , $options = array ())
{
return array (
2016-07-08 17:37:41 -07:00
'link_id' => $row [ 'page_id' ],
'link_name' => $row [ 'page_title' ] ? $row [ 'page_title' ] : 'No title' , // FIXME lan
'link_url' => e107 :: getUrl () -> create ( 'page/view' , $row , array ( 'allow' => 'page_sef,page_title,page_id,chapter_sef,book_sef' )),
'link_description' => '' ,
2017-10-17 16:22:56 -07:00
'link_button' => ( ! empty ( $options [ 'icon' ]) && $options [ 'icon' ] === 'menu_image' ) ? $row [ 'menu_image' ] : '' ,
2016-07-08 17:37:41 -07:00
'link_category' => '' ,
'link_order' => $row [ 'page_order' ],
'link_parent' => $row [ 'page_chapter' ],
'link_open' => '' ,
'link_class' => intval ( $row [ 'page_class' ]),
'link_active' => ( isset ( $options [ 'cpage' ]) && $row [ 'page_id' ] == $options [ 'cpage' ]),
'link_identifier' => 'page-nav-' . intval ( $row [ 'page_id' ]) // used for css id.
);
}
2013-10-28 21:30:24 -07:00
/**
* Return a list of all chapters from a sepcific book .
*/
2017-10-17 16:22:56 -07:00
public function chapterNav ( $book , $loadPages = false )
2013-10-28 21:30:24 -07:00
{
$sql = e107 :: getDb ();
$tp = e107 :: getParser ();
2017-10-17 16:22:56 -07:00
2013-10-28 21:30:24 -07:00
2014-02-08 16:56:57 -08:00
if ( $sql -> select ( " page_chapters " , " * " , " chapter_parent = " . intval ( $book ) . " AND chapter_visibility IN ( " . USERCLASS_LIST . " ) ORDER BY chapter_order ASC " ))
2013-10-28 21:30:24 -07:00
{
2017-10-17 16:22:56 -07:00
$chapters = array ();
$ids = array ();
2013-10-28 21:30:24 -07:00
while ( $row = $sql -> fetch ())
{
2017-10-17 16:22:56 -07:00
$ids [] = $row [ 'chapter_id' ];
$id = $row [ 'chapter_id' ];
2014-01-10 18:27:42 -08:00
$sef = $row ;
$sef [ 'chapter_sef' ] = $this -> getSef ( $row [ 'chapter_id' ]);
$sef [ 'book_sef' ] = $this -> getSef ( $row [ 'chapter_parent' ]);
2017-10-17 16:22:56 -07:00
$chapters [ $id ] = array (
2013-10-28 21:30:24 -07:00
'link_name' => $tp -> toHtml ( $row [ 'chapter_name' ], '' , 'TITLE' ),
2014-01-10 18:27:42 -08:00
'link_url' => e107 :: getUrl () -> create ( 'page/chapter/index' , $sef ), // 'page.php?ch='.$row['chapter_id'],
2013-10-28 21:30:24 -07:00
'link_description' => '' ,
2014-01-04 06:02:24 -08:00
'link_button' => $row [ 'chapter_icon' ],
2013-10-28 21:30:24 -07:00
'link_category' => '' ,
'link_order' => '' ,
'link_parent' => $row [ 'chapter_parent' ],
'link_open' => '' ,
2015-02-01 17:27:05 -08:00
'link_class' => 0 ,
2017-10-17 16:22:56 -07:00
'link_sub' => array (),
2015-02-01 17:27:05 -08:00
'link_identifier' => 'page-nav-' . intval ( $row [ 'chapter_id' ]) // used for css id.
2013-10-28 21:30:24 -07:00
);
}
2017-10-17 16:22:56 -07:00
if ( $loadPages === true )
{
$pages = $sql -> retrieve ( " SELECT * FROM #page WHERE page_title !='' AND page_chapter IN ( " . implode ( " , " , $ids ) . " ) AND page_class IN ( " . USERCLASS_LIST . " ) ORDER BY page_order " , true );
foreach ( $pages as $row )
{
$chap = $row [ 'page_chapter' ];
$chapters [ $chap ][ 'link_sub' ][] = $this -> pageArray ( $row );
}
// e107::getDebug()->log($pages);
}
2013-10-28 21:30:24 -07:00
2017-10-17 16:22:56 -07:00
return $chapters ;
2013-10-28 21:30:24 -07:00
}
}
2009-11-20 05:01:51 +00:00
2017-10-17 16:22:56 -07:00
function pageNav ( $parm = null )
2009-11-20 05:01:51 +00:00
{
2013-05-29 01:22:11 -07:00
$frm = e107 :: getForm ();
2013-02-14 13:06:16 +02:00
$options = array ();
2017-10-17 16:22:56 -07:00
if ( ! empty ( $parm ))
2012-12-19 02:03:34 -08:00
{
2017-10-17 16:22:56 -07:00
if ( is_string ( $parm ))
{
parse_str ( $parm , $options );
}
2017-10-17 17:41:43 -07:00
elseif ( is_array ( $parm ))
{
$options = $parm ;
}
2012-12-19 02:03:34 -08:00
}
2015-02-01 17:27:05 -08:00
2012-12-17 12:14:52 -08:00
$sql = e107 :: getDb ();
$sublinks = array ();
2013-02-14 15:37:42 +02:00
$arr = array ();
2017-10-17 17:41:43 -07:00
2013-03-08 13:08:06 +02:00
// map current when in auto mode
2017-10-17 17:41:43 -07:00
if ( ! empty ( $options [ 'auto' ]))
2013-03-08 13:08:06 +02:00
{
// current book found, top book not set
2017-10-17 17:41:43 -07:00
if ( ! empty ( $options [ 'cbook' ]) && empty ( $options [ 'book' ]))
2013-03-08 13:08:06 +02:00
{
$options [ 'book' ] = $options [ 'cbook' ];
}
// current chapter found, top chapter not set
2017-10-17 17:41:43 -07:00
if ( ! empty ( $options [ 'cchapter' ]) && empty ( $options [ 'chapter' ]))
2013-03-08 13:08:06 +02:00
{
$options [ 'chapter' ] = $options [ 'cchapter' ];
}
// current chapter found, top chapter not set
2017-10-17 17:41:43 -07:00
if ( ! empty ( $options [ 'cpage' ]) && empty ( $options [ 'page' ]))
2013-03-08 13:08:06 +02:00
{
$options [ 'page' ] = $options [ 'cpage' ];
}
}
2013-02-14 15:37:42 +02:00
// find the chapter if required
2017-10-17 17:41:43 -07:00
if ( ! empty ( $options [ 'page' ]) && empty ( $options [ 'chapter' ]))
2013-02-14 15:37:42 +02:00
{
$options [ 'chapter' ] = $sql -> retrieve ( 'page' , 'page_chapter' , 'page_id=' . intval ( $options [ 'page' ]));
}
2012-12-19 02:03:34 -08:00
$query = " SELECT * FROM #page WHERE " ;
2013-04-02 09:29:11 +03:00
$q = array ();
2013-05-29 01:22:11 -07:00
2013-02-14 15:37:42 +02:00
if ( vartrue ( $options [ 'chapter' ]))
{
2013-05-29 01:22:11 -07:00
$q [] = " page_title !='' AND page_chapter = " . intval ( $options [ 'chapter' ]);
2013-02-14 15:37:42 +02:00
}
elseif ( vartrue ( $options [ 'book' ]))
{
2013-05-29 19:37:06 -07:00
$q [] = " page_title !='' && page_chapter IN (SELECT chapter_id FROM #page_chapters WHERE chapter_parent= " . intval ( $options [ 'book' ]) . " ) " ;
2013-02-14 15:37:42 +02:00
}
2013-04-02 09:29:11 +03:00
// XXX discuss FIXED remove DB check, use default title - AND page_title !=''
$q [] = " page_class IN ( " . USERCLASS_LIST . " ) " ;
$query .= implode ( ' AND ' , $q ) . " ORDER BY page_order " ;
2012-12-19 02:03:34 -08:00
2012-12-17 12:14:52 -08:00
$data = $sql -> retrieve ( $query , true );
2013-02-14 13:06:16 +02:00
$_pdata = array ();
2017-10-17 16:22:56 -07:00
/*
if ( empty ( $data ))
{
e107 :: getDebug () -> log ( $query );
e107 :: getDebug () -> dump ( $data );
}
*/
2012-12-17 04:21:16 -08:00
foreach ( $data as $row )
2009-11-20 05:01:51 +00:00
{
2012-12-17 12:14:52 -08:00
$pid = $row [ 'page_chapter' ];
2014-01-10 18:27:42 -08:00
$row [ 'chapter_sef' ] = $this -> getSef ( $row [ 'page_chapter' ]);
2014-01-12 05:33:10 -08:00
$book = $this -> getParent ( $row [ 'page_chapter' ]);
$row [ 'book_sef' ] = $this -> getSef ( $book );
2014-01-10 18:27:42 -08:00
2014-01-12 12:52:16 -08:00
if ( ! vartrue ( $row [ 'page_sef' ]))
{
$row [ 'page_sef' ] = '--sef-not-assigned--' ;
}
2017-10-17 16:22:56 -07:00
$sublinks [ $pid ][] = $_pdata [] = $this -> pageArray ( $row , $options ); /* array (
2012-12-17 04:21:16 -08:00
'link_id' => $row [ 'page_id' ],
2013-04-02 09:29:11 +03:00
'link_name' => $row [ 'page_title' ] ? $row [ 'page_title' ] : 'No title' , // FIXME lan
2014-01-10 18:27:42 -08:00
'link_url' => e107 :: getUrl () -> create ( 'page/view' , $row , array ( 'allow' => 'page_sef,page_title,page_id,chapter_sef,book_sef' )),
2009-11-20 05:01:51 +00:00
'link_description' => '' ,
2014-01-04 06:02:24 -08:00
'link_button' => $row [ 'menu_image' ],
2009-11-20 05:01:51 +00:00
'link_category' => '' ,
2012-12-17 04:21:16 -08:00
'link_order' => $row [ 'page_order' ],
2012-12-17 12:14:52 -08:00
'link_parent' => $row [ 'page_chapter' ],
2009-11-20 05:01:51 +00:00
'link_open' => '' ,
2013-02-14 15:37:42 +02:00
'link_class' => intval ( $row [ 'page_class' ]),
2015-02-01 17:27:05 -08:00
'link_active' => ( isset ( $options [ 'cpage' ]) && $row [ 'page_id' ] == $options [ 'cpage' ]),
'link_identifier' => 'page-nav-' . intval ( $row [ 'page_id' ]) // used for css id.
2017-10-17 16:22:56 -07:00
); */
2009-11-20 05:01:51 +00:00
}
2012-12-19 02:03:34 -08:00
2014-02-08 16:56:57 -08:00
$filter = " chapter_visibility IN ( " . USERCLASS_LIST . " ) " ;
2012-12-19 02:03:34 -08:00
2017-10-17 16:22:56 -07:00
if ( ! empty ( $options [ 'chapter' ]))
2012-12-19 02:03:34 -08:00
{
2014-02-08 16:56:57 -08:00
$title = $sql -> retrieve ( 'page_chapters' , 'chapter_name' , 'chapter_id=' . intval ( $options [ 'chapter' ]) . ' AND chapter_visibility IN (' . USERCLASS_LIST . ')' );
2013-02-14 13:06:16 +02:00
$outArray = array ();
2017-10-17 16:22:56 -07:00
if ( ! $title )
{
return e107 :: getNav () -> compile ( $_pdata , $outArray , $options [ 'chapter' ]);
}
2013-02-14 15:37:42 +02:00
return array ( 'title' => $title , 'body' => e107 :: getNav () -> compile ( $_pdata , $outArray , $options [ 'chapter' ]));
2012-12-19 02:03:34 -08:00
}
2013-02-14 15:37:42 +02:00
$parent = 0 ;
$title = false ;
2017-10-17 16:22:56 -07:00
if ( ! empty ( $options [ 'book' ]))
2012-12-19 02:03:34 -08:00
{
2013-02-14 15:37:42 +02:00
$filter = " chapter_parent = " . intval ( $options [ 'book' ]);
2017-10-17 16:22:56 -07:00
$title = $this -> getName ( $options [ 'book' ]); // set the caption as main book title.
2012-12-19 02:03:34 -08:00
}
2014-02-08 16:56:57 -08:00
2012-12-19 02:03:34 -08:00
$books = $sql -> retrieve ( " SELECT * FROM #page_chapters WHERE " . $filter . " ORDER BY chapter_order ASC " , true );
2012-12-17 12:14:52 -08:00
foreach ( $books as $row )
{
2014-01-10 18:27:42 -08:00
$row [ 'book_sef' ] = $this -> getSef ( $row [ 'chapter_parent' ]);
2013-05-29 23:00:55 -07:00
2017-10-17 16:22:56 -07:00
if ( empty ( $row [ 'chapter_sef' ]))
2014-01-12 12:52:16 -08:00
{
$row [ 'chapter_sef' ] = '--sef-not-assigned--' ;
}
2017-10-17 16:22:56 -07:00
2012-12-17 12:14:52 -08:00
$arr [] = array (
'link_id' => $row [ 'chapter_id' ],
'link_name' => $row [ 'chapter_name' ],
2014-01-09 07:52:25 -08:00
'link_url' => ( $row [ 'chapter_parent' ] == 0 ) ? e107 :: getUrl () -> create ( 'page/book/index' , $row ) : e107 :: getUrl () -> create ( 'page/chapter/index' , $row ), // ,'page.php?bk='.$row['chapter_id'] : 'page.php?ch='.$row['chapter_id'],
2012-12-17 12:14:52 -08:00
'link_description' => '' ,
2014-01-04 06:02:24 -08:00
'link_button' => $row [ 'chapter_icon' ],
2012-12-17 12:14:52 -08:00
'link_category' => '' ,
'link_order' => $row [ 'chapter_order' ],
'link_parent' => $row [ 'chapter_parent' ],
'link_open' => '' ,
'link_class' => 0 ,
2017-10-17 16:22:56 -07:00
'link_sub' => (( empty ( $options [ 'book' ]) || ! empty ( $options [ 'pages' ])) && empty ( $options [ 'auto' ])) ? varset ( $sublinks [ $row [ 'chapter_id' ]]) : false , //XXX always test with docs template in bootstrap before changing.
2013-11-21 19:41:29 -08:00
'link_active' => $row [ 'chapter_parent' ] == 0 ? isset ( $options [ 'cbook' ]) && $options [ 'cbook' ] == $row [ 'chapter_id' ] : isset ( $options [ 'cchapter' ]) && $options [ 'cchapter' ] == $row [ 'chapter_id' ],
2015-02-01 17:27:05 -08:00
'link_identifier' => 'page-nav-' . intval ( $row [ 'chapter_id' ]) // used for css id.
2012-12-17 12:14:52 -08:00
);
2015-02-01 17:27:05 -08:00
2012-12-17 12:14:52 -08:00
}
2017-10-17 16:22:56 -07:00
/*
if ( ! empty ( $options [ 'book' ]))
{
e107 :: getDebug () -> dump ( $arr );
} */
2015-02-01 17:27:05 -08:00
2012-12-17 04:21:16 -08:00
$outArray = array ();
2013-05-30 14:16:49 -07:00
$parent = vartrue ( $options [ 'book' ]) ? intval ( $options [ 'book' ]) : 0 ;
2017-10-17 16:22:56 -07:00
2012-12-19 02:03:34 -08:00
$ret = e107 :: getNav () -> compile ( $arr , $outArray , $parent );
2013-05-30 14:16:49 -07:00
2013-02-14 15:37:42 +02:00
if ( ! $title ) return $ret ;
return array ( 'title' => $title , 'body' => $ret );
2009-11-20 05:01:51 +00:00
}
}