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