From 3cb7a7f16be016f2d563762379222fdea5767986 Mon Sep 17 00:00:00 2001 From: deva Date: Tue, 28 Apr 2009 07:36:01 +0000 Subject: Added icon module, and made the news module use it. Also made the ImageComboBox use it if supplied as the values parameter. --- utils/forms.php | 25 +++++++--- utils/modules/icons.php | 130 ++++++++++++++++++++++++++++++++++++++++++++++++ utils/modules/news.php | 30 ++++------- 3 files changed, 158 insertions(+), 27 deletions(-) create mode 100644 utils/modules/icons.php diff --git a/utils/forms.php b/utils/forms.php index d946c34..a450463 100644 --- a/utils/forms.php +++ b/utils/forms.php @@ -209,7 +209,12 @@ class ImageComboBox { public function render($indent = "") { - $width = 100; + $selheight = 64; + $height = $selheight * 0.8; + + if(get_class($this->values) == "Icons") $numicons = sizeof($this->values->icons); + else $numicons = sizeof($this->values); + $str = $indent . "
\n"; $str .= $indent . "
".$this->label."
\n"; $str .= $indent . "
\n"; @@ -220,18 +225,24 @@ class ImageComboBox { $str .= $indent . " var container = document.getElementById('icons');\n"; $str .= $indent . " var icons = document.getElementsByName('icon');\n"; $str .= $indent . " for(i = 0; i < icons.length; i++) {\n"; - $str .= $indent . " if(icons[i].src == value) icons[i].width = ".$width.";\n"; - $str .= $indent . " else icons[i].width = ".($width - 10).";\n"; + $str .= $indent . " if(icons[i].alt == value) icons[i].height = ".$selheight.";\n"; + $str .= $indent . " else icons[i].height = ".$height.";\n"; // $str .= $indent . " if(icons[i].src == value) icons[i].style.border = '10px blue';\n"; // $str .= $indent . " else { icons[i].style.border = '0px' }\n"; $str .= $indent . " }\n"; $str .= $indent . " }\n"; $str .= $indent . " \n"; - $str .= $indent . "
\n"; - $str .= $indent . "
values)."px;\">\n"; + $str .= $indent . "
\n"; + $str .= $indent . "
\n"; $str .= $indent . " name."\" name=\"vars[".$this->name."]\" type=\"hidden\" value=\"\"/>\n"; - foreach($this->values as $k => $v) { - $str .= $indent . " \n"; + if(get_class($this->values) == "Icons") { + foreach($this->values->icons as $icon) { + $str .= $indent . " \"".$icon-file."\" src=\"".$icon->prefix.$icon->file."\" onclick=\"setSelection('".$icon->file."')\"/>\n"; + } + } else { + foreach($this->values as $k => $v) { + $str .= $indent . " \"".$v."\"\n"; + } } $str .= $indent . "
\n"; // box $str .= $indent . "
\n"; // overflow diff --git a/utils/modules/icons.php b/utils/modules/icons.php new file mode 100644 index 0000000..44df7e9 --- /dev/null +++ b/utils/modules/icons.php @@ -0,0 +1,130 @@ +prefix.$this->file."\"/>\n"; + } + + public function show() + { + echo $this->src(); + } + + public function Icon($file, $prefix) + { + $this->file = $file; + $this->prefix = $prefix; + } +} + +class Icons { + public $icons = array(); + public $prefix; + + // Admin config + public $admin_title = "Icons"; + public $admin_submodules = array("Add icon" => "add", "Delete icon" => "delete"); + + public function admin_add($action, $vars) + { + global $_FILES; + switch($action) { + case "create": + if(is_uploaded_file($_FILES['icon']['tmp_name'])) { + move_uploaded_file($_FILES['icon']['tmp_name'], $this->prefix . $_FILES['icon']['name']); + $i = new Icon($_FILES['icon']['name'], $this->prefix); + $this->icons[$_FILES['icon']['name']] = $i; + $i->show(); + } else { + echo "Failed to upload file: " . $icon['name'] . " to " . $icon['tmp_name'] . "\n"; + } + + default: + echo "Existing icons\n"; + echo "
\n"; + foreach($this->icons as $icon) { + $icon->show(); + } + echo "
\n"; + + $form = new Form("create"); + $form->addWidget(new FileUpload("Select icons:", "icon")); + $form->addWidget(new Button("Upload")); + $form->render(); + break; + } + } + + public function admin_delete($action, $vars) + { + global $_FILES; + switch($action) { + case "confirm": + echo "

Are you sure you want to delete this icon?

\n"; + $this->icons[$vars['icon']]->show(); + + $form = new Form("delete"); + $form->addWidget(new Hidden($vars)); + $form->addWidget(new Button("yes")); + $form->render(); + + $form = new Form("select"); + $form->addWidget(new Hidden($vars)); + $form->addWidget(new Button("no")); + $form->render(); + break; + + case "delete": + unlink($this->prefix.$vars['icon']); + unset($this->icons[$vars['icon']]); + echo "

The icon has now been deleted.

\n"; + + case "select": + default: + $form = new Form("confirm"); + $form->addWidget(new ImageComboBox("Icon", "icon", $icon, $this)); + $form->addWidget(new Button("Delete...")); + $form->render(); + break; + } + } + + public function admin($sub, $action, $vars) + { + switch($sub) { + case "add": + $this->admin_add($action, $vars); + break; + case "delete": + $this->admin_delete($action, $vars); + break; + } + } + + public function Icons($prefix) + { + $this->prefix = $prefix; + $df = opendir($prefix); + while(false !== ($file = readdir($df))) { + if($file == '.' || $file == '..') continue; + $icon = new Icon($file, $prefix); + $this->icons[$file] = $icon; + } + closedir($df); + } + +} + +function icons_init() +{ + global $ICONS_DIR; + return new Icons($ICONS_DIR."/"); +} + +?> diff --git a/utils/modules/news.php b/utils/modules/news.php index 9667e44..95cdda1 100644 --- a/utils/modules/news.php +++ b/utils/modules/news.php @@ -3,6 +3,7 @@ include_once($UTIL_DIR . "/convert.php"); include_once($UTIL_DIR . "/forms.php"); include_once($UTIL_DIR . "/user.php"); +include_once($MODULES_DIR . "/icons.php"); class NewsEntry { public $title; @@ -14,16 +15,19 @@ class NewsEntry { public function show() { - global $users, $DATA_DIR; + global $users, $DATA_DIR, $ICONS_DIR; if(!isset($users)) $users = new Users($DATA_DIR . "/users.xml"); + $icons = new Icons($ICONS_DIR."/"); + $icon = $icons->icons[$this->icon]; + $str = "
\n"; $str .= "
" . htmlspecialchars_decode($this->title, ENT_QUOTES) . "
\n"; $str .= "
" . date("D M jS Y G:i", $this->time) . "
\n"; $str .= "
By: " . $users->findUser($this->userid)->userid . "
\n"; $str .= "
\n"; - if($this->icon) $str .= " \"icon\"icon . "\"/>\n"; + if($this->icon) $str .= " \"icon\"prefix.$icon->file . "\"/>\n"; $str .= " ".htmlspecialchars_decode($this->description, ENT_QUOTES) . "\n"; $str .= "
\n"; $str .= "
\n"; @@ -72,7 +76,7 @@ class News { public function admin_add($action, $vars) { - global $UID; + global $UID, $ICONS_DIR; switch($action) { case "add": @@ -109,19 +113,12 @@ class News { $category = $vars["category"]; $description = $vars["description"]; default: - $icons = array("http://www.executionroom.com/gfx/logos/die_logo_black_thumb.png", - "http://www.executionroom.com/gfx/logos/die_logo_bloody_thumb.png", - "http://www.executionroom.com/gfx/logos/die_logo_red_thumb_.png", - "http://www.executionroom.com/gfx/logos/die_logo_white_thumb.png", - "http://www.executionroom.com/gfx/die_group_2008_thumb.jpg", - "http://www.executionroom.com/gfx/die_group_thumb.jpg"); - $form = new Form("preview"); $form->addWidget(new LineEdit("Title", "title", $title)); $form->addWidget(new DateTimeEdit("Time", "time", $time)); $form->addWidget(new ComboBox("Category", "category", $category, array("Main" => "main", "Site" => "site"))); $form->addWidget(new TextEdit("Description", "description", $description)); - $form->addWidget(new ImageComboBox("Icon", "icon", $icon, $icons)); + $form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/"))); $form->addWidget(new Button("Post news")); $form->render(); break; @@ -130,7 +127,7 @@ class News { public function admin_edit($action, $vars) { - global $UID; + global $UID, $ICONS_DIR; switch($action) { case "add": @@ -165,13 +162,6 @@ class News { case "edit": case "retry": - $icons = array("http://www.executionroom.com/gfx/logos/die_logo_black_thumb.png", - "http://www.executionroom.com/gfx/logos/die_logo_bloody_thumb.png", - "http://www.executionroom.com/gfx/logos/die_logo_red_thumb_.png", - "http://www.executionroom.com/gfx/logos/die_logo_white_thumb.png", - "http://www.executionroom.com/gfx/die_group_2008_thumb.jpg", - "http://www.executionroom.com/gfx/die_group_thumb.jpg"); - if(isset($vars["title"])) $title = $vars["title"]; else $title = $this->news[$vars["newsid"]]->title; if(isset($vars["time_year"])) $time = DateTimeEdit::toTimestamp($vars, "time"); @@ -190,7 +180,7 @@ class News { $form->addWidget(new ComboBox("Category", "category", $category, array("Main" => "main", "Site" => "site"))); $form->addWidget(new TextEdit("Description", "description", $description)); - $form->addWidget(new ImageComboBox("Icon", "icon", $icon, $icons)); + $form->addWidget(new ImageComboBox("Icon", "icon", $icon, new Icons($ICONS_DIR."/"))); $form->addWidget(new Button("Post news")); $form->render(); break; -- cgit v1.2.3