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