summaryrefslogtreecommitdiff
path: root/utils/modules/config.php
blob: 13cdda1eb6c6989ece4c903858d93645753243cc (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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
<?php /* -*- Mode: php; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */

global $UTIL_DIR;

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

class Config {

  private $file;
  private $configs = array();

  // Admin config
  public $admin_title = "Site Config";
  public $admin_submodules = array("Title" => "title",
                                   "CSS" => "css",
                                   "Default page" => "defaultpage",
																	 "Menu" => "menu",
																	 "Splash" => "splash");

  public function admin_title($action, $vars)
  {
    switch($action) {
    case "update":
      $this->setValue("title", $vars["title"]);
			echo "The title has now been changed to \"". $this->value("title") . "\"";
			$this->write();
			break;
			
    default:
      $form = new Form("update");
      $form->addWidget(new LineEdit("Site title:", "title", $this->value("title", "Title not yet set")));
      $form->addWidget(new Button("Update"));
      $form->render();
      break;
    }
  }

  public function admin_css($action, $vars)
  {
    switch($action) {
    case "update":
      $this->setValue("css", $vars["css"]);
			echo "The stylesheet has now been changed to \"". $this->value("css") . "\"";
			$this->write();
			break;
			
    default:
      $form = new Form("update");
      $form->addWidget(new LineEdit("CSS file:", "css", $this->value("css", "CSS not yet set")));
      $form->addWidget(new Button("Update"));
      $form->render();
      break;
    }
  }

  public function admin_splash($action, $vars)
  {
    switch($action) {
    case "update":
      $this->setValue("splash", $vars["splash"]);
      $this->setValue("splashpage", $vars["splashpage"]);
      $this->setValue("splashreshow", $vars["splashreshow"]);
			echo "Splash has now been updated.";
			$this->write();
			break;
			
    default:
      $form = new Form("update");
      $form->addWidget(new LineEdit("Splash:", "splash", $this->value("splash", "false")));
      $form->addWidget(new LineEdit("Splash page:", "splashpage", $this->value("splashpage", "splash")));
      $form->addWidget(new LineEdit("Splash reshow:", "splashreshow", $this->value("splashreshow", "3600")));
      $form->addWidget(new Button("Update"));
      $form->render();
      break;
    }
  }

  public function admin_defaultpage($action, $vars)
  {
    switch($action) {
    case "update":
      $this->setValue("defaultpage", $vars["defaultpage"]);
			echo "Default page has now been set to ".$this->value("defaultpage").".";
			$this->write();
			break;
			
    default:
      $form = new Form("update");
      $form->addWidget(new LineEdit("Default page:", "defaultpage", $this->value("defaultpage", "")));
      $form->addWidget(new Button("Update"));
      $form->render();
      break;
    }
  }

  public function admin_menu($action, $vars)
  {
    switch($action) {
    case "update":
			global $menulist;
			$menu = ListEditor::splitValues($menulist);
      $this->setValue("menu", $menu);
      $this->write();
      break;
			
    default:
			global $config;
			global $pages;
			$pagetitles = array();
			foreach($pages->pages as $page) {
				$pagetitles[$page->title] = $page->title;
			}
      $form = new Form("update");
			$form->addWidget(new ListEditor("Menu items", "menulist",
																			new LineEdit("Title", "title"),
																			new ComboBox("Page", "page", "", $pagetitles),
																			$config->value("menu")));
      $form->addWidget(new Button("Update"));
      $form->render();
      break;
    }
  }

  public function admin($sub, $action, $vars)
  {
    switch($sub) {
    case "title":
      $this->admin_title($action, $vars);
      break;
    case "css":
      $this->admin_css($action, $vars);
      break;
    case "defaultpage":
      $this->admin_defaultpage($action, $vars);
      break;
    case "menu":
      $this->admin_menu($action, $vars);
      break;
    case "splash":
      $this->admin_splash($action, $vars);
      break;
    }
  }

  public function write()
  {
    $fp = fopen($this->file, "w");
    fwrite($fp, "<?xml version=\"1.0\" encoding=\"UTF-8\"?>\n");

    fwrite($fp, "<configs>\n");
    foreach($this->configs as $name => $value) {
      fwrite($fp, "  <config name=\"".$name."\"\n");
      fwrite($fp, "          value=\"".
	     htmlspecialchars(serialize($value), ENT_QUOTES, "UTF-8")."\"/>\n");
    }
    fwrite($fp, "</configs>\n");

    fclose($fp);
  }

  private function read()
  {
    $dom = new DomDocument;
    $dom->preserveWhiteSpace = TRUE;
    $dom->load($this->file);
    $configElems = $dom->getElementsByTagName('config');

    foreach ($configElems as $config) {
      $this->setValue($config->getAttribute('name'),
		      unserialize(htmlspecialchars_decode($config->getAttribute('value'), ENT_QUOTES)));
    }
  }

  public function value($name, $default = "")
  {
    if(isset($this->configs[$name])) return $this->configs[$name];
    return $default;
  }

  public function setValue($name, $value)
  {
    $this->configs[$name] = $value;
  }

  public function Config($file)
  {
    $this->file =  $file;
    if(file_exists($file)) $this->read();
  }

}

function config_init()
{
  global $DATA_DIR;
  return new Config($DATA_DIR . "/config.xml");
}

?>