1<?php
2/**
3 * DokuWiki Plugin DAVCard (Contact Syntax Component)
4 *
5 * @license GPL 2 http://www.gnu.org/licenses/gpl-2.0.html
6 * @author  Andreas Böhler <dev@aboehler.at>
7 */
8
9// must be run within Dokuwiki
10if(!defined('DOKU_INC')) die();
11
12if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
13require_once(DOKU_PLUGIN.'syntax.php');
14
15class syntax_plugin_davcard_card extends DokuWiki_Syntax_Plugin {
16
17    protected $hlp = null;
18
19    // Load the helper plugin
20    public function syntax_plugin_davcard_card() {
21        $this->hlp =& plugin_load('helper', 'davcard');
22    }
23
24
25    /**
26     * What kind of syntax are we?
27     */
28    function getType(){
29        return 'substition';
30    }
31
32    /**
33     * What about paragraphs?
34     */
35    function getPType(){
36        return 'normal';
37    }
38
39    /**
40     * Where to sort in?
41     */
42    function getSort(){
43        return 165;
44    }
45
46    /**
47     * Connect pattern to lexer
48     */
49    function connectTo($mode) {
50        $this->Lexer->addSpecialPattern('\{\{davcard>[^}]*\}\}',$mode,'plugin_davcard_card');
51        $this->Lexer->addSpecialPattern('\{\{davcardclient>[^}]*\}\}',$mode,'plugin_davcard_card');
52    }
53
54    /**
55     * Handle the match
56     */
57    function handle($match, $state, $pos, Doku_Handler $handler){
58        global $ID;
59        $data = array('name' => '',
60                      'id' => $ID,
61                      'firstname' => '',
62                      'lastname' => '',
63                      'email' => '',
64                      'uri' => '',
65                      );
66        if(strpos($match, '{{davcardclient') === 0)
67        {
68            $options = trim(substr($match,16,-2));
69            $defaultId = $this->getConf('default_client_id');
70            if(isset($defaultId) && ($defaultId != ''))
71            {
72                $data['id'] = $defaultId;
73            }
74        }
75        else
76        {
77            $options = trim(substr($match,10,-2));
78        }
79
80        $options = explode(',', $options);
81
82        foreach($options as $option)
83        {
84            list($key, $val) = explode('=', $option);
85            $key = strtolower(trim($key));
86            $val = trim($val);
87            switch($key)
88            {
89                default:
90                    $data[$key] = $val;
91            }
92        }
93        return $data;
94    }
95
96    /**
97     * Create output
98     */
99    function render($format, Doku_Renderer $R, $data) {
100        if($format == 'metadata')
101        {
102            if(strpos($data['id'], 'webdav://') === 0)
103            {
104                $connectionId = str_replace('webdav://', '', $data['id']);
105                $R->meta['plugin_davcard']['webdavclient'][] = $connectionId;
106                return true;
107            }
108        }
109        if($format != 'xhtml')
110            return false;
111
112
113        $contactdata = array();
114        $srch = '';
115        if($data['name'] !== '')
116        {
117            $contactdata = $this->hlp->getContactByFormattedName($data['id'], $data['name']);
118            $srch = $data['name'];
119        }
120        elseif(($data['firstname'] !== '') || ($data['lastname'] !== ''))
121        {
122            $contactdata = $this->hlp->getContactByStructuredName($data['id'], $data['firstname'], $data['lastname']);
123            $srch = $data['firstname'].' '.$data['lastname'];
124        }
125        elseif(($data['email'] !== ''))
126        {
127            $contactdata = $this->hlp->getContactByEmail($data['id'], $data['email']);
128            $srch = 'E-Mail = '.$data['email'];
129        }
130        elseif(($data['uri'] !== ''))
131        {
132            $contactdata = $this->hlp->getContactByUri($data['id'], $data['uri']);
133            $srch = 'URI = '.$data['uri'];
134        }
135        if($contactdata['result'] === false)
136        {
137            $R->doc .= sprintf($this->getLang('contact_not_found'), $srch);
138            return;
139        }
140
141        $R->doc .= '<a class="url fn plugin_davcard_url" href="#" data-davcarduri="'
142            .hsc($contactdata['uri']).'" data-davcardid="'.$data['id'].'" data-write="'.hsc($contactdata['write']).'">'.hsc($contactdata['formattedname']);
143        $R->doc .= '<span class="plugin_davcard_popup vcard">';
144        if(count($contactdata['addr']) > 0)
145        {
146            $R->doc .= '<span class="adr">';
147            foreach($contactdata['addr'] as $dat)
148            {
149                if(isset($data['type']))
150                    $type = $dat['type'];
151                else
152                    $type = 'other';
153                $R->doc .= '<span class="type">'.hsc($this->getLang('adr'.strtolower($type))).'</span>';
154                if($dat['address'][2] != '')
155                {
156                    $R->doc .= '<span class="street-address">'.hsc($dat['address'][2]).'</span><br>';
157                }
158                if($dat['address'][5] != '')
159                {
160                    $R->doc .= '<span class="postal-code">'.hsc($dat['address'][5]).' </span>';
161                }
162                if($dat['address'][3] != '')
163                {
164                    $R->doc .= '<span class="locality">'.hsc($dat['address'][3]).'</span><br>';
165                }
166
167                if($dat['address'][6] != '')
168                {
169                    $R->doc .= '<span class="country-name">'.hsc($dat['address'][6]).'</span>';
170                }
171            }
172            $R->doc .= '</span>';
173        }
174        if(count($contactdata['tel']) > 0)
175        {
176            $R->doc .= '<span class="tel">';
177            foreach($contactdata['tel'] as $dat)
178            {
179                if(isset($dat['type']))
180                    $type = $dat['type'];
181                else
182                    $type = 'other';
183                $R->doc .= '<span class="type">'.hsc($this->getLang('tel'.strtolower($type))).' </span>';
184                $R->doc .= hsc($dat['number']).'<br>';
185            }
186            $R->doc .= '</span>';
187        }
188        if(count($contactdata['mail']) > 0)
189        {
190            $R->doc .= '<span class="email_outer"><span class="email_type">EMail</span>';
191            foreach($contactdata['mail'] as $dat)
192            {
193                $R->doc .= '<span class="email">'.hsc($dat['mail']).'</span><br>';
194            }
195            $R->doc .= '</span>';
196        }
197
198        $R->doc .= '</span>';
199        $R->doc .= '</a>';
200
201    }
202
203
204
205}
206
207// vim:ts=4:sw=4:et:enc=utf-8:
208