xref: /plugin/approve/action/approve.php (revision 1aeb2b4d3b5dca81e54255c859b458e1ff8420ea)
1<?php
2
3if(!defined('DOKU_INC')) die();
4define(APPROVED, 'Approved');
5
6class action_plugin_approve_approve extends DokuWiki_Action_Plugin {
7
8    function register(&$controller) {
9        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_approve, array());
10        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, handle_diff_accept, array());
11        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, handle_display_banner, array());
12        $controller->register_hook('HTML_SHOWREV_OUTPUT', 'BEFORE', $this, handle_showrev, array());
13    }
14
15	function handle_diff_accept(&$event, $param) {
16		if ($event->data == 'diff' && isset($_GET['approve'])) {
17			ptln('<a href="'.DOKU_URL.'doku.php?id='.$_GET['id'].'&approve=approve">'.$this->getLang('approve').'</a>');
18		}
19	}
20
21	function handle_showrev(&$event, $param) {
22		global $ID, $REV;
23
24		$last = $this->find_lastest_approved();
25		if ($last == $REV)
26			$event->preventDefault();
27	}
28
29	function can_approve() {
30		global $ID;
31		return auth_quickaclcheck($ID) > AUTH_DELETE;
32	}
33
34	function handle_approve(&$event, $param) {
35		global $ID, $REV;
36		if ( ! $this->can_approve()) return;
37		if ($event->data == 'show' && isset($_GET['approve'])) {
38			//Add or remove the new line from the end of the page. Silly but needed.
39			$content = rawWiki($ID, '');
40			if (substr($content, -1) == "\n") {
41				$content = substr($content, 0, -1);
42			} else {
43				$content .= "\n";
44			}
45			saveWikiText($ID, $content, APPROVED);
46
47			header('Location: ?id='.$ID);
48		}
49
50		/*czytacze wydzą najnowszą zatwierdzaną*/
51		$last = $this->find_lastest_approved();
52		/*użytkownik może tylko czytać i jednocześnie istnieje jakaś zatwierdzona strona*/
53		if (auth_quickaclcheck($ID) <= AUTH_READ && $last != -1)
54			/*najnowsza zatwierdzona nie jest najnowszą*/
55			/*i jednocześnie znajdujemy się w stronach nowszych niż aktualna zatwierdzona*/
56			if ($last != 0 && ($REV > $last || $REV == 0))
57				$REV = $last;
58	}
59	function find_lastest_approved() {
60		global $ID;
61		$m = p_get_metadata($ID);
62		$sum = $m['last_change']['sum'];
63		if ($sum == APPROVED)
64			return 0;
65
66		$changelog = new PageChangeLog($ID);
67		//wyszukaj najnowszej zatwierdzonej
68		//poszukaj w dół
69		$chs = $changelog->getRevisions(0, 10000);
70		foreach ($chs as $rev) {
71			$ch = $changelog->getRevisionInfo($rev);
72			if ($ch['sum'] == APPROVED)
73				return $rev;
74		}
75		return -1;
76	}
77
78    function handle_display_banner(&$event, $param) {
79		global $ID, $REV, $INFO;
80
81        if($event->data != 'show') return;
82		if (!$INFO['exists']) return;
83
84		$m = p_get_metadata($ID);
85		$changelog = new PageChangeLog($ID);
86
87		//sprawdź status aktualnej strony
88		if ($REV != 0) {
89			$ch = $changelog->getRevisionInfo($REV);
90			$sum = $ch['sum'];
91		} else {
92			$sum = $m['last_change']['sum'];
93		}
94
95		if ($sum != APPRVOED) {
96			$class = 'approved_no';
97			$last_approved_rev = $this->find_lastest_approved();
98		}
99
100
101		ptln('<div class="approval '.($sum == APPROVED ? 'approved_yes' : 'approved_no').'">');
102
103		tpl_pageinfo();
104		ptln(' | ');
105		if ($sum == APPROVED) {
106			ptln('<span>'.$this->getLang('approved').'</span>');
107			if ($REV != 0 && auth_quickaclcheck($ID) > AUTH_READ) {
108				ptln('<a href="'.wl($ID).'">');
109				ptln($this->getLang($m['last_change']['sum'] == APPROVED ? 'newest_approved' : 'newest_draft'));
110				ptln('</a>');
111			} else if ($REV != 0 && $REV != $last_approved_rev) {
112				ptln('<a href="'.wl($ID).'">');
113				ptln($this->getLang('newest_approved'));
114				ptln('</a>');
115			}
116		} else {
117			ptln('<span>'.$this->getLang('draft').'</span>');
118
119			if (isset($last_approved_rev)) {
120				if ($last_approved_rev != 0)
121					ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev)).'">');
122				else
123					ptln('<a href="'.wl($ID).'">');
124
125					ptln($this->getLang('newest_approved'));
126				ptln('</a>');
127			} else {
128				ptln('<a href="'.wl($ID).'">');
129					ptln($this->getLang('newest_draft'));
130				ptln('</a>');
131			}
132
133			//można zatwierdzać tylko najnowsze strony
134			if ($REV == 0 && $this->can_approve()) {
135				ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev, 'do' => 'diff',
136				'approve' => 'approve')).'">');
137					ptln($this->getLang('approve'));
138				ptln('</a>');
139			}
140		}
141		ptln('</div>');
142	}
143
144}
145