1<?php
2/******************************************************************************
3*  improg Plugin */
4if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
5if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
6require_once(DOKU_PLUGIN.'syntax.php');
7
8  function improgcmp($a, $b) {
9	if ($a['improg_name'] == $b['improg_name']) {
10		return 0;
11	}
12	return ($a['improg_name'] < $b['improg_name']) ? -1 : 1;
13	}
14
15// All DokuWiki plugins to extend the parser/rendering mechanism need to inherit from this class
16
17class syntax_plugin_improg extends DokuWiki_Syntax_Plugin
18{
19	//https://www.dokuwiki.org/devel:plugin_file_structure
20	function getInfo(){ return confToHash(dirname(__FILE__).'/plugin.info.txt');}
21	function getType(){ return 'substition';}
22	function getPType(){ return 'block';}
23	function getSort(){ return 168;}
24	function connectTo($mode){ $this->Lexer->addSpecialPattern('\{\(improg>[^}]*?\)\}',$mode,'plugin_improg');}
25	function handle($match, $state, $pos, Doku_Handler $handler){
26		$match = substr($match,9,-2);
27		$data = array();
28		$params = explode('|',$match);
29		foreach($params as $param){
30			$splitparam = explode('=',$param);
31			if ($splitparam[1] != '') {
32                if ($splitparam[0] == 'value') {
33                    $data['improg_value'] = $splitparam[1];	// Umsetzungswert
34                } else if ($splitparam[0] == 'type') {
35                    $data['improg_type'] = $splitparam[1];
36                } else if ($splitparam[0]=='ns') {
37					$data['improg_ns'] = $splitparam[1]; // Verzeichnis
38				} else if ($splitparam[0]=='name') {
39					$data['improg_name'] = $splitparam[1];  // descriptive item name
40					$needles = array(":","/","\\");
41					$data['improg_name'] = str_replace($needles, "_", $data['improg_name']);
42				}
43			}
44		}
45		return $data;
46	}
47/******************************************************************************/
48/* Create output
49*/
50	function render($mode, Doku_Renderer $renderer, $data) {
51		global $ID;
52		global $auth;
53		global $lang;
54		global $conf;
55		if ($mode == 'xhtml'){
56			$renderer->info['cache']   = false;
57			$improg_value		= trim($data['improg_value']);
58			$improg_type		= trim($data['improg_type']);
59			$improg_ns			= trim($data['improg_ns']);
60			/******************************************************************************/
61			// Rating page
62			if ($improg_type=="improg") {
63			  // Assign star image
64				$improg_stars = $this->assign_star_image($improg_value);
65				$ret = '<table><tr>
66					<td style="padding-top:.4em;background-color:#fbfaf9;font-weight:bold">Umsetzungsfortschritt</td>
67					<td>
68					<div style="display:inline;margin-left:auto; margin-right:0;margin-top:-2px">
69					<div style="display:table-cell; text-align:left">
70					<img src="'.$improg_stars.'" alt="'. $improg_stars.' Sterne" />';
71				$ret .= '</div></div></td></tr></table>';
72			} elseif ($improg_type=="stat") {
73				global $conf;
74				$delim2 = ".txt";
75				clearstatcache();
76				if(strlen($improg_ns)>1) {
77					if(is_file($conf['datadir']."/".str_replace(":","/",$improg_ns))) {
78						$listPages = array($conf['datadir']."/".str_replace(":","/",$improg_ns));
79					}
80					else {
81						$r_datadir = $conf['datadir']."/".str_replace(":","/",$improg_ns);
82						$listPages = $this->list_rec_files_in_array($r_datadir, $delim2, $params_array);
83					}
84			} else {
85				$r_datadir = $conf['datadir'];
86				$listPages = $this->list_rec_files_in_array($r_datadir, $delim2, $params_array);
87			}
88			$returns = array();
89			if ($improg_type=="stat") {
90				foreach($listPages as &$page_filepath) {
91					$body = file_get_contents($page_filepath);
92					$links = array();
93
94					// define('LINK_PATTERN', "/\{\{.*\}\}/");
95					$body = preg_replace('/<code.*?\>.*?<\/code>/si', '', $body);
96					define('LINK_PATTERN', "(improg>[^}]*)");
97					if( preg_match(LINK_PATTERN, $body) ) {
98					   preg_match_all(LINK_PATTERN, $body, $links);
99					}
100
101                    if (count($links) > 0) {
102                        foreach ($links[0] as $link) {
103                            $match = substr($link, 7,-1);
104                            $params = explode('|', $match);
105                            $data = array();
106                            $data['improg_basename'] = basename($page_filepath);
107                            $data['improg_file'] = $this->__savedir($page_filepath);
108                            foreach ($params as $param) {
109                                $splitparam = explode('=', $param);
110                                if ($splitparam[1] != '') {
111                                    if ($splitparam[0] == 'value') {
112                                        $data['improg_value'] = $splitparam[1];	// Umsetzungswert
113                                    } else if ($splitparam[0] == 'type') {
114                                        $data['improg_type'] = $splitparam[1];	// Umsetzungswert
115                                    } else if ($splitparam[0] == 'name') {
116                                        $data['improg_name'] = $splitparam[1];  // descriptive item name
117                                        $needles = array(":", "/", "\\");
118                                        $data['improg_name'] = str_replace($needles, "_", $data['improg_name']);
119                                    }
120                                }
121                            }
122                            $data['improg_link'] = html_wikilink(':' . $data['improg_file'], $data['improg_name']);
123                            if ($data['improg_type'] == 'improg')
124                                array_push($returns, $data);
125                        }
126                    }
127				}
128			 }
129			usort($returns, "improgcmp");
130			$ret = '<table>
131						 <thead>
132							 <tr>
133								<th>Ma&#223;nahme</th>
134								<th>Fortschritt</th>
135							 </tr>
136						 </thead>
137						 <tbody>';
138
139			foreach($returns as $findings) {
140				$alink_id++;
141				//$anker_id = 'anker_'.$alink_id;
142                $improg_stars = $this->assign_star_image($findings['improg_value']);
143				$ret .= '<tr>'.
144						   '<td>'.$findings['improg_link'].'</td>'.
145						   '<td><img src="'.$improg_stars.'" alt="'.$findings['improg_value'].' Sterne" /></td>';
146				$ret .= '</tr>';
147			}
148			$ret .= '</tbody></form></table>'.NL;
149		}
150		$renderer->doc .= $ret;
151	}
152}
153/******************************************************************************/
154	function assign_star_image($improg_rating) {
155		if ($improg_rating == 1  ){$improg_stars = DOKU_BASE."lib/plugins/improg/img/1star.gif";}
156			else if ($improg_rating == 2  ){$improg_stars = DOKU_BASE."lib/plugins/improg/img/2star.gif";}
157			else if ($improg_rating == 3  ){$improg_stars = DOKU_BASE."lib/plugins/improg/img/3star.gif";}
158			else if ($improg_rating == 4  ){$improg_stars = DOKU_BASE."lib/plugins/improg/img/4star.gif";}
159			else if ($improg_rating >= 5  ){$improg_stars = DOKU_BASE."lib/plugins/improg/img/5star.gif";}
160			else {   $improg_stars = DOKU_BASE . "lib/plugins/improg/img/00star.gif";
161        }
162		return $improg_stars;
163	}
164	function list_rec_files_in_array($dir, $delim, $excludes) {
165		$listDir = array();
166		if($handler = opendir($dir)) {
167			while (FALSE !== ($sub = readdir($handler))) {
168				if ($sub !== "." && $sub !== "..") {
169					if(is_file($dir."/".$sub)) {
170						$x = strpos(basename($dir."/".$sub),".txt");
171						if(($delim === '.txt') && ($x > 0)){
172							$listDir[] = $dir."/".$sub;
173						  }
174						elseif($delim === 'all') {
175							$listDir[] = $dir."/".$sub;
176						}
177					}
178					elseif(is_dir($dir."/".$sub)) {
179						$listDir[$sub] = $this->list_rec_files_in_array($dir."/".$sub, $delim,$excludes);
180					}
181				}
182			}
183			closedir($handler);
184		}
185		$listDir = $this->array_flat($listDir);
186		return $listDir;
187	}
188/******************************************************************************/
189	function array_flat($array) {
190		$out=array();
191		foreach($array as $k=>$v){
192			if(is_array($array[$k]))  { $out=array_merge($out,$this->array_flat($array[$k])); }
193			else  { $out[]=$v; }
194		}
195		return $out;
196	}
197/******************************************************************************/
198	function __savedir($page_filepath) {
199		global $conf;
200		$savedir = $this->getConf('savedir');
201		if($savedir == '') $savedir='data';
202		$savedir = str_replace("../", "", $savedir);
203		$savedir = str_replace("./", "", $savedir);
204		$y_pos   = stripos($page_filepath, $savedir."/pages");
205		$t1	  = substr($page_filepath, $y_pos);
206		$t1	  = substr(str_replace( ".txt" , "" , $t1 ) , strlen($savedir."/pages"), 9999);
207		// turn it into wiki link without "pages"
208		$t2 = str_replace("/", ":", $t1);
209		$t2 = substr($t2, 1, strlen($t2));
210		return $t2;
211	}
212}