xref: /plugin/bez/syntax/nav.php (revision 5c4f5b9586015b9688a72a89d8d79cafe4a6fac2)
1<?php
2/**
3 * Plugin Now: Inserts a timestamp.
4 *
5 */
6
7// must be run within DokuWiki
8if(!defined('DOKU_INC')) die();
9
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once DOKU_PLUGIN.'syntax.php';
12include_once DOKU_PLUGIN."bez/models/issues.php";
13include_once DOKU_PLUGIN."bez/models/causes.php";
14include_once DOKU_PLUGIN."bez/models/tasks.php";
15include_once DOKU_PLUGIN."bez/models/taskactions.php";
16
17include_once DOKU_PLUGIN."bez/mdl/model.php";
18
19/**
20 * All DokuWiki plugins to extend the parser/rendering mechanism
21 * need to inherit from this class
22 */
23class syntax_plugin_bez_nav extends DokuWiki_Syntax_Plugin {
24	private $value = array();
25	private $lang_code = '';
26	private $default_lang = 'pl';
27
28	private $auth, $model, $validator;
29
30    function getPType() { return 'block'; }
31    function getType() { return 'substition'; }
32    function getSort() { return 99; }
33
34
35    function connectTo($mode) {
36		$this->Lexer->addSpecialPattern('~~BEZNAV~~',$mode,'plugin_bez_nav');
37    }
38
39	function __construct() {
40		global $conf, $INFO, $auth;
41
42		$id = $_GET['id'];
43
44		/*usuń : z początku id - link bezwzględny*/
45		if ($id[0] == ':')
46			$id = substr($id, 1);
47
48		$ex = explode(':', $_GET['id']);
49
50		//wielojęzyczność
51		if ($ex[1] == 'bez') {
52			$this->lang_code = $ex[0];
53			$ex = array_slice($ex, 1);
54
55			$old_lang = $conf['lang'];
56			$conf['lang'] = $this->lang_code;
57			$this->setupLocale();
58			$conf['lang'] = $old_lang;
59
60		} else {
61			$this->lang_code = $conf['lang'];
62		}
63
64		for ($i = 0; $i < count($ex); $i += 2)
65			$this->value[urldecode($ex[$i])] = urldecode($ex[$i+1]);
66
67		$this->model = new BEZ_mdl_Model($auth, $INFO['client'], $this->lang_code);
68	}
69
70    function handle($match, $state, $pos, Doku_Handler $handler)
71    {
72		return true;
73    }
74
75    function render($mode, Doku_Renderer $R, $pass) {
76		global $INFO, $auth;
77
78		$helper = $this->loadHelper('bez');
79		if ($mode != 'xhtml' || !$helper->user_viewer()) return false;
80
81        $R->info['cache'] = false;
82
83		$data = array(
84			'bez:start' => array('id' => 'bez:start', 'type' => 'd', 'level' => 1, 'title' => $this->getLang('bez')),
85		);
86
87
88		$data['bez:issues'] = array('id' => 'bez:issues', 'type' => 'd', 'level' => 2, 'title' => $this->getLang('bds_issues'));
89
90		$task_pages = array('issue_tasks', 'task_form', 'issue_task');
91		$cause_pages = array('issue_causes', 'issue_cause', 'cause_form', 'issue_cause_task');
92		$issue_pages = array_merge(array('issue', 'rr', '8d'), $task_pages, $cause_pages);
93
94		if ($this->value['bez'] == 'issues' || $this->value['bez'] == 'issue_report') {
95			if ($helper->user_editor()) {
96				$data['bez:issue_report'] = array('id' => 'bez:issue_report', 'type' => 'f', 'level' => 3, 'title' => $this->getLang('bds_issue_report'));
97			}
98			$data['bez:issues']['open'] = true;
99		}
100		if (in_array($this->value['bez'], $issue_pages) || ($this->value['bez'] == 'issue_report' && isset($this->value['id']))) {
101			if ($helper->user_editor()) {
102				$data['bez:issue_report'] = array('id' => 'bez:issue_report', 'type' => 'f', 'level' => 3, 'title' => $this->getLang('bds_issue_report'));
103			}
104			$data['bez:issues']['open'] = true;
105			$id = (int)$this->value[id];
106
107			$isso = new Issues();
108			$issue_opened = $isso->opened($id);
109			$issue_proposal = $isso->is_proposal($id);
110
111			$tasko = new Tasks();
112			$causo = new Causes();
113
114			$causes = $causo->get($id);
115			$tasks = $tasko->get_corrections_ids($id);
116
117			$type = 'd';
118			if (count($causes) + count($tasks) == 0) {
119				$type = 'f';
120			}
121
122			$pid = "bez:issue:id:$id";
123			$data[$pid] = array('id' => $pid, 'type' => $type, 'level' => 3,
124												'title' => "#$id", 'open' => true);
125
126			if (count($tasks) > 0) {
127				$pid = "bez:issue_tasks:id:$id";
128				$data[$pid] = array('id' => $pid, 'type' => 'd', 'level' => 4,
129									'title' => $this->getLang('correction_nav'));
130				if (in_array($this->value['bez'], $task_pages) && $this->value[cid] == '') {
131					$data[$pid][open] = true;
132
133					if ($issue_opened) {
134						$rpid = "bez:task_form:id:$id";
135						if ($helper->user_admin() && !$issue_proposal)
136							$data[$rpid] = array('id' => $rpid, 'type' => 'f', 'level' => 5,
137												'title' => $this->getLang('add_correction'));
138					}
139					foreach ($tasks as $r) {
140						$rpid = "bez:issue_task:id:$id:tid:$r[id]";
141						$data[$rpid] = array('id' => $rpid, 'type' => 'f', 'level' => 5,
142											'title' => '#z'.$r['id']);
143					}
144				}
145			}
146
147			if (count($causes) > 0) {
148				$pid = "bez:issue_causes:id:$id";
149				$data[$pid] = array('id' => $pid, 'type' => 'd', 'level' => 4,
150									'title' => $this->getLang('causes'));
151				if (in_array($this->value['bez'], $cause_pages) || $this->value[cid] != '') {
152					$data[$pid][open] = true;
153					if ($issue_opened) {
154						$rpid = "bez:cause_form:id:$id";
155						if ($helper->user_admin() && !$issue_proposal)
156							$data[$rpid] = array('id' => $rpid, 'type' => 'f', 'level' => 5,
157												'title' => $this->getLang('add_cause'));
158					}
159
160					foreach ($causes as $r) {
161						$rpid = "bez:issue_cause:id:$id:cid:$r[id]";
162						$data[$rpid] = array('id' => $rpid, 'type' => 'd', 'level' => 5,
163											'title' => '#p'.$r[id]);
164
165						if ((int)$this->value['cid'] == $r[id]) {
166							$data[$rpid][open] = true;
167
168							$pres = $tasko->get($id, $r['id']);
169							foreach ($pres as $pr) {
170								$rptid = "bez:issue_cause_task:id:$id:cid:$r[id]:tid:$pr[id]";
171								$data[$rptid] = array('id' => $rptid, 'type' => 'f', 'level' => 6,
172											'title' => '#z'.$pr[id]);
173
174							}
175						}
176					}
177
178				}
179			}
180
181		}
182
183
184		$data['bez:tasks'] = array('id' => 'bez:tasks', 'type' => 'd', 'level' => 2, 'title' => $this->getLang('bez_tasks'));
185
186		if ($this->value['bez'] == 'tasks' || $this->value['bez'] == 'show_task'
187			|| $this->value['bez'] == 'task_form_plan'
188			|| $this->value['bez'] == 'issue_task'
189			|| $this->value['bez'] == 'task_form'
190			|| $this->value['bez'] == 'task_report'
191			|| $this->value['bez'] == 'issue_cause_task') {
192			$data['bez:tasks']['open'] = true;
193
194			if (isset($this->value['year']))
195				$year = $this->value['year'];
196			else
197				$year = date('Y');
198
199
200			if (isset($this->value['tid'])) {
201				$tasko = new Tasks();
202				$this->value['tasktype']  = $tasko->get_type($this->value['tid']);
203			}
204
205
206			$page_id = "bez:tasks:tasktype:-none:year:$year";
207			if (isset($this->value['tid']) && $this->value['tasktype'] == '') {
208				$data[$page_id] = array('id' => $page_id, 'type' => 'd', 'level' => 3, 'title' => $this->getLang('tasks_no_type'), 'open' => true);
209
210				$page_id = 'bez:show_task:tid:'.$this->value['tid'];
211				//$page_id = $_GET['id'];
212				$data[$page_id.':perspective:task'] = array('id' => $page_id, 'type' => 'f', 'level' => 4, 'title' => '#z'.$this->value['tid']);
213
214			} else {
215				$data[$page_id] = array('id' => $page_id, 'type' => 'f', 'level' => 3, 'title' => $this->getLang('tasks_no_type'));
216			}
217
218			$tasktypes = $this->model->tasktypes->get_all();
219			foreach ($tasktypes as $tasktype) {
220				$page_id = "bez:tasks:tasktype:".$tasktype->id.":year:$year";
221
222				if ($this->value['tasktype'] == $tasktype->id) {
223					$data[$page_id] = array('id' => $page_id, 'type' => 'd', 'level' => 3, 'title' => $tasktype->type, 'open' => true);
224
225					if ($tasktype->get_level() >= 15) {
226						$report_id = "bez:task_report:tasktype:".$tasktype->id;
227						$data[$report_id] = array('id' => $report_id, 'type' => 'f', 'level' => 4, 'title' => $this->getLang('bds_task_report'));
228					}
229					if (isset($this->value['tid'])) {
230						$page_id = 'bez:show_task:tid:'.$this->value['tid'];
231						//$page_id = $_GET['id'];
232						$data[$page_id.':perspective:task'] = array('id' => $page_id, 'type' => 'f', 'level' => 4, 'title' => '#z'.$this->value['tid']);
233					}
234				} else {
235					$data[$page_id] = array('id' => $page_id, 'type' => 'f', 'level' => 3, 'title' => $tasktype->type);
236				}
237			}
238		}
239
240		$isso = new Issues();
241		$year_now = (int)date('Y');
242		$mon_now = (int)date('n');
243
244		$data['bez:report'] = array('id' => 'bez:report', 'type' => 'd', 'level' => 2, 'title' => $this->getLang('report'));
245		if ($this->value['bez'] == 'report') {
246			$data['bez:report']['open'] = true;
247
248			$oldest = $isso->get_oldest_close_date();
249			$year_old = (int)date('Y', $oldest);
250			$mon_old = (int)date('n', $oldest);
251
252			$mon = $mon_old;
253			for ($year = $year_old; $year <= $year_now; $year++) {
254
255				$y_key = 'bez:report:year:'.$year;
256				$data[$y_key] = array('id' => $y_key, 'type' => 'd', 'level' => 3, 'title' => $year);
257
258				if (isset($this->value['year']) && (int)$this->value['year'] == $year) {
259					$data['bez:report:year:'.$year]['open'] = true;
260
261					if ($year == $year_now)
262						$mon_max = $mon_now;
263					else
264						$mon_max = 12;
265					for ( ; $mon <= $mon_max; $mon++) {
266						$m_key = $y_key.':month:'.$mon;
267						$data[$m_key] = array('id' => $m_key, 'type' => 'f', 'level' => 4,
268						'title' => $mon < 10 ? '0'.$mon : $mon);
269					}
270				}
271				$mon = 1;
272			}
273		}
274
275
276
277		if (isset($this->value['bez'])) {
278			$data['bez:start']['open'] = true;
279		} else {
280			$data['bez:start']['open'] = false;
281			array_splice($data, 1);
282		}
283
284		if ($helper->user_admin() && $data['bez:start']['open'] == true) {
285			$data['bez:types'] = array('id' => 'bez:types', 'type' => 'f', 'level' => 2, 'title' =>
286				$this->getLang('types_manage'));
287			$data['bez:root_causes'] = array('id' => 'bez:root_causes', 'type' => 'f', 'level' => 2, 'title' =>
288				$this->getLang('root_causes'));
289			$data['bez:task_types'] = array('id' => 'bez:task_types', 'type' => 'f', 'level' => 2, 'title' =>
290				$this->getLang('task_types'));
291		}
292
293        $R->doc .= '<div class="plugin__bez">';
294        $R->doc .= html_buildlist($data,'idx',array($this,'_list'),array($this,'_li'));
295        $R->doc .= '</div>';
296
297		return true;
298	}
299
300	function _bezlink($id, $title) {
301		//$uri = wl($id);
302		$uri = DOKU_URL . 'doku.php?id='.$id;
303		return '<a href="'.$uri.'">'.($title).'</a>';
304	}
305
306    function _list($item){
307
308		$ex = explode(':', $item['id']);
309
310		for ($i = 0; $i < count($ex); $i += 2)
311			$item_value[urldecode($ex[$i])] = urldecode($ex[$i+1]);
312
313		//pola brane pod uwagę przy określaniu aktualnej strony
314		$fields = array('bez', 'tid', 'cid', 'tasktype', 'taskstate');
315		if ($item_value['bez'] == 'report' || $item_value['bez'] == 'report_open') {
316			$fields[] = 'month';
317			$fields[] = 'year';
318		}
319		if ($this->value[bez] == 'task_form' && isset($this->value[cid]))
320			unset($fields[0]);
321
322		$actual_page = true;
323		foreach ($fields as $field)
324			if ($item_value[$field] != $this->value[$field])
325				$actual_page = false;
326
327		//specjalny hak dla zadań, boję się ruszać całej procedury
328		if ($item_value['bez'] == 'issue_task' ||
329			$item_value['bez'] == 'issue_cause_task' ||
330			$item_value['bez'] == 'task_form' ||
331			$item_value['bez'] == 'show_task')
332				if ($item_value['tid'] == $this->value['tid'])
333					$actual_page = true;
334
335        if(($item['type'] == 'd' && $item['open']) ||  $actual_page) {
336			$id = $item['id'];
337			if ($this->lang_code != $this->default_lang)
338				$id = $this->lang_code.':'.$id;
339            return '<strong>'.$this->_bezlink($id, $item['title']).'</strong>';
340        }else{
341			$id = $item['id'];
342			if ($this->lang_code != $this->default_lang)
343				$id = $this->lang_code.':'.$id;
344            return $this->_bezlink($id, $item['title']);
345        }
346
347    }
348
349    function _li($item){
350        if($item['type'] == "f"){
351            return '<li class="level'.$item['level'].'">';
352        }elseif($item['open']){
353            return '<li class="open">';
354        }else{
355            return '<li class="closed">';
356        }
357    }
358}
359