xref: /plugin/approve/action/approve.php (revision e01695b75c74729c21f6cb11959d36876510c480)
1<?php
2
3if(!defined('DOKU_INC')) die();
4define(APPROVED, 'Approved');
5
6class action_plugin_approve_approve extends DokuWiki_Action_Plugin {
7
8    private $hlp;
9    function __construct(){
10        $this->hlp = plugin_load('helper', 'approve');
11    }
12
13    function register(Doku_Event_Handler $controller) {
14
15        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_approve, array());
16        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_viewer, array());
17        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, handle_diff_accept, array());
18        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, handle_display_banner, array());
19        $controller->register_hook('HTML_SHOWREV_OUTPUT', 'BEFORE', $this, handle_showrev, array());
20    }
21
22	function handle_diff_accept(Doku_Event $event, $param) {
23		global $ID;
24
25		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
26
27		if ($event->data == 'diff' && isset($_GET['approve'])) {
28			ptln('<a href="'.DOKU_URL.'doku.php?id='.$_GET['id'].'&approve=approve">'.$this->getLang('approve').'</a>');
29		}
30	}
31
32	function handle_showrev(Doku_Event $event, $param) {
33		global $ID, $REV;
34
35		$last = $this->find_lastest_approved();
36		if ($last == $REV)
37			$event->preventDefault();
38	}
39
40	function can_approve() {
41		global $ID;
42		return auth_quickaclcheck($ID) >= AUTH_DELETE;
43	}
44
45	function handle_approve(Doku_Event $event, $param) {
46		global $ID, $REV, $INFO;
47
48		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
49
50		if ($event->data == 'show' && isset($_GET['approve'])) {
51		    if ( ! $this->can_approve()) return;
52
53			//change last commit comment to Approved
54			$meta = p_read_metadata($ID);
55			$meta[current][last_change][sum] = $meta[persistent][last_change][sum] = APPROVED;
56			$meta[current][last_change][user] = $meta[persistent][last_change][user] = $INFO[client];
57			if (!array_key_exists($INFO[client], $meta[current][contributor])) {
58			    $meta[current][contributor][$INFO[client]] = $INFO[userinfo][name];
59			    $meta[persistent][contributor][$INFO[client]] = $INFO[userinfo][name];
60			}
61			p_save_metadata($ID, $meta);
62			//update changelog
63			//remove last line from file
64			$changelog_file = metaFN($ID, '.changes');
65			$changes = file($changelog_file, FILE_SKIP_EMPTY_LINES);
66			$lastLogLine = array_pop($changes);
67			$info = parseChangelogLine($lastLogLine);
68
69			$info[user] = $INFO[client];
70			$info[sum] = APPROVED;
71
72			$logline = implode("\t", $info)."\n";
73			array_push($changes, $logline);
74
75			io_saveFile($changelog_file, implode('', $changes));
76
77			header('Location: ?id='.$ID);
78		}
79	}
80    function handle_viewer(Doku_Event $event, $param) {
81        global $REV, $ID;
82        if ($event->data != 'show') return;
83        if (auth_quickaclcheck($ID) > AUTH_READ) return;
84
85	    $last = $this->find_lastest_approved();
86	    //no page is approved
87		if ($last == -1) return;
88		//approved page is the newest page
89		if ($last == 0) return;
90
91		//if we are viewing lastest revision, show last approved
92		if ($REV == 0) header("Location: ?id=$ID&rev=$last");
93	}
94	function find_lastest_approved() {
95		global $ID;
96		$m = p_get_metadata($ID);
97		$sum = $m['last_change']['sum'];
98		if ($sum == APPROVED)
99			return 0;
100
101		$changelog = new PageChangeLog($ID);
102		//wyszukaj najnowszej zatwierdzonej
103		//poszukaj w dół
104		$chs = $changelog->getRevisions(0, 10000);
105		foreach ($chs as $rev) {
106			$ch = $changelog->getRevisionInfo($rev);
107			if ($ch['sum'] == APPROVED)
108				return $rev;
109		}
110		return -1;
111	}
112
113    function handle_display_banner(Doku_Event $event, $param) {
114		global $ID, $REV, $INFO;
115
116		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
117        if ($event->data != 'show') return;
118		if (!$INFO['exists']) return;
119
120		$sum = $this->hlp->page_sum($ID, $REV);
121
122		ptln('<div class="approval '.($sum == APPROVED ? 'approved_yes' : 'approved_no').'">');
123
124		tpl_pageinfo();
125		ptln(' | ');
126		$last_approved_rev = $this->find_lastest_approved();
127		if ($sum == APPROVED) {
128			ptln('<span>'.$this->getLang('approved').'</span>');
129			if ($REV != 0 && auth_quickaclcheck($ID) > AUTH_READ) {
130				ptln('<a href="'.wl($ID).'">');
131				ptln($this->getLang($m['last_change']['sum'] == APPROVED ? 'newest_approved' : 'newest_draft'));
132				ptln('</a>');
133			} else if ($REV != 0 && $REV != $last_approved_rev) {
134				ptln('<a href="'.wl($ID).'">');
135				ptln($this->getLang('newest_approved'));
136				ptln('</a>');
137			}
138		} else {
139			ptln('<span>'.$this->getLang('draft').'</span>');
140
141			if ($last_approved_rev == -1) {
142			    if ($REV != 0) {
143				    ptln('<a href="'.wl($ID).'">');
144				    	ptln($this->getLang('newest_draft'));
145				    ptln('</a>');
146				}
147			} else {
148				if ($last_approved_rev != 0)
149					ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev)).'">');
150				else
151					ptln('<a href="'.wl($ID).'">');
152
153					ptln($this->getLang('newest_approved'));
154				ptln('</a>');
155			}
156
157			//można zatwierdzać tylko najnowsze strony
158			if ($REV == 0 && $this->can_approve()) {
159				ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev, 'do' => 'diff',
160				'approve' => 'approve')).'">');
161					ptln($this->getLang('approve'));
162				ptln('</a>');
163			}
164		}
165		ptln('</div>');
166	}
167
168}
169