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(Thread $thread, $data, $label_ids=array()) { 101 if ($thread->id != NULL) { 102 throw new \Exception('row already saved. use update_save'); 103 } 104 105 $thread->set_data($data); 106 $label_ids = array(); 107 if (isset($data['label_id']) && $data['label_id'] != '') { 108 $label_ids[] = $data['label_id']; 109 } 110 try { 111 $this->beginTransaction(); 112 parent::save($thread); 113 114 foreach($label_ids as $label_id) { 115 $thread->add_label($label_id); 116 } 117 118 $thread->set_participant_flags($thread->original_poster, array('original_poster', 'subscribent')); 119 if($thread->coordinator != null) { 120 $thread->set_participant_flags($thread->coordinator, array('coordinator', 'subscribent')); 121 } 122 123 $this->model->threadFactory->commitTransaction(); 124 } catch(Exception $exception) { 125 $this->model->threadFactory->rollbackTransaction(); 126 } 127 } 128 129 public function update_save(Thread $thread, $data, $label_ids=array()) { 130 if ($thread->id == NULL) { 131 throw new \Exception('row not saved. use initial_save()'); 132 } 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 parent::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->model->threadFactory->commitTransaction(); 162 } catch(Exception $exception) { 163 $this->model->threadFactory->rollbackTransaction(); 164 } 165 } 166} 167