2009-11-24 14:07:18 +00:00
< ? php
2019-05-28 16:59:38 +00:00
class Tips extends Extension
{
public function onInitExt ( InitExtEvent $event )
{
global $config , $database ;
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
if ( $config -> get_int ( " ext_tips_version " ) < 1 ) {
$database -> create_table ( " tips " , "
2009-11-24 14:07:18 +00:00
id SCORE_AIPK ,
enable SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N ,
image TEXT NOT NULL ,
text TEXT NOT NULL ,
" );
2019-05-28 16:59:38 +00:00
$database -> execute (
"
2009-11-24 14:07:18 +00:00
INSERT INTO tips ( enable , image , text )
VALUES ( ? , ? , ? ) " ,
2019-05-28 16:59:38 +00:00
[ " Y " , " coins.png " , " Do you like this extension? Please support us for developing new ones. <a href= \" https://www.paypal.com/cgi-bin/webscr?cmd=_s-xclick&hosted_button_id=8235933 \" target= \" _blank \" >Donate through paypal</a>. " ]
);
$config -> set_int ( " ext_tips_version " , 1 );
log_info ( " tips " , " extension installed " );
}
}
public function onPageRequest ( PageRequestEvent $event )
{
global $page , $user ;
$this -> getTip ();
2019-09-29 18:00:51 +00:00
if ( $event -> page_matches ( " tips " ) && $user -> can ( Permissions :: TIPS_ADMIN )) {
2019-05-28 16:59:38 +00:00
switch ( $event -> get_arg ( 0 )) {
case " list " :
$this -> manageTips ();
$this -> getAll ();
break ;
case " save " :
if ( $user -> check_auth_token ()) {
$this -> saveTip ();
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2019-05-28 16:59:38 +00:00
$page -> set_redirect ( make_link ( " tips/list " ));
}
break ;
case " status " :
// FIXME: HTTP GET CSRF
$tipID = int_escape ( $event -> get_arg ( 1 ));
$this -> setStatus ( $tipID );
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2019-05-28 16:59:38 +00:00
$page -> set_redirect ( make_link ( " tips/list " ));
break ;
case " delete " :
// FIXME: HTTP GET CSRF
$tipID = int_escape ( $event -> get_arg ( 1 ));
$this -> deleteTip ( $tipID );
2019-06-19 01:58:28 +00:00
$page -> set_mode ( PageMode :: REDIRECT );
2019-05-28 16:59:38 +00:00
$page -> set_redirect ( make_link ( " tips/list " ));
break ;
}
}
}
2019-08-02 19:54:48 +00:00
public function onPageSubNavBuilding ( PageSubNavBuildingEvent $event )
{
global $user ;
2019-09-29 13:30:55 +00:00
if ( $event -> parent === " system " ) {
2019-09-29 18:00:51 +00:00
if ( $user -> can ( Permissions :: TIPS_ADMIN )) {
2019-08-02 19:54:48 +00:00
$event -> add_nav_link ( " tips " , new Link ( 'tips/list' ), " Tips Editor " );
}
}
}
2019-05-28 16:59:38 +00:00
public function onUserBlockBuilding ( UserBlockBuildingEvent $event )
{
global $user ;
2019-09-29 18:00:51 +00:00
if ( $user -> can ( Permissions :: TIPS_ADMIN )) {
2019-05-28 16:59:38 +00:00
$event -> add_link ( " Tips Editor " , make_link ( " tips/list " ));
}
}
private function manageTips ()
{
$data_href = get_base_href ();
$url = $data_href . " /ext/tips/images/ " ;
$dirPath = dir ( './ext/tips/images' );
$images = [];
while (( $file = $dirPath -> read ()) !== false ) {
if ( $file [ 0 ] != " . " ) {
$images [] = trim ( $file );
}
}
$dirPath -> close ();
sort ( $images );
$this -> theme -> manageTips ( $url , $images );
}
private function saveTip ()
{
global $database ;
$enable = isset ( $_POST [ " enable " ]) ? " Y " : " N " ;
$image = html_escape ( $_POST [ " image " ]);
$text = $_POST [ " text " ];
$database -> execute (
"
2009-11-24 14:07:18 +00:00
INSERT INTO tips ( enable , image , text )
VALUES ( ? , ? , ? ) " ,
2019-05-28 16:59:38 +00:00
[ $enable , $image , $text ]
);
}
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
private function getTip ()
{
global $database ;
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$data_href = get_base_href ();
$url = $data_href . " /ext/tips/images/ " ;
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$tip = $database -> get_row ( " SELECT * " .
" FROM tips " .
" WHERE enable = 'Y' " .
" ORDER BY RAND() " .
" LIMIT 1 " );
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
if ( $tip ) {
$this -> theme -> showTip ( $url , $tip );
}
}
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
private function getAll ()
{
global $database ;
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$data_href = get_base_href ();
$url = $data_href . " /ext/tips/images/ " ;
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$tips = $database -> get_all ( " SELECT * FROM tips ORDER BY id ASC " );
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$this -> theme -> showAll ( $url , $tips );
}
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
private function setStatus ( int $tipID )
{
global $database ;
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$tip = $database -> get_row ( " SELECT * FROM tips WHERE id = ? " , [ int_escape ( $tipID )]);
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
if ( bool_escape ( $tip [ 'enable' ])) {
$enable = " N " ;
} else {
$enable = " Y " ;
}
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$database -> execute ( " UPDATE tips SET enable = ? WHERE id = ? " , [ $enable , int_escape ( $tipID )]);
}
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
private function deleteTip ( int $tipID )
{
global $database ;
$database -> execute ( " DELETE FROM tips WHERE id = ? " , [ int_escape ( $tipID )]);
}
2009-11-24 14:07:18 +00:00
}