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