1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5use dokuwiki\plugin\bez\meta\ConsistencyViolationException; 6use dokuwiki\plugin\bez\meta\Mailer; 7use dokuwiki\plugin\bez\meta\PermissionDeniedException; 8use dokuwiki\plugin\bez\meta\ValidationException; 9use dokuwiki\plugin\struct\types\DateTime; 10 11class Task extends Entity { 12 13 protected $id; 14 15 protected $original_poster, $assignee, $closed_by; 16 17 protected $private, $lock; 18 19 protected $state, $type; 20 21 protected $create_date, $last_activity_date, $last_modification_date, $close_date; 22 23 protected $cost, $plan_date, $all_day_event, $start_time, $finish_time; 24 25 protected $content, $content_html; 26 27 protected $thread_id, $thread_comment_id, $task_program_id; 28 29 /** @var \dokuwiki\plugin\bez\mdl\Thread */ 30 protected $thread; 31 32 /** @var Thread_comment */ 33 protected $thread_comment; 34 35 //virtual 36 protected $task_program_name, $priority, $coordinator; 37 38 public static function get_columns() { 39 return array('id', 40 'original_poster', 'assignee', 'closed_by', 41 'private', 'lock', 42 'state', 'type', 43 'create_date', 'last_activity_date', 'last_modification_date', 'close_date', 44 'cost', 'plan_date', 'all_day_event', 'start_time', 'finish_time', 45 'content', 'content_html', 46 'thread_id', 'thread_comment_id', 'task_program_id'); 47 } 48 49 public static function get_acl_columns() { 50 return array_merge(parent::get_acl_columns(), array('participants')); 51 } 52 53 public static function get_types() { 54 return array('correction', 'corrective', 'preventive', 'program'); 55 } 56 57 public static function get_states() { 58 return array('opened', 'done'); 59 } 60 61 public function __get($property) { 62 if ($property == 'thread') { 63 if ($this->thread_id == null) { 64 return null; 65 } 66 if ($this->thread == null || $this->thread_id != $this->thread->id) { 67 $this->thread = $this->model->threadFactory->get_one($this->thread_id); 68 } 69 return $this->thread; 70 71 } elseif($property == 'thread_comment') { 72 if ($this->thread_comment_id == null) { 73 return null; 74 } 75 if ($this->thread_comment == null || $this->thread_comment_id != $this->thread_comment->id) { 76 $this->thread_comment = $this->model->thread_commentFactory->get_one($this->thread_comment_id); 77 } 78 return $this->thread_comment; 79 80 } elseif($property == 'priority' || $property == 'coordinator' || $property == 'task_program_name') { 81 return $this->$property; 82 } 83 return parent::__get($property); 84 } 85 86 public function __construct($model, $defaults=array()) { 87 parent::__construct($model, $defaults); 88 89 $this->validator->set_rules(array( 90 'assignee' => array(array('dw_user'), 'NOT NULL'), 91 'cost' => array(array('numeric'), 'NULL'), 92 'plan_date' => array(array('iso_date'), 'NOT NULL'), 93 'all_day_event' => array(array('select', array('0', '1')), 'NOT NULL'), 94 'start_time' => array(array('time'), 'NULL'), 95 'finish_time' => array(array('time'), 'NULL'), 96 'content' => array(array('length', 10000), 'NOT NULL'), 97 'thread_comment_id' => array(array('numeric'), 'NULL'), 98 'task_program_id' => array(array('numeric'), 'NULL') 99 )); 100 101 //we've created empty object 102 if ($this->id === NULL) { 103 $this->original_poster = $this->model->user_nick; 104 $this->create_date = date('c'); 105 $this->last_activity_date = $this->create_date; 106 $this->last_modification_date = $this->create_date; 107 108 $this->state = 'opened'; 109 110 if (isset($defaults['thread'])) { 111 $this->thread = $defaults['thread']; 112 $this->thread_id = $this->thread->id; 113 $this->coordinator = $this->thread->coordinator; 114 115 if ($this->thread->private == '1') { 116 $this->private = '1'; 117 } 118 119 $this->type = 'correction'; 120 121 if (isset($defaults['thread_comment'])) { 122 $this->thread_comment = $defaults['thread_comment']; 123 $this->thread_comment_id = $this->thread_comment->id; 124 125 if ($this->thread_comment->type == 'cause') { 126 $this->type = 'corrective'; 127 } else { 128 $this->type = 'preventive'; 129 } 130 } 131 } else { 132 $this->type = 'program'; 133 } 134 135 136 if ($this->thread_id == '') { 137 $this->validator->set_rules(array( 138 'task_program_id' => array(array('numeric'), 'NOT NULL'), 139 )); 140 //this field is unused in program tasks 141 $this->validator->delete_rule('thread_comment_id'); 142 } 143 144 //everyone can report their own program tasks 145 if ($this->type == 'program') { 146 $this->acl->grant('content', BEZ_PERMISSION_CHANGE); 147 $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE); 148 $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE); 149 $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE); 150 $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE); 151 $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE); 152 $this->acl->grant('cost', BEZ_PERMISSION_CHANGE); 153 } 154 155 if ($this->type == 'program' && $this->model->get_level() >= BEZ_AUTH_LEADER) { 156 $this->acl->grant('assignee', BEZ_PERMISSION_CHANGE); 157 $this->acl->grant('participants', BEZ_PERMISSION_CHANGE); 158 } 159 160 if ($this->type != 'program' && $this->coordinator == $this->model->user_nick) { 161 $this->acl->grant('content', BEZ_PERMISSION_CHANGE); 162 $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE); 163 $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE); 164 $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE); 165 $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE); 166 $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE); 167 $this->acl->grant('cost', BEZ_PERMISSION_CHANGE); 168 169 $this->acl->grant('assignee', BEZ_PERMISSION_CHANGE); 170 $this->acl->grant('participants', BEZ_PERMISSION_CHANGE); 171 } 172 173 //we get object form db 174 } else { 175 if (isset($defaults['thread']) && $this->thread_id == $defaults['thread']->id) { 176 $this->thread = $defaults['thread']; 177 } 178 179 if (isset($defaults['thread_comment']) && $this->thread_comment_id == $defaults['thread_comment']->id) { 180 $this->thread_comment = $defaults['thread_comment']; 181 } 182 183 if ($this->thread_id == '') { 184 $this->validator->set_rules(array( 185 'task_program_id' => array(array('numeric'), 'NOT NULL'), 186 )); 187 //this field is unused in program tasks 188 $this->validator->delete_rule('thread_comment_id'); 189 } 190 191 //private tasks 192 if ($this->model->level < BEZ_AUTH_ADMIN && $this->private == '1') { 193 if ($this->get_participant($this->model->user_nick) === false && 194 ($this->thread_id != '' && $this->__get('thread')->get_participant($this->model->user_nick) === false)) { 195 $this->acl->revoke(self::get_select_columns(), BEZ_AUTH_LEADER); 196 return; 197 } 198 } 199 200 //user can close their tasks 201 if ($this->assignee == $this->model->user_nick || $this->model->get_level() >= BEZ_AUTH_LEADER) { 202 $this->acl->grant('state', BEZ_PERMISSION_CHANGE); 203 } 204 205 if ($this->type == 'program' && $this->original_poster == $this->model->user_nick) { 206 $this->acl->grant('content', BEZ_PERMISSION_CHANGE); 207 $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE); 208 $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE); 209 $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE); 210 $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE); 211 $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE); 212 $this->acl->grant('cost', BEZ_PERMISSION_CHANGE); 213 } 214 215 if (($this->type != 'program' && $this->coordinator == $this->model->user_nick) || 216 ($this->model->get_level() >= BEZ_AUTH_LEADER)) { 217 $this->acl->grant('content', BEZ_PERMISSION_CHANGE); 218 $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE); 219 $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE); 220 $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE); 221 $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE); 222 $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE); 223 $this->acl->grant('cost', BEZ_PERMISSION_CHANGE); 224 225 $this->acl->grant('assignee', BEZ_PERMISSION_CHANGE); 226 $this->acl->grant('participants', BEZ_PERMISSION_CHANGE); 227 $this->acl->grant('state', BEZ_PERMISSION_CHANGE); 228 } 229 } 230 } 231 232 public function update_virutal() { 233 if ($this->state == 'done') { 234 $this->priority = ''; 235 } else { 236 $now = date('Y-m-d'); 237 $plus_1_month = date('Y-m-d', strtotime('+1 month')); 238 239 if ($this->plan_date >= $plus_1_month) { 240 $this->priority = '0'; 241 } elseif ($this->plan_date >= $now) { 242 $this->priority = '1'; 243 } else { 244 $this->priority = '2'; 245 } 246 } 247 } 248 249 public function set_data($post, $filter=NULL) { 250 //all day event 251 if (!isset($post['all_day_event'])) { 252 $post['all_day_event'] = '0'; 253 } 254 255 parent::set_data($post); 256 257 $this->purge(); 258 259 if ($this->thread_id == '') { 260 $this->type = 'program'; 261 } elseif ($this->thread_comment_id == '') { 262 $this->type = 'correction'; 263 } elseif ($this->__get('thread_comment')->type == 'cause') { 264 $this->type = 'corrective'; 265 } else { 266 $this->type = 'preventive'; 267 } 268 269 if (!isset($post['assignee'])) { 270 $this->assignee = $this->model->user_nick; 271 } 272 273 //update dates 274 $this->last_modification_date = date('c'); 275 $this->last_activity_date = $this->last_modification_date; 276 277 //update virtual 278 $this->update_virutal(); 279 280 return true; 281 } 282 283 public function set_state($state) { 284 if ($this->acl_of('state') < BEZ_PERMISSION_CHANGE) { 285 throw new PermissionDeniedException(); 286 } 287 288 if (!in_array($state, array('opened', 'done'))) { 289 throw new ValidationException('task', array('sholud be opened or done')); 290 } 291 292 //nothing to do 293 if ($state == $this->state) { 294 return; 295 } 296 297 if ($state == 'done') { 298 $this->state = $state; 299 $this->closed_by = $this->model->user_nick; 300 $this->close_date = date('c'); 301 302 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=?, closed_by=?, close_date=? WHERE id=?", 303 $this->state, 304 $this->closed_by, 305 $this->close_date, 306 $this->id); 307 //reopen the task 308 } else { 309 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=? WHERE id=?", $state, $this->id); 310 } 311 312 $this->state = $state; 313 } 314 315 public function set_task_program($taks_program_id) { 316 $this->task_program_id = $taks_program_id; 317 } 318 319 public function update_last_activity() { 320 $this->last_activity_date = date('c'); 321 $this->model->sqlite->query('UPDATE task SET last_activity_date=? WHERE id=?', 322 $this->last_activity_date, $this->id); 323 } 324 325 public function can_add_comments() { 326 if ($this->thread_id != '' && $this->thread->state == 'closed') { 327 return false; 328 } 329 330 if ($this->state == 'opened' || 331 ($this->state == 'done' && 332 $this->acl_of('state') >= BEZ_PERMISSION_CHANGE)) { 333 return true; 334 } 335 336 return false; 337 } 338 339 public function can_add_participants() { 340 return in_array($this->state, array('opened')); 341 } 342 343 public function get_participants($filter='') { 344 if ($this->id === NULL) { 345 return array(); 346 } 347 348 $sql = 'SELECT * FROM task_participant WHERE'; 349 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 350 if ($filter != '') { 351 if (!in_array($filter, $possible_flags)) { 352 throw new \Exception("unknown flag $filter"); 353 } 354 $sql .= " $filter=1 AND"; 355 } 356 $sql .= ' task_id=? AND removed=0 ORDER BY user_id'; 357 358 $r = $this->model->sqlite->query($sql, $this->id); 359 $pars = $this->model->sqlite->res2arr($r); 360 $participants = array(); 361 foreach ($pars as $par) { 362 $participants[$par['user_id']] = $par; 363 } 364 365 return $participants; 366 } 367 368 public function get_participant($user_id, $can_be_removed=false) { 369 if ($this->id === NULL) { 370 return array(); 371 } 372 373 $q = 'SELECT * FROM task_participant WHERE task_id=? AND user_id=?'; 374 if (!$can_be_removed) { 375 $q .= ' AND removed=0'; 376 } 377 $r = $this->model->sqlite->query($q, $this->id, $user_id); 378 $par = $this->model->sqlite->res2row($r); 379 if (!is_array($par)) { 380 return false; 381 } 382 383 return $par; 384 } 385 386 public function is_subscribent($user_id=null) { 387 if ($user_id == null) { 388 $user_id = $this->model->user_nick; 389 } 390 $par = $this->get_participant($user_id); 391 if ($par['subscribent'] == 1) { 392 return true; 393 } 394 return false; 395 } 396 397 public function remove_participant_flags($user_id, $flags) { 398 //thread not saved yet 399 if ($this->id === NULL) { 400 throw new \Exception('cannot remove flags from not saved thread'); 401 } 402 403 $participant = $this->get_participant($user_id, true); 404 if ($participant === false) { 405 throw new ConsistencyViolationException("$user_id isn't participant"); 406 } 407 408 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 409 if (array_intersect($flags, $possible_flags) != $flags) { 410 throw new \Exception('unknown flags'); 411 } 412 413 $set = implode(',', array_map(function ($v) { return "$v=0"; }, $flags)); 414 415 $sql = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?"; 416 $this->model->sqlite->query($sql, $this->id, $user_id); 417 418 } 419 420 public function set_participant_flags($user_id, $flags=array()) { 421 //thread not saved yet 422 if ($this->id === NULL) { 423 throw new \Exception('cannot add flags to not saved thread'); 424 } 425 426 //validate user 427 if (!$this->model->userFactory->exists($user_id)) { 428 throw new \Exception("$user_id isn't dokuwiki user"); 429 } 430 431 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 432 if (array_intersect($flags, $possible_flags) != $flags) { 433 throw new \Exception('unknown flags'); 434 } 435 436 $participant = $this->get_participant($user_id, true); 437 if ($participant == false) { 438 $participant = array_fill_keys($possible_flags, 0); 439 440 $participant['task_id'] = $this->id; 441 $participant['user_id'] = $user_id; 442 $participant['added_by'] = $this->model->user_nick; 443 $participant['added_date'] = date('c'); 444 445 $values = array_merge($participant, array_fill_keys($flags, 1)); 446 $this->model->sqlite->storeEntry('task_participant', $values); 447 } else { 448 $set = implode(',', array_map(function($flag) { return "$flag=1"; }, $flags)); 449 450 if ($participant['removed'] == '1') { 451 $set .= ',removed=0'; 452 } 453 454 $q = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?"; 455 $this->model->sqlite->query($q, $this->id, $user_id); 456 } 457 } 458 459 public function remove_participant($user_id) { 460 //thread not saved yet 461 if ($this->id === NULL) { 462 throw new \Exception('cannot remove flags from not saved thread'); 463 } 464 465 $participant = $this->get_participant($user_id); 466 if ($participant === false) { 467 throw new ConsistencyViolationException("$user_id isn't participant"); 468 } 469 470 if ($participant['assignee'] == '1') { 471 throw new ConsistencyViolationException("cannot remove assignee"); 472 } 473 474 $q = "UPDATE task_participant SET removed=1 WHERE task_id=? AND user_id=?"; 475 $this->model->sqlite->query($q, $this->id, $user_id); 476 477 } 478 479 public function pin($thread_id) { 480 if ($this->acl_of('thread_id') < BEZ_PERMISSION_CHANGE) { 481 throw new PermissionDeniedException(); 482 } 483 if ($this->thread_id != '') { 484 throw new ConsistencyViolationException('task already pinned to thread'); 485 } 486 487 //check if thread exists and isn't closed 488 $q = "SELECT id, private FROM thread_view WHERE id = ? AND state IN ('opened', 'done')"; 489 $r = $this->model->sqlite->query($q, $thread_id); 490 $thread = $this->model->sqlite->res_fetch_assoc($r); 491 if (!$thread) { 492 throw new ValidationException("task", array('thread_id' => 'pin_task')); 493 } 494 495 //if thread was private task is also private 496 $q = "UPDATE task SET type='correction', thread_id=?"; 497 if ($thread['private']) { 498 $q .= ", private=1"; 499 } 500 $q .= " WHERE id=?"; 501 $this->model->sqlite->query($q, $thread_id, $this->id); 502 } 503 504 public function unpin() { 505 if ($this->acl_of('thread_id') < BEZ_PERMISSION_CHANGE) { 506 throw new PermissionDeniedException(); 507 } 508 509 $q = "UPDATE task SET type='program', thread_id='', private=0 WHERE id=?"; 510 $this->model->sqlite->query($q, $this->id); 511 } 512 513 public function invite($client) { 514 $this->set_participant_flags($client, array('subscribent')); 515 $this->mail_notify_invite($client); 516 } 517 518 protected function html_link_url() { 519 $tpl = $this->model->action->get_tpl(); 520 return $tpl->url('task', 'tid', $this->id); 521 } 522 523 protected function html_link_content() { 524 $ret = ''; 525 if ($this->thread_id != '') { 526 $ret .= '#'.$this->thread_id . ' '; 527 } 528 return $ret . '#z' . $this->id; 529 } 530 531 protected function getMailSubject() 532 { 533 return parent::getMailSubject() . ' #z'.$this->id. ' ' . $this->task_program_name; // TODO: Change the autogenerated stub 534 } 535 536 public function mail_task_box(&$attachedImages) { 537 $tpl = $this->model->action->get_tpl(); 538 539 //render style 540 $less = new \lessc(); 541 $less->addImportDir(DOKU_PLUGIN . 'bez/style/'); 542 $style = $less->compileFile(DOKU_PLUGIN . 'bez/style/task.less'); 543 544 //render content for mail 545 $old_content_html = $this->content_html; 546 $this->content_html = p_render('bez_xhtmlmail', p_get_instructions($this->content), $info); 547 $attachedImages = array_merge($attachedImages, $info['img']); 548 549 $tpl->set('task', $this); 550 $tpl->set('style', $style); 551 $tpl->set('no_actions', true); 552 $task_box = $this->model->action->bez_tpl_include('task_box', true); 553 554 $this->content_html = $old_content_html; 555 556 return $task_box; 557 } 558 559 public function mail_task(&$attachedImages) { 560 $tpl = $this->model->action->get_tpl(); 561 562 $task_box = $this->mail_task_box($attachedImages); 563 $tpl->set('content', $task_box); 564 $content = $this->model->action->bez_tpl_include('mail/task', true); 565 566 return $content; 567 } 568 569 public function mail_notify_assignee() { 570 $tpl = $this->model->action->get_tpl(); 571 572 //we don't want who 573 $tpl->set('who', $this->model->user_nick); 574 $tpl->set('action', 'mail_task_assignee'); 575 $attachedImages = array(); 576 $content = $this->mail_task($attachedImages); 577 $this->mail_notify($content, array($this->assignee), $attachedImages); 578 } 579 580 public function mail_notify_remind($users=false, $days=1) { 581 $tpl = $this->model->action->get_tpl(); 582 583 //we don't want who 584 $tpl->set('who', ''); 585 $tpl->set('action', 'mail_task_remind'); 586 $tpl->set('action_replacements', array($days)); 587 $attachedImages = array(); 588 $content = $this->mail_task($attachedImages); 589 $this->mail_notify($content, $users, $attachedImages); 590 } 591 592 public function mail_notify_invite($client) { 593 $users = array($client); 594 $tpl = $this->model->action->get_tpl(); 595 596 //we don't want who 597 $tpl->set('who', $this->model->user_nick); 598 $tpl->set('action', 'mail_task_invite'); 599 $attachedImages = array(); 600 $content = $this->mail_task($attachedImages); 601 $this->mail_notify($content, $users, $attachedImages); 602 } 603 604 public function mail_notify_change_state($action='') { 605 $tpl = $this->model->action->get_tpl(); 606 607 $tpl->set('who', $this->model->user_nick); 608 $tpl->set('action', $action); 609 $attachedImages = array(); 610 $content = $this->mail_task($attachedImages); 611 $this->mail_notify($content, false, $attachedImages); 612 } 613 614} 615