From 9257decc50244fbbf1a1422ac6f008bc103a4ab2 Mon Sep 17 00:00:00 2001 From: Shish Date: Sun, 27 Sep 2009 14:00:29 +0100 Subject: [PATCH] tips extension --- contrib/tips/images/coins.png | Bin 0 -> 732 bytes contrib/tips/main.php | 175 ++++++++++++++++++++++++++++++++++ contrib/tips/test.php | 11 +++ contrib/tips/theme.php | 99 +++++++++++++++++++ 4 files changed, 285 insertions(+) create mode 100644 contrib/tips/images/coins.png create mode 100644 contrib/tips/main.php create mode 100644 contrib/tips/test.php create mode 100644 contrib/tips/theme.php diff --git a/contrib/tips/images/coins.png b/contrib/tips/images/coins.png new file mode 100644 index 0000000000000000000000000000000000000000..0ca9074d66e7a008dbdd265a48ff37f454941be9 GIT binary patch literal 732 zcmV<20wev2P)7$(F5I~EPw2+A2=20Sy*#|n7r8sf)7*RgqMLv3&KWmL+^F4r z7i0Z}>kq&FSJ~Z9+<5!dQFwCOz1ndvw%m_4J#=sT@Q1U19`B}1iL1?~3va{ZF~|VI zfHw0WI@T{zdi2I_>3m@YYk3d;l(JU>d+bPFLOo8G;T$3)1!qp*$dock%HEEk|V0 z3L2&;r~p>tQh+yD{T!MxBF4;%7xC6JH1CtlE}+w%Xkuu&haUcdHC?=ZKr(+GZJfIQ zJ6@GU%>XYSae67{=&NN{rB<>+N;FbBA#_{A0WZW7cXJ0E=}(_?dx9< zr?bEW55s(NboZ^^cb|S1s~AxnBh?-gQ{Ob@*3}OO`vCi&FRqICO$~lSg*6r5QDf7DTmJy^bFNy&p3d3; O0000 + * 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. Donate through paypal.")); + + $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)); + } +} +?> + diff --git a/contrib/tips/test.php b/contrib/tips/test.php new file mode 100644 index 00000000..6a9e4110 --- /dev/null +++ b/contrib/tips/test.php @@ -0,0 +1,11 @@ +log_in_as_admin(); + $this->get_page("tips/list"); + $this->assert_title("Tips List"); + $this->click("Save Settings"); + $this->log_out(); + } +} +?> diff --git a/contrib/tips/theme.php b/contrib/tips/theme.php new file mode 100644 index 00000000..be5457cd --- /dev/null +++ b/contrib/tips/theme.php @@ -0,0 +1,99 @@ +"; + + foreach($images as $image){ + $select .= "\n"; + } + + $select .= ""; + + $html = " +
+ + + + + + + + + + + + + + + + +
Enable:
Image:{$select}
Message:
+
+"; + + $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 = " "; + } + $html = "
".$img.$tip['text']."
"; + $page->add_block(new Block(null, $html, "subheading", 10)); + } + + public function showAll($url, $tips){ + global $user, $page; + + $html = "". + "". + "". + "". + "". + ""; + + if($user->is_admin()){ + $html .= ""; + } + + $html .= ""; + + $n = 0; + foreach ($tips as $tip) + { + $oe = ($n++ % 2 == 0) ? "even" : "odd"; + + $tip_enable = ($tip['enable'] == "Y") ? "Yes" : "No"; + $set_link = "".$tip_enable.""; + + $html .= "". + "". + "". + ( + empty($tip['image']) ? + "" : + "" + ). + ""; + + $del_link = "Delete"; + + if($user->is_admin()){ + $html .= ""; + } + + $html .= ""; + } + $html .= "
IDEnabledImageTextAction
".$tip['id']."".$set_link."".$tip['text']."".$del_link."
"; + + $page->add_block(new Block("All Tips", $html, "main", 20)); + } +} +?>