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