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