1<?php 2 3//if(!defined('DOKU_INC')) die(); 4 5//require_once 'entity.php'; 6 7namespace dokuwiki\plugin\bez\mdl; 8 9use dokuwiki\plugin\bez\meta\PermissionDeniedException; 10use dokuwiki\plugin\bez\meta\ValidationException; 11 12class Thread extends Entity { 13 14 protected $id; 15 16 protected $original_poster, $coordinator; 17 18 protected $private, $lock; 19 20 protected $type, $state; 21 22 protected $create_date, $last_activity_date, $last_modification_date, $close_date; 23 24 protected $title, $content, $content_html; 25 26 protected $task_count, $task_count_open, $task_sum_cost; 27 28 public static function get_columns() { 29 return array('id', 30 'original_poster', 'coordinator', 31 'private', 'lock', 32 'type', 'state', 33 'create_date', 'last_activity_date', 'last_modification_date', 'close_date', 34 'title', 'content', 'content_html', 35 'task_count', 'task_count_open', 'task_sum_cost'); 36 } 37 38 public static function get_states() { 39 return array('proposal', 'opened', 'done', 'closed', 'rejected'); 40 } 41 42 43// private function state_string() { 44// if ($this->state === '2') { 45// return 'state_rejected'; 46// } else if ($this->coordinator === '-proposal') { 47// return 'state_proposal'; 48// } else if ( $this->state === '0' && 49// (int)$this->assigned_tasks_count > 0 && 50// (int)$this->opened_tasks_count === 0) { 51// return 'state_done'; 52// } else if ($this->state === '0') { 53// return 'state_opened'; 54// } else if ($this->state === '1') { 55// return 'state_closed'; 56// } 57// } 58// 59// private function type_string() { 60// if ($this->type === '') { 61// return ''; 62// } 63// $issuetype = $this->model->issuetypes->get_one($this->type)->get_assoc(); 64// return $issuetype[$this->model->conf['lang']]; 65// } 66// 67// private function priority() { 68// if ($this->state === '2') { 69// return '3'; 70// } 71// $min_pr = $this->model->tasks->min_priority(array('issue' => $this->id)); 72// if ($min_pr === NULL) { 73// return 'None'; 74// } 75// return $min_pr; 76// } 77 78 public function user_is_coordinator() { 79 if ($this->coordinator === $this->model->user_nick || 80 $this->model->acl->get_level() >= BEZ_AUTH_ADMIN) { 81 return true; 82 } 83 } 84 85 public function __construct($model, $defaults=array()) { 86 parent::__construct($model); 87 88 $this->validator->set_rules(array( 89 'coordinator' => array(array('dw_user'), 'NULL'), 90 'title' => array(array('length', 200), 'NOT NULL'), 91 'content' => array(array('length', 10000), 'NOT NULL') 92 )); 93 94// $this->validator->set_rules(array( 95// 'title' => array(array('length', 200), 'NOT NULL'), 96// 'description' => array(array('length', 10000), 'NOT NULL'), 97// 'state' => array(array('select', array('0', '1', '2')), 'NULL'), 98// 'opinion' => array(array('length', 10000), 'NOT NULL'), 99// 'type' => array(array('numeric'), 'NULL'), 100// 'coordinator' => array(array('dw_user'), 'NOT NULL'), 101// 'reporter' => array(array('dw_user'), 'NOT NULL'), 102// 'date' => array(array('unix_timestamp'), 'NOT NULL'), 103// 'last_mod' => array(array('unix_timestamp'), 'NULL'), 104// 'last_activity' => array(array('sqlite_datetime'), 'NOT NULL') 105// )); 106 107// $this->validator->set_rules(array( 108 //'coordinator' => array(array('dw_user'), 'NULL'), 109// 'original_poster' => array(array('dw_user'), 'NOT NULL'), 110// 'title' => array(array('length', 200), 'NOT NULL'), 111// 'content' => array(array('length', 10000), 'NOT NULL'), 112// 'state' => array(array('select', array('0', '1', '2')), 'NULL'), 113// 'opinion' => array(array('length', 10000), 'NOT NULL'), 114// 'type' => array(array('select'), 'NULL'), 115 116// 'create_date' => array(array('sqlite_datetime'), 'NOT NULL'), 117// 'last_mod' => array(array('sqlite_datetime'), 'NULL'), 118// 'last_activity' => array(array('sqlite_datetime'), 'NOT NULL') 119// )); 120 121 //we've created empty object (new record) 122 if ($this->id === NULL) { 123 $this->original_poster = $this->model->user_nick; 124 $this->create_date = date('c'); 125 $this->last_activity_date = $this->create_date; 126 $this->last_modification_date = $this->create_date; 127 128 $this->state = 'proposal'; 129 130// $this->close_date = ''; 131 132// $this->lock = '0'; 133// $this->private = '0'; 134// $this->type = '1';//type 1 - issue 135// $this->state = '0';//state 0 - proposal 136 137 //$this->update_last_activity(); 138 139 //$this->state = '0'; 140 141 if ($this->model->acl->get_level() >= BEZ_AUTH_LEADER) { 142// $this->validator->add_rule('cooridnator', array(array('dw_user'), 'NOT NULL')); 143 //throws ValidationException 144// $this->coordinator = $this->validator->validate_field('coordinator', $defaults['coordinator']); 145 if (!$this->model->userFactory->exists($defaults['coordinator'])) { 146 throw new ValidationException('thread', array('coordinator' => 'is_null')); 147 } 148 $this->coordinator = $defaults['coordinator']; 149 $this->state = 'opened'; 150 } 151// } else { 152// $this->coordinator = '-proposal'; 153// } 154 155 156// $this->add_participant($this->reporter); 157// $this->add_subscribent($this->reporter); 158// if ($this->coordinator !== '-proposal') { 159// $this->add_participant($this->coordinator); 160// $this->add_subscribent($this->coordinator); 161// } 162 163 } 164 //close_date required 165// if ($this->state !== 'state_proposal' && $this->state !== 'state_opened') { 166// $this->validator->set_rules(array( 167// 'close_date' => array(array('unix_timestamp'), 'NOT NULL') 168// )); 169// } 170 171 172// if ($this->participants !== NULL) { 173// $exp_part = explode(',', $this->participants); 174// foreach ($exp_part as $participant) { 175// $this->participants_array[$participant] = $participant; 176// } 177// } 178// 179// if ($this->subscribents !== NULL) { 180// $exp_part = explode(',', $this->subscribents); 181// foreach ($exp_part as $subscribent) { 182// $this->subscribents_array[$subscribent] = $subscribent; 183// } 184// } 185 } 186 187 public function set_data($data, $filter=NULL) { 188 $input = array('title', 'content', 'coordinator'); 189 $val_data = $this->validator->validate($data, $input); 190 191 if ($val_data === false) { 192 throw new ValidationException('issues', $this->validator->get_errors()); 193 } 194 195 196 //change coordinator at the end(!) 197 if (isset($val_data['coordinator'])) { 198 $val_coordinator = $val_data['coordinator']; 199 unset($val_data['coordinator']); 200 } 201 202 $this->set_property_array($val_data); 203 204 if (isset($val_coordinator)) { 205 $this->set_property('coordinator', $val_coordinator); 206 } 207 208 //!!! don't update activity on issue update 209 $this->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore); 210// $this->opinion_cache = $this->helper->wiki_parse($this->opinion); 211 212 //update virtuals 213 //$this->update_virtual_columns(); 214 } 215 216 public function get_meta_fields() { 217 return array('reporter', 'date', 'last_mod', 'last_activity'); 218 } 219 220 public function set_meta($post) { 221 222 if (isset($post['date'])) { 223 $unix = strtotime($post['date']); 224 //if $unix === false validator will catch it 225 if ($unix !== false) { 226 $post['date'] = (string)$unix; 227 } 228 } 229 230 if (isset($post['last_mod'])) { 231 $unix = strtotime($post['last_mod']); 232 //if $unix === false validator will catch it 233 if ($unix !== false) { 234 $post['last_mod'] = (string)$unix; 235 } 236 } 237 238 parent::set_data($post, $this->get_meta_fields()); 239 } 240 241// public function update_cache() { 242// if ($this->model->acl->get_level() < BEZ_AUTH_ADMIN) { 243// return false; 244// } 245// $this->description_cache = $this->helper->wiki_parse($this->description); 246// $this->opinion_cache = $this->helper->wiki_parse($this->opinion); 247// } 248// 249// public function set_state($data) { 250// 251// $input = array('state', 'opinion'); 252// $val_data = $this->validator->validate($data, $input); 253// 254// if ($val_data === false) { 255// throw new ValidationException('issues', $this->validator->get_errors()); 256// } 257// 258// $this->set_property_array($val_data); 259// 260// if (count($this->validator->get_errors()) > 0) { 261// throw new ValidationException('issues', $this->validator->get_errors()); 262// } 263// 264// //update activity on state update 265// $this->last_mod = time(); 266// $this->update_last_activity(); 267// $this->opinion_cache = $this->helper->wiki_parse($this->opinion); 268// 269// //update virtuals 270// //$this->update_virtual_columns(); 271// } 272 273// public function update_last_activity() { 274// $this->last_activity = $this->sqlite_date(); 275// } 276 277 private $participants; 278 public function get_participants() { 279 if ($this->acl_of('participants') < BEZ_PERMISSION_VIEW) { 280 throw new PermissionDeniedException(); 281 } 282 if ($this->id === NULL) { 283 $this->participants = array(); 284 } 285 if (is_null($this->participants)) { 286 $r = $this->model->sqlite->query('SELECT * FROM thread_participant WHERE thread_id=? ORDER BY user_id', $this->id); 287 $this->participants = $this->model->sqlite->res2arr($r); 288 } 289 return $this->participants; 290 } 291 292 public function get_participant($user_id) { 293 $participants = $this->get_participants(); 294 foreach ($participants as $participant) { 295 if ($participant['user_id'] == $user_id) { 296 return $participant; 297 } 298 } 299 return false; 300 } 301 302 public function set_participant_flags($user_id, $flags=array()) { 303 if ($this->acl_of('participants') < BEZ_PERMISSION_CHANGE) { 304 throw new PermissionDeniedException(); 305 } 306 307 $possible_flags = array('original_poster', 'coordinator', 'commentator', 'task_assignee', 'subscribent'); 308 if (array_intersect($flags, $possible_flags) != $flags) { 309 throw new \Exception('unknown flags'); 310 } 311 312 $participant = $this->get_participant($user_id); 313 if ($participant == false) { 314 $participant = array_fill_keys($possible_flags, 0); 315 } 316 $values = array_merge($participant, array_fill_keys($flags, 1)); 317 318 $values['thread_id'] = $this->id; 319 $values['user_id'] = $user_id; 320 $values['added_by'] = $this->model->dw_user; 321 $valuse['added_date'] = date('c'); 322 323 $keys = join(',', array_keys($values)); 324 $vals = join(',', array_fill(0,count($values),'?')); 325 326 $sql = "REPLACE INTO thread_participant ($keys) VALUES ($vals)"; 327 $this->model->sqlite->query($sql, array_values($valuse)); 328 329 330 331// if (! ( $this->user_is_coordinator() || 332// $participant === $this->model->user_nick || 333// $participant === $this->coordinator) //dodajemy nowego koordynatora 334// ) { 335// throw new PermissionDeniedException(); 336// } 337// if ($this->model->users->exists($participant)) { 338// $this->participants_array[$participant] = $participant; 339// $this->participants = implode(',', $this->participants_array); 340// } 341 } 342 343 private $labels; 344 public function get_labels() { 345 if ($this->acl_of('labels') < BEZ_PERMISSION_VIEW) { 346 throw new PermissionDeniedException(); 347 } 348 if ($this->id === NULL) { 349 $this->labels = array(); 350 } 351 if (is_null($this->labels)) { 352 $r = $this->model->sqlite->query('SELECT * FROM label JOIN thread_label WHERE thread_label.thread_id=?', $this->id); 353 $this->labels = $this->model->sqlite->res2arr($r); 354 } 355 return $this->labels; 356 } 357 358// public function get_label_id($name) { 359// $labels = $this->get_labels(); 360// 361// foreach ($labels as $label) { 362// if ($label['name'] == $name) { 363// return $label['label_id']; 364// } 365// } 366// return false; 367// } 368 369 public function add_label($label_id) { 370 if ($this->acl_of('labels') < BEZ_PERMISSION_CHANGE) { 371 throw new PermissionDeniedException(); 372 } 373 374 //issue not saved yet 375 if ($this->id === NULL) { 376 throw new \Exception('cannot add labels to not saved thread. use set_labels() instead'); 377 } 378 379 //label already assigned, nothing to do 380// if ($this->get_label_id($name)) return; 381 382// $r = $this->model->sqlite->query('SELECT id FROM label WHERE id=?', $label_id); 383// $label_id = $this->model->sqlite->res2single($r); 384// if (!$label_id) { 385// throw new \Exception('label does not exist'); 386// } 387 388 $this->model->sqlite->storeEntry('thread_label', 389 array('thread_id' => $this->id, 390 'label_id' => $label_id)); 391 392 } 393 394// public function remove_label($name) { 395// if ($this->acl_of('labels') < BEZ_PERMISSION_CHANGE) { 396// throw new PermissionDeniedException(); 397// } 398// //label not assigned 399// $label_id = $this->get_label($name); 400// 401// if ($label_id === false) { 402// throw new \Exception('label don not exists'); 403// } 404// 405// $this->model->sqlite->query('DELETE FROM thread_label WHERE thread_id=?, label_id=?', $this->id, $label_id); 406// } 407 408// public function add_subscribent($subscribent) { 409// if (! ( $this->user_is_coordinator() || 410// $subscribent === $this->model->user_nick || 411// $subscribent === $this->coordinator) //dodajemy nowego koordynatora) 412// ) { 413// throw new PermissionDeniedException(); 414// } 415// 416// if ($this->model->users->exists($subscribent) && 417// !in_array($subscribent, $this->subscribents_array)) { 418// $this->subscribents_array[$subscribent] = $subscribent; 419// $this->subscribents = implode(',', $this->subscribents_array); 420// return true; 421// } 422// return false; 423// } 424// 425// public function remove_subscribent($subscribent) { 426// if (! ( $this->user_is_coordinator() || 427// $subscribent === $this->model->user_nick) 428// ) { 429// throw new PermissionDeniedException(); 430// } 431// unset($this->subscribents_array[$subscribent]); 432// $this->subscribents = implode(',', $this->subscribents_array); 433// } 434// 435// public function get_subscribents() { 436// return $this->subscribents_array; 437// } 438 439// public function get_participants() { 440// $full_names = []; 441// 442// $involved = array_merge($this->subscribents_array, $this->participants_array); 443// foreach ($involved as $par) { 444// $name = $this->model->users->get_user_full_name($par); 445// if ($name == '') { 446// $full_names[$par] = $par; 447// } else { 448// $full_names[$par] = $name; 449// } 450// } 451// //coordinator on top 452// uksort($full_names, function ($a, $b) use($full_names) { 453// if ($a === $this->coordinator) { 454// return -1; 455// } else if ($b === $this->coordinator) { 456// return 1; 457// } 458// return $full_names[$a] > $full_names[$b]; 459// }); 460// 461// return $full_names; 462// } 463 464// public function is_subscribent($user=NULL) { 465// if ($user === NULL) { 466// $user = $this->model->user_nick; 467// } 468// if (in_array($user, $this->subscribents_array)) { 469// return true; 470// } 471// return false; 472// } 473// 474// public function is_task_executor($user=NULL) { 475// if ($user === NULL) { 476// $user = $this->model->user_nick; 477// } 478// $sth = $this->model->db->prepare('SELECT COUNT(*) FROM tasks 479// WHERE issue=:issue AND executor=:executor'); 480// $sth->execute(array(':issue' => $this->id, ':executor' => $user)); 481// $fetch = $sth->fetch(); 482// if ($fetch[0] === '0') { 483// return false; 484// } else { 485// return true; 486// } 487// } 488// 489// public function is_commentator($user=NULL) { 490// if ($user === NULL) { 491// $user = $this->model->user_nick; 492// } 493// $sth = $this->model->db->prepare('SELECT COUNT(*) FROM commcauses 494// WHERE issue=:issue AND reporter=:reporter'); 495// $sth->execute(array(':issue' => $this->id, ':reporter' => $user)); 496// $fetch = $sth->fetch(); 497// if ($fetch[0] === '0') { 498// return false; 499// } else { 500// return true; 501// } 502// } 503// 504// private $causes_without_tasks = -1; 505// public function causes_without_tasks_count() { 506// if ($this->causes_without_tasks === -1) { 507// $sth = $this->model->db->prepare('SELECT COUNT(*) FROM 508// (SELECT tasks.id 509// FROM commcauses LEFT JOIN tasks ON commcauses.id = tasks.cause 510// WHERE commcauses.type > 0 AND commcauses.issue = ? 511// GROUP BY commcauses.id) 512// WHERE id IS NULL'); 513// $sth->execute(array($this->id)); 514// $count = $sth->fetchColumn(); 515// 516// $this->causes_without_tasks = (int)$count; 517// } 518// return $this->causes_without_tasks; 519// } 520 521 //http://data.agaric.com/capture-all-sent-mail-locally-postfix 522 //https://askubuntu.com/questions/192572/how-do-i-read-local-email-in-thunderbird 523 public function mail_notify($replacements=array(), $users=false) { 524 $plain = io_readFile($this->model->action->localFN('issue-notification')); 525 $html = io_readFile($this->model->action->localFN('issue-notification', 'html')); 526 527 $issue_link = DOKU_URL . 'doku.php?id='.$this->model->action->id('issue', 'id', $this->id); 528 $issue_unsubscribe = DOKU_URL . 'doku.php?id='.$this->model->action->id('issue', 'id', $this->id, 'action', 'unsubscribe'); 529 530 $issue_reps = array( 531 'issue_id' => $this->id, 532 'issue_link' => $issue_link, 533 'issue_unsubscribe' => $issue_unsubscribe, 534 'custom_content' => false, 535 'action_border_color' => 'transparent', 536 'action_color' => 'transparent', 537 ); 538 539 //$replacements can override $issue_reps 540 $rep = array_merge($issue_reps, $replacements); 541 //auto title 542 if (!isset($rep['subject'])) { 543 $rep['subject'] = '#'.$this->id. ' ' .$this->title; 544 } 545 if (!isset($rep['content_html'])) { 546 $rep['content_html'] = $rep['content']; 547 } 548 if (!isset($rep['who_full_name'])) { 549 $rep['who_full_name'] = 550 $this->model->users->get_user_full_name($rep['who']); 551 } 552 553 //format when 554 $rep['when'] = $this->date_format($rep['when']); 555 556 if ($rep['custom_content'] === false) { 557 $html = str_replace('@CONTENT_HTML@', ' 558 <div style="margin: 5px 0;"> 559 <strong>@WHO_FULL_NAME@</strong> <br> 560 <span style="color: #888">@WHEN@</span> 561 </div> 562 @CONTENT_HTML@ 563 ', $html); 564 } 565 566 //we must do it manually becouse Mailer uses htmlspecialchars() 567 $html = str_replace('@CONTENT_HTML@', $rep['content_html'], $html); 568 569 $mailer = new BEZ_Mailer(); 570 $mailer->setBody($plain, $rep, $rep, $html, false); 571 572 if ($users === FALSE) { 573 $users = $this->subscribents_array; 574 unset($users[$this->model->user_nick]); 575 } 576 577 $emails = array_map(function($user) { 578 return $this->model->users->get_user_email($user); 579 }, $users); 580 581 582 $mailer->to($emails); 583 $mailer->subject($rep['subject']); 584 585 $send = $mailer->send(); 586 if ($send === false) { 587 //this may mean empty $emails 588 //throw new Exception("can't send email"); 589 } 590 } 591 592 protected function mail_issue_box_reps($replacements=array()) { 593 $replacements['custom_content'] = true; 594 595 $html = '<h2 style="font-size: 1.2em;">'; 596 $html .= '<a style="font-size:115%" href="@ISSUE_LINK@">#@ISSUE_ID@</a> '; 597 598 if ( ! empty($this->type_string)) { 599 $html .= $this->type_string; 600 } else { 601 $html .= '<i style="color: #777"> '. 602 $this->model->action->getLang('issue_type_no_specified'). 603 '</i>'; 604 } 605 606 $html .= ' ('.$this->state_string.') '; 607 608 $html .= '<span style="color: #777; font-weight: normal; font-size: 90%;">'; 609 $html .= $this->model->action->getLang('coordinator') . ': '; 610 $html .= '<span style="font-weight: bold;">'; 611 612 if ($this->coordinator === '-proposal') { 613 $html .= '<i style="font-weight: normal;">' . 614 $this->model->action->getLang('proposal') . 615 '</i>'; 616 } else { 617 $html .= $this->model->users->get_user_full_name($this->coordinator); 618 } 619 $html .= '</span></span></h2>'; 620 621 $html .= '<h2 style="font-size: 1.2em;border-bottom: 1px solid @ACTION_BORDER_COLOR@">' . $this->title . '</h2>'; 622 623 $html .= $this->description_cache; 624 625 if ($this->state !== '0') { 626 $html .= '<h3 style="font-size:100%; border-bottom: 1px dotted #bbb">'; 627 if ($this->state === '1') { 628 $html .= $this->model->action->getLang('opinion'); 629 } else { 630 $html .= $this->model->action->getLang('reason'); 631 } 632 $html .= '</h3>'; 633 $html .= $this->opinion_cache; 634 } 635 636 $replacements['content_html'] = $html; 637 638 639 switch ($this->priority) { 640 case '0': 641 $replacements['action_color'] = '#F8E8E8'; 642 $replacements['action_border_color'] = '#F0AFAD'; 643 break; 644 case '1': 645 $replacements['action_color'] = '#ffd'; 646 $replacements['action_border_color'] = '#dd9'; 647 break; 648 case '2': 649 $replacements['action_color'] = '#EEF6F0'; 650 $replacements['action_border_color'] = '#B0D2B6'; 651 break; 652 case 'None': 653 $replacements['action_color'] = '#e7f1ff'; 654 $replacements['action_border_color'] = '#a3c8ff'; 655 break; 656 default: 657 $replacements['action_color'] = '#fff'; 658 $replacements['action_border_color'] = '#bbb'; 659 break; 660 } 661 662 return $replacements; 663 } 664 665 public function mail_notify_change_state() { 666 $this->mail_notify($this->mail_issue_box_reps(array( 667 'who' => $this->model->user_nick, 668 'action' => $this->model->action->getLang('mail_mail_notify_change_state_action'), 669 //'subject' => $this->model->action->getLang('mail_mail_notify_change_state_subject') . ' #'.$this->id 670 ))); 671 } 672 673 public function mail_notify_invite($client) { 674 $this->mail_notify($this->mail_issue_box_reps(array( 675 'who' => $this->model->user_nick, 676 'action' => $this->model->action->getLang('mail_mail_notify_invite_action'), 677 //'subject' => $this->model->action->getLang('mail_mail_notify_invite_subject') . ' #'.$this->id 678 )), array($client)); 679 } 680 681 public function mail_inform_coordinator() { 682 $this->mail_notify($this->mail_issue_box_reps(array( 683 'who' => $this->model->user_nick, 684 'action' => $this->model->action->getLang('mail_mail_inform_coordinator_action'), 685 //'subject' => $this->model->action->getLang('mail_mail_inform_coordinator_subject') . ' #'.$this->id 686 )), array($this->coordinator)); 687 } 688 689 public function mail_notify_issue_inactive($users=false) { 690 $this->mail_notify($this->mail_issue_box_reps(array( 691 'who' => '', 692 'action' => $this->model->action->getLang('mail_mail_notify_issue_inactive'), 693 )), $users); 694 } 695} 696