[Plugin] Form Builder 0.1

A place to share your Plugins and Extensions

[Plugin] Form Builder 0.1

Postby raymond » Thu Dec 09, 2010 4:21 am

The Form Builder plugin is designed to generate an html web form from an array of input fields. This can be very useful when you do not want to spend valuable time writing several lines of html code.

Example:

Code: Select all
    <?php
        protected function _load() {
            $frm = $this->loadPlugin('form.builder'); // load plugin
            $html = $frm->buildForm(array(
                'to'=>'',
                'cc'=>'',
                'bcc'=>'',
                'subject' => '',
                'message' => array(
                    'type' => 'textarea',
                    'value' => 'message'
                ),
                'priority' => array(
                    'type' => 'select',
                    'value' => array('High', 'Medium', 'Low')
                ),
                'notify' => array(
                    'type' => 'checkbox',
                    'value' => 'Yes'
                ),
            ), 'Web form');

            $this->append($html);
        }
    ?>


Supported input fields: text, textarea, checkbox, radio, button, etc

More features coming soon.

Installation Instructions
To install the plugin source and save the it as raxan/plugins/form.builder.php or download and extract the attached file and copy the form.builder.php file to the raxan/plugins folder.

Here the Source code for the plugin
Code: Select all
<?php
/**
 * Form Builder Plugin
 * Copyright 2010 Raymond Irving
 * License: MIT/GPL
 *
 * Version: 0.1
 *
 */
class FormBuilderPlugin extends RaxanPlugin {
    public static $name = 'Form Builder Plugin';
    public static $description = "Generates an HTML form from an associative array";
    public static $author = "Raymond Irving";
    protected function methods() { return get_class_methods($this); }
    public static function register() { return self::instance(__CLASS__); }

    public function buildForm($arr,$title='') {
        $nl = "\n"; $form = array();
        $spaceChars = array('_','-');
        foreach($arr as $key=>$value) {
            $field = is_array($value) ? $value : array('value'=>$value);
            $value = isset($field['value']) ? $field['value'] : '';
            $type = isset($field['type']) ? strtolower($field['type']) : 'text';
            $label = isset($field['label']) ? $field['label'] : $key;
            $label = str_replace($spaceChars, ' ', $label);
            $label = ucwords($label);
            $cols = isset($field['cols']) ? $field['cols'] : 40;
            $rows = isset($field['rows']) ? $field['rows'] : 5;
            $checked = isset($field['checked']) ? true : false;
            $form[] = $this->tab().'<div class="ctrl-group">';
            if ($type=='paragraph') $form[] = $this->tab(2).'<p>'.htmlspecialchars($value).'</p>';
            else {
                $form[] = $this->tab(2).'<label>'.$label.'</label>';
                if ($type=='textarea') {    // textarea
                    $form[] = $this->tab(2).'<textarea name="'.htmlspecialchars($key).'" cols="'.$cols.'" rows="'.$rows.'">'.
                    htmlspecialchars($value).'</textarea>';
                }
                else if ($type=='select') { // select
                    $opt = '';
                    if (is_array($value)) foreach($value as $i=>$v) {
                        $opt.= $this->tab(3).'<option value="'.htmlspecialchars($i).'">'.htmlspecialchars($v).'</option>'.$nl;
                    }
                    $form[] = $this->tab(2).'<select name="'.htmlspecialchars($key).'">'.$nl.$opt.$this->tab(2).'</select>';
                }
                else if ($type!='select') { // input
                    $checked = ($checked && ($type=='checkbox'||$type=='radio')) ? ' checked="checked"' : '';
                    $form[] = $this->tab(2).'<input type="'.$type.'" name="'.
                        htmlspecialchars($key).'" value="'.htmlspecialchars($value).'"'.
                        $checked.' />';
                }
            }
            $form[] = $this->tab().'</div>';
        }
        $fields = implode("\n",$form);
        if ($fields) $fields.= $nl.$this->tab().'<div class="ctrl-button-group">'.$nl.
            $this->tab(2).'<input type="submit" name="cmdSubmit" value="Submit" />'.$nl.
            $this->tab().'</div>';
        return  $nl.'<form name="form1" action="" method="post">'.$nl.
                $this->tab().'<h2>'.htmlspecialchars($title).'</h2>'.$nl.
                $fields.'</form>'.$nl;
    }

    protected function tab($i = 1) {
        return str_repeat("    ", $i);
    }

}
FormBuilderPlugin::register();


?>
Attachments
form.builder.rar
Form Builder Plugin (RAR)
(1.13 KiB) Downloaded 207 times
form.builder.zip
Form Builder Plugin
(1.16 KiB) Downloaded 225 times
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am

Re: [Plugin] Form Builder 0.1

Postby fc_Damir » Fri Dec 10, 2010 11:23 am

Hi Raymond,

good plugin! I need this.

But I can't open the zip archive. Is corrupted.

Please can you upload it again?

Thanks

Best regards
Damir
fc_Damir
 
Posts: 33
Joined: Mon Jun 14, 2010 9:13 am

Re: [Plugin] Form Builder 0.1

Postby raymond » Fri Dec 10, 2010 2:00 pm

Hi Damir,

Can you try it again. It appears to be working because I was able to download and extract the attached zip file. Maybe a corrupt download?

__
Raymond
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am

Re: [Plugin] Form Builder 0.1

Postby fc_Damir » Fri Dec 10, 2010 2:42 pm

still not working

I download many time. When I try unzip there is a error message (no files to extract)
and if I try open with "explore" the folder is empty!
fc_Damir
 
Posts: 33
Joined: Mon Jun 14, 2010 9:13 am

Re: [Plugin] Form Builder 0.1

Postby raymond » Fri Dec 10, 2010 4:59 pm

I've uploaded a RAR version of the plugin. Give it a try and let me know if it works.
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am

Re: [Plugin] Form Builder 0.1

Postby fc_Damir » Fri Dec 10, 2010 5:08 pm

unfortunately, still not working :cry:

I try with different pc and browser, but the result is the same.
file is broken is the result after unzip (I use 7zip).
fc_Damir
 
Posts: 33
Joined: Mon Jun 14, 2010 9:13 am

Re: [Plugin] Form Builder 0.1

Postby raymond » Fri Dec 10, 2010 9:49 pm

Hi Damir,

That's very strange :(

I've add the plugin source code to the initial post. You will have to copy and save the source as raxan/plugins/form.builder.php


Best regards,

__
Raymond
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am

Re: [Plugin] Form Builder 0.1

Postby fc_Damir » Fri Dec 10, 2010 11:24 pm

Hi,
Yess, very strange.
mysteries of life... :shock:
Thanks
Best regards
fc_Damir
 
Posts: 33
Joined: Mon Jun 14, 2010 9:13 am

Re: [Plugin] Form Builder 0.1

Postby fc_Damir » Sat Dec 11, 2010 10:22 am

Hello everyone,

I modified the plugin by adding the capability to assign classes to the label, button and input so the form generated look nice.

see here preview: http://dgweb.ch/rax/_formbuilder.php

Usage:
Code: Select all
   
    $frm = $this->loadPlugin('formbuilder'); // load plugin
    $html = $frm->buildForm(array(
        'to:'=>array(
          'classLabel'=>'left c6',
          'classInput'=>'textbox c10'
        ),
        'cc:'=>array(
          'classLabel'=>'left c6',
          'classInput'=>'textbox c10'
        ),
        'bcc:'=>array(
          'classLabel'=>'left c6',
          'classInput'=>'textbox c10'
        ),
        'subject:'=>array(
          'classLabel'=>'left c6',
          'classInput'=>'textbox c10'
        ),
        'message:' => array(
          'type' => 'textarea',
          'value' => 'message',
          'classInput' => 'textbox',
          'classLabel'=>'left c6'
        ),
        'priority:' => array(
          'type' => 'select',
          'value' => array('High', 'Medium', 'Low'),
          'classLabel'=>'left c6',
          'classInput'=>'textbox c10'
        ),
        'notify:' => array(
          'type' => 'checkbox',
          'value' => 'Yes',
          'classLabel'=>'left c6'
        ),
        'button' => array(
          'classSubmit' => 'button process ltm',
          'classReset' => 'button cancel ltm',
          'prepend' => 'prepend6'  //must be same that label width
        )
    ), 'Web form');

    $this->formbuilder->append($html);


You can add also this style for align label to right, so looking better:
Code: Select all
    <style>
    label{
      text-align:right;padding-right:10px
    }
    </style>



Plugin forum builder 0.2:

Code: Select all


<?php
    /**
    * Form Builder Plugin
    * Copyright 2010 Raymond Irving
    * License: MIT/GPL
    *
    * Version: 0.2
    *
    */
    class FormBuilderPlugin extends RaxanPlugin {
        public static $name = 'Form Builder Plugin';
        public static $description = "Generates an HTML form from an associative array";
        public static $author = "Raymond Irving";
        protected function methods() { return get_class_methods($this); }
        public static function register() { return self::instance(__CLASS__); }

        public function buildForm($arr,$title='') {
            $nl = "\n"; $form = array();
            $spaceChars = array('_','-');
            foreach($arr as $key=>$value) {

                if($key == 'button') continue; //skip if is button

                $field = is_array($value) ? $value : array('value'=>$value);
                $value = isset($field['value']) ? $field['value'] : '';
                $type = isset($field['type']) ? strtolower($field['type']) : 'text';
                $label = isset($field['label']) ? $field['label'] : $key;
                $label = str_replace($spaceChars, ' ', $label);
                $label = ucwords($label);
                $cols = isset($field['cols']) ? $field['cols'] : 40;
                $rows = isset($field['rows']) ? $field['rows'] : 5;

                //add class for label and input
                $classInput = isset($field['classInput']) ? ' class="'.$field['classInput'].'"' : '';
                $classLabel = isset($field['classLabel']) ? ' class="'.$field['classLabel'].'"' : '';

                $checked = isset($field['checked']) ? true : false;
                $form[] = $this->tab().'<div class="ctrl-group">';
                if ($type=='paragraph') $form[] = $this->tab(2).'<p>'.htmlspecialchars($value).'</p>';
                else {
                    $form[] = $this->tab(2).'<label'.$classLabel.'">'.$label.'</label>';
                    if ($type=='textarea') {    // textarea
                        $form[] = $this->tab(2).'<textarea name="'.htmlspecialchars($key).'" cols="'.$cols.'" rows="'.$rows.'"'.$classInput.'>'.
                        htmlspecialchars($value).'</textarea>';
                    }
                    else if ($type=='select') { // select
                        $opt = '';
                        if (is_array($value)) foreach($value as $i=>$v) {
                            $opt.= $this->tab(3).'<option value="'.htmlspecialchars($i).'">'.htmlspecialchars($v).'</option>'.$nl;
                        }
                        $form[] = $this->tab(2).'<select name="'.htmlspecialchars($key).'"'.$classInput.'>'.$nl.$opt.$this->tab(2).'</select>';
                    }
                    else if ($type!='select') { // input
                        $checked = ($checked && ($type=='checkbox'||$type=='radio')) ? ' checked="checked"' : '';
                        $form[] = $this->tab(2).'<input type="'.$type.'"'.$classInput.' name="'.
                            htmlspecialchars($key).'" value="'.htmlspecialchars($value).'"'.
                            $checked.' />';
                    }
                }
                $form[] = $this->tab().'</div>';
            }
            $fields = implode("\n",$form);

            //check if button class is set
            $arr['button']['classSubmit'] = empty($arr['button']['classSubmit'])? '' : 'class="'.$arr['button']['classSubmit'].'"';
            $arr['button']['classReset'] = empty($arr['button']['classReset'])? '' : 'class="'.$arr['button']['classReset'].'"';

            if ($fields) $fields.= $nl.$this->tab().'<div class="ctrl-button-group '.$arr['button']['prepend'].'">'.$nl.
                $this->tab(2).'<input type="submit" name="cmdSubmit" value="Submit" '.$arr['button']['classSubmit'].' />'.$nl.
                $this->tab(2).'<input type="reset" name="cmdReset" value="Reset" '.$arr['button']['classReset'].' />'.$nl.
                $this->tab().'</div>';
            return  $nl.'<form name="form1" action="" method="post">'.$nl.
                    $this->tab().'<h2>'.htmlspecialchars($title).'</h2>'.$nl.
                    $fields.'</form>'.$nl;
        }

        protected function tab($i = 1) {
            return str_repeat("    ", $i);
        }

    }
    FormBuilderPlugin::register();

?>



fc_Damir
 
Posts: 33
Joined: Mon Jun 14, 2010 9:13 am

Re: [Plugin] Form Builder 0.1

Postby raymond » Sat Dec 11, 2010 11:54 pm

Hi Damir,

Thanks for sharing your updates.

I'll apply your changes along with a few others as soon as I get some time.

__
Raymond
raymond
Site Admin
 
Posts: 215
Joined: Tue Mar 17, 2009 5:04 am


Return to Plugins & Widgets

Who is online

Users browsing this forum: No registered users and 2 guests

cron