summaryrefslogtreecommitdiffstats
path: root/application/modules/dev/controllers/GroupController.php
blob: c86e4fecb457e47538b6464061bff36d7888e2fe (plain) (blame)
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
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
<?php

class dev_GroupController extends Zend_Controller_Action
{
	protected $groupMapper;
	protected $groupGroupsMapper;
	protected $membershipMapper;
	protected $groupRequestMapper;
	protected $personmapper;
	protected $rolemapper;
	protected $groupList;

	public function init()
	{
		if (Zend_Auth::getInstance()->hasIdentity()) {
			$this->groupMapper = new Application_Model_GroupMapper();
			$this->groupGroupsMapper = new Application_Model_GroupGroupsMapper();
			$this->membershipMapper = new Application_Model_MembershipMapper();
			$this->groupRequestMapper = new Application_Model_GroupRequestMapper();
			$this->personmapper = new Application_Model_PersonMapper();
			$this->rolemapper = new Application_Model_RoleMapper();
			$this->groupList = $this->groupMapper->fetchAll();
		} else {
			$this->_helper->redirector('login', 'auth');
			return;
		}
	}

	public function indexAction()
	{
		$this->view->groupList = $this->groupList;
	}

	public function addAction()
	{
		if (!isset($_POST["add"])){
			$addForm = new dev_Form_GroupAdd(array('grouplist' => $this->groupList));
		} else {
			$addForm = new dev_Form_GroupAdd(array('grouplist' => $this->groupList),$_POST);

			if ($addForm->isValid($_POST)) {
				$group = new Application_Model_Group($_POST);
				try {
					$this->groupMapper->save($group);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
				if($_POST['superordinatedGroupID'] != -1) {
					$lastID = $this->groupMapper->getDbTable()->getDefaultAdapter()->lastInsertId();
					$groupgroups = new Application_Model_GroupGroups();
					$groupgroups->setParentID($_POST['superordinatedGroupID']);
					$groupgroups->setGroupID($lastID);
					try {
						$this->groupGroupsMapper->save($groupgroups);
					} catch(Zend_Exception $e)
					{
						echo "Caught exception: " . get_class($e) . "<br/>";
						echo "Message: " . $e->getMessage() . "<br/>";
						return;
					}

				}
				$this->_helper->redirector('', 'group');
				return;
			}
		}

		$this->view->addForm = $addForm;
	}

	public function editAction()
	{
		$groupID = $this->_request->getParam('groupID');
		if(!isset($groupID)) {
			$addForm = new dev_Form_GroupAdd(array('grouplist' => $this->groupList));
			$this->view->addForm = $addForm;
			return;
		}
		if (!isset($_POST["save"])){
			$group = $this->groupMapper->find($groupID);
			$_POST['title'] = $group->getTitle();
			$_POST['description'] = $group->getDescription();
			$editForm = new dev_Form_GroupEdit();
		} else {
			$editForm = new dev_Form_GroupEdit($_POST);
			if ($editForm->isValid($_POST)) {
				$group = new Application_Model_Group($_POST);
				try {
					$this->groupMapper->save($group);
				} catch(Zend_Exception $e)
				{
					echo "Email Address already existing.";
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
				$this->_helper->redirector('', 'group');
				return;
			}
		}

		$this->view->editForm = $editForm;
	}

	public function showAction()
	{
		$groupID = $this->_request->getParam('groupID');
		if($groupID) {
			$groupRequests = $this->groupRequestMapper->findBy('groupID', $groupID);
			if(isset($groupRequests)) {
				foreach($groupRequests as $groupRequest) {
					$person = $this->personmapper->find($groupRequest['personID']);
					$groupRequestList[] = array(
					'grouprequestID' => $groupRequest['grouprequestID'],
					'person' => $person
					);
				}
				if(is_array($groupRequestList)) {
					$this->view->groupRequestList = $groupRequestList;
				}
				$this->view->roleList = $this->rolemapper->findBy('groupID', $groupID);
			}
			$members = $this->membershipMapper->findBy('groupID', $groupID);
			if(isset($members)) {
				foreach($members as $member) {
					$person = $this->personmapper->find($member['personID']);
					$membersList[] = array(
					'membershipID' => $member['membershipID'],
					'person' => $person
					);
				}
				if(is_array($membersList)) {
					$this->view->membersList = $membersList;
				}
			}
			$groupgroups = $this->groupGroupsMapper->findBy('groupID', $groupID);
			if(is_object($groupgroups)) {
				$parentGroup = $this->groupMapper->find($groupgroups->getParentID());
				$this->view->$parentGroup = $parentGroup;
			}
			$group = $this->groupMapper->find($groupID);
			$this->view->group = $group;
			$this->view->groupID = $groupID;
		}
	}

	public function linkAction()
	{
		if (!isset($_POST["link"])){
			$linkForm = new dev_Form_GroupLink(array('grouplist' => $this->groupList));
		} else {
			$linkForm = new dev_Form_GroupLink(array('grouplist' => $this->groupList),$_POST);

			if ($linkForm->isValid($_POST)) {
				$groupgroups = new Application_Model_GroupGroups();
				$groupgroups->setParentID($_POST['superordinatedGroupID']);
				$groupgroups->setGroupID($_POST['groupID']);
				try {
					$this->groupGroupsMapper->save($groupgroups);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
				$this->_helper->redirector('', 'group');
				return;
			}
		}

		$this->view->linkForm = $linkForm;
	}

	public function deleteAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$groupID = $this->_request->getParam('groupID');
		if (isset($groupID)){
			$group = $this->groupMapper->find($groupID);
			try {
				$this->groupMapper->delete($group);
			} catch(Zend_Exception $e)
			{
				echo "Caught exception: " . get_class($e) . "<br/>";
				echo "Message: " . $e->getMessage() . "<br/>";
				return;
			}
			$this->_helper->redirector('', 'group');
			return;
		}
	}

	public function grantpersonAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		if(isset($_POST['grouprequestID']) && isset($_POST['roleID'])) {
			$groupRequest = $this->groupRequestMapper->find($_POST['grouprequestID']);
			try {
				$this->groupRequestMapper->delete($groupRequest);
			} catch(Zend_Exception $e)
			{
				echo "Caught exception: " . get_class($e) . "<br/>";
				echo "Message: " . $e->getMessage() . "<br/>";
				return;
			}
			$membership = new Application_Model_Membership();
			$membership->setGroupID($groupRequest->getGroupID());
			$membership->setPersonID($groupRequest->getPersonID());
			$membership->setRoleID($_POST['roleID']);
			try {
				$this->membershipMapper->save($membership);
			} catch(Zend_Exception $e)
			{
				echo "Caught exception: " . get_class($e) . "<br/>";
				echo "Message: " . $e->getMessage() . "<br/>";
				return;
			}
			$this->_redirect("/dev/group/show/groupID/" . $groupRequest->getGroupID());
		}
	}

	public function revokepersonAction()
	{
		$this->_helper->viewRenderer->setNoRender();
		$membershipID = $this->_request->getParam('membershipID');
		if(isset($membershipID)) {
			$membership = $this->membershipMapper->find($membershipID);
			if(isset($membership)) {
				try {
					$this->membershipMapper->delete($membership);
				} catch(Zend_Exception $e)
				{
					echo "Caught exception: " . get_class($e) . "<br/>";
					echo "Message: " . $e->getMessage() . "<br/>";
					return;
				}
				$this->_redirect("/dev/group/edit/groupID/" . $membership->getGroupID());
			}

		}
	}
}