xref: /plugin/approve/action/approve.php (revision 50481663bb5b029a51d78c31b82bbf584493fb95)
11aeb2b4dSghi<?php
21aeb2b4dSghi
31aeb2b4dSghiif(!defined('DOKU_INC')) die();
41aeb2b4dSghidefine(APPROVED, 'Approved');
51aeb2b4dSghi
61aeb2b4dSghiclass action_plugin_approve_approve extends DokuWiki_Action_Plugin {
71aeb2b4dSghi
8*50481663SSzymon Olewniczak    private $hlp;
9*50481663SSzymon Olewniczak    function __construct(){
10*50481663SSzymon Olewniczak        $this->hlp = plugin_load('helper', 'approve');
11*50481663SSzymon Olewniczak    }
12*50481663SSzymon Olewniczak
13*50481663SSzymon Olewniczak    function register(Doku_Event_Handler $controller) {
14*50481663SSzymon Olewniczak
151aeb2b4dSghi        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_approve, array());
1638d03fbdSghi        $controller->register_hook('ACTION_ACT_PREPROCESS', 'BEFORE', $this, handle_viewer, array());
171aeb2b4dSghi        $controller->register_hook('TPL_ACT_RENDER', 'AFTER', $this, handle_diff_accept, array());
181aeb2b4dSghi        $controller->register_hook('TPL_ACT_RENDER', 'BEFORE', $this, handle_display_banner, array());
191aeb2b4dSghi        $controller->register_hook('HTML_SHOWREV_OUTPUT', 'BEFORE', $this, handle_showrev, array());
201aeb2b4dSghi    }
211aeb2b4dSghi
221aeb2b4dSghi	function handle_diff_accept(&$event, $param) {
23*50481663SSzymon Olewniczak		global $ID;
24*50481663SSzymon Olewniczak
25*50481663SSzymon Olewniczak		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
26*50481663SSzymon Olewniczak
271aeb2b4dSghi		if ($event->data == 'diff' && isset($_GET['approve'])) {
281aeb2b4dSghi			ptln('<a href="'.DOKU_URL.'doku.php?id='.$_GET['id'].'&approve=approve">'.$this->getLang('approve').'</a>');
291aeb2b4dSghi		}
301aeb2b4dSghi	}
311aeb2b4dSghi
321aeb2b4dSghi	function handle_showrev(&$event, $param) {
331aeb2b4dSghi		global $ID, $REV;
341aeb2b4dSghi
351aeb2b4dSghi		$last = $this->find_lastest_approved();
361aeb2b4dSghi		if ($last == $REV)
371aeb2b4dSghi			$event->preventDefault();
381aeb2b4dSghi	}
391aeb2b4dSghi
401aeb2b4dSghi	function can_approve() {
411aeb2b4dSghi		global $ID;
422cf0ddf9Sghi		return auth_quickaclcheck($ID) >= AUTH_DELETE;
431aeb2b4dSghi	}
441aeb2b4dSghi
451aeb2b4dSghi	function handle_approve(&$event, $param) {
46c7f8f6c0Sghi		global $ID, $REV, $INFO;
47*50481663SSzymon Olewniczak
48*50481663SSzymon Olewniczak		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
49*50481663SSzymon Olewniczak
501aeb2b4dSghi		if ($event->data == 'show' && isset($_GET['approve'])) {
5138d03fbdSghi		    if ( ! $this->can_approve()) return;
5238d03fbdSghi
53c7f8f6c0Sghi			//change last commit comment to Approved
54c7f8f6c0Sghi			$meta = p_read_metadata($ID);
55c7f8f6c0Sghi			$meta[current][last_change][sum] = $meta[persistent][last_change][sum] = APPROVED;
56c7f8f6c0Sghi			$meta[current][last_change][user] = $meta[persistent][last_change][user] = $INFO[client];
579f9f0967Sghi			if (!array_key_exists($INFO[client], $meta[current][contributor])) {
58c7f8f6c0Sghi			    $meta[current][contributor][$INFO[client]] = $INFO[userinfo][name];
59c7f8f6c0Sghi			    $meta[persistent][contributor][$INFO[client]] = $INFO[userinfo][name];
601aeb2b4dSghi			}
61c7f8f6c0Sghi			p_save_metadata($ID, $meta);
62c7f8f6c0Sghi			//update changelog
63c7f8f6c0Sghi			//remove last line from file
64c7f8f6c0Sghi			$changelog_file = metaFN($ID, '.changes');
65c7f8f6c0Sghi			$changes = file($changelog_file, FILE_SKIP_EMPTY_LINES);
66c7f8f6c0Sghi			$lastLogLine = array_pop($changes);
67c7f8f6c0Sghi			$info = parseChangelogLine($lastLogLine);
68c7f8f6c0Sghi
69c7f8f6c0Sghi			$info[user] = $INFO[client];
70c7f8f6c0Sghi			$info[sum] = APPROVED;
71c7f8f6c0Sghi
72c7f8f6c0Sghi			$logline = implode("\t", $info)."\n";
73c7f8f6c0Sghi			array_push($changes, $logline);
74c7f8f6c0Sghi
75c7f8f6c0Sghi			io_saveFile($changelog_file, implode('', $changes));
761aeb2b4dSghi
771aeb2b4dSghi			header('Location: ?id='.$ID);
781aeb2b4dSghi		}
7938d03fbdSghi	}
8038d03fbdSghi    function handle_viewer(&$event, $param) {
8138d03fbdSghi        global $REV, $ID;
8238d03fbdSghi        if ($event->data != 'show') return;
8338d03fbdSghi        if (auth_quickaclcheck($ID) > AUTH_READ) return;
841aeb2b4dSghi
851aeb2b4dSghi	    $last = $this->find_lastest_approved();
8638d03fbdSghi	    //no page is approved
8738d03fbdSghi		if ($last == -1) return;
8838d03fbdSghi		//approved page is the newest page
8938d03fbdSghi		if ($last == 0) return;
9038d03fbdSghi
9138d03fbdSghi		//if we are viewing lastest revision, show last approved
9238d03fbdSghi		if ($REV == 0) header("Location: ?id=$ID&rev=$last");
931aeb2b4dSghi	}
941aeb2b4dSghi	function find_lastest_approved() {
951aeb2b4dSghi		global $ID;
961aeb2b4dSghi		$m = p_get_metadata($ID);
971aeb2b4dSghi		$sum = $m['last_change']['sum'];
981aeb2b4dSghi		if ($sum == APPROVED)
991aeb2b4dSghi			return 0;
1001aeb2b4dSghi
1011aeb2b4dSghi		$changelog = new PageChangeLog($ID);
1021aeb2b4dSghi		//wyszukaj najnowszej zatwierdzonej
1031aeb2b4dSghi		//poszukaj w dół
1041aeb2b4dSghi		$chs = $changelog->getRevisions(0, 10000);
1051aeb2b4dSghi		foreach ($chs as $rev) {
1061aeb2b4dSghi			$ch = $changelog->getRevisionInfo($rev);
1071aeb2b4dSghi			if ($ch['sum'] == APPROVED)
1081aeb2b4dSghi				return $rev;
1091aeb2b4dSghi		}
1101aeb2b4dSghi		return -1;
1111aeb2b4dSghi	}
1121aeb2b4dSghi
1131aeb2b4dSghi    function handle_display_banner(&$event, $param) {
1141aeb2b4dSghi		global $ID, $REV, $INFO;
1151aeb2b4dSghi
116*50481663SSzymon Olewniczak		if ($this->hlp->in_namespace($this->getConf('no_apr_namespaces'), $ID)) return;
1171aeb2b4dSghi        if ($event->data != 'show') return;
1181aeb2b4dSghi		if (!$INFO['exists']) return;
1191aeb2b4dSghi
1201aeb2b4dSghi		$m = p_get_metadata($ID);
1211aeb2b4dSghi		$changelog = new PageChangeLog($ID);
1221aeb2b4dSghi
1231aeb2b4dSghi		//sprawdź status aktualnej strony
1241aeb2b4dSghi		if ($REV != 0) {
1251aeb2b4dSghi			$ch = $changelog->getRevisionInfo($REV);
1261aeb2b4dSghi			$sum = $ch['sum'];
1271aeb2b4dSghi		} else {
1281aeb2b4dSghi			$sum = $m['last_change']['sum'];
1291aeb2b4dSghi		}
1301aeb2b4dSghi
1311aeb2b4dSghi		ptln('<div class="approval '.($sum == APPROVED ? 'approved_yes' : 'approved_no').'">');
1321aeb2b4dSghi
1331aeb2b4dSghi		tpl_pageinfo();
1341aeb2b4dSghi		ptln(' | ');
135274d699aSghi		$last_approved_rev = $this->find_lastest_approved();
1361aeb2b4dSghi		if ($sum == APPROVED) {
1371aeb2b4dSghi			ptln('<span>'.$this->getLang('approved').'</span>');
1381aeb2b4dSghi			if ($REV != 0 && auth_quickaclcheck($ID) > AUTH_READ) {
1391aeb2b4dSghi				ptln('<a href="'.wl($ID).'">');
1401aeb2b4dSghi				ptln($this->getLang($m['last_change']['sum'] == APPROVED ? 'newest_approved' : 'newest_draft'));
1411aeb2b4dSghi				ptln('</a>');
1421aeb2b4dSghi			} else if ($REV != 0 && $REV != $last_approved_rev) {
1431aeb2b4dSghi				ptln('<a href="'.wl($ID).'">');
1441aeb2b4dSghi				ptln($this->getLang('newest_approved'));
1451aeb2b4dSghi				ptln('</a>');
1461aeb2b4dSghi			}
1471aeb2b4dSghi		} else {
1481aeb2b4dSghi			ptln('<span>'.$this->getLang('draft').'</span>');
1491aeb2b4dSghi
150274d699aSghi			if ($last_approved_rev == -1) {
151274d699aSghi			    if ($REV != 0) {
152274d699aSghi				    ptln('<a href="'.wl($ID).'">');
153274d699aSghi				    	ptln($this->getLang('newest_draft'));
154274d699aSghi				    ptln('</a>');
155274d699aSghi				}
156274d699aSghi			} else {
1571aeb2b4dSghi				if ($last_approved_rev != 0)
1581aeb2b4dSghi					ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev)).'">');
1591aeb2b4dSghi				else
1601aeb2b4dSghi					ptln('<a href="'.wl($ID).'">');
1611aeb2b4dSghi
1621aeb2b4dSghi					ptln($this->getLang('newest_approved'));
1631aeb2b4dSghi				ptln('</a>');
1641aeb2b4dSghi			}
1651aeb2b4dSghi
1661aeb2b4dSghi			//można zatwierdzać tylko najnowsze strony
1671aeb2b4dSghi			if ($REV == 0 && $this->can_approve()) {
1681aeb2b4dSghi				ptln('<a href="'.wl($ID, array('rev' => $last_approved_rev, 'do' => 'diff',
1691aeb2b4dSghi				'approve' => 'approve')).'">');
1701aeb2b4dSghi					ptln($this->getLang('approve'));
1711aeb2b4dSghi				ptln('</a>');
1721aeb2b4dSghi			}
1731aeb2b4dSghi		}
1741aeb2b4dSghi		ptln('</div>');
1751aeb2b4dSghi	}
1761aeb2b4dSghi
1771aeb2b4dSghi}
178