Dotclear

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

Revision 728, 4.8 KB checked in by Oaz, 15 years ago (diff)

new plugin : myForms (create custom forms)

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

Sites map