1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5use dokuwiki\plugin\bez\meta\PermissionDeniedException;
6use dokuwiki\plugin\bez\meta\ValidationException;
7
8class Thread_comment extends Entity {
9
10	//real
11	protected $id, $thread_id, $type, $author, $create_date, $last_modification_date, $content, $content_html, $task_count;
12
13	//virtual
14	protected $coordinator;
15
16	/** @var Thread */
17	protected  $thread;
18
19    //protected $parse_int = array('tasks_count');
20	public static function get_columns() {
21		return array('id', 'thread_id', 'type', 'author',
22                     'create_date', 'last_modification_date', 'content', 'content_html', 'task_count');
23	}
24
25    public function __get($property) {
26        if ($property == 'thread') {
27            if ($this->thread_id == null) {
28                return null;
29            }
30            if ($this->thread == null) {
31                $this->thread = $this->model->threadFactory->get_one($this->thread_id);
32            }
33            return $this->thread;
34
35        } elseif ($property == 'coordinator') {
36	        return $this->$property;
37        }
38        return parent::__get($property);
39    }
40
41    //defaults: isssue, type
42	public function __construct($model, $defaults=array()) {
43		parent::__construct($model, $defaults);
44
45        $this->validator->set_rules(array(
46            'content' => array(array('length', 10000), 'NOT NULL'),
47            'type' => array(
48                array('select', array('comment', 'cause')),
49                'NOT NULL')
50        ));
51
52		//new object
53		if ($this->id === NULL) {
54
55            $this->author = $this->model->user_nick;
56            $this->create_date = date('c');
57            $this->last_modification_date = $this->create_date;
58
59
60            if (!isset($defaults['thread'])) {
61                throw new \Exception('$defaults[thread] not set');
62            }
63            $this->thread = $defaults['thread'];
64			$this->thread_id = $this->thread->id;
65            $this->coordinator = $this->thread->coordinator;
66
67            $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
68            if ($this->coordinator == $this->model->user_nick) {
69                $this->acl->grant('type', BEZ_PERMISSION_CHANGE);
70            }
71
72		} else {
73            if (isset($defaults['thread']) && $this->thread_id == $defaults['thread']->id) {
74                $this->thread = $defaults['thread'];
75            }
76
77            //we can change our own comments only when they are "comment"
78            if ($this->author == $this->model->user_nick && $this->type == 'comment') {
79                //we can only delete records when there is no tasks subscribed to issue
80                if ($this->task_count == '0') {
81                    $this->acl->grant('id', BEZ_PERMISSION_DELETE);
82                }
83                $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
84            }
85
86            if ($this->coordinator == $this->model->user_nick) {
87                //we can only delete records when there is no tasks subscribed to issue
88                if ($this->task_count == '0') {
89                    $this->acl->grant('id', BEZ_PERMISSION_DELETE);
90                }
91                $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
92                $this->acl->grant('type', BEZ_PERMISSION_CHANGE);
93            }
94        }
95	}
96
97    public function set_data($post) {
98	    //no all can change type
99        if ($this->acl_of('type') < BEZ_PERMISSION_CHANGE) {
100            unset($post['type']);
101        }
102        parent::set_data($post);
103        $this->purge();
104    }
105
106    protected function html_link_url() {
107        $tpl = $this->model->action->get_tpl();
108        return $tpl->url('thread', 'id', $this->thread_id) . '#k' . $this->id;
109    }
110
111    protected function html_link_content() {
112        return '#k' . $this->id;
113    }
114
115    public function mail_notify_add() {
116        $tpl = $this->model->action->get_tpl();
117
118        $info = array();
119        $html =  p_render('bez_xhtmlmail', p_get_instructions($this->content), $info);
120        $tpl->set('content', $html);
121        $tpl->set('who', $this->author);
122        $tpl->set('when', $this->create_date);
123        if ($this->type == 'comment') {
124            $action = 'mail_comment_added';
125        } else {
126            $action = 'mail_cause_added';
127            $tpl->set('action_border_color', '#ddb68d');
128            $tpl->set('action_background_color', '#ffeedc');
129        }
130        $tpl->set('action', $action);
131        $content = $this->model->action->bez_tpl_include('mail/thread_comment', true);
132
133        $this->thread->mail_notify($content, false, $info['img']);
134    }
135}
136