1<?php
2	class cms_role_controller extends controller {
3		public function show_role_overview() {
4			if (($roles = $this->model->get_all_roles()) === false) {
5				$this->output->add_tag("result", "Database error.");
6			} else {
7				$this->output->open_tag("overview");
8
9				$this->output->open_tag("roles");
10				foreach ($roles as $role) {
11					$this->output->add_tag("role", $role["name"], array("id" => $role["id"], "users" => $role["users"]));
12				}
13				$this->output->close_tag();
14
15				$this->output->close_tag();
16			}
17		}
18
19		public function show_role_form($role) {
20			if (isset($role["id"]) == false) {
21				$params = array(
22					"editable" => "yes");
23			} else {
24				$params = array(
25					"id"       => $role["id"],
26					"editable" => show_boolean($role["id"] != ADMIN_ROLE_ID));
27			}
28
29			if (($pages = $this->model->get_restricted_pages()) === false) {
30				$this->output->add_tag("result", "Database error.");
31				return;
32			}
33			sort($pages);
34
35			$this->output->open_tag("edit");
36
37			/* Roles
38			 */
39			$this->output->add_tag("role", $role["name"], $params);
40			$this->output->open_tag("pages");
41			foreach ($pages as $page) {
42				if (($value = $role[$page]) == null) {
43					$value = 0;
44				}
45				$params = array(
46					"value" => $value);
47				$this->output->add_tag("page", $page, $params);
48			}
49			$this->output->close_tag();
50
51			$this->output->open_tag("members");
52			if (($users = $this->model->get_role_members($role["id"])) !== false) {
53				foreach ($users as $user) {
54					$this->output->open_tag("member", array("id" => $user["id"]));
55					$this->output->add_tag("fullname", $user["fullname"]);
56					$this->output->add_tag("email", $user["email"]);
57					$this->output->close_tag();
58				}
59			}
60			$this->output->close_tag();
61
62			$this->output->close_tag();
63		}
64
65		public function execute() {
66			if ($_SERVER["REQUEST_METHOD"] == "POST") {
67				if ($_POST["submit_button"] == "Save role") {
68					/* Save role
69					 */
70					if ($this->model->save_oke($_POST) == false) {
71						$this->show_role_form($_POST);
72					} else if (isset($_POST["id"]) == false) {
73						/* Create role
74						 */
75						if ($this->model->create_role($_POST) === false) {
76							$this->output->add_message("Database error while creating role.");
77							$this->show_role_form($_POST);
78						} else {
79							$this->user->log_action("role %d created", $this->db->last_insert_id);
80							$this->show_role_overview();
81						}
82					} else {
83						/* Update role
84						 */
85						if ($this->model->update_role($_POST) === false) {
86							$this->output->add_message("Database error while updating role.");
87							$this->show_role_form($_POST);
88						} else {
89							$this->user->log_action("role %d updated", $_POST["id"]);
90							$this->show_role_overview();
91						}
92					}
93				} else if ($_POST["submit_button"] == "Delete role") {
94					/* Delete role
95					 */
96					if ($this->model->delete_oke($_POST) == false) {
97						$this->output->add_tag("result", "This role cannot be deleted.");
98					} else if ($this->model->delete_role($_POST["id"]) == false) {
99						$this->output->add_tag("result", "Database error while deleting role.");
100					} else {
101						$this->user->log_action("role %d deleted", $_POST["id"]);
102						$this->show_role_overview();
103					}
104				} else {
105					$this->show_role_overview();
106				}
107			} else if (valid_input($this->page->pathinfo[2], VALIDATE_NUMBERS, VALIDATE_NONEMPTY)) {
108				/* Show the role webform
109				 */
110				if (($role = $this->model->get_role($this->page->pathinfo[2])) != false) {
111					$this->show_role_form($role);
112				} else {
113					$this->output->add_tag("result", "Role not found.");
114				}
115			} else if ($this->page->pathinfo[2] == "new") {
116				/* Show the role webform
117				 */
118				$role = array("profile" => true);
119				$this->show_role_form($role);
120			} else {
121				/* Show a list of all roles
122				 */
123				$this->show_role_overview();
124			}
125		}
126	}
127?>
128