XOOPS RMCommon Utilities  2.1.8.91RC
 All Classes Namespaces Files Functions Variables
form.class.php
Go to the documentation of this file.
1 <?php
2 // $Id: form.class.php 1016 2012-08-26 23:28:48Z i.bitcero $
3 // --------------------------------------------------------------
4 // Red México Common Utilities
5 // A framework for Red México Modules
6 // Author: Eduardo Cortés <i.bitcero@gmail.com>
7 // Email: i.bitcero@gmail.com
8 // License: GPL 2.0
9 // --------------------------------------------------------------
10 
11 
13 
14 include_once RMCPATH.'/class/fields/formelement.class.php';
15 include_once RMCPATH.'/class/fields/text.class.php';
16 include_once RMCPATH.'/class/fields/select.class.php';
17 include_once RMCPATH.'/class/fields/file.class.php';
18 include_once RMCPATH.'/class/fields/hidden.class.php';
19 include_once RMCPATH.'/class/fields/button.class.php';
20 include_once RMCPATH.'/class/fields/editor.class.php';
21 include_once RMCPATH.'/class/fields/groups.class.php';
22 include_once RMCPATH.'/class/fields/checks.class.php';
23 include_once RMCPATH.'/class/fields/formuser.class.php';
24 include_once RMCPATH.'/class/fields/theme.class.php';
25 include_once RMCPATH.'/class/fields/language.class.php';
26 include_once RMCPATH.'/class/fields/modules.class.php';
27 include_once RMCPATH.'/class/fields/timezone.class.php';
28 include_once RMCPATH.'/class/fields/cachemod.class.php';
29 include_once RMCPATH.'/class/fields/avatar.class.php';
30 include_once RMCPATH.'/class/fields/textoptions.class.php';
31 include_once RMCPATH.'/class/fields/formuser.class.php';
32 include_once RMCPATH.'/class/fields/date.class.php';
33 include_once RMCPATH.'/class/fields/formimage.class.php';
34 include_once RMCPATH.'/api/editors/tinymce/tinyeditor.php';
35 
36 $rmEvents->run_event("rmcommon.form.loader");
37 
42 $tiny->configuration = array('mode'=>'exact',
43  'theme'=>'advanced',
44  'skin'=>"exm_theme",
45  'inlinepopups_skin'=>'exm',
46  //'plugins'=>"inlinepopups,spellchecker,media,fullscreen,exmsystem",
47  'plugins' => $rmEvents->run_event('rmcommon.tiny.plugins.editor',"exmsystem,autolink,lists,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template"),
48  'theme_advanced_buttons1'=>RMEvents::get()->run_event('rmcommon.tinybuttons.toolbar1', "bold,italic,strikethrough,|,bullist,numlist,blockquote,|,justifyleft,justifycenter,justifyright,|,link,unlink,|,image,fullscreen,|,exm_more,exm_adv"),
49  'theme_advanced_buttons2'=>RMEvents::get()->run_event('rmcommon.tinybuttons.toolbar2', "underline,justifyfull,forecolor,|,removeformat,|,media,charmap,|,outdent,indent,|,undo,redo,|,exm_page,exm_img,exm_icons"),
50  'theme_advanced_buttons3'=>RMEvents::get()->run_event('rmcommon.tinybuttons.toolbar3', "styleselect,formatselect,insertimage,|,tablecontrols"),
51  'theme_advanced_buttons4'=>RMEvents::get()->run_event('rmcommon.tinybuttons.toolbar4', ""),
52  'spellchecker_languages'=>"+English=en,Danish=da,Dutch=nl,Finnish=fi,French=fr,German=de,Italian=it,Polish=pl,Portuguese=pt,Spanish=es,Swedish=sv",
53  'theme_advanced_toolbar_location'=>"top",
54  'theme_advanced_toolbar_align'=>"left",
55  'theme_advanced_statusbar_location'=>"bottom",
56  'theme_advanced_resizing'=>"1",
57  'theme_advanced_resize_horizontal'=>"",
58  'dialog_type'=>"modal",
59  'relative_urls'=>"",
60  'remove_script_host'=>"",
61  'convert_urls'=>"",
62  'apply_source_formatting'=>"",
63  'remove_linebreaks'=>"1",
64  'paste_convert_middot_lists'=>"1",
65  'paste_remove_spans'=>"1",
66  'paste_remove_styles'=>"1",
67  'gecko_spellcheck'=>"1",
68  'entities'=>"38,amp,60,lt,62,gt",
69  'accessibility_focus'=>"1",
70  'tab_focus'=>"'=>prev,'=>next",
71  'save_callback'=>"switchEditors.saveCallback",
72  'content_css'=>RMTemplate::get()->style_url('editor.css','rmcommon'));
73 
78 class RMForm
79 {
80  private $_fields = array();
81  protected $_name = '';
82  protected $_action = '';
83  protected $_extra = '';
84  protected $_method = '';
85  protected $_title = '';
86 
87  private $_tableClass = 'outer';
88  private $_thClass = '';
89  private $_headClass = 'head';
90  private $_evenClass = 'even';
91  private $_oddClass = 'odd';
92  private $_footClass = 'foot';
93  private $_addtoken = true;
94  private $_oddStyle='';
95  private $_oddSpanStyle;
96  private $_evenStyle='';
97  private $_headStyle='';
98  private $_thStyle='';
99  private $_footStyle='';
100 
101  private $_othervalidates = '';
102  private $_alertColor = '#FF0000';
103  private $_okColor = '#000';
104 
105  private $editores = ''; // LIsta de editores Tiny
106  private $_tinytheme = 'advanced';
107  private $_tinycss = '';
108  private $tiny_valid_tags = 'a[name|href|target|title|onclick],code[class,id],img[class|src|border=0|alt|title|hspace|vspace|width|height|align|onmouseover|onmouseout|name|longdesc|style],hr[class|width|size|noshade],font[face|size|color|style],span[class|align|style]';
109 
110  private $row_extras = array();
111 
118  function __construct($title, $name, $action, $method='post',$addtoken=true)
119  {
120  global $xoopsSecurity;
121 
122  static $instance;
123  $instance = $this;
124  $this->_name = $name;
125  $this->_action = $action;
126  $this->_method = $method;
127  $this->_title = $title;
128  $this->_addtoken = $addtoken;
129 
130  RMTemplate::get()->add_style('forms.css', 'rmcommon');
131  RMTemplate::get()->add_style('js-widgets.css','rmcommon');
132  RMTemplate::get()->add_jquery(true);
133  RMTemplate::get()->add_script(RMCURL.'/include/js/jquery.validate.min.js');
134  RMTemplate::get()->add_local_script('forms.js','rmcommon','include');
135 
136  if ($addtoken) $this->addElement(new RMFormHidden('XOOPS_TOKEN_REQUEST', $xoopsSecurity->createToken()));
137 
138  }
139 
144  public function setTitle($value){
145  $this->_title = $value;
146  }
151  public function getTitle(){
152  return $this->_title;
153  }
161  public function setExtra($extra){
162  $this->_extra = $extra;
163  }
168  public function getExtra(){
169  return $this->_extra;
170  }
176  public function setMethod($method){
177  if ($method == 'post' || $method == 'get'){
178  $this->_method = $method;
179  }
180  }
181  public function method(){
182  return $this->_method;
183  }
188  public function setName($name){
189  $this->_name = trim($name);
190  }
195  public function getName(){
196  return $this->_name;
197  }
202  public function setAction($action){
203  $this->_action = $action;
204  }
209  public function getAction(){
210  return $this->_action;
211  }
212 
216  public function cssfile(){
217  return 'forms.css';
218  }
223  public function clear($field = ''){
224  if ($field!=''){
225  if (isset($this->_fields[$field])) unset($this->_fields[$field]);
226  return;
227  }
228  $this->_fields = array();
229  $this->_name = '';
230  $this->_action = '';
231  $this->_extra = '';
232  $this->_method = '';
233  $this->_title = '';
234  }
242  public function addElement(&$element, $required=false, $css_type=''){
243  $element->setForm($this->_name);
244  $ret['field'] = $element;
245  $ret['class'] = get_class($element);
246  if (get_class($element)=='RMFormEditor'){
247  if ($element->getType()=='tiny') $this->editores[] = $element->getName();
248  }
249 
250  if ($required)
251  $element->addClass('required');
252 
253  if ($css_type!='')
254  $element->addClass($css_type);
255 
256  if ($element->getName()!=''){
257  $this->_fields[$element->getName()] = $ret;
258  }else{
259  $this->_fields[] = $ret;
260  }
261 
262  return $element;
263 
264  }
265 
266  public function elements(){
267  return $this->_fields;
268  }
269 
270  public function &element($name){
271  if (isset($this->_fields[$name])){
272  return $this->_fields[$name]['field'];
273  }
274  }
283  public function tableClass($value){
284  if ($value!='') $this->_tableClass = $value;
285  }
290  public function thClass($value){
291  if ($value!='') $this->_thClass = $value;
292  }
297  public function headClass($value){
298  if ($value!='') $this->_headClass = $value;
299  }
304  public function evenClass($value){
305  if ($value!='') $this->_evenClass = $value;
306  }
311  public function oddClass($value){
312  if ($value!='') $this->_oddClass = $value;
313  }
318  public function footClass($value){
319  if (trim($value)!='') $this->_footClass = $value;
320  }
321 
327  public function styles($style,$id){
328  switch ($id){
329  case 'odd':
330  $this->_oddStyle=$style;
331  break;
332  case 'even':
333  $this->_evenStyle=$style;
334  break;
335  case 'head':
336  $this->_headStyle=$style;
337  break;
338  case 'th':
339  $this->_thStyle=$style;
340  break;
341  case 'foot':
342  $this->_footStyle=$style;
343  break;
344  case 'oddspan':
345  $this->_oddSpanStyle = $style;
346  break;
347  }
348 
349  }
350 
351 
355  public function addValidateField($name, $type='', $required=0, $text=''){
356  if ($name=='') return;
357  if ($text=='') return;
358 
359  $this->_othervalidates = ($this->_othervalidates=='' ? "" : ",")."$name|$type|$required|$text";
360 
361  }
365  public function tinyCSS($url){
367  $tiny->add_config('content_css', $url);
368  }
369  public function getTinyCSS(){
371  return $tiny->configuration['content_css'];
372  }
381  public function setRowExtras($extra, $id){
382  $this->row_extras[$id] = $extra;
383  }
391  public function render($form_tag=true){
395  $form =& $this;
396  ob_start();
397  include RMTemplate::get_template('forms.php','module','rmcommon');
398  return ob_get_clean();
399 
400  }
408  public function renderForTemplate(){
409 
410  $form = array();
411 
412  $req = '';
413  $callmethod = '';
414  foreach ($this->_fields as $field){
415 
416  $element = $field['field'];
417  $form['fields'][] = array(
418  'type'=>get_class($element),
419  'content'=>$element->render(),
420  'caption'=>$element->getCaption(),
421  'desc'=>$element->getDescription(),
422  'name'=>$element->getName()
423  );
424 
425  if (is_a($element, 'RMFormEditor')){
426  if ($element->getType()=='tiny'){
427  $callmethod = 'tinyMCE.triggerSave(); ';
428  }
429  }
430 
431  }
432 
433  if ($this->_addtoken){
434  $form['fields'][] = array('type'=>'RMFormHidden','content'=>$GLOBALS['xoopsSecurity']->getTokenHTML(),
435  'caption'=>'','desc'=>'');
436  }
437 
438  $req .= $req=='' ? ($this->_othervalidates!='' ? $this->_othervalidates : '') : ($this->_othervalidates!='' ? ','.$this->_othervalidates : '');
439 
440  $rtn = "<form name='".$this->_name."' id='".$this->_name."' action='".$this->_action."' method='".$this->_method."'";
441  if ($req!=''){
442  $rtn .= " onsubmit=\"".$callmethod."rmValidateForm(this, '$req');return document.rmValidateReturnValue;\"";
443  }
444  if ($this->_extra != ''){
445  $rtn .= " ".$this->_extra;
446  }
447  $rtn .= ">";
448  $form['title'] = $this->_title;
449  $form['tag'] = $rtn;
450  $form['lang_req'] = __('Fields marked with (*) are required.','rmcommon');
451 
452  return $form;
453 
454  }
463  private function getType($type){
464  if ($type=='Email'){
465  return "email";
466  }elseif ($type=='Num'){
467  return "num";
468  }elseif (substr($type,0,5)=='Range'){
469  return "range".str_replace("Range", "", $type);
470  }elseif (substr($type,0,6)=='Select'){
471  return "difto".str_replace("Select", "", $type);
472  }else{
473  return $type;
474  };
475  }
479  public function display($js=true){
480  $form =& $this;
481  include RMTemplate::get_template('forms.php','rmcommon');
482  //echo $this->render($js);
483  }
488  public function setTinyTags($tags){
490  $tiny->configuration['extended_valid_elements'] = $tags;
491  }
492  public function tinyTags(){
494  return $tiny->configuration['extended_valid_elements'];
495  }
501 }