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 $this->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore); 128 129 //update dates 130 $this->last_modification_date = date('c'); 131 $this->last_activity_date = $this->last_modification_date; 132 } 133 134 public function set_state($state) { 135 if ($this->acl_of('state') < BEZ_PERMISSION_CHANGE) { 136 throw new PermissionDeniedException(); 137 } 138 139 if (!in_array($state, array('opened', 'closed', 'rejected'))) { 140 throw new ValidationException('thread', array('state should be opened, closed or rejected')); 141 } 142 143 //nothing to do 144 if ($state == $this->state) { 145 return; 146 } 147 148 if ($state == 'closed' || $state == 'rejected') { 149 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=?, closed_by=?, close_date=? WHERE id=?", 150 $state, 151 $this->model->user_nick, 152 date('c'), 153 $this->id); 154 //reopen the task 155 } else { 156 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=? WHERE id=?", $state, $this->id); 157 } 158 159 $this->state = $state; 160 } 161 162 public function set_private_flag($flag) { 163 $private = '0'; 164 if ($flag) { 165 $private = '1'; 166 } 167 168 if ($private == $this->private) { 169 return; 170 } 171 172 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET private=? WHERE id=?", $private, $this->id); 173 174 } 175 176 public function update_last_activity() { 177 $this->last_activity_date = date('c'); 178 $this->model->sqlite->query('UPDATE thread SET last_activity_date=? WHERE id=?', 179 $this->last_activity_date, $this->id); 180 } 181 182 public function get_participants($filter='') { 183 if ($this->id === NULL) { 184 return array(); 185 } 186 187 $sql = 'SELECT * FROM thread_participant WHERE'; 188 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 189 if ($filter != '') { 190 if (!in_array($filter, $possible_flags)) { 191 throw new \Exception("unknown flag $filter"); 192 } 193 $sql .= " $filter=1 AND"; 194 } 195 $sql .= ' thread_id=? AND removed=0 ORDER BY user_id'; 196 197 $r = $this->model->sqlite->query($sql, $this->id); 198 $pars = $this->model->sqlite->res2arr($r); 199 $participants = array(); 200 foreach ($pars as $par) { 201 $participants[$par['user_id']] = $par; 202 } 203 204 return $participants; 205 } 206 207 public function get_participant($user_id, $can_be_removed=false) { 208 if ($this->id === NULL) { 209 return array(); 210 } 211 212 $q = 'SELECT * FROM thread_participant WHERE thread_id=? AND user_id=?'; 213 if (!$can_be_removed) { 214 $q .= ' AND removed=0'; 215 } 216 $r = $this->model->sqlite->query($q, $this->id, $user_id); 217 $par = $this->model->sqlite->res2row($r); 218 if (!is_array($par)) { 219 return false; 220 } 221 222 return $par; 223 } 224 225 public function is_subscribent($user_id=null) { 226 if ($user_id == null) { 227 $user_id = $this->model->user_nick; 228 } 229 $par = $this->get_participant($user_id); 230 if ($par['subscribent'] == 1) { 231 return true; 232 } 233 return false; 234 } 235 236 public function remove_participant_flags($user_id, $flags) { 237 //thread not saved yet 238 if ($this->id === NULL) { 239 throw new \Exception('cannot remove flags from not saved thread'); 240 } 241 242 $participant = $this->get_participant($user_id, true); 243 if ($participant === false) { 244 throw new ConsistencyViolationException("$user_id isn't participant"); 245 } 246 247 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 248 if (array_intersect($flags, $possible_flags) != $flags) { 249 throw new \Exception('unknown flags'); 250 } 251 252 $set = implode(',', array_map(function ($v) { return "$v=0"; }, $flags)); 253 254 $sql = "UPDATE thread_participant SET $set WHERE thread_id=? AND user_id=?"; 255 $this->model->sqlite->query($sql, $this->id, $user_id); 256 257 } 258 259 public function set_participant_flags($user_id, $flags=array()) { 260 //thread not saved yet 261 if ($this->id === NULL) { 262 throw new \Exception('cannot add flags to not saved thread'); 263 } 264 265 //validate user 266 if (!$this->model->userFactory->exists($user_id)) { 267 throw new \Exception("$user_id isn't dokuwiki user"); 268 } 269 270 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 271 if (array_intersect($flags, $possible_flags) != $flags) { 272 throw new \Exception('unknown flags'); 273 } 274 275 $participant = $this->get_participant($user_id, true); 276 if ($participant == false) { 277 $participant = array_fill_keys($possible_flags, 0); 278 279 $participant['thread_id'] = $this->id; 280 $participant['user_id'] = $user_id; 281 $participant['added_by'] = $this->model->user_nick; 282 $participant['added_date'] = date('c'); 283 284 $values = array_merge($participant, array_fill_keys($flags, 1)); 285 286 $this->model->sqlite->storeEntry('thread_participant', $values); 287 } else { 288 $set = implode(',', array_map(function($flag) { return "$flag=1"; }, $flags)); 289 290 if ($participant['removed'] == '1') { 291 $set .= ',removed=0'; 292 } 293 294 $q = "UPDATE thread_participant SET $set WHERE thread_id=? AND user_id=?"; 295 $this->model->sqlite->query($q, $this->id, $user_id); 296 } 297 298 } 299 300 public function remove_participant($user_id) { 301 //thread not saved yet 302 if ($this->id === NULL) { 303 throw new \Exception('cannot remove flags from not saved thread'); 304 } 305 306 $participant = $this->get_participant($user_id); 307 if ($participant === false) { 308 throw new ConsistencyViolationException("$user_id isn't participant"); 309 } 310 311 if ($participant['coordinator'] == '1') { 312 throw new ConsistencyViolationException("cannot remove coordinator"); 313 } 314 315 if ($participant['task_assignee'] == '1') { 316 throw new ConsistencyViolationException("cannot remove task_assignee"); 317 } 318 319 $q = "UPDATE thread_participant SET removed=1 WHERE thread_id=? AND user_id=?"; 320 $this->model->sqlite->query($q, $this->id, $user_id); 321 322 } 323 324 public function invite($client) { 325 $this->set_participant_flags($client, array('subscribent')); 326 $this->mail_notify_invite($client); 327 } 328 329 public function get_labels() { 330 //record not saved 331 if ($this->id === NULL) { 332 return array(); 333 } 334 335 $labels = array(); 336 $r = $this->model->sqlite->query('SELECT * FROM label JOIN thread_label ON label.id = thread_label.label_id 337 WHERE thread_label.thread_id=?', $this->id); 338 $arr = $this->model->sqlite->res2arr($r); 339 foreach ($arr as $label) { 340 $labels[$label['id']] = $label; 341 } 342 343 return $labels; 344 } 345 346 public function add_label($label_id) { 347 //issue not saved yet 348 if ($this->id === NULL) { 349 throw new \Exception('cannot add labels to not saved thread. use initial_save() instead'); 350 } 351 352 $r = $this->model->sqlite->query('SELECT id FROM label WHERE id=?', $label_id); 353 $label_id = $this->model->sqlite->res2single($r); 354 if (!$label_id) { 355 throw new \Exception("label($label_id) doesn't exist"); 356 } 357 358 359 $this->model->sqlite->storeEntry('thread_label', 360 array('thread_id' => $this->id, 361 'label_id' => $label_id)); 362 363 } 364 365 public function remove_label($label_id) { 366 //issue not saved yet 367 if ($this->id === NULL) { 368 throw new \Exception('cannot remove labels from not saved thread. use initial_save() instead'); 369 } 370 371 /** @var \PDOStatement $r */ 372 $r = $this->model->sqlite->query('DELETE FROM thread_label WHERE thread_id=? AND label_id=?',$this->id, $label_id); 373 if ($r->rowCount() != 1) { 374 throw new \Exception('label was not assigned to this thread'); 375 } 376 377 } 378 379 public function get_causes() { 380 $r = $this->model->sqlite->query("SELECT id FROM thread_comment WHERE type LIKE 'cause_%' AND thread_id=?", 381 $this->id); 382 $arr = $this->model->sqlite->res2arr($r); 383 $causes = array(); 384 foreach ($arr as $cause) { 385 $causes[] = $cause['id']; 386 } 387 388 return $causes; 389 } 390 391 public function can_add_comments() { 392 return in_array($this->state, array('proposal', 'opened', 'done')); 393 } 394 395 public function can_add_causes() { 396 return $this->type == 'issue' && in_array($this->state, array('opened', 'done')); 397 } 398 399 public function can_add_tasks() { 400 return in_array($this->state, array('opened', 'done')); 401 } 402 403 public function can_add_participants() { 404 return in_array($this->state, array('opened', 'done')); 405 } 406 407 public function can_be_closed() { 408 $res = $this->model->sqlite->query("SELECT thread_comment.id FROM thread_comment 409 LEFT JOIN task ON thread_comment.id = task.thread_comment_id 410 WHERE thread_comment.thread_id = ? AND 411 thread_comment.type LIKE 'cause_%' AND task.id IS NULL", $this->id); 412 413 $causes_without_tasks = $this->model->sqlite->res2row($res) ? true : false; 414 return $this->state == 'done' && 415 ! $causes_without_tasks; 416 417 } 418 419 public function can_be_rejected() { 420 return $this->state != 'rejected' && $this->task_count == 0; 421 } 422 423 public function can_be_reopened() { 424 return in_array($this->state, array('closed', 'rejected')); 425 } 426 427 public function closing_comment() { 428 $r = $this->model->thread_commentFactory->get_from_thread($this, array(), 'id DESC', 1); 429 $thread_comment = $r->fetch(); 430 431 return $thread_comment->content_html; 432 } 433 434 //http://data.agaric.com/capture-all-sent-mail-locally-postfix 435 //https://askubuntu.com/questions/192572/how-do-i-read-local-email-in-thunderbird 436 public function mail_notify($content, $users=false, $attachedImages=array()) { 437 $mailer = new \Mailer(); 438 $mailer->setBody($content, array(), array(), $content, false); 439 440 if ($users == FALSE) { 441 $users = $this->get_participants('subscribent'); 442 443 //don't notify myself 444 unset($users[$this->model->user_nick]); 445 } 446 447 $emails = array_map(function($user) { 448 if (is_array($user)) { 449 $user = $user['user_id']; 450 } 451 return $this->model->userFactory->get_user_email($user); 452 }, $users); 453 454 455 $mailer->to($emails); 456 $mailer->subject('#'.$this->id. ' ' .$this->title); 457 458 foreach ($attachedImages as $img) { 459 $mailer->attachFile($img['path'], $img['mime'], $img['name'], $img['embed']); 460 } 461 462 $send = $mailer->send(); 463 if ($send === false) { 464 //this may mean empty $emails 465 //throw new Exception("can't send email"); 466 } 467 } 468 469 public function mail_thread_box(&$attachedImages) { 470 $tpl = $this->model->action->get_tpl(); 471 472 //render style 473 $less = new \lessc(); 474 $less->addImportDir(DOKU_PLUGIN . 'bez/style/'); 475 $style = $less->compileFile(DOKU_PLUGIN . 'bez/style/thread.less'); 476 477 //render content for mail 478 $old_content_html = $this->content_html; 479 $this->content_html = p_render('bez_xhtmlmail', p_get_instructions($this->content), $info); 480 $attachedImages = array_merge($attachedImages, $info['img']); 481 482 $tpl->set('thread', $this); 483 $tpl->set('style', $style); 484 $tpl->set('no_actions', true); 485 $thread_box = $this->model->action->bez_tpl_include('thread_box', true); 486 487 $this->content_html = $old_content_html; 488 489 return $thread_box; 490 } 491 492 public function mail_thread(&$attachedImages) { 493 $tpl = $this->model->action->get_tpl(); 494 495 $thread_box = $this->mail_thread_box($attachedImages); 496 497 $tpl->set('content', $thread_box); 498 $content = $this->model->action->bez_tpl_include('mail/thread', true); 499 500 return $content; 501 } 502 503 public function mail_notify_change_state($action='') { 504 if (!$action) { 505 $action = 'mail_mail_notify_change_state_action'; 506 } 507 $tpl = $this->model->action->get_tpl(); 508 509 $tpl->set('who', $this->model->user_nick); 510 $tpl->set('action', $action); 511 $attachedImages = array(); 512 $content = $this->mail_thread($attachedImages); 513 $this->mail_notify($content, false, $attachedImages); 514 } 515 516 public function mail_notify_invite($client) { 517 $tpl = $this->model->action->get_tpl(); 518 519 $tpl->set('who', $this->model->user_nick); 520 $tpl->set('action', 'mail_mail_notify_invite_action'); 521 $attachedImages = array(); 522 $content = $this->mail_thread($attachedImages); 523 $this->mail_notify($content, array($client), $attachedImages); 524 } 525 526 public function mail_inform_coordinator() { 527 $tpl = $this->model->action->get_tpl(); 528 529 $tpl->set('who', $this->model->user_nick); 530 $tpl->set('action', 'mail_mail_inform_coordinator_action'); 531 $attachedImages = array(); 532 $content = $this->mail_thread($attachedImages); 533 $this->mail_notify($content, array($this->coordinator), $attachedImages); 534 } 535 536 public function mail_inform_admins() { 537 $tpl = $this->model->action->get_tpl(); 538 539 $tpl->set('who', $this->model->user_nick); 540 $tpl->set('action', 'mail_mail_inform_admins_action'); 541 $attachedImages = array(); 542 $content = $this->mail_thread($attachedImages); 543 $this->mail_notify($content, $this->model->userFactory->users_of_group(array('admin', 'bez_admin')), $attachedImages); 544 } 545 546 public function mail_notify_inactive($users=false) { 547 $tpl = $this->model->action->get_tpl(); 548 549 $tpl->set('who', $this->model->user_nick); 550 $tpl->set('action', 'mail_mail_notify_issue_inactive'); 551 $attachedImages = array(); 552 $content = $this->mail_thread($attachedImages); 553 $this->mail_notify($content, $users, $attachedImages); 554 } 555 556 public function mail_notify_task_added(Task $task) { 557 $tpl = $this->model->action->get_tpl(); 558 559 //we don't want who 560 $tpl->set('who', $this->model->user_nick); 561 $tpl->set('action', 'mail_thread_task_added'); 562 $attachedImages = array(); 563 $task_box = $task->mail_task_box($attachedImages); 564 565 $tpl->set('thread', $this); 566 $tpl->set('content', $task_box); 567 $content = $this->model->action->bez_tpl_include('mail/thread', true); 568 569 $this->mail_notify($content, false, $attachedImages); 570 } 571 572 public function mail_notify_task_state_changed(Task $task) { 573 $tpl = $this->model->action->get_tpl(); 574 575 if ($task->state == 'done') { 576 $action = 'mail_thread_task_done'; 577 } else { 578 $action = 'mail_thread_task_reopened'; 579 } 580 581 //we don't want who 582 $tpl->set('who', $this->model->user_nick); 583 $tpl->set('action', $action); 584 $attachedImages = array(); 585 $task_box = $task->mail_task_box($attachedImages); 586 587 $tpl->set('thread', $this); 588 $tpl->set('content', $task_box); 589 $content = $this->model->action->bez_tpl_include('mail/thread', true); 590 591 $this->mail_notify($content, false, $attachedImages); 592 } 593 594} 595