xref: /plugin/bez/mdl/Thread_comment.php (revision ff14b1073c2dab2f863cab3b8baf8b1a01f7993a)
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//	public function get_virtual_columns() {
42//		return array('coordinator', 'tasks_count');
43//	}
44//
45//	public function get_table_name() {
46//		return 'commcauses';
47//	}
48
49    //defaults: isssue, type
50	public function __construct($model, $defaults=array()) {
51		parent::__construct($model, $defaults);
52
53//		$this->validator->set_rules(array(
54//			'issue' => array(array('numeric'), 'NOT NULL'),
55//			'datetime'	=> array(array('sqlite_datetime'), 'NOT NULL'),
56//			'reporter' => array(array('dw_user'), 'NOT NULL'),
57//			'type' => array(array('select', array('0', '1', '2')), 'NOT NULL'),
58//			'content' => array(array('length', 10000), 'NOT NULL'),
59//			'content_cache' => array(array('length', 10000), 'NOT NULL'),
60//
61//			'coordinator' => array(array('dw_user', array('-proposal')), 'NOT NULL')
62//		));
63
64
65
66        $this->validator->set_rules(array(
67            //'type' => array(array('select', array('0', '1', '2')), 'NOT NULL'),
68            'content' => array(array('length', 10000), 'NOT NULL')
69        ));
70
71		//new object
72		if ($this->id === NULL) {
73
74            $this->author = $this->model->user_nick;
75            $this->create_date = date('c');
76            $this->last_modification_date = $this->create_date;
77
78
79            if (!isset($defaults['thread'])) {
80                throw new \Exception('$defaults[thread] not set');
81            }
82            $this->thread = $defaults['thread'];
83			$this->thread_id = $this->thread->id;
84            $this->coordinator = $this->thread->coordinator;
85
86//            //we are coordinator of newly created object
87//            if ($issue->user_is_coordinator()) {
88//                //throws ValidationException
89//                $this->type =
90//                    $this->validator->validate_field('type', $defaults['type']);
91//            } else {
92//                $this->type = '0';
93//            }
94
95//			$this->reporter = $this->model->user_nick;
96//			$this->datetime = $this->sqlite_date();
97		} else {
98            if (isset($defaults['thread']) && $this->thread_id == $defaults['thread']->id) {
99                $this->thread = $defaults['thread'];
100            }
101        }
102
103
104		//set validation
105        if ($this->thread->user_is_coordinator()) {
106            $this->validator->set_rules(
107                array(
108                    'type' => array(
109                        array('select', array('comment', 'cause_real', 'cause_potential', 'closing_comment')),
110                        'NOT NULL')
111                )
112            );
113        }
114	}
115
116//    public function update_cache() {
117//		if ($this->model->acl->get_level() < BEZ_AUTH_ADMIN) {
118//			return false;
119//		}
120//		$this->content_cache = $this->helper->wiki_parse($this->content);
121//	}
122//
123//	public function set_data($data, $filter=NULL) {
124//        $input = array('content', 'type');
125//        $val_data = $this->validator->validate($data, $input);
126//
127//		if ($val_data === false) {
128//			throw new ValidationException('issues',	$this->validator->get_errors());
129//		}
130//
131//        $this->set_property_array($val_data);
132
133//		$this->content_cache = $this->helper->wiki_parse($this->content);
134//    }
135
136    public function set_data($post) {
137        parent::set_data($post);
138        $this->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore);
139    }
140
141//    public function get_meta_fields() {
142//        return array('reporter', 'datetime');
143//    }
144//
145//    public function set_meta($post) {
146//        parent::set_data($post, $this->get_meta_fields());
147//    }
148
149    public function mail_notify_add() {
150//        if ($thread->id !== $this->thread_id) {
151//            throw new Exception('issue object id and commcause->issue does not match');
152//        }
153
154        $rep = array(
155            'content' => $this->content,
156            'content_html' => $this->content_html,
157            'who' => $this->author,
158            'when' => $this->create_date
159        );
160
161        if ($this->type > 0) {
162            $rep['action'] = $this->model->action->getLang('mail_cause_added');
163            $rep['action_color'] = '#ffeedc';
164            $rep['action_border_color'] = '#ddb68d';
165        } else {
166            $rep['action'] = $this->model->action->getLang('mail_comment_added');
167            $rep['action_color'] = 'transparent';
168            $rep['action_border_color'] = '#E5E5E5';
169        }
170
171        $this->thread->mail_notify($rep);
172    }
173}
174