1<?php
2
3/**
4 * Add online ordering forms to dokuwiki.
5 *
6 * @license    GPL 2 (http://www.gnu.org/licenses/gpl.html)
7 * @author     Jannes Drost-Tenfelde <info@drost-tenfelde.de>
8 */
9if(!defined('DOKU_INC')) define('DOKU_INC',realpath(dirname(__FILE__).'/../../').'/');
10if(!defined('DOKU_PLUGIN')) define('DOKU_PLUGIN',DOKU_INC.'lib/plugins/');
11require_once(DOKU_PLUGIN.'syntax.php');
12
13/**
14 * OnlineOrdering plugin.
15 *
16 * Form based ordering.
17 */
18class syntax_plugin_onlineordering extends DokuWiki_Syntax_Plugin {
19
20    /**
21     * Returns plugin information.
22     *
23     * @return array with plugin information.
24     */
25    function getInfo() {
26        return array(
27                'author' => 'Jannes Drost-Tenfelde',
28                'email'  => 'info@drost-tenfelde.de',
29                'date'   => '2011-09-27',
30                'name'   => 'Online Ordering',
31                'desc'   => 'Allows users to order items online using forms.',
32                'url'    => 'http://www.drost-tenfelde.de/?id=dokuwiki:plugins:onlineordering',
33        );
34    }
35
36    /**
37	 * Returns the syntax type of the plugin.
38	 *
39	 * @return type.
40	 */
41	function getType(){
42		return 'substition';
43	}
44
45	/**
46	 * Returns the paragraph type.
47	 *
48	 * @return paragraph type.
49	 */
50	function getPType(){
51		return 'block';
52	}
53
54	/**
55	 * Where to sort in?
56	 */
57	function getSort(){
58		return 301;
59	}
60
61  /**
62	 * Connects the syntax pattern to the lexer
63	 */
64	function connectTo($mode) {
65		$this->Lexer->addSpecialPattern('\{\{onlineordering>[^}]*\}\}', $mode, 'plugin_onlineordering');
66	}
67
68	/**
69	 * Handle the match
70	 */
71	function handle($match, $state, $pos, &$handler){
72		global $ID;
73
74        //strip markup from start and end
75		$match = substr($match, 17, -2);
76
77		// Get the parameters (key=value), seperated by &
78		$pairs = explode('&', $match);
79
80		// Turn the pairs into key=>value
81        foreach ($pairs as $pair) {
82			list($key, $value) = explode('=', $pair, 2);
83			$data[trim($key)] = trim($value);
84        }
85		// Turn all keys to lower case
86        $data = array_change_key_case($data, CASE_LOWER);
87
88        // Adjust price
89        $price =  $data['price'];
90        if ( isset( $price ) )
91        {
92            $price = str_replace('.', '', $price);
93            $price = str_replace(',', '.', $price);
94            $data['price'] = floatval( $price );
95        }
96
97        // Adjust porto
98        $porto_default = $data['porto_default'];
99        if ( isset( $porto_default ) )
100        {
101            $porto_default = str_replace('.', '', $porto_default);
102            $porto_default = str_replace(',', '.', $porto_default);
103            $data['porto_default'] = floatval( $porto_default );
104        }
105		return $data;
106	}
107
108	/**
109	 * Evaluates POST parameters and renders the appropriate for the current onlineticket stage.
110	 *
111	 * @param mode Display mode.
112	 *
113	 * @param renderer Renderer to which the output is sent.
114	 *
115	 * @param data data provided within the onlineticket tag.
116	 *
117	 */
118	function render($mode, &$renderer, $data) {
119        global $conf;
120		global $lang;
121
122		if ($mode != 'xhtml') {
123            return false;
124        }
125
126  		$content = '<div id="onlineordering_plugin">';
127          $message = '';
128
129  		// Make sure the parameters are set properly
130  		if ( $this->_validate_configuration( $data, $message ) ) {
131            // Get the form variables
132            $form = $this->_get_form_variables();
133
134            // Ensure the variables are valid
135            $message = '';
136            if ( !$this->_validate_form( $form, $message ) )
137            {
138                // Display the error
139                $content .= $this->getLang('error').'<br />'.$message;
140                // Go one stage back
141                $form['stage'] = 1;
142            }
143
144            // Get the querystring
145            $querystring = $_SERVER['QUERY_STRING'];
146
147            // Get the appropriate form
148            switch ( $form['stage'] )
149      		{
150                default:
151      			case 1:
152                    @include('order_form.php');
153                    $content .= $output;
154      				break;
155      			case 2:
156                    @include('confirm_form.php');
157                    $content .= $output;
158      				break;
159      			case 3:
160      			   @include('send_form.php');
161                    $content .= $output;
162      				break;
163      		}
164      	}
165      	else {
166            $content .= $this->getLang('error').'<br />'.$message;
167        }
168        $content .= '</div>';
169        $renderer->doc .= $content;
170
171		return true;
172	}
173
174    /**
175	 * Checks if the data necessary for the onlineticket system is valid.
176	 *
177	 * @param data Array with data provided within the onlineticket tag.
178	 *
179	 * @param message Validation message.
180	 *
181	 * @return boolean
182	 */
183	function _validate_configuration( &$data, &$message )
184	{
185        global $conf;
186        global $lang;
187
188        $validation = true;
189
190        /* Parameters */
191        // Ensure that the item name is set
192        if ( !isset( $data['item_name'] ) ) {
193            $message .= $this->_get_error_message('error_item_name_not_set');
194            $validation = false;
195        }
196        // Ensure that the abbreviation is set
197        if ( !isset( $data['abbreviation'] ) ) {
198            $message .= $this->_get_error_message('error_abbreviation_not_set');
199            $validation = false;
200        }
201        // Ensure that the currencty is set
202        if ( !isset( $data['currency'] ) ) {
203            $message .= $this->_get_error_message('error_currency_not_set');
204            $validation = false;
205        }
206        // Ensure that the price is set
207        if ( !isset( $data['price'] ) ) {
208            $message .= $this->_get_error_message('error_price_not_set');
209            $validation = false;
210        }
211
212        /* Configuration */
213
214        // Ensure Bank details have been set
215        $bank_account = $data['bank_account'];
216        if ( !isset($bank_account)) {
217            $bank_account = $this->getConf('bank_account');
218        }
219        if ( (!isset($bank_account)) || $bank_account == '' ) {
220            $message .= $this->_get_error_message('error_conf_bank_account_not_set');
221            $validation = false;
222        }
223        // Ensure the Sender E-mail is set
224        $sender_email = $data['sender_email'];
225        if ( !isset($sender_email)) {
226            $sender_email = $this->getConf('sender_email');
227        }
228        if ( (!isset($sender_email)) || $sender_email == '' ) {
229            $message .= $this->_get_error_message('error_conf_sender_email_not_set');
230            $validation = false;
231        }
232        // Ensure the Sender E-mail is set
233        $countries = $data['countries'];
234        if ( !isset($countries)) {
235            $countries = $this->getConf('countries');
236        }
237        if ( (!isset($countries)) || $countries == '' ) {
238            $message .= $this->_get_error_message('error_conf_countries_not_set');
239            $validation = false;
240        }
241
242        return $validation;
243    }
244
245    function _validate_form( $form, &$message )
246    {
247        $valid = true;
248
249        $stage = $form['stage'];
250        $message = '';
251
252        switch ( $stage )
253        {
254            // Empty form
255            default:
256            case 1:
257                break;
258            // Confirmation form
259            case 2:
260            case 3:
261                // First name
262                if ( ( !isset($form['firstname']) ) || $form['firstname'] == '' )
263                {
264                    $message .= $this->_get_error_message('error_firstname_not_set');
265                    $valid = false;
266                }
267                // Last name
268                if ( ( !isset($form['lastname']) ) || $form['lastname'] == '' )
269                {
270                    $message .= $this->_get_error_message('error_lastname_not_set');
271                    $valid = false;
272                }
273                // Street + nr
274                if ( ( !isset($form['street']) ) || $form['street'] == '' )
275                {
276                    $message .= $this->_get_error_message('error_street_not_set');
277                    $valid = false;
278                }
279                // Zipcode
280                if ( ( !isset($form['postcode']) ) || $form['postcode'] == '' )
281                {
282                    $message .= $this->_get_error_message('error_postcode_not_set');
283                    $valid = false;
284                }
285                // Place
286                if ( ( !isset($form['place']) ) || $form['place'] == '' )
287                {
288                    $message .= $this->_get_error_message('error_place_not_set');
289                    $valid = false;
290                }
291                // Country
292                if ( ( !isset($form['country']) ) || $form['country'] == '' )
293                {
294                    $message .= $this->_get_error_message('error_country_not_set');
295                    $valid = false;
296                }
297                // E-mail
298                if ( ( !isset($form['email']) ) || $form['email'] == '' )
299                {
300                    $message .= $this->_get_error_message('error_email_not_set');
301                    $valid = false;
302                }
303                else {
304                    // Check that the email is of a valid format
305                    if (!eregi( "^[_a-z0-9-]+(\.[_a-z0-9-]+)*@[a-z�-��?*-� 0-9-]+(\.[a-z�-��?*-� 0-9-]+)*(\.[a-z]{2,5})$", $form['email'] ) ) {
306                        $message .= $this->_get_error_message('error_email_invalid');
307                        $valid = false;
308                    }
309                }
310                // Tickets
311                if ( ( !isset($form['tickets']) ) || $form['tickets'] == '' )
312                {
313                    $message .= $this->_get_error_message('error_tickets_not_set');
314                    $valid = false;
315                }
316                else {
317                    if ( eregi('[^0-9]', $form['tickets']) || $form['tickets'] <= 0 ) {
318                        $message .= $this->_get_error_message('error_tickets_invalid');
319                        $valid = false;
320                    }
321                }
322                break;
323            // Send order
324            case 2:
325                break;
326        }
327        return $valid;
328    }
329
330    /**
331     * Returns the appropriate error message.
332     *
333     * @return formatted error message.
334     */
335    function _get_error_message( $id )
336    {
337        return '<div id="onlineordering_error">'.$this->getLang( $id ).'</div>';
338    }
339
340    /**
341     * Returns the form variables.
342     *
343     * @return form variables.
344     */
345    function _get_form_variables()
346    {
347        $form = array();
348
349        $stage = $_POST['onlineordering_stage'];
350        if ( !isset( $stage ) )
351        {
352            $stage = 1;
353        }
354        else {
355            $stage = $stage + 1;
356        }
357        $form['stage'] = $stage;
358        $form['title'] = $_POST['onlineordering_title'];
359        if ( !isset( $form['title'] ) )
360        {
361            $form['title'] = '';
362        }
363
364        $form['firstname'] = $_POST['onlineordering_firstname'];
365        $form['lastname'] = $_POST['onlineordering_lastname'];
366        $form['street'] = $_POST['onlineordering_street'];
367        $form['postcode'] = $_POST['onlineordering_postcode'];
368        $form['place'] = $_POST['onlineordering_place'];
369        $form['country'] = $_POST['onlineordering_country'];
370        $form['email'] = $_POST['onlineordering_email'];
371        $form['tickets'] = $_POST['onlineordering_tickets'];
372        $form['remarks'] = $_POST['onlineordering_remarks'];
373
374        return $form;
375    }
376
377    /**
378     * Returns the countries for which the orders can be made.
379     *
380     * @return Array() of countries.
381     */
382    function _get_countries()
383    {
384        // Get the configured countries
385        $conf_countries = $this->getConf('countries');
386        $countries = explode( ",", $conf_countries );
387
388        return $countries;
389    }
390
391    /**
392     * Returns the porto settings for a specific country.
393     *
394     * @param data plugin parameters.
395     * @param country Country name.
396     * @return porto.
397     */
398    function _get_porto( $data, $country )
399    {
400        // Attempt to get the porto for the country
401        $porto = $data['porto_'.strtolower($country)];
402        if ( isset( $porto ) )
403        {
404            $porto = str_replace('.', '', $porto);
405            $porto = str_replace(',', '.', $porto);
406            $porto = floatval( $porto );
407        }
408        else {
409            // Get the default porto
410            $porto = $data['porto_default'];
411
412            if ( !isset( $porto ) ) {
413                // No porto then
414                $porto = 0;
415            }
416        }
417
418        return $porto;
419    }
420}
421
422//Setup VIM: ex: et ts=4 enc=utf-8 :
423