1<?php
2/**
3 * The syntax plugin to handle <use> tags
4 *
5 */
6
7require_once(dirname(__FILE__).'/../conf.php');
8require_once(dirname(__FILE__).'/../lib/tools.php');
9require_once DOKU_PLUGIN . 'syntax.php';
10require_once DOKU_INC . 'inc/common.php';
11
12class syntax_plugin_projects_use extends DokuWiki_Syntax_Plugin {
13    /**
14     * return some info
15     */
16    function getInfo(){
17        return array(
18            'author' => 'Junling Ma',
19            'email'  => 'junlingm@gmail.com',
20            'date'   => '2010-12-16',
21            'name'   => 'Project-file use Plugin',
22            'desc'   => 'display project files',
23            'url'    => 'http://www.math.uvic.ca/~jma',
24        );
25    }
26
27    function getType() {
28        return 'substition';
29    }
30
31    function getSort() {
32    	return 8;
33    }
34
35    function connectTo($mode) {
36    	$this->Lexer->addSpecialPattern('<' . USE_TAG . ' .*?/>',
37    		$mode, 'plugin_projects_use');
38    }
39
40    /**
41     * Handle the match
42     */
43    function handle($match, $state, $pos, &$handler){
44        if ($state != DOKU_LEXER_SPECIAL) return NULL;
45 		$xml = DOMDocument::loadXML($match);
46 		if (!$xml) return NULL;
47 		$name = $xml->firstChild->getAttribute('name');
48        return array("use" => trim($name), "pos" => $pos,
49        	"end" => $pos + strlen($match) + 1);
50    }
51
52    /**
53     * Create output
54     */
55    function render($mode, &$renderer, $data) {
56		if ($data == NULL) return;
57        switch ($mode) {
58        	case 'metadata':
59				global $ID;
60				$file = $renderer->meta['ProjectFile'];
61				if ($file != NULL) {
62					$use = $data['use'];
63					if ($use) $file->add_dependency($data["use"]);
64				}
65				break;
66        	case 'xhtml' :
67        		$this->render_xhtml($renderer, $data);
68        		break;
69        }
70    }
71
72    private function button_rename_use($range) {
73        global $ID;
74        if (auth_quickaclcheck($ID) < AUTH_EDIT) return '';
75        $self = noNS($ID);
76		$project = Project::project();
77        if ($project == NULL) return '';
78        $files = array('');
79        foreach (array_keys($project->files()) as $file)
80                if ($file != $self) $files[] = $file;
81        $form = new Doku_Form("change_use");
82        $form->addHidden('do', 'change_use');
83        $form->addHidden('range', $range);
84        $form->addElement(form_makeMenuField('use',  $files, '', '', '', '',
85                array("onchange" => "submit();")));
86        return $form->getForm();
87    }
88
89	private function render_xhtml(&$renderer, $data) {
90		if (!$data || !isset($data['use'])) return;
91		$use = $data['use'];
92		global $ID;
93		$name = array_pop(explode(":", $ID));
94		$project = Project::project();
95		//use
96		$renderer->doc .= '<div class="use">';
97		//header
98		$renderer->doc .= '<div class="tag_header">Use</div>';
99		//use_use
100		$renderer->doc .= '<div class="use_use">';
101		$range = $data['pos'] . '-' . $data['end'];
102		if (!$use) {
103			$choices = array_diff(array_keys($project->files()),
104				array($name));
105			array_unshift($choices, "");
106			$renderer->doc .= $this->button_rename_use($range, $choices);
107		}
108		else {
109			$use_id = $project->id($use);
110			if ($use_id)
111				$renderer->internallink($project->id($use));
112			else $renderer->doc .= "$use";
113		}
114		// end use_use
115		$renderer->doc .= "</div>";
116		$renderer->doc .= "<div class=\"spacer\"></div>";
117		// buttons
118		$renderer->doc .= "<div class=\"use_buttons\">";
119		$renderer->doc .= button_remove($range, "Use");
120		// end buttons
121		$renderer->doc .= "</div>";
122		if ($project != NULL) {
123			$errors = $project->error($use);
124			if ($errors) foreach ($errors as $error)
125				if ($error == 'undefined') {
126					$render->doc .="<div class=\"error\">Do not know how to generate this file.</div>";
127				}
128			$errors = $project->error($name);
129			if ($errors) foreach ($errors as $error)
130				if (is_array($error) && isset($error['dependency'])
131					&& $error['dependency'] == $use) {
132					$renderer->doc .="<div class=\"error\">This file cannot be generated.</div>";
133				}
134		}
135		$renderer->doc .= "<div class=\"use_content\"></div>";
136		// closing use
137		$renderer->doc .= "</div>";
138	}
139}
140
141?>