1<?php
2/**
3 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
4 * @author     Etienne M. <emauvaisfr@yahoo.fr>
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.'action.php');
12
13class action_plugin_favoris extends DokuWiki_Action_Plugin {
14
15    /**
16     * return some info
17     */
18    function getInfo() {
19        return array(
20                'author' => 'Etienne M.',
21                'email'  => 'emauvaisfr@yahoo.fr',
22                'date'   => @file_get_contents(DOKU_PLUGIN.'favoris/VERSION'),
23                'name'   => 'favoris Plugin',
24                );
25    }
26
27    /**
28     * Constructor
29     */
30    function action_plugin_favoris() {
31      $this->setupLocale();
32    }
33
34    /**
35     * register the eventhandlers
36     */
37    function register(&$contr) {
38        $contr->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, '_update_cookie', array());
39        $contr->register_hook('TPL_ACT_UNKNOWN', 'BEFORE', $this, '_handle_tpl_act', array());
40    }
41
42    function _update_cookie(&$event, $param) {
43        global $INFO;
44
45        if($event->data == 'snapfavoris') {
46          $event->preventDefault();
47        }
48
49        //On ignore les medias (:lib:, :media:)
50        if (preg_match("/:lib:/i",$INFO['id']) || preg_match("/:media:/i",$INFO['id'])) return;
51
52        if (isset($_COOKIE['favoris'])) {
53          $fav=$_COOKIE['favoris'];
54
55          //Si on ne souhaite pas suivre les favoris
56          if ($fav['off']==1) {
57            //On efface les eventuels cookies existants (sauf off)
58            foreach ($_COOKIE['favoris'] as $page => $cpt) if ($page != "off") setCookie("favoris[$page]", "", time()-3600, '/');
59            return;
60          }
61
62          //Si on est en mode de remise a zero des compteurs (mais on garde les pages exclues quand meme
63          if ($fav['off']==2) {
64            //On efface tous les cookies (y compris off)
65            foreach ($_COOKIE['favoris'] as $page => $cpt) {
66              list($cpt, $date)=explode(";",$cpt);
67              if ($cpt != "-1") setCookie("favoris[$page]", "", time()-3600, '/');
68            }
69            return;
70          }
71
72          list($cpt, $date)=explode(";", $fav[$INFO['id']]);
73
74          //Si la page est a ne pas prendre en compte (-1)
75          if ($cpt==-1) return;
76
77          //S'il existe, on recupere l'ancien compteur de visites et on l'incremente (sinon, on commence a 1)
78          if ($cpt!=0 && $cpt!="")
79            $cpt++;
80          else $cpt=1;
81        }
82        else $cpt=1;
83
84        //On positionne le cookie
85        setCookie("favoris[".$INFO['id']."]","$cpt;".time(), time()+60*60*24*7, '/');
86    }
87
88
89    function _handle_tpl_act(&$event, $param) {
90      if($event->data != 'snapfavoris') return;
91      $event->preventDefault();
92
93      print "<h1>".$this->getLang('fav_mosaique')."</h1>";
94
95      $fav=$_COOKIE['favoris'];
96      if (!$fav) {
97        print $this->getLang('fav_pasencore');
98        return false;
99      }
100
101      uasort($fav, create_function('$a, $b', '
102                           list($cpt1, $date)=explode(";", $a);
103                           list($cpt2, $date)=explode(";", $b);
104
105                           $cpt1=intval($cpt1);
106                           $cpt2=intval($cpt2);
107
108                           if ($cpt1==$cpt2) return 0;
109                           return ($cpt1 > $cpt2) ? -1 : 1;
110                         '));
111
112     print "<div>";
113     $idx=0;
114     foreach ($fav as $page => $cpt) {
115       if ($page=='off' || $cpt<1) continue;
116       $snap=plugin_load('helper','snap');
117       if (!$snap) {
118         print $this->getLang('fav_snapnotfound')."<br />";
119         print "</div>";
120         return false;
121       }
122
123       list($imagePath, $titrePage, $target)=$snap->getSnap($page, 200, 150, true);
124       if (!$snap->succeed || !$imagePath) {
125         print $this->getLang('fav_pbsnap')." $page<br />";
126         print "</div>";
127         return false;
128       }
129
130       if ($titrePage) $titrePage=" - ".$titrePage;
131       $cpt=explode(";",$cpt);
132       if ($cpt) $titrePage.= " - ".$cpt[0]." ".$this->getLang('fav_visites');
133       if ($snap->snapTimeFormatted) $titrePage.=" (".$snap->snapTimeFormatted.")";
134
135       if (!@file_exists(fullpath(wikiFN($page)))) $style="style=\"border:1px dashed red; margin:2px;\"";
136       else $style="style=\"border:1px solid #C0C0C0; margin:2px;\"";
137
138       print "<a href=\"".$snap->url."\" title=\"$page$titrePage\" $target>";
139
140       print "<img src=\"".DOKU_URL."lib/plugins/snap/image.php?image=".rawurlencode($imagePath)."\" $style/>";
141       print "</a>";
142
143       $idx++;
144       if ($idx>=9) break;
145       if ($idx%3==0) print "<br />";
146     }
147
148     print "</div>";
149     print "<br /><hr />";
150    }
151}
152
153// vim:ts=4:sw=4:et:enc=utf-8:
154