1<?php
2/**
3 * Plugin metadisplay"
4 *
5 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author     Myron Turner <turnermm02@shaw.ca
7 */
8
9
10/**
11 * All DokuWiki plugins to extend the admin function
12 * need to inherit from this class
13 */
14define ("METADISP_CMDL",  'php ' .DOKU_INC . 'bin/plugin.php metadisplay ');
15class admin_plugin_metadisplay extends DokuWiki_Admin_Plugin {
16
17    private $output ='';
18	private $month = "";
19    private $year = "";
20    private $day = "";
21    private $page ="";
22    private $startdir = "";
23    private $CommandLine = "";
24    private $search = "";
25    private $stchecked_exact = 'checked';
26    private $stchecked_fuzzy = "";
27    private $dtype="";
28    private $ltype="";
29    private $media_checked = "";
30    private $links_checked = "";
31    private $descr_checked = "";
32    private $creator_checked = "";
33    private $contrib_checked = "";
34
35    /**
36     * handle user request
37     */
38    function handle() {
39
40
41      if (!isset($_REQUEST['cmd']) && empty($_REQUEST['help'])) return;   // first time - nothing to do
42      $this->output = '';
43      if (!checkSecurityToken()) return;
44      if(!empty($_REQUEST['help'])) {
45          $this->output = '<pre>' . shell_exec(METADISP_CMDL  .'-h') .'</pre>';
46            return;
47      }
48
49   $commands =  $_REQUEST['cmd'];
50   //msg('<pre>'.print_r($_REQUEST,1).'</pre>');
51    $start_dir = $commands['namespace'] ? $commands['namespace'] : '.';
52    $cmdline = METADISP_CMDL  .'-n ' . $start_dir;
53    if(!empty($commands['page'])) {
54         $cmdline .= " -p " . $commands['page'];
55         if(!empty($commands['exact'])) {
56            $cmdline .= " -e " . $commands['exact'];
57        }
58        else  $cmdline .= " -e " . 'off';
59    }
60    if(!empty($commands['search'])) {
61        $this->search = $commands['search'];
62        $this->stchecked_exact = "";
63        $this->stchecked_fuzzy = "";
64        if($commands['srch_type'] == 'fuzzy') {
65            $cmdline .= " -f " .  $this->search;
66            $this->stchecked_fuzzy = 'checked';
67        }
68        else {
69            $cmdline .= " -s " .  $this->search;
70            $this->stchecked_exact = 'checked';
71        }
72
73    $ltype = $_REQUEST['ltype'];
74    if($ltype) {
75       if($ltype == 'links') {
76           $this->links_checked = 'checked';
77       }
78       else if($ltype == 'media') {
79           $this->media_checked = 'checked';
80       }
81       else if($ltype == 'descr') {
82           $this->descr_checked = 'checked';
83       }
84       else if($ltype == 'creator') {
85           $this->creator_checked = 'checked';
86       }
87           else if($ltype == 'contrib') {
88               $this->contrib_checked = 'checked';
89           if($commands['srch_type'] == 'fuzzy') {
90               msg($this->getLang('nofuzzy'),2);
91           }
92
93       }
94
95       $cmdline .= " -l $ltype ";
96    }
97    }
98     if(!empty($commands['pcreated']) || !empty($commands['pmodified']) ) {
99		 $date_not_set = "";
100		 if(empty($commands['year'] )) {
101			 $commands['year'] = date('Y');
102			 $date_not_set =  $commands['year'] ;
103		 }
104		 if(empty($commands['month'] )) {
105			 $date_not_set .= '1';
106			 $commands['month'] = '1';
107		 }
108		 if(empty($commands['day'] )) {
109			 $date_not_set .= '1';
110			 $commands['day'] = '1';
111		 }
112		  if($date_not_set) {
113			  $date_not_set =   $commands['year'] . '-' . $commands['month'] . '-' .  $commands['day'] = '1';
114			  msg("Using defaults for missing date fields: $date_not_set" ,1);
115		  }
116	     $timestamp = $commands['year'] .'-'. $commands['month'] .'-'. $commands['day'];
117         $w = (isset($_REQUEST['when']) && $_REQUEST['when'] == 'earlier') ? ' -b  ': ' -a ';
118          $cmdline .= $w . "$timestamp";
119		  $dtm = $commands['pcreated']?'created':'modified';
120		  $cmdline .= " --dtype $dtm";
121         $this->dtype = $dtm;
122    }
123    $cmdline .= ' -c html';
124
125   // msg('<pre>'.print_r($commands,1).'</pre>');
126    $this->year = $commands['year'];
127    $this->month = $commands['month'];
128    $this->day = $commands['day'];
129    $this->start_dir = $start_dir;
130    $this->page = (!empty($commands['page'])) ? $commands['page'] : "";
131    if(!$commands['testcl']) {
132        $this->output =shell_exec($cmdline);
133    } else {
134       $this->CommandLine = preg_replace('#^'. METADISP_CMDL .'(.*?)-c html#','php plugin.php metadisplay '."$1",$cmdline);
135    }
136
137
138    }
139
140    function html() {
141
142      ptln('<form action="'.wl($ID).'" method="post">');
143
144      // output hidden values to ensure dokuwiki will return back to this plugin
145          ptln('  <input type="hidden" name="do"   value="admin" />');
146          ptln('<input type="hidden" name="page" value="'.$this->getPluginName().'" />');
147          formSecurityToken();
148          /*Namespace/page*/
149          ptln('<div>Namespace: <input type="text" name="cmd[namespace]" placeholder="namespace:n2:n3. . ." value = "' . $this->start_dir .'" />');
150          ptln('&nbsp; Page: <input type="text" name="cmd[page]" placeholder="page without extension" value = "' . $this->page .'" />');
151          ptln('&nbsp; ' .$this->getLang('exact').':&nbsp <input type = "checkbox" name="cmd[exact]" />');
152
153          /*Date*/
154          ptln('<br />');
155          ptln($this->getLang('date') . ':&nbsp;&nbsp;');
156          ptln('<input type="text" size = "6" name="cmd[year]" placeholder="Year"  value = "'. $this->year  .'"/>');
157          ptln('<input type="text" size = "12" name="cmd[month]" placeholder="Month (1-12)"  value = "' . $this->month .'"/>');
158          ptln('<input type="text" size = "12" name="cmd[day]" placeholder="Day (1-31)" value = "'.$this->day .'" />');
159
160
161          ptln('<table><tr><td>&nbsp;&nbsp;');
162          ptln($this->getLang('when') );
163          $dtype_c = "";
164          $dtype_m = "";
165          if($this->dtype == 'modified') {
166              $dtype_m = 'checked';           ;
167          }
168          if($this->dtype == 'created') {
169              $dtype_c = 'checked';
170          }
171
172          ptln( '<input type="checkbox" ' . $dtype_c . ' id = "pcreated" name="cmd[pcreated]">');
173          ptln($this->getLang('andor') . ' <input type="checkbox" '.$dtype_m .' id="pmodified" name="cmd[pmodified]"');
174          ptln ('<ul><li> <input type="radio" id="earlier" name="when" value="earlier"><label for="earlier"> ' .$this->getLang('earlier').'</label></li>');
175          ptln('<li> <input type="radio" id="later" name="when" value="later"><label for="later"> ' .$this->getLang('later').'</label></li></ul>');
176
177           /* Search */
178          ptln($this->getLang("search") . ':&nbsp; <input type = "text" size = "20" name = "cmd[search]" value="'.$this->search .'" placeholder = "Search term" />');
179          $_fchecked = $this->stchecked_fuzzy; $_echecked = $this->stchecked_exact;
180          ptln ('&nbsp;&nbsp;&nbsp;<input type="radio" id="exact_match" name="cmd[srch_type]" value="exact" ' ." $_echecked " .'/><label for="exact_match"> '.$this->getLang('exact_match').'</label>');
181          ptln('&nbsp;<input type="radio" id="fuzzy_match" name="cmd[srch_type]" value="fuzzy" ' . " $_fchecked " . '><label for="fuzzy_match"> ' .$this->getLang('fuzzy_match').'</label>');
182
183          ptln('&nbsp;&nbsp;&nbsp;<input type="radio" id="link" name="ltype" value="links" '. $this->links_checked .'><label for="link"> ' .$this->getLang('links').'</label>');
184          ptln(' <input type="radio" id="media" name="ltype" value="media" ' . $this->media_checked .'><label for="media"> ' .$this->getLang('media').'</label>');
185          ptln(' <input type="radio" id="descr" name="ltype" value="descr" '. $this->descr_checked .'><label for="descr"> ' .$this->getLang('descr').'</label>');
186
187         ptln('&nbsp;&nbsp;&nbsp;<input type="radio" id="mdcreator" name="ltype" value="creator" '. $this->creator_checked .'><label for="mdcreator"> ' .$this->getLang('creator').'</label>');
188         ptln(' <input type="radio" id="mdcontrib" name="ltype" value="contrib" ' . $this->contrib_checked .'><label for="mdcontrib"> ' .$this->getLang('contrib').'</label>');
189
190          ptln('<div><input type="checkbox" id = "testcl" name="cmd[testcl]"> Test Command line: '. $this->CommandLine .'</div>');
191          ptln('</div>');
192
193          ptln('<div style="line-height:2">');
194          ptln('<input type="submit" name="submit"/>&nbsp;&nbsp;<input type="submit" value="help" name="help" /></div>');
195
196      ptln('</form>');
197      ptln('<div><br />'.$this->output.'</div>');
198    }
199
200}