1<?php
2/**
3 * DokuWiki Plugin dirpictures (Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  jerome <jerome_at_reso-nance.org>
7 *
8 * Usage :
9 * ~~DIRPICTURES~~ : sort by name
10 * ~~DIRPICTURES sortByDate~~ : sort by date
11 * ~~DIRPICTURES sortByDate sortDesc~~ : sort by date (descendant)
12 *
13 * Add default.jpg picture into the namespace of the page ...
14 */
15
16if(!defined('DOKU_INC')) {
17    define ('DOKU_INC', realpath(dirname(__FILE__).'/../../').'/');
18}
19if(!defined('DOKU_PLUGIN')) {
20    define ('DOKU_PLUGIN', DOKU_INC.'lib/plugins/');
21}
22
23require_once (DOKU_PLUGIN.'syntax.php');
24require_once (DOKU_INC.'inc/search.php');
25
26// Some functions
27require_once (DOKU_PLUGIN.'dirpictures/sortPages.php');
28
29class syntax_plugin_dirpictures extends DokuWiki_Syntax_Plugin {
30
31	function getType() { return 'substition'; }
32    function getPType() { return 'block'; }
33    function getSort() { return 168; }
34
35    // Some infos
36    function getInfo() {
37        return array(
38            'author' => 'Jérôme Abel',
39            'email'  => 'jerome_at_reso-nance.org',
40            'date'   => '2016-05-31',
41            'name'   => 'Dirpictures Plugin',
42            'desc'   => 'Show subnamespaces with small pictures',
43            'url'    => 'https://www.dokuwiki.org/plugin:dirpictures',
44        );
45    }
46
47    // Pattern Dokuwiki
48    function connectTo($mode) {
49        $this->Lexer->addSpecialPattern('~~DIRPICTURES.*~~', $mode, 'plugin_dirpictures');
50    }
51
52    // Handle arguments
53    function handle($match, $state, $pos, &$handler){
54		//TODO : add arguments (namespace, depth, sort type, sort order, ...)
55
56		// Get arguments
57		$args = $match;
58		$args = str_replace('DIRPICTURES', '', $args);
59		$args = str_replace('~~', '', $args);
60		$args = trim($args);
61		$args = explode (' ', $args);
62
63		// Prepare data options
64		$data = array();
65		$data['sort'] = "name";
66		$data['order'] = "asc";
67
68		// Set options
69		foreach($args as $arg){
70			$arg = trim($arg);
71			if($arg == "sortByDate") {
72				$data['sort'] = "date";
73			}
74			else if($arg == "sortDesc") {
75				$data['order'] = "desc";
76			}
77		}
78
79		// Send data options
80		return $data;
81	}
82
83    // Render html
84    function render($mode, &$renderer, $data) {
85		global $conf;
86		global $ID;
87
88		if($mode == 'xhtml'){
89
90			// Search subnamespaces
91			$searchOptions = array(
92				'depth' => 1, 'listdirs'  => true, 'pagesonly'=> true,
93				'sneakyacl' => true, 'meta'=> true, 'firsthead'=> true
94				);
95			$files = array();
96			$ns = getNS($ID);
97			search($files, $conf['datadir'], 'search_universal', $searchOptions, $ns);
98
99		    // Fill new array with data
100		    // needed to sort by date
101		    $new_files = array();
102			$i=0;
103
104			foreach($files as $item){
105			   $id = $item['id'].":".$conf['start'];
106			   $meta = p_get_metadata($id);
107			   $picture = $meta['relation']['firstimage'];
108			   if ($picture == "") $picture = $ns.":default.jpg"; // default image
109			   $dim = array('w'=>350, 'h'=>220);
110
111			   //Useful data
112			   $new_files[$i]['id'] = $id;
113			   $new_files[$i]['date'] = $meta['date']['modified'];
114			   $new_files[$i]['title'] = $meta['title'];
115			   $new_files[$i]['img'] = ml($picture, $dim, true);
116			   $new_files[$i]['url'] = wl($id,'',true);
117
118			   $i++;
119		    }
120
121		    // Sort files with arguments
122			sortPages::sort($new_files, $data['sort'], $data['order']);
123
124		    // Start html
125			$renderer->doc .= '<div class="mosaicMain">';
126
127			// Item loop
128			foreach($new_files as $item){
129
130				// Html test
131				$renderer->doc .= '<a href="'.$item['url'].'" title="'.$item['title'].'">';
132				$renderer->doc .= '<div class="mosaicImg" style="background-image:url('.$item['img'].')">';
133				$renderer->doc .= '<span class="mosaicTitle">'.$item['title'];
134				$renderer->doc .= '</span><span class="mosaicDate">' . date('d/m/Y', $item['date']);
135				$renderer->doc .= '</span></div></a>';
136				}
137
138			// End Html
139			$renderer->doc .= '</div>';
140			return true;
141			}
142		return false;
143	 }
144}
145