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