2020-01-26 13:19:35 +00:00
< ? php declare ( strict_types = 1 );
2009-11-24 14:07:18 +00:00
2020-01-29 00:49:21 +00:00
class CreateTipEvent extends Event
{
2021-03-14 23:43:50 +00:00
public bool $enable ;
public string $image ;
public string $text ;
2020-01-29 00:49:21 +00:00
public function __construct ( bool $enable , string $image , string $text )
{
2020-01-28 21:19:59 +00:00
parent :: __construct ();
$this -> enable = $enable ;
$this -> image = $image ;
$this -> text = $text ;
}
}
2020-01-29 00:49:21 +00:00
class DeleteTipEvent extends Event
{
2021-03-14 23:43:50 +00:00
public int $tip_id ;
2020-01-29 00:49:21 +00:00
public function __construct ( int $tip_id )
{
2020-01-28 21:19:59 +00:00
parent :: __construct ();
$this -> tip_id = $tip_id ;
}
}
2019-05-28 16:59:38 +00:00
class Tips extends Extension
{
2020-01-26 13:19:35 +00:00
/** @var TipsTheme */
2021-03-14 23:43:50 +00:00
protected ? Themelet $theme ;
2020-01-26 13:19:35 +00:00
2019-11-03 17:19:37 +00:00
public function onDatabaseUpgrade ( DatabaseUpgradeEvent $event )
2019-05-28 16:59:38 +00:00
{
2020-01-26 13:19:35 +00:00
global $database ;
2009-11-24 14:07:18 +00:00
2019-11-03 19:49:52 +00:00
if ( $this -> get_version ( " ext_tips_version " ) < 1 ) {
2019-05-28 16:59:38 +00:00
$database -> create_table ( " tips " , "
2020-10-26 18:22:47 +00:00
id SCORE_AIPK ,
2020-10-26 22:37:25 +00:00
enable BOOLEAN NOT NULL DEFAULT FALSE ,
2020-10-26 18:22:47 +00:00
image TEXT NOT NULL ,
text TEXT NOT NULL ,
" );
2009-11-24 14:07:18 +00:00
2019-05-28 16:59:38 +00:00
$database -> execute (
"
2009-11-24 14:07:18 +00:00
INSERT INTO tips ( enable , image , text )
2019-11-27 11:22:46 +00:00
VALUES ( : enable , : image , : text ) " ,
2020-10-26 18:22:47 +00:00
[ " enable " => true , " image " => " coins.png " , " text " => " 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>. " ]
2019-05-28 16:59:38 +00:00
);
2020-10-26 18:22:47 +00:00
$this -> set_version ( " ext_tips_version " , 2 );
}
if ( $this -> get_version ( " ext_tips_version " ) < 2 ) {
$database -> standardise_boolean ( " tips " , " enable " );
$this -> set_version ( " ext_tips_version " , 2 );
2019-05-28 16:59:38 +00:00
}
}
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 ()) {
2020-01-28 21:19:59 +00:00
send_event ( new CreateTipEvent ( isset ( $_POST [ " enable " ]), $_POST [ " image " ], $_POST [ " text " ]));
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 ));
2020-01-28 21:19:59 +00:00
send_event ( new DeleteTipEvent ( $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 );
}
2020-01-29 00:49:21 +00:00
public function onCreateTip ( CreateTipEvent $event )
{
2019-05-28 16:59:38 +00:00
global $database ;
$database -> execute (
"
2009-11-24 14:07:18 +00:00
INSERT INTO tips ( enable , image , text )
2019-11-27 11:22:46 +00:00
VALUES ( : enable , : image , : text ) " ,
2020-10-26 18:22:47 +00:00
[ " enable " => $event -> enable , " image " => $event -> image , " text " => $event -> text ]
2019-05-28 16:59:38 +00:00
);
}
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
2020-10-26 18:30:42 +00:00
$tip = $database -> get_row ( "
SELECT *
FROM tips
WHERE enable = : true
ORDER BY RAND ()
LIMIT 1
" , [ " true " =>true]);
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
2020-01-26 13:19:35 +00:00
$tip = $database -> get_row ( " SELECT * FROM tips WHERE id = :id " , [ " id " => $tipID ]);
2009-11-24 14:07:18 +00:00
2020-10-26 18:22:47 +00:00
$enable = bool_escape ( $tip [ 'enable' ]);
2009-11-24 14:07:18 +00:00
2020-01-26 13:19:35 +00:00
$database -> execute ( " UPDATE tips SET enable = :enable WHERE id = :id " , [ " enable " => $enable , " id " => $tipID ]);
2019-05-28 16:59:38 +00:00
}
2009-11-24 14:07:18 +00:00
2020-01-29 00:49:21 +00:00
public function onDeleteTip ( DeleteTipEvent $event )
{
2019-05-28 16:59:38 +00:00
global $database ;
2020-01-28 21:19:59 +00:00
$database -> execute ( " DELETE FROM tips WHERE id = :id " , [ " id " => $event -> tip_id ]);
2019-05-28 16:59:38 +00:00
}
2009-11-24 14:07:18 +00:00
}