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