tips extension

This commit is contained in:
Shish 2009-09-27 14:00:29 +01:00
parent 2182a39648
commit 9257decc50
4 changed files with 285 additions and 0 deletions

Binary file not shown.

After

Width:  |  Height:  |  Size: 732 B

175
contrib/tips/main.php Normal file
View File

@ -0,0 +1,175 @@
<?php
/**
* Name: Random Tip
* Author: Sein Kraft <mail@seinkraft.info>
* License: GPLv2
* Description: Show a random line of text in the subheader space
* Documentation:
* Formatting is done with HTML
*/
class Tips extends SimpleExtension {
public function onInitExt($event) {
global $config, $database;
if ($config->get_int("ext_tips_version") < 1){
$database->create_table("tips", "
id SCORE_AIPK,
enable SCORE_BOOL NOT NULL DEFAULT SCORE_BOOL_N,
image TEXT NOT NULL,
text TEXT NOT NULL,
INDEX (id)
");
$database->execute("
INSERT INTO tips (enable, image, text)
VALUES (?, ?, ?)",
array("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($event) {
global $page, $user;
$this->getTip();
if($event->page_matches("tips")) {
switch($event->get_arg(0)) {
case "list":
{
if($user->is_admin()) {
$this->manageTips();
$this->getAll();
}
break;
}
case "new":
{
break;
}
case "save":
{
if($user->is_admin()) {
$this->saveTip();
$page->set_mode("redirect");
$page->set_redirect(make_link("tips/list"));
}
break;
}
case "status":
{
if($user->is_admin()) {
$tipID = int_escape($event->get_arg(1));
$this->setStatus($tipID);
$page->set_mode("redirect");
$page->set_redirect(make_link("tips/list"));
}
break;
}
case "delete":
{
if($user->is_admin()) {
$tipID = int_escape($event->get_arg(1));
$this->deleteTip($tipID);
$page->set_mode("redirect");
$page->set_redirect(make_link("tips/list"));
}
break;
}
}
}
}
public function onUserBlockBuilding($event) {
global $user;
if($user->is_admin()) {
$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 = array();
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("
INSERT INTO tips (enable, image, text)
VALUES (?, ?, ?)",
array($enable, $image, $text));
}
private function getTip() {
global $database;
$data_href = get_base_href();
$url = $data_href."/ext/tips/images/";
$tip = $database->get_row("SELECT * ".
"FROM tips ".
"WHERE enable = 'Y' ".
"ORDER BY RAND() ".
"LIMIT 1");
if($tip) {
$this->theme->showTip($url, $tip);
}
}
private function getAll() {
global $database;
$data_href = get_base_href();
$url = $data_href."/ext/tips/images/";
$tips = $database->get_all("SELECT * FROM tips ORDER BY id ASC");
$this->theme->showAll($url, $tips);
}
private function setStatus($tipID) {
global $database;
$tip = $database->get_row("SELECT * FROM tips WHERE id = ? ", array($tipID));
if($tip['enable'] == "Y") {
$enable = "N";
} elseif($tip['enable'] == "N") {
$enable = "Y";
}
$database->execute("UPDATE tips SET enable = ? WHERE id = ?", array ($enable, $tipID));
}
private function deleteTip($tipID) {
global $database;
$database->execute("DELETE FROM tips WHERE id = ?", array($tipID));
}
}
?>

11
contrib/tips/test.php Normal file
View File

@ -0,0 +1,11 @@
<?php
class TipsTest extends SCoreWebTestCase {
function testTips() {
$this->log_in_as_admin();
$this->get_page("tips/list");
$this->assert_title("Tips List");
$this->click("Save Settings");
$this->log_out();
}
}
?>

99
contrib/tips/theme.php Normal file
View File

@ -0,0 +1,99 @@
<?php
class TipsTheme extends Themelet {
public function manageTips($url, $images) {
global $page;
$select = "<select name='image'><option value=''>- Select Image -</option>";
foreach($images as $image){
$select .= "<option style='background-image:url(".$url.$image."); background-repeat:no-repeat; padding-left:20px;' value=\"".$image."\">".$image."</option>\n";
}
$select .= "</select>";
$html = "
<form action='".make_link("tips/save")."' method='POST'>
<table>
<tr>
<td>Enable:</td>
<td><input name='enable' type='checkbox' value='Y' checked/></td>
</tr>
<tr>
<td>Image:</td>
<td>{$select}</td>
</tr>
<tr>
<td>Message:</td>
<td><textarea name='text'></textarea></td>
</tr>
<tr>
<td colspan='2'><input type='submit' value='Submit' /></td>
</tr>
</table>
</form>
";
$page->set_title("Tips List");
$page->set_heading("Tips List");
$page->add_block(new NavBlock());
$page->add_block(new Block("Add Tip", $html, "main", 10));
}
public function showTip($url, $tip) {
global $page;
$img = "";
if(!empty($tip['image'])) {
$img = "<img src=".$url.$tip['image']." /> ";
}
$html = "<div id='tips'>".$img.$tip['text']."</div>";
$page->add_block(new Block(null, $html, "subheading", 10));
}
public function showAll($url, $tips){
global $user, $page;
$html = "<table id='poolsList' class='zebra'>".
"<thead><tr>".
"<th>ID</th>".
"<th>Enabled</th>".
"<th>Image</th>".
"<th>Text</th>";
if($user->is_admin()){
$html .= "<th>Action</th>";
}
$html .= "</tr></thead>";
$n = 0;
foreach ($tips as $tip)
{
$oe = ($n++ % 2 == 0) ? "even" : "odd";
$tip_enable = ($tip['enable'] == "Y") ? "Yes" : "No";
$set_link = "<a href='".make_link("tips/status/".$tip['id'])."'>".$tip_enable."</a>";
$html .= "<tr class='$oe'>".
"<td>".$tip['id']."</td>".
"<td>".$set_link."</td>".
(
empty($tip['image']) ?
"<td></td>" :
"<td><img src=".$url.$tip['image']." /></td>"
).
"<td class='left'>".$tip['text']."</td>";
$del_link = "<a href='".make_link("tips/delete/".$tip['id'])."'>Delete</a>";
if($user->is_admin()){
$html .= "<td>".$del_link."</td>";
}
$html .= "</tr>";
}
$html .= "</tbody></table>";
$page->add_block(new Block("All Tips", $html, "main", 20));
}
}
?>