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