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