1<?php
2
3// must be run within Dokuwiki
4if(!defined('DOKU_INC')) die();
5
6
7class action_plugin_bugzillaint_fetch extends DokuWiki_Action_Plugin {
8
9
10    /**
11     * @param Doku_Event_Handler $controller The plugin controller
12     */
13    public function register(Doku_Event_Handler $controller) {
14        $controller->register_hook('AJAX_CALL_UNKNOWN', 'BEFORE', $this, 'handle_ajax');
15    }
16
17
18    /**
19     * @param Doku_Event $event
20     */
21    public function handle_ajax(Doku_Event $event) {
22        global $INPUT;
23        global $USERINFO;
24
25        // event handling
26    	if ($event->data != 'plugin_bugzillaint_fetch') {
27        	return;
28        }
29        $event->preventDefault();
30        $event->stopPropagation();
31
32		try {
33
34			$bugzillaclient = $this->loadHelper('bugzillaint_bugzillaclient', false);
35
36			// TODO good point to extend auth to user-specific auth
37			$bugzillaclient->setCredentials(
38				$this->getConf('bugzilla_login'),
39				$this->getConf('bugzilla_password')
40			);
41
42	        $result = $this->fetchData(
43	        	$bugzillaclient,
44	        	$INPUT->param('lists', array(), true),
45	        	$INPUT->param('trees', array(), true),
46	        	$INPUT->param('links', array(), true)
47	        );
48
49	        $json = new JSON();
50	        header('Content-Type: application/json');
51	        print $json->encode( $result );
52
53        } catch (Exception $e) {
54        	http_status(500);
55        	header('Content-Type: text/plain');
56        	print $e->getMessage();
57        }
58
59    }
60
61
62    private function fetchData( $bugzillaClient, $lists, $trees, $links ) {
63    	$result = array();
64
65    	if ( count($lists) > 0 ) {
66    		$result['lists'] = array();
67    		foreach ( $lists as $i ) {
68    			$result['lists'][] = $bugzillaClient->quicksearch( $i['quicksearch'], explode(',', $i['extras']), $i['group_by'] );
69    		}
70    	}
71
72    	if ( count($trees) > 0 ) {
73    		$result['trees'] = array();
74    		foreach ( $trees as $i ) {
75    			$tree = $bugzillaClient->getBugDependencyTrees( $i['id'], $i['depth'], explode(',', $i['extras']) );
76    			foreach ($tree as $k => $v) {
77    				$result['trees'][ $k ] = $v;
78    			}
79    		}
80    	}
81
82    	if ( count($links) > 0 ) {
83    		$extras = array();
84    		$ids = array();
85    		foreach ( $links as $i ) {
86    			$ids[] = $i['id'];
87    			$extras = array_merge( $extras, explode(',', $i['extras']) );
88    		}
89    		$result['links'] = $bugzillaClient->getBugsInfos( $ids, $extras);
90    	}
91
92    	return $result;
93    }
94
95
96}