PageRenderTime 44ms CodeModel.GetById 15ms RepoModel.GetById 0ms app.codeStats 0ms

/application/controllers/TaskController.class.php

https://gitlab.com/x33n/ProjectPier-Core
PHP | 1133 lines | 838 code | 144 blank | 151 comment | 139 complexity | 0407e26e34fe4b1f3ddf169fc9c567a0 MD5 | raw file
  1. <?php
  2. /**
  3. * Controller for handling task list and task related requests
  4. *
  5. * @version 1.0
  6. * @http://www.projectpier.org/
  7. */
  8. class TaskController extends ApplicationController {
  9. /**
  10. * Construct the TaskController
  11. *
  12. * @access public
  13. * @param void
  14. * @return TaskController
  15. */
  16. function __construct() {
  17. parent::__construct();
  18. prepare_company_website_controller($this, 'project_website');
  19. } // __construct
  20. /**
  21. * Show index page
  22. *
  23. * @access public
  24. * @param void
  25. * @return null
  26. */
  27. function index() {
  28. $this->addHelper('textile');
  29. tpl_assign('open_task_lists', active_project()->getOpenTaskLists());
  30. tpl_assign('completed_task_lists', active_project()->getCompletedTaskLists());
  31. $this->canGoOn();
  32. $this->setSidebar(get_template_path('index_sidebar', 'task'));
  33. } // index
  34. /**
  35. * Download task list as attachment
  36. *
  37. * @access public
  38. * @param void
  39. * @return null
  40. */
  41. function download_list() {
  42. $task_list = ProjectTaskLists::findById(get_id());
  43. if (!($task_list instanceof ProjectTaskList)) {
  44. flash_error(lang('task list dnx'));
  45. $this->redirectTo('task');
  46. } // if
  47. $this->canGoOn();
  48. if (!$task_list->canView(logged_user())) {
  49. flash_error(lang('no access permissions'));
  50. $this->redirectToReferer(get_url('task'));
  51. } // if
  52. $output = array_var($_GET, 'output', 'csv');
  53. $project_name = active_project()->getName();
  54. $task_list_name = $task_list->getName();
  55. $task_count = 0;
  56. if ($output == 'pdf' ) {
  57. Env::useLibrary('fpdf');
  58. $download_name = "{$project_name}-tasks.pdf";
  59. $download_type = 'application/pdf';
  60. $pdf = new FPDF("P","mm");
  61. $pdf->AddPage();
  62. $pdf->SetTitle($project_name);
  63. $pdf->SetCompression(true);
  64. $pdf->SetCreator('ProjectPier');
  65. $pdf->SetDisplayMode('fullpage', 'single');
  66. $pdf->SetSubject(active_project()->getObjectName());
  67. $pdf->SetFont('Arial','B',16);
  68. $task_lists = active_project()->getOpenTaskLists();
  69. $pdf->Cell(0,10, lang('project') . ': ' . active_project()->getObjectName(),'B',0,'C');
  70. $pdf->Ln(14);
  71. $w = array( 0 => 12, 10, 20, 30, 40, 50, 60, 70, 80, 90, 100, 120, 140 );
  72. foreach($task_lists as $task_list) {
  73. $pdf->SetFont('Arial','B',14);
  74. $pdf->Write(10, lang('task list') . ': ' . $task_list->getObjectName());
  75. $pdf->Ln(14);
  76. $tasks = $task_list->getTasks();
  77. $pdf->SetFont('Arial','I',14);
  78. $pdf->SetFillColor(230,230,230);
  79. $pdf->Cell($w[1],6, '#',1,0,'C',true);
  80. $pdf->Cell($w[3],6, lang('status'),1,0,'C',true);
  81. $pdf->Cell($w[10],6, lang('info') ,1,0,'C',true);
  82. $pdf->Cell(0,6, lang(user) ,1,0,'C',true);
  83. $pdf->Ln();
  84. foreach($tasks as $task) {
  85. $line++;
  86. if ($task->isCompleted()) {
  87. $task_status = lang('completed');
  88. $task_status_color_R = 0;
  89. $task_status_color_G = 150;
  90. $task_status_color_B = 0;
  91. $task_completion_info = lang('completed task') . ' : ' . format_date($task->getCompletedOn()) . ' @ ' . format_time($task->getCompletedOn());
  92. } else {
  93. $task_status = lang('open');
  94. $task_status_color_R = 200;
  95. $task_status_color_G = 0;
  96. $task_status_color_B = 0;
  97. $task_completion_info = lang('due date') . ' : ' . lang('not assigned');
  98. $task_completion_info_color_R = 200;
  99. $task_completion_info_color_G = 0;
  100. $task_completion_info_color_B = 0;
  101. if ($task->getDueDate()) {
  102. $task_completion_info = lang('due date') . ' : ' . format_date($task->getDueDate()) . ' @ ' . format_time($task->getDueDate());
  103. $task_completion_info_color_R = 0;
  104. $task_completion_info_color_G = 0;
  105. $task_completion_info_color_B = 0;
  106. }
  107. }
  108. if ($task->getAssignedTo()) {
  109. $task_assignee = $task->getAssignedTo()->getObjectName();
  110. $task_assignee_color_R = 0;
  111. $task_assignee_color_G = 0;
  112. $task_assignee_color_B = 0;
  113. } else {
  114. $task_assignee = lang('not assigned');
  115. $task_assignee_color_R = 200;
  116. $task_assignee_color_G = 0;
  117. $task_assignee_color_B = 0;
  118. }
  119. $pdf->SetFillColor(245,245,245);
  120. $pdf->Cell($w[1],6, $line,1,0,'C',true);
  121. $pdf->SetTextColor($task_status_color_R, $task_status_color_G, $task_status_color_B);
  122. $pdf->Cell($w[3],6, $task_status,1,0,'C',true);
  123. $pdf->SetTextColor($task_completion_info_color_R, $task_completion_info_color_G, $task_completion_info_color_B);
  124. $pdf->Cell($w[10],6,$task_completion_info,1,0,'C',true);
  125. $pdf->SetTextColor($task_assignee_color_R, $task_assignee_color_G, $task_assignee_color_B);
  126. $pdf->Cell(0,6, $task_assignee ,1,0,'C',true);
  127. $pdf->SetTextColor(0, 0, 0);
  128. $pdf->Ln();
  129. $pdf->MultiCell(0,6,$task->getText(),1);
  130. //$pdf->Ln();
  131. }
  132. }
  133. $pdf->Output($download_name, 'I');
  134. }
  135. if ($output == 'txt' ) {
  136. $download_name = "{$project_name}-tasks.txt";
  137. $download_type = 'text/csv';
  138. $txt_lang_1 = lang('project');
  139. $txt_lang_2 = lang('milestone');
  140. $txt_lang_3 = lang('task list');
  141. $txt_lang_4 = lang('status');
  142. $txt_lang_5 = lang('description');
  143. $txt_lang_6 = lang('id');
  144. $txt_lang_7 = lang('status');
  145. $txt_lang_8 = lang('completion info');
  146. $txt_lang_9 = lang('assigned to');
  147. $s .= "$txt_lang_1\t$txt_lang_2\t$txt_lang_3\t$txt_lang_4\t$txt_lang_5\t$txt_lang_6\t$txt_lang_7\t$txt_lang_8\t$txt_lang_9";
  148. $s .= "\n";
  149. $task_lists = active_project()->getOpenTaskLists();
  150. foreach($task_lists as $task_list) {
  151. /*$s .= $task_list->getObjectName();
  152. $s .= "\n";
  153. $task_list_desc = $task_list->getDescription();
  154. $task_list_desc = strtr($task_list_desc,"\r\n\t"," ");
  155. $task_list_desc_100 = substr($task_list_desc,0,100);
  156. $s .= $task_list_desc_100;
  157. $s .= "\n";*/
  158. $milestone=$task_list->getMilestone();
  159. $tasks = $task_list->getTasks();
  160. foreach($tasks as $task) {
  161. $s .= $project_name;
  162. $s .= "\t";
  163. $milestone_name = lang(none);
  164. if ($milestone instanceof ProjectMilestone) {
  165. $milestone_name=$milestone->getName();
  166. }
  167. $s .= $milestone_name;
  168. $s .= "\t";
  169. $s .= $task_list->getObjectName();
  170. $s .= "\t";
  171. $task_list_name=$task_list->getName();
  172. if ($task_list->isCompleted()) {
  173. $task_list_status = lang('completed');
  174. } else {
  175. $task_list_status = lang('open');
  176. }
  177. $s .= $task_list_status;
  178. $s .= "\t";
  179. $task_list_desc2 = $task_list->getDescription();
  180. $task_list_desc2 = strtr($task_list_desc2,"\r\n\t"," ");
  181. $task_list_desc2_100 = substr($task_list_desc2,0,50);
  182. $s .= $task_list_desc2_100;
  183. $s .= "\t";
  184. $s .= $task->getId();
  185. $s .= "\t";
  186. if ($task->isCompleted()) {
  187. $task_status = lang('completed');
  188. $task_completion_info = format_date($task->getCompletedOn()) ." @ ". format_time($task->getCompletedOn());
  189. } else {
  190. $task_status = lang('open');
  191. $task_completion_info = format_date($task->getDueDate()) ." @ ". format_time($task->getDueDate());
  192. }
  193. $s .= $task_status;
  194. $s .= "\t";
  195. $s .= $task_completion_info;
  196. $s .= "\t";
  197. if ($task->getAssignedTo()) {
  198. $task_assignee = $task->getAssignedTo()->getObjectName();
  199. } else {
  200. $task_assignee = lang('not assigned');
  201. }
  202. $s .= $task_assignee;
  203. $s .= "\n";
  204. }
  205. }
  206. $download_contents = $s;
  207. download_headers( $download_name, $download_type, strlen($download_contents), true);
  208. echo $download_contents;
  209. } else {
  210. $download_name = "{$project_name}-{$task_list_name}-tasks.csv";
  211. $download_type = 'text/csv';
  212. $download_contents = $task_list->getDownloadText($task_count, "\t", true);
  213. download_contents($download_contents, $download_type, $download_name, strlen($download_contents));
  214. }
  215. die();
  216. }
  217. /**
  218. * View task lists page
  219. *
  220. * @access public
  221. * @param void
  222. * @return null
  223. */
  224. function view_list() {
  225. $this->addHelper('textile');
  226. $task_list = ProjectTaskLists::findById(get_id());
  227. if (!($task_list instanceof ProjectTaskList)) {
  228. flash_error(lang('task list dnx'));
  229. $this->redirectTo('task');
  230. } // if
  231. $this->canGoOn();
  232. if (!$task_list->canView(logged_user())) {
  233. flash_error(lang('no access permissions'));
  234. $this->redirectToReferer(get_url('task'));
  235. } // if
  236. tpl_assign('task_list', $task_list);
  237. // Sidebar
  238. tpl_assign('open_task_lists', active_project()->getOpenTaskLists());
  239. tpl_assign('completed_task_lists', active_project()->getCompletedTaskLists());
  240. $this->setSidebar(get_template_path('index_sidebar', 'task'));
  241. } // view_list
  242. /**
  243. * Add new task list
  244. *
  245. * @access public
  246. * @param void
  247. * @return null
  248. */
  249. function add_list() {
  250. if (!ProjectTaskList::canAdd(logged_user(), active_project())) {
  251. flash_error(lang('no access permissions'));
  252. $this->redirectToReferer(get_url('task'));
  253. } // if
  254. $task_list = new ProjectTaskList();
  255. $task_list->setProjectId(active_project()->getId());
  256. $task_list_data = array_var($_POST, 'task_list');
  257. if (!is_array($task_list_data)) {
  258. $task_list_data = array(
  259. 'milestone_id' => array_var($_GET, 'milestone_id'),
  260. 'start_date' => DateTimeValueLib::now(),
  261. 'is_private' => config_option('default_private', false),
  262. 'task0' => array( 'start_date' => DateTimeValueLib::now() ),
  263. 'task1' => array( 'start_date' => DateTimeValueLib::now() ),
  264. 'task2' => array( 'start_date' => DateTimeValueLib::now() ),
  265. 'task3' => array( 'start_date' => DateTimeValueLib::now() ),
  266. 'task4' => array( 'start_date' => DateTimeValueLib::now() ),
  267. 'task5' => array( 'start_date' => DateTimeValueLib::now() ),
  268. ); // array
  269. } else {
  270. for ($i = 0; $i < 6; $i++) {
  271. $due_date = $_POST["task_list_task{$i}_due_date"];
  272. $task_list_data["task{$i}"]['due_date'] = $due_date;
  273. $start_date = $_POST["task_list_task{$i}_start_date"];
  274. $task_list_data["task{$i}"]['start_date'] = $start_date;
  275. }
  276. } // if
  277. tpl_assign('task_list_data', $task_list_data);
  278. tpl_assign('task_list', $task_list);
  279. if (is_array(array_var($_POST, 'task_list'))) {
  280. if (isset($_POST['task_list_start_date'])) {
  281. $task_list_data['start_date'] = DateTimeValueLib::makeFromString($_POST['task_list_start_date']);
  282. }
  283. if (isset($_POST['task_list_due_date'])) {
  284. $task_list_data['due_date'] = DateTimeValueLib::makeFromString($_POST['task_list_due_date']);
  285. }
  286. //$task_list_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_list_due_date_month', 1), array_var($_POST, 'task_list_due_date_day', 1), array_var($_POST, 'task_list_due_date_year', 1970));
  287. $task_list->setFromAttributes($task_list_data);
  288. if (!logged_user()->isMemberOfOwnerCompany()) {
  289. $task_list->setIsPrivate(false);
  290. }
  291. $tasks = array();
  292. for ($i = 0; $i < 6; $i++) {
  293. if (isset($task_list_data["task{$i}"]) && is_array($task_list_data["task{$i}"]) && (trim(array_var($task_list_data["task{$i}"], 'text')) <> '')) {
  294. $assigned_to = explode(':', array_var($task_list_data["task{$i}"], 'assigned_to', ''));
  295. if (isset($_POST["task_list_task{$i}_start_date"])) {
  296. $start_date = DateTimeValueLib::makeFromString($_POST["task_list_task{$i}_start_date"]);
  297. }
  298. if (isset($_POST["task_list_task{$i}_due_date"])) {
  299. $due_date = DateTimeValueLib::makeFromString($_POST["task_list_task{$i}_due_date"]);
  300. }
  301. $tasks[] = array(
  302. 'text' => array_var($task_list_data["task{$i}"], 'text'),
  303. 'order' => 1 + $i ,
  304. 'start_date' => $start_date,
  305. 'due_date' => $due_date,
  306. 'assigned_to_company_id' => array_var($assigned_to, 0, 0),
  307. 'assigned_to_user_id' => array_var($assigned_to, 1, 0),
  308. 'send_notification' => array_var($task_list_data["task{$i}"], 'send_notification')
  309. ); // array
  310. } // if
  311. } // for
  312. try {
  313. DB::beginWork();
  314. $task_list->save();
  315. if (plugin_active('tags')) {
  316. $task_list->setTagsFromCSV(array_var($task_list_data, 'tags'));
  317. }
  318. foreach ($tasks as $task_data) {
  319. $task = new ProjectTask();
  320. $task->setFromAttributes($task_data);
  321. $task_list->attachTask($task);
  322. $task->save();
  323. tpl_assign('task', $task);
  324. // notify user
  325. if (array_var($task_data, 'send_notification') == 'checked') {
  326. try {
  327. $notify_people = array();
  328. $project_companies = array();
  329. if($task->getAssignedTo() == null)
  330. $project_companies = active_project()->getCompanies();
  331. if($task->getAssignedTo() instanceof Company)
  332. $project_companies = array($task->getAssignedTo());
  333. if($task->getAssignedTo() instanceof User)
  334. $notify_people = array($task->getAssignedTo());
  335. foreach($project_companies as $project_company) {
  336. $company_users = $project_company->getUsersOnProject(active_project());
  337. if(is_array($company_users))
  338. foreach($company_users as $company_user)
  339. $notify_people[] = $company_user;
  340. } // if
  341. Notifier::newTask($task, $notify_people);
  342. } catch(Exception $e) {
  343. Logger::log("Error: Notification failed, " . $e->getMessage(), Logger::ERROR);
  344. } // try
  345. } // if
  346. } // foreach
  347. ApplicationLogs::createLog($task_list, active_project(), ApplicationLogs::ACTION_ADD);
  348. DB::commit();
  349. flash_success(lang('success add task list', $task_list->getName()));
  350. $this->redirectToUrl($task_list->getViewUrl());
  351. } catch(Exception $e) {
  352. DB::rollback();
  353. tpl_assign('error', $e);
  354. } // try
  355. } // if
  356. } // add_list
  357. /**
  358. * Edit task list
  359. *
  360. * @access public
  361. * @param void
  362. * @return null
  363. */
  364. function edit_list() {
  365. $this->setTemplate('add_list');
  366. $task_list = ProjectTaskLists::findById(get_id());
  367. if (!($task_list instanceof ProjectTaskList)) {
  368. flash_error(lang('task list dnx'));
  369. $this->redirectTo('task');
  370. } // if
  371. if (!$task_list->canEdit(logged_user())) {
  372. flash_error(lang('no access permissions'));
  373. $this->redirectTo('task');
  374. } // if
  375. $task_list_data = array_var($_POST, 'task_list');
  376. if (!is_array($task_list_data)) {
  377. $tag_names = plugin_active('tags') ? $task_list->getTagNames() : '';
  378. $task_list_data = array(
  379. 'name' => $task_list->getName(),
  380. 'priority' => $task_list->getPriority(),
  381. 'score' => $task_list->getScore(),
  382. 'description' => $task_list->getDescription(),
  383. 'start_date' => $task_list->getStartDate(),
  384. 'due_date' => $task_list->getDueDate(),
  385. 'milestone_id' => $task_list->getMilestoneId(),
  386. 'tags' => is_array($tag_names) && count($tag_names) ? implode(', ', $tag_names) : '',
  387. 'is_private' => $task_list->isPrivate()
  388. ); // array
  389. } // if
  390. tpl_assign('task_list', $task_list);
  391. tpl_assign('task_list_data', $task_list_data);
  392. if (is_array(array_var($_POST, 'task_list'))) {
  393. $old_is_private = $task_list->isPrivate();
  394. if (isset($_POST['task_list_start_date'])) {
  395. $task_list_data['start_date'] = DateTimeValueLib::makeFromString($_POST['task_list_start_date']);
  396. }
  397. if (isset($_POST['task_list_due_date'])) {
  398. $task_list_data['due_date'] = DateTimeValueLib::makeFromString($_POST['task_list_due_date']);
  399. }
  400. //$task_list_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_list_due_date_month', 1), array_var($_POST, 'task_list_due_date_day', 1), array_var($_POST, 'task_list_due_date_year', 1970));
  401. $task_list->setFromAttributes($task_list_data);
  402. if (!logged_user()->isMemberOfOwnerCompany()) {
  403. $task_list->setIsPrivate($old_is_private);
  404. }
  405. try {
  406. DB::beginWork();
  407. $task_list->save();
  408. if (plugin_active('tags')) {
  409. $task_list->setTagsFromCSV(array_var($task_list_data, 'tags'));
  410. }
  411. ApplicationLogs::createLog($task_list, active_project(), ApplicationLogs::ACTION_EDIT);
  412. DB::commit();
  413. flash_success(lang('success edit task list', $task_list->getName()));
  414. $this->redirectToUrl($task_list->getViewUrl());
  415. } catch(Exception $e) {
  416. DB::rollback();
  417. tpl_assign('error', $e);
  418. } // try
  419. } // if
  420. } // edit_list
  421. /**
  422. * Copy task list then redirect to edit
  423. *
  424. * @access public
  425. * @param void
  426. * @return null
  427. */
  428. function copy_list() {
  429. $task_list = ProjectTaskLists::findById(get_id());
  430. if (!($task_list instanceof ProjectTaskList)) {
  431. flash_error(lang('task list dnx'));
  432. $this->redirectTo('task', 'index');
  433. } // if
  434. if (!$task_list->canAdd(logged_user(), active_project())) {
  435. flash_error(lang('no access permissions'));
  436. $this->redirectTo('task', 'index');
  437. } // if
  438. try {
  439. DB::beginWork();
  440. $source_task_list = $task_list;
  441. $task_list = new ProjectTaskList();
  442. $task_list->setName($source_task_list->getName().' ('.lang('copy').')');
  443. $task_list->setPriority($source_task_list->getPriority());
  444. $task_list->setDescription($source_task_list->getDescription());
  445. $task_list->setMilestoneId($source_task_list->getMilestoneId());
  446. $task_list->setDueDate($source_task_list->getDueDate());
  447. $task_list->setIsPrivate($source_task_list->getIsPrivate());
  448. $task_list->setOrder($source_task_list->getOrder());
  449. $task_list->setProjectId($source_task_list->getProjectId());
  450. $task_list->save();
  451. $task_count = 0;
  452. $source_tasks = $source_task_list->getTasks();
  453. if (is_array($source_tasks)) {
  454. foreach($source_tasks as $source_task) {
  455. $task = new ProjectTask();
  456. $task->setText($source_task->getText());
  457. $task->setAssignedToUserId($source_task->getAssignedToUserId());
  458. $task->setAssignedToCompanyId($source_task->getAssignedToCompanyId());
  459. $task->setOrder($source_task->getOrder());
  460. $task->setDueDate($source_task->getDueDate());
  461. $task_list->attachTask($task);
  462. $task_count++;
  463. }
  464. }
  465. ApplicationLogs::createLog($task_list, active_project(), ApplicationLogs::ACTION_ADD);
  466. DB::commit();
  467. flash_success(lang('success copy task list', $source_task_list->getName(), $task_list->getName(), $task_count));
  468. //$this->redirectToUrl($task_list->getEditUrl());
  469. $this->redirectTo('task', 'index');
  470. } catch(Exception $e) {
  471. DB::rollback();
  472. tpl_assign('error', $e);
  473. } // try
  474. } // copy_list
  475. /**
  476. * Move task list
  477. *
  478. * @access public
  479. * @param void
  480. * @return null
  481. */
  482. function move_list() {
  483. $this->setTemplate('move_list');
  484. $task_list = ProjectTaskLists::findById(get_id());
  485. if (!($task_list instanceof ProjectTaskList)) {
  486. flash_error(lang('task list dnx'));
  487. $this->redirectTo('task', 'index');
  488. } // if
  489. if (!$task_list->canDelete(logged_user(), active_project())) {
  490. flash_error(lang('no access permissions'));
  491. $this->redirectTo('task', 'index');
  492. } // if
  493. $move_data = array_var($_POST, 'move_data');
  494. tpl_assign('task_list', $task_list);
  495. tpl_assign('move_data', $move_data);
  496. if (is_array($move_data)) {
  497. $target_project_id = $move_data['target_project_id'];
  498. $target_project = Projects::findById($target_project_id);
  499. if (!($target_project instanceof Project)) {
  500. flash_error(lang('project dnx'));
  501. $this->redirectToUrl($task_list->getMoveUrl());
  502. } // if
  503. if (!$task_list->canAdd(logged_user(), $target_project)) {
  504. flash_error(lang('no access permissions'));
  505. $this->redirectToUrl($task_list->getMoveUrl());
  506. } // if
  507. try {
  508. DB::beginWork();
  509. $task_list->setProjectId($target_project_id);
  510. $task_list->save();
  511. ApplicationLogs::createLog($task_list, active_project(), ApplicationLogs::ACTION_DELETE);
  512. ApplicationLogs::createLog($task_list, $target_project, ApplicationLogs::ACTION_ADD);
  513. DB::commit();
  514. flash_success(lang('success move task list', $task_list->getName(), active_project()->getName(), $target_project->getName() ));
  515. } catch(Exception $e) {
  516. DB::rollback();
  517. flash_error(lang('error move task list'));
  518. } // try
  519. $this->redirectToUrl($task_list->getViewUrl());
  520. }
  521. } // move_list
  522. /**
  523. * Delete task list
  524. *
  525. * @access public
  526. * @param void
  527. * @return null
  528. */
  529. function delete_list() {
  530. $this->setTemplate('del_list');
  531. $task_list = ProjectTaskLists::findById(get_id());
  532. if (!($task_list instanceof ProjectTaskList)) {
  533. flash_error(lang('task list dnx'));
  534. $this->redirectTo('task');
  535. } // if
  536. if (!$task_list->canDelete(logged_user())) {
  537. flash_error(lang('no access permissions'));
  538. $this->redirectTo('task');
  539. } // if
  540. $delete_data = array_var($_POST, 'deleteTaskList');
  541. tpl_assign('task_list', $task_list);
  542. tpl_assign('delete_data', $delete_data);
  543. if (!is_array($delete_data)) {
  544. $delete_data = array(
  545. 'really' => 0,
  546. 'password' => '',
  547. ); // array
  548. tpl_assign('delete_data', $delete_data);
  549. } else if ($delete_data['really'] == 1) {
  550. $password = $delete_data['password'];
  551. if (trim($password) == '') {
  552. tpl_assign('error', new Error(lang('password value missing')));
  553. return $this->render();
  554. }
  555. if (!logged_user()->isValidPassword($password)) {
  556. tpl_assign('error', new Error(lang('invalid login data')));
  557. return $this->render();
  558. }
  559. try {
  560. DB::beginWork();
  561. $task_list->delete();
  562. ApplicationLogs::createLog($task_list, active_project(), ApplicationLogs::ACTION_DELETE);
  563. DB::commit();
  564. flash_success(lang('success delete task list', $task_list->getName()));
  565. } catch(Exception $e) {
  566. DB::rollback();
  567. flash_error(lang('error delete task list'));
  568. } // try
  569. $this->redirectTo('task');
  570. } else {
  571. flash_error(lang('error delete task list'));
  572. $this->redirectToUrl($task_list->getViewUrl());
  573. }
  574. } // delete_list
  575. /**
  576. * Show and process reorder tasks form
  577. *
  578. * @param void
  579. * @return null
  580. */
  581. function reorder_tasks() {
  582. $task_list = ProjectTaskLists::findById(get_id('task_list_id'));
  583. if (!($task_list instanceof ProjectTaskList)) {
  584. flash_error(lang('task list dnx'));
  585. $this->redirectTo('task');
  586. } // if
  587. $back_to_list = (boolean) array_var($_GET, 'back_to_list');
  588. $redirect_to = $back_to_list ? $task_list->getViewUrl() : get_url('task');
  589. if (!$task_list->canReorderTasks(logged_user())) {
  590. flash_error(lang('no access permissions'));
  591. $this->redirectToUrl($redirect_to);
  592. } // if
  593. $tasks = $task_list->getOpenTasks();
  594. if (!is_array($tasks) || (count($tasks) < 1)) {
  595. flash_error(lang('no open task in task list'));
  596. $this->redirectToUrl($redirect_to);
  597. } // if
  598. tpl_assign('task_list', $task_list);
  599. tpl_assign('tasks', $tasks);
  600. tpl_assign('back_to_list', $back_to_list);
  601. if (array_var($_POST, 'submitted') == 'submitted') {
  602. $updated = 0;
  603. foreach ($tasks as $task) {
  604. $new_value = (integer) array_var($_POST, 'task_' . $task->getId());
  605. if ($new_value <> $task->getOrder()) {
  606. $task->setOrder($new_value);
  607. if ($task->save()) {
  608. $updated++;
  609. } // if
  610. } // if
  611. } // foreach
  612. flash_success(lang('success n tasks updated', $updated));
  613. $this->redirectToUrl($redirect_to);
  614. } // if
  615. } // reorder_tasks
  616. // ---------------------------------------------------
  617. // Tasks
  618. // ---------------------------------------------------
  619. /**
  620. * Add single task
  621. *
  622. * @access public
  623. * @param void
  624. * @return null
  625. */
  626. function add_task() {
  627. $task_list = ProjectTaskLists::findById(get_id('task_list_id'));
  628. if (!($task_list instanceof ProjectTaskList)) {
  629. flash_error(lang('task list dnx'));
  630. $this->redirectTo('task');
  631. } // if
  632. if (!$task_list->canAddTask(logged_user())) {
  633. flash_error(lang('no access permissions'));
  634. $this->redirectTo('task');
  635. } // if
  636. $back_to_list = array_var($_GET, 'back_to_list');
  637. $task = new ProjectTask();
  638. $task_data = array_var($_POST, 'task');
  639. if (!is_array($task_data)) {
  640. $task_data = array(
  641. 'due_date' => DateTimeValueLib::now(),
  642. ); // array
  643. } // if
  644. tpl_assign('task', $task);
  645. tpl_assign('task_list', $task_list);
  646. tpl_assign('back_to_list', $back_to_list);
  647. tpl_assign('task_data', $task_data);
  648. // Form is submitted
  649. if (is_array(array_var($_POST, 'task'))) {
  650. $old_owner = $task->getAssignedTo();
  651. //$task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
  652. if (isset($_POST['task_start_date'])) {
  653. $task_data['start_date'] = DateTimeValueLib::makeFromString($_POST['task_start_date']);
  654. } else {
  655. $task_data['start_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_start_date_month', 1), array_var($_POST, 'task_start_date_day', 1), array_var($_POST, 'task_start_date_year', 1970));
  656. }
  657. if (isset($_POST['task_due_date'])) {
  658. $task_data['due_date'] = DateTimeValueLib::makeFromString($_POST['task_due_date']);
  659. } else {
  660. $task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
  661. }
  662. $task->setFromAttributes($task_data);
  663. $assigned_to = explode(':', array_var($task_data, 'assigned_to', ''));
  664. $task->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
  665. $task->setAssignedToUserId(array_var($assigned_to, 1, 0));
  666. try {
  667. DB::beginWork();
  668. $task->save();
  669. $task_list->attachTask($task);
  670. ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_ADD);
  671. DB::commit();
  672. // notify user
  673. if (array_var($task_data, 'send_notification') == 'checked') {
  674. try {
  675. $notify_people = array();
  676. $project_companies = array();
  677. if($task->getAssignedTo() == null)
  678. $project_companies = active_project()->getCompanies();
  679. if($task->getAssignedTo() instanceof Company)
  680. $project_companies = array($task->getAssignedTo());
  681. if($task->getAssignedTo() instanceof User)
  682. $notify_people = array($task->getAssignedTo());
  683. foreach($project_companies as $project_company) {
  684. $company_users = $project_company->getUsersOnProject(active_project());
  685. if(is_array($company_users))
  686. foreach($company_users as $company_user)
  687. $notify_people[] = $company_user;
  688. } // if
  689. Notifier::newTask($task, $notify_people);
  690. } catch(Exception $e) {
  691. Logger::log("Error: Notification failed, " . $e->getMessage(), Logger::ERROR);
  692. } // try
  693. } // if
  694. flash_success(lang('success add task'));
  695. if ($back_to_list) {
  696. $this->redirectToUrl($task_list->getViewUrl());
  697. } else {
  698. $this->redirectTo('task');
  699. } // if
  700. } catch(Exception $e) {
  701. DB::rollback();
  702. tpl_assign('error', $e);
  703. } // try
  704. } // if
  705. } // add_task
  706. /**
  707. * Edit task
  708. *
  709. * @access public
  710. * @param void
  711. * @return null
  712. */
  713. function edit_task() {
  714. $this->setTemplate('add_task');
  715. $task = ProjectTasks::findById(get_id());
  716. if (!($task instanceof ProjectTask)) {
  717. flash_error(lang('task dnx'));
  718. $this->redirectTo('task');
  719. } // if
  720. $task_list = $task->getTaskList();
  721. if (!($task_list instanceof ProjectTaskList)) {
  722. flash_error('task list dnx');
  723. $this->redirectTo('task');
  724. } // if
  725. if (!$task->canEdit(logged_user())) {
  726. flash_error(lang('no access permissions'));
  727. $this->redirectTo('task');
  728. } // if
  729. $task_data = array_var($_POST, 'task');
  730. if (!is_array($task_data)) {
  731. $task_data = array(
  732. 'text' => $task->getText(),
  733. 'start_date' => $task->getStartDate(),
  734. 'due_date' => $task->getDueDate(),
  735. 'task_list_id' => $task->getTaskListId(),
  736. 'assigned_to' => $task->getAssignedToCompanyId() . ':' . $task->getAssignedToUserId(),
  737. 'send_notification' => config_option('send_notification_default', '0')
  738. ); // array
  739. } // if
  740. tpl_assign('task', $task);
  741. tpl_assign('task_list', $task_list);
  742. tpl_assign('task_data', $task_data);
  743. if (is_array(array_var($_POST, 'task'))) {
  744. $old_owner = $task->getAssignedTo();
  745. //$task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
  746. if (isset($_POST['task_start_date'])) {
  747. $task_data['start_date'] = DateTimeValueLib::makeFromString($_POST['task_start_date']);
  748. } else {
  749. $task_data['start_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_start_date_month', 1), array_var($_POST, 'task_start_date_day', 1), array_var($_POST, 'task_start_date_year', 1970));
  750. }
  751. if (isset($_POST['task_due_date'])) {
  752. $task_data['due_date'] = DateTimeValueLib::makeFromString($_POST['task_due_date']);
  753. } else {
  754. $task_data['due_date'] = DateTimeValueLib::make(0, 0, 0, array_var($_POST, 'task_due_date_month', 1), array_var($_POST, 'task_due_date_day', 1), array_var($_POST, 'task_due_date_year', 1970));
  755. }
  756. $task->setFromAttributes($task_data);
  757. $task->setTaskListId($task_list->getId()); // keep old task list id
  758. $assigned_to = explode(':', array_var($task_data, 'assigned_to', ''));
  759. $task->setAssignedToCompanyId(array_var($assigned_to, 0, 0));
  760. $task->setAssignedToUserId(array_var($assigned_to, 1, 0));
  761. try {
  762. DB::beginWork();
  763. $task->save();
  764. // Move?
  765. $new_task_list_id = (integer) array_var($task_data, 'task_list_id');
  766. if ($new_task_list_id && ($task->getTaskListId() <> $new_task_list_id)) {
  767. // Move!
  768. $new_task_list = ProjectTaskLists::findById($new_task_list_id);
  769. if ($new_task_list instanceof ProjectTaskList) {
  770. $task_list->detachTask($task, $new_task_list); // detach from old and attach to new list
  771. } // if
  772. } // if
  773. ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_EDIT);
  774. DB::commit();
  775. trace(__FILE__,'edit_task: notify user');
  776. // notify user
  777. if (array_var($task_data, 'send_notification') == 'checked') {
  778. try {
  779. if (Notifier::notifyNeeded($task->getAssignedTo(), $old_owner)) {
  780. Notifier::taskAssigned($task);
  781. }
  782. } catch(Exception $e) {
  783. Logger::log("Error: Notification failed, " . $e->getMessage(), Logger::ERROR);
  784. } // try
  785. } // if
  786. flash_success(lang('success edit task'));
  787. // Redirect to task list. Check if we have updated task list ID first
  788. if (isset($new_task_list) && ($new_task_list instanceof ProjectTaskList)) {
  789. $this->redirectToUrl($new_task_list->getViewUrl());
  790. } else {
  791. $this->redirectToUrl($task_list->getViewUrl());
  792. } // if
  793. } catch(Exception $e) {
  794. DB::rollback();
  795. tpl_assign('error', $e);
  796. } // try
  797. } // if
  798. } // edit_task
  799. /**
  800. * http://haris.tv htv edit
  801. * View task details page
  802. *
  803. * @access public
  804. * @param void
  805. * @return null
  806. */
  807. function view_task() {
  808. $this->setTemplate('view_task');
  809. $this->addHelper('textile');
  810. // taken from edit_task - htv
  811. $task = ProjectTasks::findById(get_id());
  812. if(!($task instanceof ProjectTask)) {
  813. flash_error(lang('task dnx'));
  814. $this->redirectTo('task');
  815. } // if
  816. $task_list = $task->getTaskList();
  817. if(!($task_list instanceof ProjectTaskList)) {
  818. flash_error('task list dnx');
  819. $this->redirectTo('task');
  820. } // if
  821. if(!$task->canView(logged_user())) {
  822. flash_error(lang('no access permissions'));
  823. $this->redirectTo('task');
  824. } // if
  825. $task_data = array_var($_POST, 'task');
  826. if(!is_array($task_data)) {
  827. $task_data = array(
  828. 'text' => $task->getText(),
  829. 'due_date' => $task->getDueDate(),
  830. 'task_list_id' => $task->getTaskListId(),
  831. 'assigned_to' => $task->getAssignedToCompanyId() . ':' . $task->getAssignedToUserId()
  832. ); // array
  833. } // if
  834. tpl_assign('task', $task);
  835. tpl_assign('task_list', $task_list);
  836. tpl_assign('task_data', $task_data);
  837. } // task_details
  838. /**
  839. * Delete specific task
  840. *
  841. * @access public
  842. * @param void
  843. * @return null
  844. */
  845. function delete_task() {
  846. $this->setTemplate('del_task');
  847. $task = ProjectTasks::findById(get_id());
  848. if (!($task instanceof ProjectTask)) {
  849. flash_error(lang('task dnx'));
  850. $this->redirectTo('task');
  851. } // if
  852. $task_list = $task->getTaskList();
  853. if (!($task_list instanceof ProjectTaskList)) {
  854. flash_error('task list dnx');
  855. $this->redirectTo('task');
  856. } // if
  857. if (!$task->canDelete(logged_user())) {
  858. flash_error(lang('no access permissions'));
  859. $this->redirectTo('task');
  860. } // if
  861. $delete_data = array_var($_POST, 'deleteTask');
  862. tpl_assign('task', $task);
  863. tpl_assign('task_list', $task_list);
  864. tpl_assign('delete_data', $delete_data);
  865. if (!is_array($delete_data)) {
  866. $delete_data = array(
  867. 'really' => 0,
  868. 'password' => '',
  869. ); // array
  870. tpl_assign('delete_data', $delete_data);
  871. } else if ($delete_data['really'] == 1) {
  872. $password = $delete_data['password'];
  873. if (trim($password) == '') {
  874. tpl_assign('error', new Error(lang('password value missing')));
  875. return $this->render();
  876. }
  877. if (!logged_user()->isValidPassword($password)) {
  878. tpl_assign('error', new Error(lang('invalid login data')));
  879. return $this->render();
  880. }
  881. try {
  882. DB::beginWork();
  883. $task->delete();
  884. ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_DELETE);
  885. DB::commit();
  886. flash_success(lang('success delete task'));
  887. } catch(Exception $e) {
  888. DB::rollback();
  889. flash_error(lang('error delete task'));
  890. } // try
  891. $this->redirectToUrl($task_list->getViewUrl());
  892. } else {
  893. flash_error(lang('error delete task'));
  894. $this->redirectToUrl($task_list->getViewUrl());
  895. }
  896. } // delete_task
  897. /**
  898. * Complete single project task
  899. *
  900. * @access public
  901. * @param void
  902. * @return null
  903. */
  904. function complete_task() {
  905. $task = ProjectTasks::findById(get_id());
  906. if (!($task instanceof ProjectTask)) {
  907. flash_error(lang('task dnx'));
  908. $this->redirectTo('task');
  909. } // if
  910. $task_list = $task->getTaskList();
  911. if (!($task_list instanceof ProjectTaskList)) {
  912. flash_error(lang('task list dnx'));
  913. $this->redirectTo('task');
  914. } // if
  915. if (!$task->canChangeStatus(logged_user())) {
  916. flash_error(lang('no access permissions'));
  917. $this->redirectTo('task');
  918. } // if
  919. $redirect_to = array_var($_GET, 'redirect_to');
  920. if (!is_valid_url($redirect_to)) {
  921. $redirect_to = get_referer($task_list->getViewUrl());
  922. } // if
  923. try {
  924. DB::beginWork();
  925. $task->completeTask();
  926. ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_CLOSE);
  927. DB::commit();
  928. flash_success(lang('success complete task'));
  929. } catch(Exception $e) {
  930. flash_error(lang('error complete task'));
  931. DB::rollback();
  932. } // try
  933. $this->redirectToUrl($redirect_to);
  934. } // complete_task
  935. /**
  936. * Reopen completed project task
  937. *
  938. * @access public
  939. * @param void
  940. * @return null
  941. */
  942. function open_task() {
  943. $task = ProjectTasks::findById(get_id());
  944. if (!($task instanceof ProjectTask)) {
  945. flash_error(lang('task dnx'));
  946. $this->redirectTo('task');
  947. } // if
  948. $task_list = $task->getTaskList();
  949. if (!($task_list instanceof ProjectTaskList)) {
  950. flash_error(lang('task list dnx'));
  951. $this->redirectTo('task');
  952. } // if
  953. if (!$task->canChangeStatus(logged_user())) {
  954. flash_error(lang('no access permissions'));
  955. $this->redirectTo('task');
  956. } // if
  957. $redirect_to = array_var($_GET, 'redirect_to');
  958. if ((trim($redirect_to) == '') || !is_valid_url($redirect_to)) {
  959. $redirect_to = get_referer($task_list->getViewUrl());
  960. } // if
  961. try {
  962. DB::beginWork();
  963. $task->openTask();
  964. ApplicationLogs::createLog($task, active_project(), ApplicationLogs::ACTION_OPEN);
  965. DB::commit();
  966. flash_success(lang('success open task'));
  967. } catch(Exception $e) {
  968. flash_error(lang('error open task'));
  969. DB::rollback();
  970. } // try
  971. $this->redirectToUrl($redirect_to);
  972. } // open_task
  973. /**
  974. * Reopen completed project task
  975. *
  976. * @access public
  977. * @param void
  978. * @return null
  979. */
  980. function edit_score() {
  981. $task = ProjectTasks::findById(get_id());
  982. if (!($task instanceof ProjectTask)) {
  983. flash_error(lang('task dnx'));
  984. //$this->redirectTo('task');
  985. } // if
  986. include '../views/editscore.html';
  987. } // open_task
  988. } // TaskController
  989. ?>