xref: /plugin/approve/action/approve.php (revision c7f8f6c0ebba88db6d91e3d2e37c09e9fcf1660f)
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, $INFO;
36		if ( ! $this->can_approve()) return;
37		if ($event->data == 'show' && isset($_GET['approve'])) {
38			//change last commit comment to Approved
39			$meta = p_read_metadata($ID);
40			$meta[current][last_change][sum] = $meta[persistent][last_change][sum] = APPROVED;
41			$meta[current][last_change][user] = $meta[persistent][last_change][user] = $INFO[client];
42			if (!array_key_exists($meta[current][contributor], $INFO[client])) {
43			    $meta[current][contributor][$INFO[client]] = $INFO[userinfo][name];
44			    $meta[persistent][contributor][$INFO[client]] = $INFO[userinfo][name];
45			}
46			p_save_metadata($ID, $meta);
47			//update changelog
48			//remove last line from file
49			$changelog_file = metaFN($ID, '.changes');
50			$changes = file($changelog_file, FILE_SKIP_EMPTY_LINES);
51			$lastLogLine = array_pop($changes);
52			$info = parseChangelogLine($lastLogLine);
53
54			$info[user] = $INFO[client];
55			$info[sum] = APPROVED;
56
57			$logline = implode("\t", $info)."\n";
58			array_push($changes, $logline);
59
60			io_saveFile($changelog_file, implode('', $changes));
61
62			header('Location: ?id='.$ID);
63		}
64
65		/*czytacze wydzą najnowszą zatwierdzaną*/
66		$last = $this->find_lastest_approved();
67		/*użytkownik może tylko czytać i jednocześnie istnieje jakaś zatwierdzona strona*/
68		if (auth_quickaclcheck($ID) <= AUTH_READ && $last != -1)
69			/*najnowsza zatwierdzona nie jest najnowszą*/
70			/*i jednocześnie znajdujemy się w stronach nowszych niż aktualna zatwierdzona*/
71			if ($last != 0 && ($REV > $last || $REV == 0))
72				$REV = $last;
73	}
74	function find_lastest_approved() {
75		global $ID;
76		$m = p_get_metadata($ID);
77		$sum = $m['last_change']['sum'];
78		if ($sum == APPROVED)
79			return 0;
80
81		$changelog = new PageChangeLog($ID);
82		//wyszukaj najnowszej zatwierdzonej
83		//poszukaj w dół
84		$chs = $changelog->getRevisions(0, 10000);
85		foreach ($chs as $rev) {
86			$ch = $changelog->getRevisionInfo($rev);
87			if ($ch['sum'] == APPROVED)
88				return $rev;
89		}
90		return -1;
91	}
92
93    function handle_display_banner(&$event, $param) {
94		global $ID, $REV, $INFO;
95
96        if($event->data != 'show') return;
97		if (!$INFO['exists']) return;
98
99		$m = p_get_metadata($ID);
100		$changelog = new PageChangeLog($ID);
101
102		//sprawdź status aktualnej strony
103		if ($REV != 0) {
104			$ch = $changelog->getRevisionInfo($REV);
105			$sum = $ch['sum'];
106		} else {
107			$sum = $m['last_change']['sum'];
108		}
109
110		if ($sum != APPRVOED) {
111			$class = 'approved_no';
112			$last_approved_rev = $this->find_lastest_approved();
113		}
114
115
116		ptln('<div class="approval '.($sum == APPROVED ? 'approved_yes' : 'approved_no').'">');
117
118		tpl_pageinfo();
119		ptln(' | ');
120		if ($sum == APPROVED) {
121			ptln('<span>'.$this->getLang('approved').'</span>');
122			if ($REV != 0 && auth_quickaclcheck($ID) > AUTH_READ) {
123				ptln('<a href="'.wl($ID).'">');
124				ptln($this->getLang($m['last_change']['sum'] == APPROVED ? 'newest_approved' : 'newest_draft'));
125				ptln('</a>');
126			} else if ($REV != 0 && $REV != $last_approved_rev) {
127				ptln('<a href="'.wl($ID).'">');
128				ptln($this->getLang('newest_approved'));
129				ptln('</a>');
130			}
131		} else {
132			ptln('<span>'.$this->getLang('draft').'</span>');
133
134			if (isset($last_approved_rev)) {
135				if ($last_approved_rev != 0)
136					ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev)).'">');
137				else
138					ptln('<a href="'.wl($ID).'">');
139
140					ptln($this->getLang('newest_approved'));
141				ptln('</a>');
142			} else {
143				ptln('<a href="'.wl($ID).'">');
144					ptln($this->getLang('newest_draft'));
145				ptln('</a>');
146			}
147
148			//można zatwierdzać tylko najnowsze strony
149			if ($REV == 0 && $this->can_approve()) {
150				ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev, 'do' => 'diff',
151				'approve' => 'approve')).'">');
152					ptln($this->getLang('approve'));
153				ptln('</a>');
154			}
155		}
156		ptln('</div>');
157	}
158
159}
160