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 purge() { 136 $this->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore); 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 LIKE 'cause_%' 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 can_be_closed() { 422 $res = $this->model->sqlite->query("SELECT thread_comment.id FROM thread_comment 423 LEFT JOIN task ON thread_comment.id = task.thread_comment_id 424 WHERE thread_comment.thread_id = ? AND 425 thread_comment.type LIKE 'cause_%' AND task.id IS NULL", $this->id); 426 427 $causes_without_tasks = $this->model->sqlite->res2row($res) ? true : false; 428 return $this->state == 'done' && 429 ! $causes_without_tasks; 430 431 } 432 433 public function can_be_rejected() { 434 return $this->state != 'rejected' && $this->task_count == 0; 435 } 436 437 public function can_be_reopened() { 438 return in_array($this->state, array('closed', 'rejected')); 439 } 440 441 public function closing_comment() { 442 $r = $this->model->thread_commentFactory->get_from_thread($this, array(), 'id DESC', 1); 443 $thread_comment = $r->fetch(); 444 445 return $thread_comment->content_html; 446 } 447 448 //http://data.agaric.com/capture-all-sent-mail-locally-postfix 449 //https://askubuntu.com/questions/192572/how-do-i-read-local-email-in-thunderbird 450 public function mail_notify($content, $users=false, $attachedImages=array()) { 451 $mailer = new \Mailer(); 452 $mailer->setBody($content, array(), array(), $content, false); 453 454 if ($users == FALSE) { 455 $users = $this->get_participants('subscribent'); 456 457 //don't notify myself 458 unset($users[$this->model->user_nick]); 459 } 460 461 $emails = array_map(function($user) { 462 if (is_array($user)) { 463 $user = $user['user_id']; 464 } 465 return $this->model->userFactory->get_user_email($user); 466 }, $users); 467 468 469 $mailer->to($emails); 470 $mailer->subject('#'.$this->id. ' ' .$this->title); 471 472 foreach ($attachedImages as $img) { 473 $mailer->attachFile($img['path'], $img['mime'], $img['name'], $img['embed']); 474 } 475 476 $send = $mailer->send(); 477 if ($send === false) { 478 //this may mean empty $emails 479 //throw new Exception("can't send email"); 480 } 481 } 482 483 public function mail_thread_box(&$attachedImages) { 484 $tpl = $this->model->action->get_tpl(); 485 486 //render style 487 $less = new \lessc(); 488 $less->addImportDir(DOKU_PLUGIN . 'bez/style/'); 489 $style = $less->compileFile(DOKU_PLUGIN . 'bez/style/thread.less'); 490 491 //render content for mail 492 $old_content_html = $this->content_html; 493 $this->content_html = p_render('bez_xhtmlmail', p_get_instructions($this->content), $info); 494 $attachedImages = array_merge($attachedImages, $info['img']); 495 496 $tpl->set('thread', $this); 497 $tpl->set('style', $style); 498 $tpl->set('no_actions', true); 499 $thread_box = $this->model->action->bez_tpl_include('thread_box', true); 500 501 $this->content_html = $old_content_html; 502 503 return $thread_box; 504 } 505 506 public function mail_thread(&$attachedImages) { 507 $tpl = $this->model->action->get_tpl(); 508 509 $thread_box = $this->mail_thread_box($attachedImages); 510 511 $tpl->set('content', $thread_box); 512 $content = $this->model->action->bez_tpl_include('mail/thread', true); 513 514 return $content; 515 } 516 517 public function mail_notify_change_state($action='') { 518 if (!$action) { 519 $action = 'mail_mail_notify_change_state_action'; 520 } 521 $tpl = $this->model->action->get_tpl(); 522 523 $tpl->set('who', $this->model->user_nick); 524 $tpl->set('action', $action); 525 $attachedImages = array(); 526 $content = $this->mail_thread($attachedImages); 527 $this->mail_notify($content, false, $attachedImages); 528 } 529 530 public function mail_notify_invite($client) { 531 $tpl = $this->model->action->get_tpl(); 532 533 $tpl->set('who', $this->model->user_nick); 534 $tpl->set('action', 'mail_mail_notify_invite_action'); 535 $attachedImages = array(); 536 $content = $this->mail_thread($attachedImages); 537 $this->mail_notify($content, array($client), $attachedImages); 538 } 539 540 public function mail_inform_coordinator() { 541 $tpl = $this->model->action->get_tpl(); 542 543 $tpl->set('who', $this->model->user_nick); 544 $tpl->set('action', 'mail_mail_inform_coordinator_action'); 545 $attachedImages = array(); 546 $content = $this->mail_thread($attachedImages); 547 $this->mail_notify($content, array($this->coordinator), $attachedImages); 548 } 549 550 public function mail_inform_admins() { 551 $tpl = $this->model->action->get_tpl(); 552 553 $tpl->set('who', $this->model->user_nick); 554 $tpl->set('action', 'mail_mail_inform_admins_action'); 555 $attachedImages = array(); 556 $content = $this->mail_thread($attachedImages); 557 $this->mail_notify($content, $this->model->userFactory->users_of_group(array('admin', 'bez_admin')), $attachedImages); 558 } 559 560 public function mail_notify_inactive($users=false) { 561 $tpl = $this->model->action->get_tpl(); 562 563 $tpl->set('who', $this->model->user_nick); 564 $tpl->set('action', 'mail_mail_notify_issue_inactive'); 565 $attachedImages = array(); 566 $content = $this->mail_thread($attachedImages); 567 $this->mail_notify($content, $users, $attachedImages); 568 } 569 570 public function mail_notify_task_added(Task $task) { 571 $tpl = $this->model->action->get_tpl(); 572 573 //we don't want who 574 $tpl->set('who', $this->model->user_nick); 575 $tpl->set('action', 'mail_thread_task_added'); 576 $attachedImages = array(); 577 $task_box = $task->mail_task_box($attachedImages); 578 579 $tpl->set('thread', $this); 580 $tpl->set('content', $task_box); 581 $content = $this->model->action->bez_tpl_include('mail/thread', true); 582 583 $this->mail_notify($content, false, $attachedImages); 584 } 585 586 public function mail_notify_task_state_changed(Task $task) { 587 $tpl = $this->model->action->get_tpl(); 588 589 if ($task->state == 'done') { 590 $action = 'mail_thread_task_done'; 591 } else { 592 $action = 'mail_thread_task_reopened'; 593 } 594 595 //we don't want who 596 $tpl->set('who', $this->model->user_nick); 597 $tpl->set('action', $action); 598 $attachedImages = array(); 599 $task_box = $task->mail_task_box($attachedImages); 600 601 $tpl->set('thread', $this); 602 $tpl->set('content', $task_box); 603 $content = $this->model->action->bez_tpl_include('mail/thread', true); 604 605 $this->mail_notify($content, false, $attachedImages); 606 } 607 608} 609