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