PageRenderTime 203ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/MilestoneController.class.php

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