summaryrefslogtreecommitdiff
blob: af20eba0bb730b03cf4e66c6f7c0b3781bf0223f (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
<?php
abstract class conf_build_common extends sql_row_obj {
	private $info;
	private function set_vars() {
		if (isset($this->info)) {
			return;
		}
		$array=array(
			'sql_configuration' => array(
				'optsclass' => 'sql_configopt',
				'optstable' => 'configopts',
				'self' => 'configuration',
			),
			'sql_build' => array(
				'optsclass' => 'sql_buildopt',
				'optstable' => 'buildopts',
				'self' => 'build'
			)
		);
		$this->info=$array[get_class($this)];
	}
	// Fetches all available configopts pertaining to this configuration in a nice array (cached)
	private $opt_cache;
	public function &get_opts($get_objs=false) {
		$this->set_vars();
		global $S;
		if (!isset($this->opt_cache)) {
			$this->opt_cache=array();
			$r=query('SELECT * FROM `'.$this->info['optstable'].'` WHERE `'.$this->info['self'].'`="'.$this->id.'"');
			while ($opt=$r->fetch(PDO::FETCH_ASSOC)) {
				$this->opt_cache[$opt['name']]=new $this->info['optsclass']($opt);
			}
		}
		if ($get_objs) {
			return $this->opt_cache;
		} else {
			$opts=array();
			foreach ($this->opt_cache as $opt) {
				$opts[$opt->name]=$opt->value;
			}
		}
		return $opts;
	}
	// Returns the value (or object if $get_obj is true) of the option given by $name, or false if unset
	public function get_opt($name, $get_obj=false) {
		$opts=$this->get_opts($get_obj);
		return array_key_exists($name, $opts)?$opts[$name]:false;
	}
	// Returns true if the given option is set and equals the given value, false otherwise
	public function opt_is($name, $val) {
		return ($this->get_opt($name) == $val);
	}
	// Generates a unique id and sets status to 1, writes self to db and returns id
	public function init() {
		global $S;
		$this->owner=$S['user']->id;
		$this->status=1;
		$fails=0;
		while (true) {
			$id=randstring(6);
			debug("Trying id=$id...");
			$r=query('SELECT `id` FROM `'.$this->table.'` WHERE `id`="'.$id.'"');
			if ($r->rowCount() == 0) {
				break;
			}
			if (++$fails == 10) {
				throw_exception('Failed 10 times to find a unique id in table `'.$this->table.'`... this shouldn\'t happen.');
			}
		}
		$this->id=$id;
		$this->write();
		return $this->id;
	}
	public function summary() {
		$opts=$this->get_opts();
		$r=array();
		foreach($opts as $name => $val) {
			$name=htmlentities($name);
			$val=htmlentities($val);
			$r[]="$name</td><td class=\"val\">$val";
		}
		if ($r) {
			return '<table class="configsummary"><tr><td class="name">'.implode('</td></tr><tr><td class="name">', $r).'</td></tr></table>';
		} else {
			return '<i>No options set</i>';
		}
	}
	// Sets a particular config/buildopt, using either UPDATE or INSERT
	public function set_opt($name, $val) {
		$this->set_vars();
		$opts=$this->get_opts(true);
		if (isset($opts[$name])) {
			$opts[$name]->value=$val;
			$opts[$name]->write();
		} else {
			$this->opt_cache[$name]=new $this->info['optsclass']($this->id, $name, $val);
			$this->opt_cache[$name]->write();
		}
	}
	// Deletes the given option if it exists
	public function delete_opt($name) {
		$opts=$this->get_opts(true);
		if (isset($opts[$name])) {
			$opts[$name]->delete();
		}
		unset($opts[$name]);
	}
}
?>