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