1<?php 2 3namespace dokuwiki\plugin\bez\mdl; 4 5//if(!defined('DOKU_INC')) die(); 6 7//require_once 'factory.php'; 8//require_once 'thread.php'; 9 10 11 12use Assetic\Exception\Exception; 13 14class ThreadFactory extends Factory { 15 16// public function __construct($model) { 17// parent::__construct($model); 18 19 /* state_string: 20 0 -> opened 21 1 -> closed 22 2 -> rejected 23 if state = 0 and all tasks are done -> done 24 */ 25// $this->select_query = "SELECT *, 26// (CASE 27// WHEN state = 2 28// THEN '".$this->model->action->getLang('state_rejected')."' 29// WHEN coordinator = '-proposal' 30// THEN '".$this->model->action->getLang('state_proposal')."' 31// WHEN state = 0 AND assigned_tasks_count > 0 32// AND opened_tasks_count = 0 33// THEN '".$this->model->action->getLang('state_done')."' 34// WHEN state = 0 35// THEN '".$this->model->action->getLang('state_opened')."' 36// WHEN state = 1 37// THEN '".$this->model->action->getLang('state_closed')."' 38// END) AS state_string, 39// 40// (CASE 41// WHEN state = 2 42// THEN '2' 43// WHEN coordinator = '-proposal' 44// THEN '-proposal' 45// WHEN state = 0 AND assigned_tasks_count > 0 46// AND opened_tasks_count = 0 47// THEN '-done' 48// WHEN state = 0 49// THEN '0' 50// WHEN state = 1 51// THEN '1' 52// END) AS full_state, 53// 54// (CASE WHEN state = 2 then '3' 55// WHEN task_priority IS NULL THEN 'None' 56// ELSE task_priority 57// END) AS priority 58// 59// FROM (SELECT issues.*, 60// (SELECT COUNT(*) FROM tasks 61// WHERE tasks.issue = issues.id) 62// AS assigned_tasks_count, 63// (SELECT COUNT(*) FROM tasks 64// WHERE tasks.issue = issues.id AND tasks.state = 0) 65// AS opened_tasks_count, 66// (SELECT MIN((CASE WHEN tasks.state > 0 THEN '3' 67// WHEN tasks.plan_date >= date('now', '+1 month') THEN '2' 68// WHEN tasks.plan_date >= date('now') THEN '1' 69// ELSE '0' END)) FROM tasks WHERE tasks.issue = issues.id) 70// AS task_priority, 71// (SELECT SUM(tasks.cost) FROM tasks 72// WHERE tasks.issue = issues.id) 73// AS cost, 74// issuetypes.".$this->model->conf['lang']." AS type_string 75// FROM issues 76// LEFT JOIN issuetypes ON issues.type = issuetypes.id)"; 77// } 78 79 protected function select_query() { 80 return "SELECT thread.*, label.id AS label_id, label.name AS label_name FROM thread 81 LEFT JOIN thread_label ON thread.id = thread_label.thread_id 82 LEFT JOIN label ON label.id = thread_label.label_id"; 83 } 84 85 public function get_years_scope() { 86 $r = $this->model->sqlite->query('SELECT create_date FROM thread ORDER BY id LIMIT 1'); 87 $date = $this->model->sqlite->res2single($r); 88 89 //get only year 90 $first = (int) substr($date, 0, strpos($date, '-')); 91 $last = (int) date('Y'); 92 93 $years = array(); 94 for ($year = $first; $year <= $last; $year++) { 95 $years[] = (string) $year; 96 } 97 return $years; 98 } 99 100 public function initial_save(Entity $thread, $data) { 101 parent::initial_save($thread, $data); 102 103 $thread->set_data($data); 104 $label_ids = array(); 105 if (isset($data['label_id']) && $data['label_id'] != '') { 106 $label_ids[] = $data['label_id']; 107 } 108 try { 109 $this->beginTransaction(); 110 $this->save($thread); 111 112 foreach($label_ids as $label_id) { 113 $thread->add_label($label_id); 114 } 115 116 $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent')); 117 if($thread->coordinator != null) { 118 $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent')); 119 } 120 121 $this->commitTransaction(); 122 } catch(Exception $exception) { 123 $this->rollbackTransaction(); 124 } 125 126 if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) { 127 $thread->mail_inform_coordinator(); 128 } 129 } 130 131 public function update_save(Entity $thread, $data) { 132 parent::update_save($thread, $data); 133 134 $prev_coordinator = $thread->coordinator; 135 $thread->set_data($data); 136 $label_ids = array(); 137 if (isset($data['label_id']) && $data['label_id'] != '') { 138 $label_ids[] = $data['label_id']; 139 } 140 try { 141 $this->beginTransaction(); 142 $this->save($thread); 143 144 $cur_label_ids = array_keys($thread->get_labels()); 145 $labels_to_add = array_diff($label_ids, $cur_label_ids); 146 $labels_to_rem = array_diff($cur_label_ids, $label_ids); 147 148 foreach($labels_to_add as $label_id) { 149 $thread->add_label($label_id); 150 } 151 152 foreach($labels_to_rem as $label_id) { 153 $thread->remove_label($label_id); 154 } 155 156 if($thread->coordinator != null && $thread->coordinator != $prev_coordinator) { 157 $thread->remove_participant_flags($prev_coordinator, array('coordinator')); 158 $thread->set_participant_flags($thread->coordinator, array('subscribent', 'coordinator')); 159 } 160 161 $this->commitTransaction(); 162 } catch(Exception $exception) { 163 $this->rollbackTransaction(); 164 } 165 166 if ($thread->state != 'proposal' && $this->model->user_nick != $thread->coordinator) { 167 $thread->mail_inform_coordinator(); 168 } 169 } 170} 171