Dotclear

source: plugins/myForms/trunk/TplCore.php @ 2208

Revision 2208, 5.5 KB checked in by Oaz, 14 years ago (diff)

Unstable commit. Completed refactoring and added tests for checkboxes and radio buttons.

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('myformsContext',array('MyFormsTplCore','Context'));
22$core->tpl->addBlock('myformsPassword',array('MyFormsTplCore','Password'));
23$core->tpl->addBlock('myformsInfo',array('MyFormsTplCore','Info'));
24$core->tpl->addValue('myformsInfo',array('MyFormsTplCore','DisplayInfo'));
25$core->tpl->addBlock('myformsOnInit',array('MyFormsTplCore','OnInit'));
26$core->tpl->addValue('myformsDisplay',array('MyFormsTplCore','Display'));
27$core->tpl->addBlock('myformsOnSubmit',array('MyFormsTplCore','OnSubmit'));
28$core->tpl->addBlock('myformsOnSuccess',array('MyFormsTplCore','OnSuccess'));
29$core->tpl->addValue('myformsOnSuccess',array('MyFormsTplCore','OnSuccessGoto'));
30$core->tpl->addBlock('myformsOnError',array('MyFormsTplCore','OnError'));
31$core->tpl->addValue('myformsOnErrorGoto',array('MyFormsTplCore','OnErrorGoto'));
32$core->tpl->addBlock('myformsErrors',array('MyFormsTplCore','Errors'));
33$core->tpl->addValue('myformsError',array('MyFormsTplCore','Error'));
34
35class MyFormsTplCore
36{
37  // functions management
38  private static function DefineFunction($name,$content)
39  {
40    return '<?php
41function '.self::GetFunction($name).'() { global $core, $_ctx; ?>'.$content.'<?php
42} ?>';
43  }
44 
45  public static function GetFunction($name)
46  {
47    return 'myforms_'.ereg_replace("[^A-Za-z0-9]", "", MyForms::$formID ).'_'.$name;
48  }
49
50  // Check the form context
51  public static function Context($attr)
52  {
53    $checks = '<?php ';
54    if( isset($attr['query']) )
55      $checks .= 'MyForms::checkQueryMatches("'.$attr['query'].'");';
56    if( isset($attr['blog']) )
57      $checks .= 'MyForms::checkBlogMatches("'.$attr['blog'].'");';
58    $checks .= ' ?>';
59    return $checks;
60  }
61
62  // Define the form password
63  public static function Password($attr,$content)
64  {
65    return '<?php MyForms::password("'.$content.'"); ?>';
66  }
67
68  // Define the form information
69  public static function Info($attr,$content)
70  {
71    return self::DefineFunction('Info_'.$attr['name'],$content);
72  }
73
74  // Display the current form information
75  public static function DisplayInfo($attr)
76  {
77    return '<?php MyForms::info("'.$attr['name'].'"); ?>';
78  }
79
80  // Process / Display the current form
81  public static function Display($attr)
82  {
83    return '<?php MyForms::display(); ?>';
84  }
85 
86  // Define the form fields
87  public static function OnInit($attr,$content)
88  {
89    return
90    self::DefineFunction('Declare',
91      "<?php\n"
92      ."\$fields = new MyFormsFieldSet();\n"
93      ."?>\n"
94      .preg_replace(
95        array('#<tpl:(.*?)( .*?)?>#','#</tpl:(.*?)>#','#{{tpl:(.*?)( .*?)?}}#'),
96        array('<tpl:$1_Declare$2>','</tpl:$1_Declare>','{{tpl:$1_Declare$2}}'),
97        $content
98      )."\n"
99      ."<?php\n"
100      ."return \$fields;\n"
101      ."?>"
102    )
103    ."\n"
104    .self::DefineFunction('Display',"<form action='<?php print \$core->blog->url; ?>form' method='post' enctype='multipart/form-data'><input type='hidden' name='myforms[formID]' value='".MyForms::$formID."' />".$content."</form>");
105  }
106 
107  // Define the form actions
108  public static function OnSubmit($attr,$content)
109  {
110    return self::DefineFunction('OnSubmit_'.$attr['name'],$content)
111           .'<?php MyForms::registerEvent("'.$attr['name'].'"); ?>';
112  }
113 
114  // Display the action result
115  public static function OnSuccess($attr,$content)
116  {
117    return '<?php if(!MyForms::hasErrors()) { ?>'.$content.'<?php } ?>';
118  }
119  public static function OnError($attr,$content)
120  {
121    if( isset($attr['class']) || isset($attr['message']) )
122      return '<?php if(MyForms::hasError("'.$attr['class'].'","'.$attr['message'].'")) { ?>'.$content.'<?php } ?>';
123    else
124      return '<?php if(MyForms::hasErrors()) { ?>'.$content.'<?php } ?>';
125  }
126 
127  // Move to another form
128  public static function OnSuccessGoto($attr,$content)
129  {
130    return '<?php if(!MyForms::hasErrors()) MyForms::goto("'.$attr['goto'].'"); ?>';
131  }
132  public static function OnErrorGoto($attr,$content)
133  {
134    if( isset($attr['class']) || isset($attr['message']) )
135      return '<?php if(MyForms::hasError("'.$attr['class'].'","'.$attr['message'].'")) MyForms::goto("'.$attr['goto'].'"); ?>';
136    else
137      return '<?php if(MyForms::hasErrors()) MyForms::goto("'.$attr['goto'].'"); ?>';
138  }
139
140  public static $currentError;
141  public static function Errors($attr,$content)
142  {
143    return 
144    '<?php foreach(MyForms::allErrors() as MyFormsTplCore::$currentError) :
145    ?>'.
146    $content.
147    '<?php endforeach; ?>';
148  }
149  public static function Error($attr,$content)
150  {
151    if($attr['text'] == 'class')
152      return '<?php print MyFormsTplCore::$currentError[0]; ?>';
153    else if($attr['text'] == 'message')
154      return '<?php print MyFormsTplCore::$currentError[1]; ?>';
155    else
156      return '';
157  }
158
159}
160?>
Note: See TracBrowser for help on using the repository browser.

Sites map