xref: /plugin/bez/mdl/Task.php (revision b331b8925cd7271407de49196821e2fbf4786dba)
1<?php
2
3namespace dokuwiki\plugin\bez\mdl;
4
5use dokuwiki\plugin\bez\meta\Mailer;
6use dokuwiki\plugin\bez\meta\PermissionDeniedException;
7use dokuwiki\plugin\bez\meta\ValidationException;
8use dokuwiki\plugin\struct\types\DateTime;
9
10class Task extends Entity {
11
12    protected $id;
13
14	protected $original_poster, $assignee, $closed_by;
15
16	protected $private, $lock;
17
18	protected $state, $type;
19
20	protected $create_date, $last_activity_date, $last_modification_date, $close_date;
21
22	protected $cost, $plan_date, $all_day_event, $start_time, $finish_time;
23
24	protected $content, $content_html;
25
26	protected $thread_id, $thread_comment_id, $task_program_id;
27
28	/** @var \dokuwiki\plugin\bez\mdl\Thread */
29	protected $thread;
30
31	/** @var Thread_comment */
32	protected $thread_comment;
33
34	//virtual
35    protected $task_program_name, $priority, $coordinator;
36
37	public static function get_columns() {
38		return array('id',
39            'original_poster', 'assignee', 'closed_by',
40            'private', 'lock',
41            'state', 'type',
42            'create_date', 'last_activity_date', 'last_modification_date', 'close_date',
43            'cost', 'plan_date', 'all_day_event', 'start_time', 'finish_time',
44            'content', 'content_html',
45            'thread_id', 'thread_comment_id', 'task_program_id');
46	}
47
48	public static function get_acl_columns() {
49	    return array_merge(parent::get_acl_columns(), array('participants'));
50    }
51
52	public static function get_types() {
53	    return array('correction', 'corrective', 'preventive', 'program');
54    }
55
56    public static function get_states() {
57        return array('opened', 'done');
58    }
59
60    public function __get($property) {
61        if ($property == 'thread') {
62            if ($this->thread_id == null) {
63                return null;
64            }
65            if ($this->thread == null) {
66                $this->thread = $this->model->threadFactory->get_one($this->thread_id);
67            }
68            return $this->thread;
69
70        } elseif($property == 'thread_comment') {
71            if ($this->thread_comment_id == null) {
72                return null;
73            }
74            if ($this->thread_comment == null) {
75                $this->thread_comment = $this->model->thread_commentFactory->get_one($this->thread_comment_id);
76            }
77            return $this->thread_comment;
78
79        } elseif($property == 'priority' || $property == 'coordinator' || $property == 'task_program_name') {
80            return $this->$property;
81        }
82        return parent::__get($property);
83    }
84
85	public function __construct($model, $defaults=array()) {
86		parent::__construct($model, $defaults);
87
88		$this->validator->set_rules(array(
89            'assignee' => array(array('dw_user'), 'NOT NULL'),
90            'cost' => array(array('numeric'), 'NULL'),
91			'plan_date' => array(array('iso_date'), 'NOT NULL'),
92			'all_day_event' => array(array('select', array('0', '1')), 'NOT NULL'),
93			'start_time' => array(array('time'), 'NULL'),
94			'finish_time' => array(array('time'), 'NULL'),
95            'content' => array(array('length', 10000), 'NOT NULL'),
96            'thread_comment_id' => array(array('numeric'), 'NULL'),
97            'task_program_id' => array(array('numeric'), 'NULL')
98		));
99
100		//we've created empty object
101		if ($this->id === NULL) {
102            $this->original_poster = $this->model->user_nick;
103            $this->create_date = date('c');
104            $this->last_activity_date = $this->create_date;
105            $this->last_modification_date = $this->create_date;
106
107            $this->state = 'opened';
108
109            if (isset($defaults['thread'])) {
110                $this->thread = $defaults['thread'];
111                $this->thread_id = $this->thread->id;
112                $this->coordinator = $this->thread->coordinator;
113
114                if ($this->thread->private == '1') {
115                    $this->private = '1';
116                }
117
118                $this->type = 'correction';
119
120                if (isset($defaults['thread_comment'])) {
121                    $this->thread_comment = $defaults['thread_comment'];
122                    $this->thread_comment_id = $this->thread_comment->id;
123
124                    if ($this->thread_comment->type == 'cause_real') {
125                        $this->type = 'corrective';
126                    } else {
127                        $this->type = 'preventive';
128                    }
129                }
130            } else {
131                $this->type = 'program';
132            }
133
134
135            if ($this->thread_id == '') {
136                $this->validator->set_rules(array(
137                                                'task_program_id' => array(array('numeric'), 'NOT NULL'),
138                                            ));
139                //this field is unused in program tasks
140                $this->validator->delete_rule('thread_comment_id');
141            }
142
143            //everyone can report their own program tasks
144            if ($this->type == 'program') {
145                $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
146                $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE);
147                $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE);
148                $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE);
149                $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE);
150                $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE);
151                $this->acl->grant('cost', BEZ_PERMISSION_CHANGE);
152            }
153
154            if ($this->type == 'program' && $this->model->get_level() >= BEZ_AUTH_LEADER) {
155                $this->acl->grant('assignee', BEZ_PERMISSION_CHANGE);
156                $this->acl->grant('participants', BEZ_PERMISSION_CHANGE);
157            }
158
159            if ($this->type != 'program' && $this->coordinator == $this->model->user_nick) {
160                $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
161                $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE);
162                $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE);
163                $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE);
164                $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE);
165                $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE);
166                $this->acl->grant('cost', BEZ_PERMISSION_CHANGE);
167
168                $this->acl->grant('assignee', BEZ_PERMISSION_CHANGE);
169                $this->acl->grant('participants', BEZ_PERMISSION_CHANGE);
170            }
171
172        //we get object form db
173		} else {
174            if (isset($defaults['thread']) && $this->thread_id == $defaults['thread']->id) {
175                $this->thread = $defaults['thread'];
176            }
177
178            if (isset($defaults['thread_comment']) && $this->thread_comment_id == $defaults['thread_comment']->id) {
179                $this->thread_comment = $defaults['thread_comment'];
180            }
181
182            if ($this->thread_id == '') {
183                $this->validator->set_rules(array(
184                                                'task_program_id' => array(array('numeric'), 'NOT NULL'),
185                                            ));
186                //this field is unused in program tasks
187                $this->validator->delete_rule('thread_comment_id');
188            }
189
190            //private tasks
191            if ($this->model->level < BEZ_AUTH_ADMIN && $this->private == '1') {
192                if ($this->get_participant($this->model->user_nick) === false &&
193                    ($this->thread_id != '' && $this->__get('thread')->get_participant($this->model->user_nick) === false)) {
194                    $this->acl->revoke(self::get_select_columns(), BEZ_AUTH_LEADER);
195                    return;
196                }
197            }
198
199            //user can close their tasks
200            if ($this->assignee == $this->model->user_nick || $this->model->get_level() >= BEZ_AUTH_LEADER) {
201                $this->acl->grant('state', BEZ_PERMISSION_CHANGE);
202            }
203
204            if ($this->type == 'program' && $this->original_poster == $this->model->user_nick) {
205                $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
206                $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE);
207                $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE);
208                $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE);
209                $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE);
210                $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE);
211                $this->acl->grant('cost', BEZ_PERMISSION_CHANGE);
212            }
213
214            if (($this->type != 'program' && $this->coordinator == $this->model->user_nick) ||
215                ($this->model->get_level() >= BEZ_AUTH_LEADER)) {
216                $this->acl->grant('content', BEZ_PERMISSION_CHANGE);
217                $this->acl->grant('plan_date', BEZ_PERMISSION_CHANGE);
218                $this->acl->grant('start_time', BEZ_PERMISSION_CHANGE);
219                $this->acl->grant('finish_time', BEZ_PERMISSION_CHANGE);
220                $this->acl->grant('all_day_event', BEZ_PERMISSION_CHANGE);
221                $this->acl->grant('task_program_id', BEZ_PERMISSION_CHANGE);
222                $this->acl->grant('cost', BEZ_PERMISSION_CHANGE);
223
224                $this->acl->grant('assignee', BEZ_PERMISSION_CHANGE);
225                $this->acl->grant('participants', BEZ_PERMISSION_CHANGE);
226                $this->acl->grant('state', BEZ_PERMISSION_CHANGE);
227            }
228        }
229    }
230
231    public function update_virutal() {
232        if ($this->state == 'done') {
233            $this->priority = '';
234        } else {
235            $now = date('Y-m-d');
236            $plus_1_month = date('Y-m-d', strtotime('+1 month'));
237
238            if ($this->plan_date >= $plus_1_month) {
239                $this->priority = '0';
240            } elseif ($this->plan_date >= $now) {
241                $this->priority = '1';
242            } else {
243                $this->priority = '2';
244            }
245        }
246    }
247
248	public function set_data($post, $filter=NULL) {
249        //all day event
250        if (!isset($post['all_day_event'])) {
251            $post['all_day_event'] = '0';
252        }
253
254        parent::set_data($post);
255
256        $this->content_html = p_render('xhtml',p_get_instructions($this->content), $ignore);
257
258        if (!isset($post['assignee'])) {
259            $this->assignee = $this->model->user_nick;
260        }
261
262        //update dates
263        $this->last_modification_date = date('c');
264        $this->last_activity_date = $this->last_modification_date;
265
266        //update virtual
267        $this->update_virutal();
268
269		return true;
270	}
271
272    public function set_state($state) {
273	    if ($this->acl_of('state') < BEZ_PERMISSION_CHANGE) {
274	        throw new PermissionDeniedException();
275        }
276
277        if (!in_array($state, array('opened', 'done'))) {
278	        throw new ValidationException('task', array('sholud be opened or done'));
279        }
280
281        //nothing to do
282        if ($state == $this->state) {
283	        return;
284        }
285
286        if ($state == 'done') {
287            $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=?, closed_by=?, close_date=? WHERE id=?",
288                $state,
289                $this->model->user_nick,
290                date('c'),
291                $this->id);
292        //reopen the task
293        } else {
294            $this->model->sqlite->query("UPDATE {$this->get_table_name()} SET state=? WHERE id=?", $state, $this->id);
295        }
296
297        $this->state = $state;
298    }
299
300    public function update_last_activity() {
301        $this->last_activity_date = date('c');
302        $this->model->sqlite->query('UPDATE task SET last_activity_date=? WHERE id=?',
303                                    $this->last_activity_date, $this->id);
304    }
305
306    public function can_add_comments() {
307        if ($this->thread_id != '' && $this->thread->state == 'closed') {
308            return false;
309        }
310
311        if ($this->state == 'opened' ||
312            ($this->state == 'done' &&
313                $this->acl_of('state') >= BEZ_PERMISSION_CHANGE)) {
314            return true;
315        }
316
317        return false;
318    }
319
320    public function can_add_participants() {
321        return in_array($this->state, array('opened'));
322    }
323
324    public function get_participants($filter='') {
325        if ($this->id === NULL) {
326            return array();
327        }
328
329        $sql = 'SELECT * FROM task_participant WHERE';
330        $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent');
331        if ($filter != '') {
332            if (!in_array($filter, $possible_flags)) {
333                throw new \Exception("unknown flag $filter");
334            }
335            $sql .= " $filter=1 AND";
336        }
337        $sql .= ' task_id=? AND removed=0 ORDER BY user_id';
338
339        $r = $this->model->sqlite->query($sql, $this->id);
340        $pars = $this->model->sqlite->res2arr($r);
341        $participants = array();
342        foreach ($pars as $par) {
343            $participants[$par['user_id']] = $par;
344        }
345
346        return $participants;
347    }
348
349    public function get_participant($user_id, $can_be_removed=false) {
350        if ($this->id === NULL) {
351            return array();
352        }
353
354        $q = 'SELECT * FROM task_participant WHERE task_id=? AND user_id=?';
355        if (!$can_be_removed) {
356            $q .= ' AND removed=0';
357        }
358        $r = $this->model->sqlite->query($q, $this->id, $user_id);
359        $par = $this->model->sqlite->res2row($r);
360        if (!is_array($par)) {
361            return false;
362        }
363
364        return $par;
365    }
366
367    public function is_subscribent($user_id=null) {
368        if ($user_id == null) {
369            $user_id = $this->model->user_nick;
370        }
371        $par = $this->get_participant($user_id);
372        if ($par['subscribent'] == 1) {
373            return true;
374        }
375        return false;
376    }
377
378    public function remove_participant_flags($user_id, $flags) {
379        //thread not saved yet
380        if ($this->id === NULL) {
381            throw new \Exception('cannot remove flags from not saved thread');
382        }
383
384        $participant = $this->get_participant($user_id, true);
385        if ($participant === false) {
386            throw new ConsistencyViolationException("$user_id isn't participant");
387        }
388
389        $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent');
390        if (array_intersect($flags, $possible_flags) != $flags) {
391            throw new \Exception('unknown flags');
392        }
393
394        $set = implode(',', array_map(function ($v) { return "$v=0"; }, $flags));
395
396        $sql = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?";
397        $this->model->sqlite->query($sql, $this->id, $user_id);
398
399    }
400
401    public function set_participant_flags($user_id, $flags=array()) {
402        //thread not saved yet
403        if ($this->id === NULL) {
404            throw new \Exception('cannot add flags to not saved thread');
405        }
406
407        //validate user
408        if (!$this->model->userFactory->exists($user_id)) {
409            throw new \Exception("$user_id isn't dokuwiki user");
410        }
411
412        $possible_flags = array('original_poster', 'assignee', 'commentator', 'subscribent');
413        if (array_intersect($flags, $possible_flags) != $flags) {
414            throw new \Exception('unknown flags');
415        }
416
417        $participant = $this->get_participant($user_id, true);
418        if ($participant == false) {
419            $participant = array_fill_keys($possible_flags, 0);
420
421            $participant['task_id'] = $this->id;
422            $participant['user_id'] = $user_id;
423            $participant['added_by'] = $this->model->user_nick;
424            $participant['added_date'] = date('c');
425
426            $values = array_merge($participant, array_fill_keys($flags, 1));
427            $this->model->sqlite->storeEntry('task_participant', $values);
428        } else {
429            $set = implode(',', array_map(function($flag) { return "$flag=1"; }, $flags));
430
431            if ($participant['removed'] == '1') {
432                $set .= ',removed=0';
433            }
434
435            $q = "UPDATE task_participant SET $set WHERE task_id=? AND user_id=?";
436            $this->model->sqlite->query($q, $this->id, $user_id);
437        }
438    }
439
440    public function remove_participant($user_id) {
441        //thread not saved yet
442        if ($this->id === NULL) {
443            throw new \Exception('cannot remove flags from not saved thread');
444        }
445
446        $participant = $this->get_participant($user_id);
447        if ($participant === false) {
448            throw new ConsistencyViolationException("$user_id isn't participant");
449        }
450
451        if ($participant['assignee'] == '1') {
452            throw new ConsistencyViolationException("cannot remove assignee");
453        }
454
455        $q = "UPDATE task_participant SET removed=1 WHERE task_id=? AND user_id=?";
456        $this->model->sqlite->query($q, $this->id, $user_id);
457
458    }
459
460    public function invite($client) {
461        $this->set_participant_flags($client, array('subscribent'));
462        $this->mail_notify_invite($client);
463    }
464
465    public function mail_notify($content, $users=false, $attachedImages=array()) {
466        $mailer = new \Mailer();
467        $mailer->setBody($content, array(), array(), $content, false);
468
469        if ($users === FALSE) {
470            $users = $this->get_participants('subscribent');
471
472            //don't notify current user
473            unset($users[$this->model->user_nick]);
474        }
475
476        $emails = array_map(function($user) {
477            if (is_array($user)) {
478                $user = $user['user_id'];
479            }
480            return $this->model->userFactory->get_user_email($user);
481        }, $users);
482
483        $mailer->to($emails);
484        $mailer->subject('#z'.$this->id. ' ' . $this->task_program_name);
485
486        //add images
487        foreach ($attachedImages as $img) {
488            $mailer->attachFile($img['path'], $img['mime'], $img['name'], $img['embed']);
489        }
490
491        $send = $mailer->send();
492        if ($send === false) {
493            //this may mean empty $emails
494            //throw new Exception("can't send email");
495        }
496    }
497
498    public function mail_task_box(&$attachedImages) {
499        $tpl = $this->model->action->get_tpl();
500
501        //render style
502        $less = new \lessc();
503        $less->addImportDir(DOKU_PLUGIN . 'bez/style/');
504        $style = $less->compileFile(DOKU_PLUGIN . 'bez/style/task.less');
505
506        //render content for mail
507        $old_content_html = $this->content_html;
508        $this->content_html = p_render('bez_xhtmlmail', p_get_instructions($this->content), $info);
509        $attachedImages = array_merge($attachedImages, $info['img']);
510
511        $tpl->set('task', $this);
512        $tpl->set('style', $style);
513        $tpl->set('no_actions', true);
514        $task_box = $this->model->action->bez_tpl_include('task_box', true);
515
516        $this->content_html = $old_content_html;
517
518        return $task_box;
519    }
520
521    public function mail_task(&$attachedImages) {
522        $tpl = $this->model->action->get_tpl();
523
524        $task_box = $this->mail_task_box($attachedImages);
525        $tpl->set('content', $task_box);
526        $content = $this->model->action->bez_tpl_include('mail/task', true);
527
528        return $content;
529    }
530
531    public function mail_notify_assignee() {
532        $tpl = $this->model->action->get_tpl();
533
534        //we don't want who
535        $tpl->set('who', $this->model->user_nick);
536        $tpl->set('action', 'mail_task_assignee');
537        $attachedImages = array();
538        $content = $this->mail_task($attachedImages);
539        $this->mail_notify($content, array($this->assignee), $attachedImages);
540    }
541
542    public function mail_notify_remind($users=false) {
543        $tpl = $this->model->action->get_tpl();
544
545        //we don't want who
546        $tpl->set('who', '');
547        $tpl->set('action', 'mail_task_remind');
548        $attachedImages = array();
549        $content = $this->mail_task($attachedImages);
550        $this->mail_notify($content, $users, $attachedImages);
551    }
552
553    public function mail_notify_invite($client) {
554        $users = array($client);
555        $tpl = $this->model->action->get_tpl();
556
557        //we don't want who
558        $tpl->set('who', $this->model->user_nick);
559        $tpl->set('action', 'mail_task_invite');
560        $attachedImages = array();
561        $content = $this->mail_task($attachedImages);
562        $this->mail_notify($content, $users, $attachedImages);
563    }
564
565    public function mail_notify_change_state($action='') {
566        $tpl = $this->model->action->get_tpl();
567
568        $tpl->set('who', $this->model->user_nick);
569        $tpl->set('action', $action);
570        $attachedImages = array();
571        $content = $this->mail_task($attachedImages);
572        $this->mail_notify($content, false, $attachedImages);
573    }
574
575}
576