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) { 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) { 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->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore); 258 259 if (!isset($post['assignee'])) { 260 $this->assignee = $this->model->user_nick; 261 } 262 263 //update dates 264 $this->last_modification_date = date('c'); 265 $this->last_activity_date = $this->last_modification_date; 266 267 //update virtual 268 $this->update_virutal(); 269 270 return true; 271 } 272 273 public function set_state($state) { 274 if ($this->acl_of('state') < BEZ_PERMISSION_CHANGE) { 275 throw new PermissionDeniedException(); 276 } 277 278 if (!in_array($state, array('opened', 'done'))) { 279 throw new ValidationException('task', array('sholud be opened or done')); 280 } 281 282 //nothing to do 283 if ($state == $this->state) { 284 return; 285 } 286 287 if ($state == 'done') { 288 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=?, closed_by=?, close_date=? WHERE id=?", 289 $state, 290 $this->model->user_nick, 291 date('c'), 292 $this->id); 293 //reopen the task 294 } else { 295 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=? WHERE id=?", $state, $this->id); 296 } 297 298 $this->state = $state; 299 } 300 301 public function update_last_activity() { 302 $this->last_activity_date = date('c'); 303 $this->model->sqlite->query('UPDATE task SET last_activity_date=? WHERE id=?', 304 $this->last_activity_date, $this->id); 305 } 306 307 public function can_add_comments() { 308 if ($this->thread_id != '' && $this->thread->state == 'closed') { 309 return false; 310 } 311 312 if ($this->state == 'opened' || 313 ($this->state == 'done' && 314 $this->acl_of('state') >= BEZ_PERMISSION_CHANGE)) { 315 return true; 316 } 317 318 return false; 319 } 320 321 public function can_add_participants() { 322 return in_array($this->state, array('opened')); 323 } 324 325 public function get_participants($filter='') { 326 if ($this->id === NULL) { 327 return array(); 328 } 329 330 $sql = 'SELECT * FROM task_participant WHERE'; 331 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 332 if ($filter != '') { 333 if (!in_array($filter, $possible_flags)) { 334 throw new \Exception("unknown flag $filter"); 335 } 336 $sql .= " $filter=1 AND"; 337 } 338 $sql .= ' task_id=? AND removed=0 ORDER BY user_id'; 339 340 $r = $this->model->sqlite->query($sql, $this->id); 341 $pars = $this->model->sqlite->res2arr($r); 342 $participants = array(); 343 foreach ($pars as $par) { 344 $participants[$par['user_id']] = $par; 345 } 346 347 return $participants; 348 } 349 350 public function get_participant($user_id, $can_be_removed=false) { 351 if ($this->id === NULL) { 352 return array(); 353 } 354 355 $q = 'SELECT * FROM task_participant WHERE task_id=? AND user_id=?'; 356 if (!$can_be_removed) { 357 $q .= ' AND removed=0'; 358 } 359 $r = $this->model->sqlite->query($q, $this->id, $user_id); 360 $par = $this->model->sqlite->res2row($r); 361 if (!is_array($par)) { 362 return false; 363 } 364 365 return $par; 366 } 367 368 public function is_subscribent($user_id=null) { 369 if ($user_id == null) { 370 $user_id = $this->model->user_nick; 371 } 372 $par = $this->get_participant($user_id); 373 if ($par['subscribent'] == 1) { 374 return true; 375 } 376 return false; 377 } 378 379 public function remove_participant_flags($user_id, $flags) { 380 //thread not saved yet 381 if ($this->id === NULL) { 382 throw new \Exception('cannot remove flags from not saved thread'); 383 } 384 385 $participant = $this->get_participant($user_id, true); 386 if ($participant === false) { 387 throw new ConsistencyViolationException("$user_id isn't participant"); 388 } 389 390 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 391 if (array_intersect($flags, $possible_flags) != $flags) { 392 throw new \Exception('unknown flags'); 393 } 394 395 $set = implode(',', array_map(function ($v) { return "$v=0"; }, $flags)); 396 397 $sql = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?"; 398 $this->model->sqlite->query($sql, $this->id, $user_id); 399 400 } 401 402 public function set_participant_flags($user_id, $flags=array()) { 403 //thread not saved yet 404 if ($this->id === NULL) { 405 throw new \Exception('cannot add flags to not saved thread'); 406 } 407 408 //validate user 409 if (!$this->model->userFactory->exists($user_id)) { 410 throw new \Exception("$user_id isn't dokuwiki user"); 411 } 412 413 $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent'); 414 if (array_intersect($flags, $possible_flags) != $flags) { 415 throw new \Exception('unknown flags'); 416 } 417 418 $participant = $this->get_participant($user_id, true); 419 if ($participant == false) { 420 $participant = array_fill_keys($possible_flags, 0); 421 422 $participant['task_id'] = $this->id; 423 $participant['user_id'] = $user_id; 424 $participant['added_by'] = $this->model->user_nick; 425 $participant['added_date'] = date('c'); 426 427 $values = array_merge($participant, array_fill_keys($flags, 1)); 428 $this->model->sqlite->storeEntry('task_participant', $values); 429 } else { 430 $set = implode(',', array_map(function($flag) { return "$flag=1"; }, $flags)); 431 432 if ($participant['removed'] == '1') { 433 $set .= ',removed=0'; 434 } 435 436 $q = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?"; 437 $this->model->sqlite->query($q, $this->id, $user_id); 438 } 439 } 440 441 public function remove_participant($user_id) { 442 //thread not saved yet 443 if ($this->id === NULL) { 444 throw new \Exception('cannot remove flags from not saved thread'); 445 } 446 447 $participant = $this->get_participant($user_id); 448 if ($participant === false) { 449 throw new ConsistencyViolationException("$user_id isn't participant"); 450 } 451 452 if ($participant['assignee'] == '1') { 453 throw new ConsistencyViolationException("cannot remove assignee"); 454 } 455 456 $q = "UPDATE task_participant SET removed=1 WHERE task_id=? AND user_id=?"; 457 $this->model->sqlite->query($q, $this->id, $user_id); 458 459 } 460 461 public function pin($thread_id) { 462 if ($this->acl_of('thread_id') < BEZ_PERMISSION_CHANGE) { 463 throw new PermissionDeniedException(); 464 } 465 if ($this->thread_id != '') { 466 throw new ConsistencyViolationException('task already pinned to thread'); 467 } 468 469 //check if thread exists and isn't closed 470 $q = "SELECT id FROM thread_view WHERE id = ? AND state IN ('opened', 'done')"; 471 $r = $this->model->sqlite->query($q, $thread_id); 472 if ($this->model->sqlite->res2count($r) == 0) { 473 throw new ValidationException("task", array('thread_id' => 'pin_task')); 474 } 475 $q = "UPDATE task SET type='correction', thread_id=? WHERE id=?"; 476 $this->model->sqlite->query($q, $thread_id, $this->id); 477 } 478 479 public function unpin() { 480 if ($this->acl_of('thread_id') < BEZ_PERMISSION_CHANGE) { 481 throw new PermissionDeniedException(); 482 } 483 484 $q = "UPDATE task SET type='program', thread_id='' WHERE id=?"; 485 $this->model->sqlite->query($q, $this->id); 486 } 487 488 public function invite($client) { 489 $this->set_participant_flags($client, array('subscribent')); 490 $this->mail_notify_invite($client); 491 } 492 493 public function mail_notify($content, $users=false, $attachedImages=array()) { 494 $mailer = new \Mailer(); 495 $mailer->setBody($content, array(), array(), $content, false); 496 497 if ($users === FALSE) { 498 $users = $this->get_participants('subscribent'); 499 500 //don't notify current user 501 unset($users[$this->model->user_nick]); 502 } 503 504 $emails = array_map(function($user) { 505 if (is_array($user)) { 506 $user = $user['user_id']; 507 } 508 return $this->model->userFactory->get_user_email($user); 509 }, $users); 510 511 $mailer->to($emails); 512 $mailer->subject('#z'.$this->id. ' ' . $this->task_program_name); 513 514 //add images 515 foreach ($attachedImages as $img) { 516 $mailer->attachFile($img['path'], $img['mime'], $img['name'], $img['embed']); 517 } 518 519 $send = $mailer->send(); 520 if ($send === false) { 521 //this may mean empty $emails 522 //throw new Exception("can't send email"); 523 } 524 } 525 526 public function mail_task_box(&$attachedImages) { 527 $tpl = $this->model->action->get_tpl(); 528 529 //render style 530 $less = new \lessc(); 531 $less->addImportDir(DOKU_PLUGIN . 'bez/style/'); 532 $style = $less->compileFile(DOKU_PLUGIN . 'bez/style/task.less'); 533 534 //render content for mail 535 $old_content_html = $this->content_html; 536 $this->content_html = p_render('bez_xhtmlmail', p_get_instructions($this->content), $info); 537 $attachedImages = array_merge($attachedImages, $info['img']); 538 539 $tpl->set('task', $this); 540 $tpl->set('style', $style); 541 $tpl->set('no_actions', true); 542 $task_box = $this->model->action->bez_tpl_include('task_box', true); 543 544 $this->content_html = $old_content_html; 545 546 return $task_box; 547 } 548 549 public function mail_task(&$attachedImages) { 550 $tpl = $this->model->action->get_tpl(); 551 552 $task_box = $this->mail_task_box($attachedImages); 553 $tpl->set('content', $task_box); 554 $content = $this->model->action->bez_tpl_include('mail/task', true); 555 556 return $content; 557 } 558 559 public function mail_notify_assignee() { 560 $tpl = $this->model->action->get_tpl(); 561 562 //we don't want who 563 $tpl->set('who', $this->model->user_nick); 564 $tpl->set('action', 'mail_task_assignee'); 565 $attachedImages = array(); 566 $content = $this->mail_task($attachedImages); 567 $this->mail_notify($content, array($this->assignee), $attachedImages); 568 } 569 570 public function mail_notify_remind($users=false) { 571 $tpl = $this->model->action->get_tpl(); 572 573 //we don't want who 574 $tpl->set('who', ''); 575 $tpl->set('action', 'mail_task_remind'); 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