Dotclear

source: plugins/myForms/trunk/TplFields.php @ 2207

Revision 2207, 4.5 KB checked in by Oaz, 14 years ago (diff)

Unstable commit. Plugin under refactoring. Added Field classes hierarchy. Added Combo field. Added tests suite.

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
21$core->tpl->addValue('myformsFileFieldValue',array('MyFormsTplFields','FileFieldValue'));
22$core->tpl->addBlock('myformsFieldWarning',array('MyFormsTplFields','FieldWarning'));
23$core->tpl->addBlock('myformsTextArea',array('MyFormsTplFields','TextArea'));
24$core->tpl->addValue('myformsCheckbox',array('MyFormsTplFields','Checkbox'));
25$core->tpl->addValue('myformsRadioButton',array('MyFormsTplFields','RadioButton'));
26$core->tpl->addValue('myformsFileField',array('MyFormsTplFields','FileField'));
27$core->tpl->addValue('myformsHiddenField',array('MyFormsTplFields','HiddenField'));
28$core->tpl->addValue('myformsCaptchaField',array('MyFormsTplFields','CaptchaField'));
29$core->tpl->addBlock('myformsCaptchaWarning',array('MyFormsTplFields','CaptchaWarning'));
30
31class MyFormsTplFields
32{
33  // Field Attributes
34  private static function GetAttributes($attr,$name=false,$id=false)
35  {
36    if( $name )
37      $attr['name'] = $name;
38    if( !$id )
39      $id = $attr['name'];
40    $attributes = "id='myforms_".$id."' name='myforms[".$attr['name']."]'";
41    foreach( $attr as $k => $v )
42      if( $k != 'name' )
43        $attributes .= " ".$k."='".$v."'";
44    return $attributes;
45  }
46 
47  /*
48  // Field Value
49  private static function getFieldValue($attr,$content,$asHtml)
50  {
51    if( $asHtml )
52      return '<?php ob_start(); ?>'.$content.'<?php echo nl2br(htmlentities(MyForms::getFieldValue("'.$attr['name'].'",ob_get_clean()),ENT_QUOTES,"UTF-8")); ?>';
53    else
54      return '<?php ob_start(); ?>'.$content.'<?php echo MyForms::getFieldValue("'.$attr['name'].'",ob_get_clean()); ?>';
55  }
56  */
57 
58  // File Field Value
59  public static function FileFieldValue($attr)
60  {
61    return '<?php echo MyForms::getFileFieldValue("'.$attr['name'].'","'.$attr['data'].'"); ?>';
62  }
63 
64  // Validate Field
65  public static function FieldWarning($attr,$content)
66  {
67    return '<?php if( !MyForms::validateField("'.$attr['name'].'","'.$attr['validate'].'") ) { ?>'.$content.'<?php } ?>';
68  }
69 
70  // Display TextArea Field
71  public static function TextArea($attr,$content)
72  {
73    return "<textarea ".self::GetAttributes($attr).">".self::getFieldValue($attr,$content)."</textarea>";
74  }
75
76  // Display Checkbox Field
77  public static function Checkbox($attr)
78  {
79    $checked = '<?php echo MyForms::getFieldValue("'.$attr['name'].'","")?" checked=\'1\'":""; ?>';
80    return "<input type='checkbox' ".self::GetAttributes($attr)." value='checked'".$checked." />";
81  }
82
83  // Display Radio Button Field
84  public static function RadioButton($attr)
85  {
86    $checked = '<?php echo (MyForms::getFieldValue("'.$attr['name'].'","")=="'.$attr['value'].'")?" checked=\'1\'":""; ?>';
87    return "<input type='radio' ".self::GetAttributes($attr,$attr['name'],$attr['value']).$checked." />";
88  }
89
90  // Display File Upload Field
91  public static function FileField($attr)
92  {
93    return "<input type='file' ".self::GetAttributes($attr)." />";
94  }
95
96  // Display Hidden Field
97  public static function HiddenField($attr)
98  {
99    return "<input type='hidden' ".self::GetAttributes($attr)." value='".self::getFieldValue($attr,'')."' />";
100  }
101
102  // Display Captcha Field
103  public static function CaptchaField($attr,$content)
104  {
105    return '<?php MyFormsCaptcha::display("'.self::GetAttributes($attr,'captcharef').'","'.self::GetAttributes($attr,'captcha').'"); ?>';
106  }
107
108  // Display Warning when Captcha code do not match
109  public static function CaptchaWarning($attr,$content)
110  {
111    return '<?php if( !MyForms::validateCaptcha() ) { ?>'.$content.'<?php } ?>';
112  }
113
114  // Display Submit Field
115  public static function Submit($attr,$content)
116  {
117    return "<input type='submit' ".self::GetAttributes($attr)." value='".self::getFieldValue($attr,$content)."' />";
118  }
119
120}
121?>
Note: See TracBrowser for help on using the repository browser.

Sites map