summaryrefslogtreecommitdiff
blob: d8fc9e8f6438a74092b09ac8fc1c9cfff2e01e92 (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
<?php
function init_wizard() {
	global $S, $request, $conf;
	if (!isset($S['user'])) {
		return 'login';
	}
	// Make it so you can just pop over to config build=x and it will go to the right step
	if (isset($request['build']) && preg_match('/^[a-zA-Z0-9]{6}$/', $request['build'])) {
		$r=$S['pdo']->query('SELECT * FROM `builds` WHERE `owner`='.$S['user']->id.' AND `id`="'.$request['build'].'"');
		if ($r->rowCount()) {
			$S['wizard.build']=new sql_build($r->fetch(PDO::FETCH_ASSOC));
			$build=&$S['wizard.build'];
			$opts=$build->get_buildopts();
			$S['wizard.module']=$opts['frontend_module'];
			$module=&$S['wizard.module'];
			if (!preg_match('#^config/step([0-9]+)$#', $build->status, $match)) {
				debug('wizard', 'build not in config stage (status '.$build->status.') - PANIC!');
				throw_exception('We haven\'t implemented this yet.');
			}
			$S['wizard.step']=$match[1];
			$step=&$S['wizard.step'];
			debug('wizard', 'Build '.$build->id." is at step $step");
			if (isset($request['wizard_submit'])) {
				debug('wizard', "Processing step $step");
				if (!is_file(FRONTEND."/modules/$module/step$step.php")) {
					throw_exception("$modules step $step doesn't exist!");
				}
				require_once(FRONTEND."/modules/$module/step$step.php");
				$proc=$module.'_process_step'.$step;
				if (function_exists($proc)) {
					if ($proc() === true) {
						debug('wizard', "Step $step returned <i>true</i> - config finished!");
						$build->status='build/ready';
						$build->ctime=time();
						$build->write();
						$S['wizard.done']=true;
						return array('title' => 'Config Finished');
					}
				} else {
					debug('wizard', "No processing function for $wizard step $step");
				}
				$build->status='config/step'.++$step;
				$build->write();
				debug('wizard', "Continuing to step $step");
			} else {
				debug('wizard', "Not ready for processing... staying at step $step");
			}
		} else {
			throw_exception('Build not found');
		}
	} elseif (isset($request['init'])) {
		$r=$S['pdo']->query('SELECT * FROM `builds` WHERE `owner`='.$S['user']->id.' AND `status`="config/step1"');
		if ($r->rowCount()) {
			$S['wizard.build']=new sql_build($r->fetch(PDO::FETCH_ASSOC));
		} else {
			$S['wizard.build']=new sql_build();
			$S['wizard.build']->init();
		}
		$build=&$S['wizard.build'];		
		$S['wizard.build']->name=$request['name'];
		$S['wizard.build']->write();
		$femods=explode(' ', $conf['frontend_modules']);
		$bemods=explode(' ', $conf['backend_modules']);
		$femod=isset($request['femod']) && isset($femods[$request['femod']])?$femods[$request['femod']]:$femods[0];
		$bemod=isset($request['bemod']) && isset($bemods[$request['bemod']])?$bemods[$request['bemod']]:$bemods[0];
		$beopt=new sql_buildopt($build->id, 'backend_module', $bemod);
		$feopt=new sql_buildopt($build->id, 'frontend_module', $femod);
		debug('wizard', "Backend: $bemod; Frontend: $femod");
		$beopt->write();
		$feopt->write();
		$S['wizard.module']=$femod;
		$module=&$S['wizard.module'];
		$S['wizard.step']=1;
	}
	if (isset($S['wizard.build'], $S['wizard.step'])) {
		$step=&$S['wizard.step'];
		if (is_file(FRONTEND."/modules/$module/step$step.php")) {
			require_once(FRONTEND."/modules/$module/step$step.php");
			$S['title']='Create - Step '.$step;
			$proc=$module.'_init_step'.$step;
			if (function_exists($proc)) {
				return $proc();
			} else {
				debug('wizard', 'No init function for step '.$step);
				return;
			}
		} else {
			throw_exception("$module step $step doesn't exist");
		}
	}
	return array('title' => 'Create');
}
function body_wizard() {
	global $S, $conf;
	if (isset($S['wizard.build'])) {
		if (isset($S['wizard.done'])) {
			echo '<h3>Config finished!</h3><p>Check your build\'s status <a href="'.url('logs/'.$S['wizard.build']->id).'">here</a></p>';
		} else {
			$build=&$S['wizard.build'];
			$module=&$S['wizard.module'];
			$step=&$S['wizard.step'];
			echo '<h3>'.$S['title'].'</h3>';
			echo '<form action="'.url('create/'.$build->id).'" method="post">';
			require_once(FRONTEND."/modules/$module/step$step.php");
			$proc=$module.'_body_step'.$step;
			if (!function_exists($proc)) {
				throw_exception("Body function doesn't exist for $module step $step");
			}
			$proc();
			echo '<input type="submit" name="wizard_submit" value="Continue" /></form>';
		}
	} else {
		echo '<form action="'.url('create').'" method="post"><h3>Request an image built</h3>Name of your build (optional): <input name="name" /><br/>';
		$femods=explode(' ', $conf['frontend_modules']);
		$bemods=explode(' ', $conf['backend_modules']);
		if (count($femods) > 1) {
			echo 'Frontend module: <select name="femod">';
			$i=0;
			foreach ($femods as $mod)
				echo '<option value="'.$i++."\">$mod</option>";
			echo '</select><br/>';
		}
		if (count($bemods) > 1) {
			echo 'Backend module: <select name="bemod">';
			$i=0;
			foreach ($bemods as $mod)
				echo '<option value="'.$i++."\">$mod</option>";
			echo '</select><br/>';
		}
		echo '<input type="submit" name="init" value="Start" /></form>';
	}
}
?>