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