1<?php
2/**
3 * DokuWiki Plugin dlcounter (Admin Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Phil Ide <phil@pbih.eu>
7 */
8
9// must be run within Dokuwiki
10if (!defined('DOKU_INC')) {
11    die();
12}
13
14class admin_plugin_dlcounter extends DokuWiki_Admin_Plugin
15{
16
17    private $mydata;
18
19    /**
20     * @return int sort number in admin menu
21     */
22    public function getMenuSort()
23    {
24        return 200;
25    }
26
27    /**
28     * @return bool true if only access for superuser, false is for superusers and moderators
29     */
30    public function forAdminOnly()
31    {
32        return false;
33    }
34
35    /**
36     * Should carry out any processing required by the plugin.
37     */
38
39    public function handle(){
40        $data = array(
41                    'command' => 'name',
42                    'file'    => '',
43                    'sort'    => 'sort',
44                    'strip'   => true,
45                    'align'   => 'right',
46                    'minwidth' => 0,
47                    'cpad'    => 1,
48                    'halign'  => 'center',
49                    'bold'    => 'b',
50                    'header'  => true,
51                    'htext'   => 'Downloads'
52                );
53        $this->mydata = $data;
54    }
55
56
57    /**
58     * Render HTML output, e.g. helpful text and a form
59     */
60
61    public function html(){
62        $html = "";
63        $data = $this->mydata;
64
65        $fname = DOKU_INC.'data/counts/download_counts.json';
66        $json = json_decode( file_get_contents($fname), TRUE );
67
68        $command = $data['command'];
69        $file = $data['file'];
70
71        if( $command == 'file' ){
72            // just want a counter
73            $count = 0;
74            if( $file != "" && array_key_exists( $file, $json ) ){
75                $count = $json[$file];
76                $html .= $count;
77            }
78        }
79        else {
80            // dump all the data in a table
81            $sort = $data['sort'];
82
83            $json = $this->stripKeys( $json );
84
85            if( $command == 'name' ){
86                if( $sort == 'sort' ){
87                    ksort($json);
88                }
89                else if( $sort == 'rsort' ){
90                    krsort($json);
91                }
92            }
93            else if( $command == 'count' ){
94                if( $sort == 'sort' ) asort( $json );
95                else if( $sort == 'rsort' ) arsort( $json );
96            }
97
98            $table = "<table>";
99            if( $data['header'] ){
100                $table .= "<tr><th colspan=2 style='text-align:".$data['halign'].";'>".$data['htext']."</th></tr>";
101            }
102            foreach( $json as $file => $count ){
103                $table .= "<tr><td style='text-align:".$data['align'].";'>$file</td>".
104                            "<td align=right style='text-align: right;min-width: ".$data['minwidth']."em;padding-left: ".$data['cpad']."em;'>$count</td></tr>";
105            }
106            $table .= "</table>";
107            $html .= $table;
108        }
109        echo $html;
110    }
111
112    function stripKeys( $arr ){
113        $newArr = array();
114        foreach( $arr as $key => $value ){
115            $keyX = explode(':',$key);
116            $n = count($keyX)-1;
117            $newArr[$keyX[$n]] = $value;
118        }
119        return $newArr;
120    }
121
122    function dlcounter_switchKeys( $arr, $back2Front ){
123        $keys = array_keys( $arr );
124        for( $i = 0; $i < count($keys); $i++ ){
125            if( $back2Front ) $keys[$i] = $this->switchKeyHelperA( $keys[$i] );
126            else $keys[$i] = $this->switchKeyHelperB( $keys[$i] );
127        }
128        return array_combine( $keys, $arr );
129    }
130
131    // move the fileame to the front of the path
132    function switchKeyHelperA( $v ){
133        $a = explode(':', $v);
134        $f = array_pop($a);
135        array_unshift( $a, $f );
136        return implode(':', $a );
137    }
138
139    // move the filename from the front of the path to the end
140    function switchKeyHelperB( $v ){
141        $a = explode(':', $v);
142        $f = array_shift($a);
143        array_push( $a, $f );
144        return implode(':', $a );
145    }
146}
147