Dotclear

source: plugins/myForms/trunk/_public.php @ 1048

Revision 1048, 6.2 KB checked in by Oaz, 15 years ago (diff)

added <FieldMatches? name="" pattern=""> tag
added "checkboxes" example

Line 
1<?php
2# ***** BEGIN LICENSE BLOCK *****
3# Copyright (c) 2009 Olivier Azeau and contributors. All rights  reserved.
4#
5# This is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with DotClear; if not, write to the Free Software
17# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18#
19# ***** END LICENSE BLOCK *****
20
21require_once("TplCore.php");
22require_once("Captcha.php");
23require_once("TplFields.php");
24
25require_once("TplEmail.php");
26require_once("TplDatabase.php");
27
28$core->url->register('formGet','form','^form/(.+)$',array('MyForms','formGet'));
29$core->url->register('formPost','form','^form$',array('MyForms','formPost'));
30
31class MyForms extends dcUrlHandlers
32{
33  public static $formID;
34  private static $nextFormID;
35  public static $events;
36  private static $errors;
37  private static $htmlOut;
38  private static $formHTML;
39  private static $allFieldsAreValidated;
40  private static $captchaIsValidated;
41 
42  public static function formGet($args)
43  {
44    self::$formID = $args;
45    self::form();
46  }
47 
48  public static function formPost($args)
49  {
50    global $_REQUEST;
51    self::$formID = @$_REQUEST["myforms"]["formID"];
52    self::form();
53  }
54 
55  public static function form()
56  {
57    global $core, $_REQUEST;
58   
59    // add all 'default-templates' folders to form search path
60    $tplPath = $core->tpl->getPath();
61          $plugins = $core->plugins->getModules();
62          foreach($plugins as $plugin)
63               array_push($tplPath, $plugin['root'].'/default-templates');
64    $core->tpl->setPath($tplPath);
65   
66    self::loadForm();
67   
68    // process  form post
69    if( isset($_REQUEST["myforms"]) && self::$captchaIsValidated && self::$allFieldsAreValidated ) {
70      self::$nextFormID = false;
71      self::$errors = array();
72      $currentEventCallback = MyFormsTplCore::GetFunction('OnSubmit_'.self::getCurrentEvent());
73      ob_start();
74      $currentEventCallback();
75      self::$htmlOut = ob_get_clean();
76      if( self::$nextFormID ) {
77        self::$formID = self::$nextFormID;
78        self::loadForm();
79      }
80    }
81   
82    // display current form page
83    self::serveDocument('myforms.html');
84  }
85 
86  private static function loadForm()
87  {
88    global $core;
89    $formTpl = self::$formID.'.myforms.html';
90   
91    if (!$core->tpl->getFilePath($formTpl)) {
92      header('Content-Type: text/plain; charset=UTF-8');
93      echo 'Unable to find template for form <'.self::$formID.'>.';
94      exit;
95    }
96   
97    // include form template template which, in turn, defines 'myforms' functions
98    self::$events = array();
99    $core->tpl->getData($formTpl);
100   
101    // field display and validation
102    self::$allFieldsAreValidated = true;
103    self::$captchaIsValidated = true;
104    ob_start();
105    $displayFunction = MyFormsTplCore::GetFunction('Display');
106    $displayFunction();
107    self::$htmlOut = ob_get_clean();
108  }
109 
110  public static function validateCaptcha() {
111    global $_REQUEST;
112    $captchaIsValid = !isset($_REQUEST["myforms"]) || MyFormsCaptcha::isValid($_REQUEST["myforms"]["captcha"],$_REQUEST["myforms"]["captcharef"]);
113    self::$captchaIsValidated = self::$captchaIsValidated && $captchaIsValid;
114    return $captchaIsValid;
115  }
116 
117  public static function validateField($fieldName,$condition) {
118    global $_REQUEST;
119    $fieldIsValid = !isset($_REQUEST["myforms"]) || preg_match('#'.$condition.'#', @$_REQUEST["myforms"][$fieldName]);
120    self::$allFieldsAreValidated = self::$allFieldsAreValidated && $fieldIsValid;
121    return $fieldIsValid;
122  }
123 
124  public static function matchField($fieldName,$pattern) {
125    global $_REQUEST;
126    return preg_match('#'.$pattern.'#', @$_REQUEST["myforms"][$fieldName]);
127  }
128 
129  public static function checkQueryMatches($queryFilter)
130  {
131    if( !preg_match('#'.$queryFilter.'#', $_SERVER['REQUEST_METHOD']) )
132      self::p404();
133  }
134 
135  public static function checkBlogMatches($blogFilter)
136  {
137    global $core;
138    if( !preg_match('#'.$blogFilter.'#', $core->blog->id) )
139      self::p404();
140  }
141 
142  public static function display()
143  {
144    print self::$htmlOut;
145  }
146 
147  public static function info($name)
148  {
149    $infoFunction = MyFormsTplCore::GetFunction('Info_'.$name);
150    $infoFunction();
151  }
152 
153  public static function registerEvent($name)
154  {
155    self::$events[] = $name;
156  }
157 
158  private static function getCurrentEvent()
159  {
160    foreach( self::$events as $event )
161      if( isset($_REQUEST["myforms"][$event]) )
162        return $event;
163  }
164 
165  public static function execute($returnedError)
166  {
167    if($returnedError)
168      self::$errors[] = $returnedError;
169  }
170 
171  public static function hasErrors()
172  {
173    return count(self::$errors) > 0;
174  }
175 
176  public static function hasError($class,$message)
177  {
178    foreach( self::$errors as $error ) {
179      $hasClass = false;
180      $hasMessage = false;
181      if( preg_match('#'.$class.'#', $error[0]) )
182        $hasClass = true;
183      if( preg_match('#'.$message.'#', $error[1]) )
184        $hasMessage = true;
185      if($hasClass && $hasMessage)
186        return true;
187    }
188    return false;
189  }
190 
191  public static function allErrors()
192  {
193    return self::$errors;
194  }
195 
196  public static function goto($formID)
197  {
198    self::$nextFormID = $formID;
199  }
200 
201  public static function getFieldValue($name,$defaultValue)
202  {
203    global $_REQUEST;
204    if(isset($_REQUEST["myforms"][$name]))
205      return $_REQUEST["myforms"][$name];
206    else
207      return $defaultValue;
208  }
209 
210  public static function getFileFieldValue($name,$data)
211  {
212    global $_FILES;
213    if(isset($_FILES["myforms"][$data][$name]))
214      return $_FILES["myforms"][$data][$name];
215    else
216      return '';
217  }
218
219}
220
221?>
Note: See TracBrowser for help on using the repository browser.

Sites map