/application/controllers/MilestoneController.class.php
PHP | 445 lines | 298 code | 72 blank | 75 comment | 47 complexity | 57b818748d734963330c8a256cfa0f7d MD5 | raw file
1<?php
2
3 /**
4 * Milestone controller
5 *
6 * @http://www.projectpier.org/
7 */
8 class MilestoneController extends ApplicationController {
9
10 /**
11 * Construct the MilestoneController
12 *
13 * @access public
14 * @param void
15 * @return MilestoneController
16 */
17 function __construct() {
18 parent::__construct();
19 prepare_company_website_controller($this, 'project_website');
20 } // __construct
21
22 /**
23 * List all milestones in specific (this) project
24 *
25 * @access public
26 * @param void
27 * @return null
28 */
29 function index() {
30 $this->addHelper('textile');
31 $project = active_project();
32
33 $this->canGoOn();
34
35 // Gets desired view 'detail' or 'list'
36 // $view_type is from URL, Cookie or set to default: 'list'
37 $view_type = array_var($_GET, 'view', Cookie::getValue('milestonesViewType', 'list'));
38 $expiration = Cookie::getValue('remember'.TOKEN_COOKIE_NAME) ? REMEMBER_LOGIN_LIFETIME : null;
39 Cookie::setValue('milestonesViewType', $view_type, $expiration);
40 $filter_assigned = array_var($_GET, 'assigned', Cookie::getValue('milestonesFilterAssigned', 'all'));
41 $expiration = Cookie::getValue('remember'.TOKEN_COOKIE_NAME) ? REMEMBER_LOGIN_LIFETIME : null;
42 Cookie::setValue('milestonesFilterAssigned', $filter_assigned, $expiration);
43
44 $all_milestones_visible_to_user = $project->getMilestones();
45
46 $all_assigned_to = array();
47 if (logged_user()->isMemberOfOwnerCompany() && is_array($all_milestones_visible_to_user) ) {
48 foreach($all_milestones_visible_to_user as $milestone) {
49 $assigned_to = $milestone->getAssignedTo();
50 if ($assigned_to) {
51 $all_assigned_to[$assigned_to->getDisplayName()]=$assigned_to;
52 }
53 }
54 }
55
56 tpl_assign('filter_assigned', $filter_assigned);
57 tpl_assign('view_type', $view_type);
58 tpl_assign('late_milestones', $project->getLateMilestones());
59 tpl_assign('today_milestones', $project->getTodayMilestones());
60 tpl_assign('upcoming_milestones', $project->getUpcomingMilestones());
61 tpl_assign('completed_milestones', $project->getCompletedMilestones());
62 tpl_assign('all_visible_milestones', $all_milestones_visible_to_user);
63 tpl_assign('assigned_to_milestones', $all_assigned_to);
64
65 $this->setSidebar(get_template_path('index_sidebar', 'milestone'));
66 } // index
67
68 /**
69 * Show view milestone page
70 *
71 * @access public
72 * @param void
73 * @return null
74 */
75 function view() {
76 $this->addHelper('textile');
77
78 $milestone = ProjectMilestones::findById(get_id());
79 if (!($milestone instanceof ProjectMilestone)) {
80 flash_error(lang('milestone dnx'));
81 $this->redirectTo('milestone', 'index');
82 } // if
83
84 if (!$milestone->canView(logged_user())) {
85 flash_error(lang('no access permissions'));
86 $this->redirectToReferer(get_url('milestone', 'index'));
87 } // if
88
89 tpl_assign('milestone', $milestone);
90 } // view
91
92 /**
93 * Show and process add milestone form
94 *
95 * @access public
96 * @param void
97 * @return null
98 */
99 function add() {
100 $this->addHelper('textile');
101 $this->setTemplate('add_milestone');
102
103 if (!ProjectMilestone::canAdd(logged_user(), active_project())) {
104 flash_error(lang('no access permissions'));
105 $this->redirectToReferer(get_url('milestone', 'index'));
106 } // if
107
108 $milestone_data = array_var($_POST, 'milestone');
109 if (!is_array($milestone_data)) {
110 $milestone_data = array(
111 'due_date' => DateTimeValueLib::now(),
112 'is_private' => config_option('default_private', false),
113 'send_notification' => config_option('send_notification_default', false),
114 ); // array
115 } // if
116 $milestone = new ProjectMilestone();
117 $milestone->setProjectId(active_project()->getId());
118 tpl_assign('milestone_data', $milestone_data);
119 tpl_assign('milestone', $milestone);
120 $this->setSidebar(get_template_path('textile_help_sidebar'));
121
122 if (is_array(array_var($_POST, 'milestone'))) {
123 if (isset($_POST['milestone_due_date'])) {
124 $milestone_data['due_date'] = DateTimeValueLib::makeFromString($_POST['milestone_due_date']);
125 } else {
126 $milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'milestone_due_date_month', 1), array_var($_POST, 'milestone_due_date_day', 1), array_var($_POST, 'milestone_due_date_year', 1970));
127 }
128
129 $assigned_to = explode(':', array_var($milestone_data, 'assigned_to', ''));
130
131 $milestone->setFromAttributes($milestone_data);
132 if (!logged_user()->isMemberOfOwnerCompany()) {
133 $milestone->setIsPrivate(false);
134 }
135
136 $milestone->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
137 $milestone->setAssignedToUserId(array_var($assigned_to, 1, 0));
138
139 try {
140 DB::beginWork();
141
142 $milestone->save();
143 if (plugin_active('tags')) {
144 $milestone->setTagsFromCSV(array_var($milestone_data, 'tags'));
145 }
146 ApplicationLogs::createLog($milestone, active_project(), ApplicationLogs::ACTION_ADD);
147
148 DB::commit();
149
150 // Send notification
151 try {
152 if (array_var($milestone_data, 'send_notification') == 'checked') {
153 Notifier::milestoneAssigned($milestone); // send notification
154 } // if
155 } catch(Exception $e) {
156
157 } // try
158
159 flash_success(lang('success add milestone', $milestone->getName()));
160 $this->redirectTo('milestone', 'index');
161
162 } catch(Exception $e) {
163 DB::rollback();
164 tpl_assign('error', $e);
165 } // try
166 } // if
167 } // add
168
169 /**
170 * Show and process edit milestone form
171 *
172 * @access public
173 * @param void
174 * @return null
175 */
176 function edit() {
177 $this->addHelper('textile');
178 $this->setTemplate('add_milestone');
179
180 $milestone = ProjectMilestones::findById(get_id());
181 if (!($milestone instanceof ProjectMilestone)) {
182 flash_error(lang('milestone dnx'));
183 $this->redirectTo('milestone', 'index');
184 } // if
185
186 if (!$milestone->canEdit(logged_user())) {
187 flash_error(lang('no access permissions'));
188 $this->redirectToReferer(get_url('milestone', 'index'));
189 }
190
191 $milestone_data = array_var($_POST, 'milestone');
192 if (!is_array($milestone_data)) {
193 $tag_names = plugin_active('tags') ? $milestone->getTagNames() : '';
194 $milestone_data = array(
195 'name' => $milestone->getName(),
196 'goal' => $milestone->getGoal(),
197 'due_date' => $milestone->getDueDate(),
198 'description' => $milestone->getDescription(),
199 'assigned_to' => $milestone->getAssignedToCompanyId() . ':' . $milestone->getAssignedToUserId(),
200 'tags' => is_array($tag_names) ? implode(', ', $tag_names) : '',
201 'is_private' => $milestone->isPrivate(),
202 'send_notification' => config_option('send_notification_default', false),
203 ); // array
204 } // if
205
206 tpl_assign('milestone_data', $milestone_data);
207 tpl_assign('milestone', $milestone);
208 $this->setSidebar(get_template_path('textile_help_sidebar'));
209
210 if (is_array(array_var($_POST, 'milestone'))) {
211 $old_owner = $milestone->getAssignedTo(); // remember the old owner
212 if (isset($_POST['milestone_due_date'])) {
213 $milestone_data['due_date'] = DateTimeValueLib::makeFromString($_POST['milestone_due_date']);
214 } else {
215 $milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'milestone_due_date_month', 1), array_var($_POST, 'milestone_due_date_day', 1), array_var($_POST, 'milestone_due_date_year', 1970));
216 }
217 //$milestone_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'milestone_due_date_month', 1), array_var($_POST, 'milestone_due_date_day', 1), array_var($_POST, 'milestone_due_date_year', 1970));
218
219 $assigned_to = explode(':', array_var($milestone_data, 'assigned_to', ''));
220
221 $old_is_private = $milestone->isPrivate();
222 $milestone->setFromAttributes($milestone_data);
223 if (!logged_user()->isMemberOfOwnerCompany()) {
224 $milestone->setIsPrivate($old_is_private);
225 }
226
227 $milestone->setProjectId(active_project()->getId());
228 $milestone->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
229 $milestone->setAssignedToUserId(array_var($assigned_to, 1, 0));
230
231 try {
232 DB::beginWork();
233 $milestone->save();
234 if (plugin_active('tags')) {
235 $milestone->setTagsFromCSV(array_var($milestone_data, 'tags'));
236 }
237
238 ApplicationLogs::createLog($milestone, active_project(), ApplicationLogs::ACTION_EDIT);
239 DB::commit();
240
241 // If owner is changed send notification but don't break submission
242 try {
243 $new_owner = $milestone->getAssignedTo();
244 if (array_var($milestone_data, 'send_notification') == 'checked') {
245 if ($old_owner instanceof User) {
246 // We have a new owner and it is different than old owner
247 if ($new_owner instanceof User && $new_owner->getId() <> $old_owner->getId()) {
248 Notifier::milestoneAssigned($milestone);
249 }
250 } else {
251 // We have new owner
252 if ($new_owner instanceof User) {
253 Notifier::milestoneAssigned($milestone);
254 }
255 } // if
256 } // if
257 } catch(Exception $e) {
258
259 } // try
260
261 flash_success(lang('success edit milestone', $milestone->getName()));
262 $this->redirectTo('milestone', 'index');
263
264 } catch(Exception $e) {
265 DB::rollback();
266 tpl_assign('error', $e);
267 } // try
268 } // if
269 } // edit
270
271 /**
272 * Delete single milestone
273 *
274 * @access public
275 * @param void
276 * @return null
277 */
278 function delete() {
279 $this->setTemplate('del_milestone');
280
281 $milestone = ProjectMilestones::findById(get_id());
282 if (!($milestone instanceof ProjectMilestone)) {
283 flash_error(lang('milestone dnx'));
284 $this->redirectTo('milestone', 'index');
285 } // if
286
287 if (!$milestone->canDelete(logged_user())) {
288 flash_error(lang('no access permissions'));
289 $this->redirectToReferer(get_url('milestone', 'index'));
290 } // if
291
292 $delete_data = array_var($_POST, 'deleteMilestone');
293 tpl_assign('milestone', $milestone);
294 tpl_assign('delete_data', $delete_data);
295
296 if (!is_array($delete_data)) {
297 $delete_data = array(
298 'really' => 0,
299 'password' => '',
300 ); // array
301 tpl_assign('delete_data', $delete_data);
302 } else if ($delete_data['really'] == 1) {
303 $password = $delete_data['password'];
304 if (trim($password) == '') {
305 tpl_assign('error', new Error(lang('password value missing')));
306 return $this->render();
307 }
308 if (!logged_user()->isValidPassword($password)) {
309 tpl_assign('error', new Error(lang('invalid login data')));
310 return $this->render();
311 }
312 try {
313
314 DB::beginWork();
315 $milestone->delete();
316 ApplicationLogs::createLog($milestone, $milestone->getProject(), ApplicationLogs::ACTION_DELETE);
317 DB::commit();
318
319 flash_success(lang('success deleted milestone', $milestone->getName()));
320 } catch(Exception $e) {
321 DB::rollback();
322 flash_error(lang('error delete milestone'));
323 } // try
324
325 $this->redirectTo('milestone', 'index');
326 } else {
327 flash_error(lang('error delete milestone'));
328 $this->redirectTo('milestone', 'index');
329 }
330 } // delete
331
332 /**
333 * Complete specific milestone
334 *
335 * @access public
336 * @param void
337 * @return null
338 */
339 function complete() {
340 $milestone = ProjectMilestones::findById(get_id());
341 if (!($milestone instanceof ProjectMilestone)) {
342 flash_error(lang('milestone dnx'));
343 $this->redirectTo('milestone', 'index');
344 } // if
345
346 if (!$milestone->canChangeStatus(logged_user())) {
347 flash_error(lang('no access permissions'));
348 $this->redirectToReferer(get_url('milestone', 'index'));
349 } // if
350
351 try {
352
353 $milestone->setCompletedOn(DateTimeValueLib::now());
354 $milestone->setCompletedById(logged_user()->getId());
355
356 DB::beginWork();
357 $milestone->save();
358 ApplicationLogs::createLog($milestone, active_project(), ApplicationLogs::ACTION_CLOSE);
359 DB::commit();
360
361 flash_success(lang('success complete milestone', $milestone->getName()));
362
363 } catch(Exception $e) {
364 DB::rollback();
365 flash_error(lang('error complete milestone'));
366 } // try
367
368 $this->redirectToReferer($milestone->getViewUrl());
369 } // complete
370
371 /**
372 * Open specific milestone
373 *
374 * @access public
375 * @param void
376 * @return null
377 */
378 function open() {
379 $milestone = ProjectMilestones::findById(get_id());
380 if (!($milestone instanceof ProjectMilestone)) {
381 flash_error(lang('milestone dnx'));
382 $this->redirectTo('milestone', 'index');
383 } // if
384
385 if (!$milestone->canChangeStatus(logged_user())) {
386 flash_error(lang('no access permissions'));
387 $this->redirectToReferer(get_url('milestone', 'index'));
388 } // if
389
390 try {
391
392 $milestone->setCompletedOn(null);
393 $milestone->setCompletedById(0);
394
395 DB::beginWork();
396 $milestone->save();
397 ApplicationLogs::createLog($milestone, active_project(), ApplicationLogs::ACTION_OPEN);
398 DB::commit();
399
400 flash_success(lang('success open milestone', $milestone->getName()));
401
402 } catch(Exception $e) {
403 DB::rollback();
404 flash_error(lang('error open milestone'));
405 } // try
406
407 $this->redirectToReferer($milestone->getViewUrl());
408 } // open
409
410 /**
411 * Show calendar view milestone page
412 *
413 * @access public
414 * @param void
415 * @return null
416 */
417 function calendar() {
418 $this->addHelper('textile');
419
420 $project = active_project();
421 $id = get_id();
422 if (strlen($id) == 0) {
423 $id = gmdate('Ym');
424 }
425 if (preg_match('/^(\d{4})(\d{2})$/', $id, $matches)) {
426 list (, $year, $month) = $matches;
427 tpl_assign('year', $year);
428 tpl_assign('month', $month);
429 } else {
430 flash_error(lang('id missing'));
431 $this->redirectToReferer(get_url('milestone', 'index'));
432 }
433
434 $view_type = array_var($_GET, 'view', Cookie::getValue('milestonesViewType', 'list'));
435 $expiration = Cookie::getValue('remember'.TOKEN_COOKIE_NAME) ? REMEMBER_LOGIN_LIFETIME : null;
436 Cookie::setValue('milestonesViewType', $view_type, $expiration);
437
438 tpl_assign('view_type', $view_type);
439 tpl_assign('milestones', $project->getMilestonesByMonth($year, $month));
440 tpl_assign('task_lists', $project->getTaskListsByMonth($year, $month));
441 } // calendar
442
443 } // MilestoneController
444
445?>