1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5use dokuwiki\plugin\bez\meta\Mailer; 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 } 98 } 99 100 if ($this->state == 'proposal' && $this->original_poster == $this->model->user_nick) { 101 $this->acl->grant('title', BEZ_PERMISSION_CHANGE); 102 $this->acl->grant('content', BEZ_PERMISSION_CHANGE); 103 $this->acl->grant('type', BEZ_PERMISSION_CHANGE); 104 } 105 106 if ($this->coordinator == $this->model->user_nick) { 107 $this->acl->grant('title', BEZ_PERMISSION_CHANGE); 108 $this->acl->grant('content', BEZ_PERMISSION_CHANGE); 109 $this->acl->grant('coordinator', BEZ_PERMISSION_CHANGE); 110 $this->acl->grant('label_id', BEZ_PERMISSION_CHANGE); 111 $this->acl->grant('private', BEZ_PERMISSION_CHANGE); 112 113 $this->acl->grant('state', BEZ_PERMISSION_CHANGE); 114 $this->acl->grant('type', BEZ_PERMISSION_CHANGE); 115 } 116 } 117 } 118 119 public function set_data($data, $filter=NULL) { 120 parent::set_data($data, $filter=NULL); 121 122 if (isset($data['coordinator']) && $this->state == 'proposal') { 123 $this->state = 'opened'; 124 } 125 126 $this->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore); 127 128 //update dates 129 $this->last_modification_date = date('c'); 130 $this->last_activity_date = $this->last_modification_date; 131 } 132 133 public function set_state($state) { 134 if ($this->acl_of('state') < BEZ_PERMISSION_CHANGE) { 135 throw new PermissionDeniedException(); 136 } 137 138 if (!in_array($state, array('opened', 'closed', 'rejected'))) { 139 throw new ValidationException('thread', array('state should be opened, closed or rejected')); 140 } 141 142 //nothing to do 143 if ($state == $this->state) { 144 return; 145 } 146 147 if ($state == 'closed' || $state == 'rejected') { 148 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=?, closed_by=?, close_date=? WHERE id=?", 149 $state, 150 $this->model->user_nick, 151 date('c'), 152 $this->id); 153 //reopen the task 154 } else { 155 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=? WHERE id=?", $state, $this->id); 156 } 157 158 $this->state = $state; 159 } 160 161 public function set_private_flag($flag) { 162 $private = '0'; 163 if ($flag) { 164 $private = '1'; 165 } 166 167 if ($private == $this->private) { 168 return; 169 } 170 171 $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET private=? WHERE id=?", $private, $this->id); 172 173 } 174 175 public function update_last_activity() { 176 $this->last_activity_date = date('c'); 177 $this->model->sqlite->query('UPDATE thread SET last_activity_date=? WHERE id=?', 178 $this->last_activity_date, $this->id); 179 } 180 181 public function get_participants($filter='') { 182 if ($this->id === NULL) { 183 return array(); 184 } 185 186 $sql = 'SELECT * FROM thread_participant WHERE'; 187 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 188 if ($filter != '') { 189 if (!in_array($filter, $possible_flags)) { 190 throw new \Exception("unknown flag $filter"); 191 } 192 $sql .= " $filter=1 AND"; 193 } 194 $sql .= ' thread_id=? ORDER BY user_id'; 195 196 $r = $this->model->sqlite->query($sql, $this->id); 197 $pars = $this->model->sqlite->res2arr($r); 198 $participants = array(); 199 foreach ($pars as $par) { 200 $participants[$par['user_id']] = $par; 201 } 202 203 return $participants; 204 } 205 206 public function get_participant($user_id) { 207 if ($this->id === NULL) { 208 return array(); 209 } 210 211 $r = $this->model->sqlite->query('SELECT * FROM thread_participant WHERE thread_id=? AND user_id=?', $this->id, $user_id); 212 $par = $this->model->sqlite->res2row($r); 213 if (!is_array($par)) { 214 return false; 215 } 216 217 return $par; 218 } 219 220 public function is_subscribent($user_id=null) { 221 if ($user_id == null) { 222 $user_id = $this->model->user_nick; 223 } 224 $par = $this->get_participant($user_id); 225 if ($par['subscribent'] == 1) { 226 return true; 227 } 228 return false; 229 } 230 231 public function remove_participant_flags($user_id, $flags) { 232 //thread not saved yet 233 if ($this->id === NULL) { 234 throw new \Exception('cannot remove flags from not saved thread'); 235 } 236 237 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 238 if (array_intersect($flags, $possible_flags) != $flags) { 239 throw new \Exception('unknown flags'); 240 } 241 242 $set = implode(',', array_map(function ($v) { return "$v=0"; }, $flags)); 243 244 $sql = "UPDATE thread_participant SET $set WHERE thread_id=? AND user_id=?"; 245 $this->model->sqlite->query($sql, $this->id, $user_id); 246 247 } 248 249 public function set_participant_flags($user_id, $flags=array()) { 250 //thread not saved yet 251 if ($this->id === NULL) { 252 throw new \Exception('cannot add flags to not saved thread'); 253 } 254 255 //validate user 256 if (!$this->model->userFactory->exists($user_id)) { 257 throw new \Exception("$user_id isn't dokuwiki user"); 258 } 259 260 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 261 if (array_intersect($flags, $possible_flags) != $flags) { 262 throw new \Exception('unknown flags'); 263 } 264 265 $participant = $this->get_participant($user_id); 266 if ($participant == false) { 267 $participant = array_fill_keys($possible_flags, 0); 268 269 $participant['thread_id'] = $this->id; 270 $participant['user_id'] = $user_id; 271 $participant['added_by'] = $this->model->user_nick; 272 $participant['added_date'] = date('c'); 273 } 274 $values = array_merge($participant, array_fill_keys($flags, 1)); 275 276 $keys = join(',', array_keys($values)); 277 $vals = join(',', array_fill(0,count($values),'?')); 278 279 $sql = "REPLACE INTO thread_participant ($keys) VALUES ($vals)"; 280 $this->model->sqlite->query($sql, array_values($values)); 281 } 282 283 284 public function invite($client) { 285 $this->set_participant_flags($client, array('subscribent')); 286 $this->mail_notify_invite($client); 287 } 288 289 public function get_labels() { 290 //record not saved 291 if ($this->id === NULL) { 292 return array(); 293 } 294 295 $labels = array(); 296 $r = $this->model->sqlite->query('SELECT * FROM label JOIN thread_label ON label.id = thread_label.label_id 297 WHERE thread_label.thread_id=?', $this->id); 298 $arr = $this->model->sqlite->res2arr($r); 299 foreach ($arr as $label) { 300 $labels[$label['id']] = $label; 301 } 302 303 return $labels; 304 } 305 306 public function add_label($label_id) { 307 //issue not saved yet 308 if ($this->id === NULL) { 309 throw new \Exception('cannot add labels to not saved thread. use initial_save() instead'); 310 } 311 312 $r = $this->model->sqlite->query('SELECT id FROM label WHERE id=?', $label_id); 313 $label_id = $this->model->sqlite->res2single($r); 314 if (!$label_id) { 315 throw new \Exception("label($label_id) doesn't exist"); 316 } 317 318 319 $this->model->sqlite->storeEntry('thread_label', 320 array('thread_id' => $this->id, 321 'label_id' => $label_id)); 322 323 } 324 325 public function remove_label($label_id) { 326 //issue not saved yet 327 if ($this->id === NULL) { 328 throw new \Exception('cannot remove labels from not saved thread. use initial_save() instead'); 329 } 330 331 /** @var \PDOStatement $r */ 332 $r = $this->model->sqlite->query('DELETE FROM thread_label WHERE thread_id=? AND label_id=?',$this->id, $label_id); 333 if ($r->rowCount() != 1) { 334 throw new \Exception('label was not assigned to this thread'); 335 } 336 337 } 338 339 public function get_causes() { 340 $r = $this->model->sqlite->query("SELECT id FROM thread_comment WHERE type LIKE 'cause_%' AND thread_id=?", 341 $this->id); 342 $arr = $this->model->sqlite->res2arr($r); 343 $causes = array(); 344 foreach ($arr as $cause) { 345 $causes[] = $cause['id']; 346 } 347 348 return $causes; 349 } 350 351 public function can_add_comments() { 352 return in_array($this->state, array('proposal', 'opened', 'done')); 353 } 354 355 public function can_add_causes() { 356 return $this->type == 'issue' && in_array($this->state, array('opened', 'done')); 357 } 358 359 public function can_add_tasks() { 360 return in_array($this->state, array('opened', 'done')); 361 } 362 363 public function can_add_participants() { 364 return in_array($this->state, array('opened', 'done')); 365 } 366 367 public function can_be_closed() { 368 $res = $this->model->sqlite->query("SELECT thread_comment.id FROM thread_comment 369 LEFT JOIN task ON thread_comment.id = task.thread_comment_id 370 WHERE thread_comment.thread_id = ? AND 371 thread_comment.type LIKE 'cause_%' AND task.id IS NULL", $this->id); 372 373 $causes_without_tasks = $this->model->sqlite->res2row($res) ? true : false; 374 return $this->state == 'done' && 375 ! $causes_without_tasks; 376 377 } 378 379 public function can_be_rejected() { 380 return $this->state != 'rejected' && $this->task_count == 0; 381 } 382 383 public function can_be_reopened() { 384 return in_array($this->state, array('closed', 'rejected')); 385 } 386 387 public function closing_comment() { 388 $r = $this->model->thread_commentFactory->get_from_thread($this, array(), 'id', 1); 389 $thread_comment = $r->fetch(); 390 391 return $thread_comment->content_html; 392 } 393 394 //http://data.agaric.com/capture-all-sent-mail-locally-postfix 395 //https://askubuntu.com/questions/192572/how-do-i-read-local-email-in-thunderbird 396 public function mail_notify($replacements=array(), $users=false, $attachedImages=array()) { 397 $plain = io_readFile($this->model->action->localFN('thread-notification')); 398 $html = io_readFile($this->model->action->localFN('thread-notification', 'html')); 399 400 $thread_reps = array( 401 'thread_id' => $this->id, 402 'thread_link' => $this->model->action->url('thread', 'id', $this->id), 403 'thread_unsubscribe' => 404 $this->model->action->url('thread', 'id', $this->id, 'action', 'unsubscribe'), 405 'custom_content' => false, 406 'action_border_color' => 'transparent', 407 'action_color' => 'transparent', 408 ); 409 410 //$replacements can override $issue_reps 411 $rep = array_merge($thread_reps, $replacements); 412 //auto title 413 if (!isset($rep['subject'])) { 414 $rep['subject'] = '#'.$this->id. ' ' .$this->title; 415 } 416 if (!isset($rep['content_html'])) { 417 $rep['content_html'] = $rep['content']; 418 } 419 if (!isset($rep['who_full_name'])) { 420 $rep['who_full_name'] = 421 $this->model->userFactory->get_user_full_name($rep['who']); 422 } 423 424 //format when 425 $rep['when'] = dformat(strtotime($rep['when']), '%Y-%m-%d %H:%M'); 426 427 if ($rep['custom_content'] === false) { 428 $html = str_replace('@CONTENT_HTML@', ' 429 <div style="margin: 5px 0;"> 430 <strong>@WHO_FULL_NAME@</strong> <br> 431 <span style="color: #888">@WHEN@</span> 432 </div> 433 @CONTENT_HTML@ 434 ', $html); 435 } 436 437 //we must do it manually becouse Mailer uses htmlspecialchars() 438 $html = str_replace('@CONTENT_HTML@', $rep['content_html'], $html); 439 440 $mailer = new Mailer(); 441 $mailer->setBody($plain, $rep, $rep, $html, false); 442 443 if ($users == FALSE) { 444 445 $users = $this->get_participants('subscribent'); 446 //don't notify myself 447 unset($users[$this->model->user_nick]); 448 } 449 450 $emails = array_map(function($user) { 451 if (is_array($user)) { 452 $user = $user['user_id']; 453 } 454 return $this->model->userFactory->get_user_email($user); 455 }, $users); 456 457 458 $mailer->to($emails); 459 $mailer->subject($rep['subject']); 460 461 foreach ($attachedImages as $img) { 462 $mailer->attachFile($img['path'], $img['mime'], $img['name'], $img['embed']); 463 } 464 465 $send = $mailer->send(); 466 if ($send === false) { 467 //this may mean empty $emails 468 //throw new Exception("can't send email"); 469 } 470 } 471 472 protected function mail_issue_box_reps(&$replacements, &$attachedImages) { 473 $replacements['custom_content'] = true; 474 475 $html = '<h2 style="font-size: 1.2em;">'; 476 $html .= '<a style="font-size:115%" href="@THREAD_LINK@">#@THREAD_ID@</a> '; 477 478 if ( ! empty($this->type_string)) { 479 $html .= $this->type_string; 480 } else { 481 $html .= '<i style="color: #777"> '. 482 $this->model->action->getLang('issue_type_no_specified'). 483 '</i>'; 484 } 485 486 $html .= ' ('. $this->model->action->getLang('state_' . $this->state ) .') '; 487 488 $html .= '<span style="color: #777; font-weight: normal; font-size: 90%;">'; 489 $html .= $this->model->action->getLang('coordinator') . ': '; 490 $html .= '<span style="font-weight: bold;">'; 491 492 if ($this->state == 'proposal') { 493 $html .= '<i style="font-weight: normal;">' . 494 $this->model->action->getLang('proposal') . 495 '</i>'; 496 } else { 497 $html .= $this->model->userFactory->get_user_full_name($this->coordinator); 498 } 499 $html .= '</span></span></h2>'; 500 501 $html .= '<h2 style="font-size: 1.2em;border-bottom: 1px solid @ACTION_BORDER_COLOR@">' . $this->title . '</h2>'; 502 503 $html .= p_render('bez_xhtmlmail', p_get_instructions($this->content), $info); 504 $attachedImages = array_merge($attachedImages, $info['img']); 505 506 $replacements['content_html'] = $html; 507 508 509 switch ($this->priority) { 510 case '0': 511 $replacements['action_color'] = '#F8E8E8'; 512 $replacements['action_border_color'] = '#F0AFAD'; 513 break; 514 case '1': 515 $replacements['action_color'] = '#ffd'; 516 $replacements['action_border_color'] = '#dd9'; 517 break; 518 case '2': 519 $replacements['action_color'] = '#EEF6F0'; 520 $replacements['action_border_color'] = '#B0D2B6'; 521 break; 522 case 'None': 523 $replacements['action_color'] = '#e7f1ff'; 524 $replacements['action_border_color'] = '#a3c8ff'; 525 break; 526 default: 527 $replacements['action_color'] = '#fff'; 528 $replacements['action_border_color'] = '#bbb'; 529 break; 530 } 531 } 532 533 public function mail_notify_change_state() { 534 $replacements = array( 535 'who' => $this->model->user_nick, 536 'action' => $this->model->action->getLang('mail_mail_notify_change_state_action') 537 ); 538 $attachedImages = array(); 539 $this->mail_issue_box_reps($replacements, $attachedImages); 540 $this->mail_notify($replacements, false, $attachedImages); 541 } 542 543 public function mail_notify_invite($client) { 544 $replacements = array( 545 'who' => $this->model->user_nick, 546 'action' => $this->model->action->getLang('mail_mail_notify_invite_action') 547 ); 548 $attachedImages = array(); 549 $this->mail_issue_box_reps($replacements, $attachedImages); 550 $this->mail_notify($replacements, array($client), $attachedImages); 551 } 552 553 public function mail_inform_coordinator() { 554 $replacements = array( 555 'who' => $this->model->user_nick, 556 'action' => $this->model->action->getLang('mail_mail_inform_coordinator_action') 557 ); 558 $attachedImages = array(); 559 $this->mail_issue_box_reps($replacements, $attachedImages); 560 $this->mail_notify($replacements, array($this->coordinator), $attachedImages); 561 } 562 563 public function mail_notify_issue_inactive($users=false) { 564 $replacements = array( 565 'who' => '', 566 'action' => $this->model->action->getLang('mail_mail_notify_issue_inactive') 567 ); 568 $attachedImages = array(); 569 $this->mail_issue_box_reps($replacements, $attachedImages); 570 $this->mail_notify($replacements, $users, $attachedImages); 571 } 572 573} 574