1<?php
2/**
3 * DokuWiki Plugin likeit
4 *
5 * @license GPL 2 (http://www.gnu.org/licenses/gpl.html)
6 * @author  lisps
7 */
8
9if (!defined('DOKU_INC')) die();
10if (!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN', DOKU_INC . 'lib/plugins/');
11require_once (DOKU_PLUGIN . 'action.php');
12
13class action_plugin_likeit extends DokuWiki_Action_Plugin {
14
15	/**
16	 * Register the eventhandlers
17	 */
18	function register(Doku_Event_Handler $controller) {
19		$controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE',  $this, '_ajax_call');
20	}
21
22
23	function _ajax_call(Doku_Event $event,$param) {
24		if ($event->data !== 'plugin_likeit') {
25			return;
26		}
27		//no other ajax call handlers needed
28		$event->stopPropagation();
29		$event->preventDefault();
30
31		/* @var $INPUT \Input */
32		global $INPUT;
33
34		$input_index = $INPUT->int('id'); //input index on the server
35
36		$user = $_SERVER['REMOTE_USER'];
37
38
39		/* @var $Hajax \helper_plugin_ajaxedit */
40		$Hajax = $this->loadHelper('ajaxedit');
41
42		/* @var $Hfsinput \helper_plugin_likeit */
43		$Hlikeit = $this->loadHelper('likeit');
44
45		$likewithread = $this->getConf('likewithread')?AUTH_READ:AUTH_EDIT;
46		$data=$Hajax->getWikiPage(true, $likewithread); //
47
48		//find "our" fsinput fields
49		$found=explode("<likeit",$data);
50
51		if ($input_index < count($found)) {
52
53			$found[$input_index+1] = ltrim($found[$input_index+1]);
54			$stop=strpos($found[$input_index+1],">");
55			if ($stop === FALSE) {
56				$Hajax->error('Cannot find object, please contact your admin!');
57			}
58			else {
59				$olduserlist = substr($found[$input_index+1],0,$stop);
60
61				$newuserlist_r=explode(" ",trim($olduserlist));
62				if(in_array($user,$newuserlist_r)) {
63					$Hajax->success(array(
64						'msg'=>$this->getLang('already_liked')
65
66					));
67				}
68
69				$newuserlist_r[] = $user;
70				sort($newuserlist_r);
71				$newuserlist_r=array_unique($newuserlist_r);
72				$newuserlist=implode(" ",$newuserlist_r);
73
74
75				if($stop == 0){
76					$found[$input_index+1]= " ".$newuserlist." ".$found[$input_index+1];
77				}
78				else {
79					$found[$input_index+1]=str_replace($olduserlist," ".$newuserlist." ",$found[$input_index+1]);
80				}
81			}
82
83			$Hlikeit->setUser($newuserlist);
84
85			$data=implode("<likeit",$found);
86			$param = array(
87					'msg' => $this->getLang('added'),
88					'count' => $Hlikeit->getUserCount(),
89					'list'  => $Hlikeit->renderUserList()
90			);
91			$summary = "Likeit ".$input_index." liked";
92			$Hajax->saveWikiPage($data,$summary,true,$param);
93
94		}
95	}
96
97}
98