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_real') { 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_real') { 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 update_last_activity() { 316 $this->last_activity_date = date('c'); 317 $this->model->sqlite->query('UPDATE task SET last_activity_date=? WHERE id=?', 318 $this->last_activity_date, $this->id); 319 } 320 321 public function can_add_comments() { 322 if ($this->thread_id != '' && $this->thread->state == 'closed') { 323 return false; 324 } 325 326 if ($this->state == 'opened' || 327 ($this->state == 'done' && 328 $this->acl_of('state') >= BEZ_PERMISSION_CHANGE)) { 329 return true; 330 } 331 332 return false; 333 } 334 335 public function can_add_participants() { 336 return in_array($this->state, array('opened')); 337 } 338 339 public function get_participants($filter='') { 340 if ($this->id === NULL) { 341 return array(); 342 } 343 344 $sql = 'SELECT * FROM task_participant WHERE'; 345 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 346 if ($filter != '') { 347 if (!in_array($filter, $possible_flags)) { 348 throw new \Exception("unknown flag $filter"); 349 } 350 $sql .= " $filter=1 AND"; 351 } 352 $sql .= ' task_id=? AND removed=0 ORDER BY user_id'; 353 354 $r = $this->model->sqlite->query($sql, $this->id); 355 $pars = $this->model->sqlite->res2arr($r); 356 $participants = array(); 357 foreach ($pars as $par) { 358 $participants[$par['user_id']] = $par; 359 } 360 361 return $participants; 362 } 363 364 public function get_participant($user_id, $can_be_removed=false) { 365 if ($this->id === NULL) { 366 return array(); 367 } 368 369 $q = 'SELECT * FROM task_participant WHERE task_id=? AND user_id=?'; 370 if (!$can_be_removed) { 371 $q .= ' AND removed=0'; 372 } 373 $r = $this->model->sqlite->query($q, $this->id, $user_id); 374 $par = $this->model->sqlite->res2row($r); 375 if (!is_array($par)) { 376 return false; 377 } 378 379 return $par; 380 } 381 382 public function is_subscribent($user_id=null) { 383 if ($user_id == null) { 384 $user_id = $this->model->user_nick; 385 } 386 $par = $this->get_participant($user_id); 387 if ($par['subscribent'] == 1) { 388 return true; 389 } 390 return false; 391 } 392 393 public function remove_participant_flags($user_id, $flags) { 394 //thread not saved yet 395 if ($this->id === NULL) { 396 throw new \Exception('cannot remove flags from not saved thread'); 397 } 398 399 $participant = $this->get_participant($user_id, true); 400 if ($participant === false) { 401 throw new ConsistencyViolationException("$user_id isn't participant"); 402 } 403 404 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 405 if (array_intersect($flags, $possible_flags) != $flags) { 406 throw new \Exception('unknown flags'); 407 } 408 409 $set = implode(',', array_map(function ($v) { return "$v=0"; }, $flags)); 410 411 $sql = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?"; 412 $this->model->sqlite->query($sql, $this->id, $user_id); 413 414 } 415 416 public function set_participant_flags($user_id, $flags=array()) { 417 //thread not saved yet 418 if ($this->id === NULL) { 419 throw new \Exception('cannot add flags to not saved thread'); 420 } 421 422 //validate user 423 if (!$this->model->userFactory->exists($user_id)) { 424 throw new \Exception("$user_id isn't dokuwiki user"); 425 } 426 427 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 428 if (array_intersect($flags, $possible_flags) != $flags) { 429 throw new \Exception('unknown flags'); 430 } 431 432 $participant = $this->get_participant($user_id, true); 433 if ($participant == false) { 434 $participant = array_fill_keys($possible_flags, 0); 435 436 $participant['task_id'] = $this->id; 437 $participant['user_id'] = $user_id; 438 $participant['added_by'] = $this->model->user_nick; 439 $participant['added_date'] = date('c'); 440 441 $values = array_merge($participant, array_fill_keys($flags, 1)); 442 $this->model->sqlite->storeEntry('task_participant', $values); 443 } else { 444 $set = implode(',', array_map(function($flag) { return "$flag=1"; }, $flags)); 445 446 if ($participant['removed'] == '1') { 447 $set .= ',removed=0'; 448 } 449 450 $q = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?"; 451 $this->model->sqlite->query($q, $this->id, $user_id); 452 } 453 } 454 455 public function remove_participant($user_id) { 456 //thread not saved yet 457 if ($this->id === NULL) { 458 throw new \Exception('cannot remove flags from not saved thread'); 459 } 460 461 $participant = $this->get_participant($user_id); 462 if ($participant === false) { 463 throw new ConsistencyViolationException("$user_id isn't participant"); 464 } 465 466 if ($participant['assignee'] == '1') { 467 throw new ConsistencyViolationException("cannot remove assignee"); 468 } 469 470 $q = "UPDATE task_participant SET removed=1 WHERE task_id=? AND user_id=?"; 471 $this->model->sqlite->query($q, $this->id, $user_id); 472 473 } 474 475 public function pin($thread_id) { 476 if ($this->acl_of('thread_id') < BEZ_PERMISSION_CHANGE) { 477 throw new PermissionDeniedException(); 478 } 479 if ($this->thread_id != '') { 480 throw new ConsistencyViolationException('task already pinned to thread'); 481 } 482 483 //check if thread exists and isn't closed 484 $q = "SELECT id FROM thread_view WHERE id = ? AND state IN ('opened', 'done')"; 485 $r = $this->model->sqlite->query($q, $thread_id); 486 if ($this->model->sqlite->res2count($r) == 0) { 487 throw new ValidationException("task", array('thread_id' => 'pin_task')); 488 } 489 $q = "UPDATE task SET type='correction', thread_id=? WHERE id=?"; 490 $this->model->sqlite->query($q, $thread_id, $this->id); 491 } 492 493 public function unpin() { 494 if ($this->acl_of('thread_id') < BEZ_PERMISSION_CHANGE) { 495 throw new PermissionDeniedException(); 496 } 497 498 $q = "UPDATE task SET type='program', thread_id='' WHERE id=?"; 499 $this->model->sqlite->query($q, $this->id); 500 } 501 502 public function invite($client) { 503 $this->set_participant_flags($client, array('subscribent')); 504 $this->mail_notify_invite($client); 505 } 506 507 protected function html_link_url() { 508 $tpl = $this->model->action->get_tpl(); 509 return $tpl->url('task', 'tid', $this->id); 510 } 511 512 protected function html_link_content() { 513 $ret = ''; 514 if ($this->thread_id != '') { 515 $ret .= '#'.$this->thread_id . ' '; 516 } 517 return $ret . '#z' . $this->id; 518 } 519 520 protected function getMailSubject() 521 { 522 return parent::getMailSubject() . '#z'.$this->id. ' ' . $this->task_program_name; // TODO: Change the autogenerated stub 523 } 524 525 public function mail_task_box(&$attachedImages) { 526 $tpl = $this->model->action->get_tpl(); 527 528 //render style 529 $less = new \lessc(); 530 $less->addImportDir(DOKU_PLUGIN . 'bez/style/'); 531 $style = $less->compileFile(DOKU_PLUGIN . 'bez/style/task.less'); 532 533 //render content for mail 534 $old_content_html = $this->content_html; 535 $this->content_html = p_render('bez_xhtmlmail', p_get_instructions($this->content), $info); 536 $attachedImages = array_merge($attachedImages, $info['img']); 537 538 $tpl->set('task', $this); 539 $tpl->set('style', $style); 540 $tpl->set('no_actions', true); 541 $task_box = $this->model->action->bez_tpl_include('task_box', true); 542 543 $this->content_html = $old_content_html; 544 545 return $task_box; 546 } 547 548 public function mail_task(&$attachedImages) { 549 $tpl = $this->model->action->get_tpl(); 550 551 $task_box = $this->mail_task_box($attachedImages); 552 $tpl->set('content', $task_box); 553 $content = $this->model->action->bez_tpl_include('mail/task', true); 554 555 return $content; 556 } 557 558 public function mail_notify_assignee() { 559 $tpl = $this->model->action->get_tpl(); 560 561 //we don't want who 562 $tpl->set('who', $this->model->user_nick); 563 $tpl->set('action', 'mail_task_assignee'); 564 $attachedImages = array(); 565 $content = $this->mail_task($attachedImages); 566 $this->mail_notify($content, array($this->assignee), $attachedImages); 567 } 568 569 public function mail_notify_remind($users=false, $days=1) { 570 $tpl = $this->model->action->get_tpl(); 571 572 //we don't want who 573 $tpl->set('who', ''); 574 $tpl->set('action', 'mail_task_remind'); 575 $tpl->set('action_replacements', array($days)); 576 $attachedImages = array(); 577 $content = $this->mail_task($attachedImages); 578 $this->mail_notify($content, $users, $attachedImages); 579 } 580 581 public function mail_notify_invite($client) { 582 $users = array($client); 583 $tpl = $this->model->action->get_tpl(); 584 585 //we don't want who 586 $tpl->set('who', $this->model->user_nick); 587 $tpl->set('action', 'mail_task_invite'); 588 $attachedImages = array(); 589 $content = $this->mail_task($attachedImages); 590 $this->mail_notify($content, $users, $attachedImages); 591 } 592 593 public function mail_notify_change_state($action='') { 594 $tpl = $this->model->action->get_tpl(); 595 596 $tpl->set('who', $this->model->user_nick); 597 $tpl->set('action', $action); 598 $attachedImages = array(); 599 $content = $this->mail_task($attachedImages); 600 $this->mail_notify($content, false, $attachedImages); 601 } 602 603} 604