summaryrefslogtreecommitdiff
path: root/utils/modules/icons.php
blob: d916ec4038c90f4d82a4d69a951edaf2c5bda45c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
<?php
/* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

include_once($UTIL_DIR . "/modules.php");

class Icon {
  public $file;
	public $prefix;

  public function src()
  {
		return "<img alt=\"icon\" src=\"".$this->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 "<div style=\"width: 500px; height: 200px; overflow: auto\">\n";
			foreach($this->icons as $icon) {
				$icon->show();
			}
 			echo "</div>\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 "<p>Are you sure you want to delete this icon?</p>\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 "<p>The icon has now been deleted.</p>\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;
		if(is_dir($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."/");
}

?>