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