Location: PHPKode > scripts > AuriumForm > auriumform/class.auriumformfields.php
<?php
// ---------------------------------------------------------------------------
// Forms fields manipulator and generator by Alfred Reinold Baudisch <hide@address.com>
// Copyright © 2005 AuriumSoft - www.auriumsoft.com.br
// ---------------------------------------------------------------------------
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
// ---------------------------------------------------------------------------

/**
 * Forms fields manipulator and generator
 *
 * @author Alfred Reinold Baudisch <hide@address.com>
 * @since Feb 12, 2005
 * @version 1.2.3 - Feb 17, 2005
 *
 * -> Feb 16, 2005 - Added property maxlength to text box and password box,
 * added function AddMaxlength
 *
 * -> Feb 17, 2005
 * - Now all the fields accepts the attribute no_template, then
 * only the field and label (if given) will be returned by the class
 * - Added new function (new field) Field_brstates that generates a select box
 * with Brazilian States
 * - Add brackets on the name of multiple selectboxes that don't have them.
 * E.g.: your selectbox is multiple, but you gave the name preferences. Function
 * will change the name to preferences[]
 */
class AuriumFormFields
{
    function AuriumFormFields()
    {
        echo 'You can\'t use this class directly! It must be extended by the class AuriumForm. Then, use the AuriumForm class!';
        die;
    }

    /**
     * FILE BOX
     *
     * Values of the Data array:
     * Required:
     * type = file
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - size
     * - css
     * - id
     * - extras
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data File box properties
     * @access private
     */
    function Field_file(&$Data)
    {
        $this->IncrementCount('file');

        $return .= '<input type="file"';
        $return .= $this->AddName($Data);
        $return .= $this->AddSize($Data);
        $return .= $this->AddCss($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';

        return $return;
    }

    /**
     * TEXT BOX
     *
     * Values of the Data array:
     * Required:
     * type = text
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - size
     * - value
     * - css
     * - id
     * - extras
     * - css_error
     * - errors (array)
     * - maxlength
     * - no_template
     *
     * @param array $Data Text box properties
     * @version Feb 16, 2005
     * @access private
     */
    function Field_text(&$Data)
    {
        $this->IncrementCount('text');

        $return .= '<input type="text"';
        $return .= $this->AddName($Data);
        $return .= $this->AddSize($Data);
        $return .= $this->AddValue($Data);
        $return .= $this->AddCss($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddMaxlength($Data['maxlength']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';

        return $return;
    }

    /**
     * PASSWORD BOX
     *
     * Values of the Data array:
     * Required:
     * type = password
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - size
     * - value
     * - css
     * - id
     * - extras
     * - css_error
     * - errors (array)
     * - maxlength
     * - no_template
     *
     * @param array $Data Password box properties
     * @version Feb 16, 2005
     * @access private
     */
    function Field_password(&$Data)
    {
        $this->IncrementCount('password');

        $return .= '<input type="password"';
        $return .= $this->AddName($Data);
        $return .= $this->AddSize($Data);
        $return .= $this->AddValue($Data);
        $return .= $this->AddCss($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddMaxlength($Data['maxlength']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';

        return $return;
    }

    /**
     * TEXTAREA
     *
     * Values of the Data array:
     * Required:
     * - type = textarea
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - size (array(cols, rows))
     * - value
     * - css
     * - id
     * - extras
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data Textarea properties
     * @access private
     */
    function Field_textarea(&$Data)
    {
        $this->IncrementCount('textarea');

        $return .= '<textarea';
        $return .= $this->AddName($Data);
        $return .= $this->AddSize($Data);        
        $return .= $this->AddCss($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';
        $return .= $this->AddValue($Data);
        $return .= '</textarea>';

        return $return;
    }

    /**
     * BRAZILIAN STATES BOX
     *
     * Returns a select box with Brazilian States
     *
     * Values of the Data array:
     * Required:
     * - type = brstates
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name = if not given, use the name from var $BrStatesDefaultName
     * - size
     * - selected
     * - css
     * - id
     * - extras
     *    + must be 'multiple' if you want a multiple selectbox
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data Selectbox properties
     * @since Feb 17, 2005
     * @access private
     */
    function Field_brstates(&$Data)
    {
        // Default name for BR States box
        static $BrStatesDefaultName = 'estado';

        static $BRStates = array(
            '00' => 'Selecione',
            '01' => '===================',
            'AC' => 'Acre',
            'AL' => 'Alagoas',
            'AP' => 'Amapá',
            'AM' => 'Amazonas',
            'BA' => 'Bahia',
            'CE' => 'Ceará',
            'DF' => 'Distrito Federal',
            'ES' => 'Espirito Santo',
            'GO' => 'Goiás',
            'PB' => 'Paraíba',
            'PR' => 'Paraná',
            'PE' => 'Pernambuco',
            'PI' => 'Piauí',
            'RR' => 'Roraima',
            'MA' => 'Maranhão',
            'RO' => 'Rondônia',
            'MT' => 'Mato Grosso',
            'MS' => 'Mato Grosso do Sul',
            'MG' => 'Minas Gerais',
            'RJ' => 'Rio de Janeiro',
            'RN' => 'Rio Grande do Norte',
            'RS' => 'Rio Grande do Sul',
            'SC' => 'Santa Catarina',
            'SP' => 'São Paulo',
            'SE' => 'Sergipe',
            'TO' => 'Tocantins'
        );
        
        // Data to send to selectbox function
        $NewData = array();
        // Set items
        $NewData['items'] =& $BRStates;
        // Set selectbox type
        $NewData['type'] = 'selectbox';
        
        // If name wasn't given, use a default name
        if(!$Data['name'])
        {
            $NewData['name'] = $BrStatesDefaultName;
        }
        else
        {
            $NewData['name'] = $Data['name'];
        }

        // Send the atributes to the new array
        foreach($Data as $_key => $_val)
        {
            if($_key == 'name' || $_key == 'type')
            {
                continue;
            }

            $NewData[$_key] = $_val;
        }

        $return = $this->Field_selectbox($NewData);

        return $return;
    }

    /**
     * SELECTBOX
     *
     * Values of the Data array:
     * Required:
     * - type = selectbox
     * - items = array(value => text, value1 => text1, ...)
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - size
     * - selected:
     *     + for normal selectbox, it must be a string
     *     + for multiple selectbox, it must be an array with the selected value(s)
     * - css
     * - id
     * - extras
     *    + must be 'multiple' if you want a multiple selectbox
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data Selectbox properties
     * @version 1.1 Feb 17, 2005
     * @access private
     */
    function Field_selectbox(&$Data)
    {
        $this->IncrementCount('selectbox');

        if(!is_array($Data['items']))
        {
            $this->ShowError('SELECTBOX - Property ITEMS must be an array, example: array(\'BRA\' => \'Brazil\', \'GER\' => \'GERMANY\')');
            return false;
        }

        // If is multiple and name doesn't have brackets, add them
        if(eregi('multiple', $Data['extras']) && !eregi('\[\]$', $Data['name']))
        {
            $Data['name'] .= '[]';
        }

        $return .= '<select';
        $return .= $this->AddName($Data);
        $return .= $this->AddSize($Data);
        $return .= $this->AddCss($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';
        
        foreach($Data['items'] as $Value => $Text)
        {
            $return .= '<option value="' . $Value . '"';
            
            // Normal selectbox
            if(!is_array($Data['selected']) && $Data['selected'] == $Value)
            {
                $return .= ' selected';
            }
            // Multiple selectbox, multiple selected values
            elseif(is_array($Data['selected']) && in_array($Value, $Data['selected']))
            {
                $return .= ' selected';
            }

            $return .= '>' . $Text . '</option>';
        }

        $return .= '</select>';

        return $return;
    }

    /**
     * CHECK BOX
     *
     * Values of the Data array:
     * Required:
     * type = checkbox
     * - value
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     *
     * - text
     * - name
     * - selected
     *    + Can be string or array
     *
     * - text_clickable = true|false (if the text is clickable) - true for default
     * - css
     * - id
     * - extras
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data Check box properties
     * @access private
     */
    function Field_checkbox(&$Data)
    {
        // Default is true
        if(!isset($Data['text_clickable']))
        {
            $Data['text_clickable'] = true;
        }

        if(is_array($Data['value']))
        {
            for($i = 0; $i < sizeof($Data['value']); ++$i)
            {
                $this->IncrementCount('checkbox');

                $return .= '<input type="checkbox"';

                if(is_array($Data['name']))
                {
                    $return .= $this->AddName($Data, $i);
                }
                else
                {
                    $return .= $this->AddName($Data);
                }

                $return .= $this->AddValue($Data, $i);
                $return .= $this->AddCss($Data);

                if($Data['text_clickable'])
                {
                    $Data['id'] = $Data['type'] . '_' . $this->Count[$Data['type']];
                }

                $return .= $this->AddId($Data['id']);
                $return .= $this->AddExtras($Data['extras']);

                if(is_array($Data['selected']) && in_array($Data['value'][$i], $Data['selected']))
                {
                    $return .= ' checked';
                }
                elseif($Data['selected'] == $Data['value'][$i])
                {
                    $return .= ' checked';
                }

                $return .= '> ';
                
                if($Data['text_clickable'])
                {
                    $return .= '<label for="' . $Data['id'] . '">';
                }

                $return .= $Data['text'][$i];

                if($Data['text_clickable'])
                {
                    $return .= '</label>';
                }

                $return .= ' ';
            }
        }
        else
        {
            $this->IncrementCount('checkbox');

            $return .= '<input type="checkbox"';
            $return .= $this->AddName($Data);
            $return .= $this->AddValue($Data);
            $return .= $this->AddCss($Data);

            if($Data['text_clickable'])
            {
                $Data['id'] = $Data['type'] . '_' . $this->Count[$Data['type']];
            }

            $return .= $this->AddId($Data['id']);
            $return .= $this->AddExtras($Data['extras']);

            if(is_array($Data['selected']) && in_array($Data['value'], $Data['selected']))
            {
                $return .= ' checked';
            }
            elseif($Data['selected'] == $Data['value'])
            {
                $return .= ' checked';
            }

            $return .= '> ';

            if($Data['text_clickable'])
            {
                $return .= '<label for="' . $Data['id'] . '">';
            }

            $return .= $Data['text'];

            if($Data['text_clickable'])
            {
                $return .= '</label>';
            }

            $return .= ' ';
        }

        return $return;
    }

    /**
     * RADIO BUTTON
     *
     * Values of the Data array:
     * Required:
     * type = radio
     * - value (array)
     * - text (array)
     *
     * Optional:
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - text_clickable = true|false (if the text is clickable) - true for default
     * - name
     * - size
     * - selected
     * - css
     * - id
     * - extras
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data Radio button properties
     * @access private
     */
    function Field_radio(&$Data)
    {
        // Default is true
        if(!isset($Data['text_clickable']))
        {
            $Data['text_clickable'] = true;
        }

        for($i = 0; $i < sizeof($Data['value']); ++$i)
        {
            $this->IncrementCount('radio');

            $return .= '<input type="radio"';
            $return .= $this->AddName($Data);
            $return .= $this->AddValue($Data, $i);
            $return .= $this->AddCss($Data);

            if($Data['text_clickable'])
            {
                $Data['id'] = $Data['type'] . '_' . $this->Count[$Data['type']];
            }

            $return .= $this->AddId($Data['id']);
            $return .= $this->AddExtras($Data['extras']);

            if($Data['selected'] == $Data['value'][$i])
            {
                $return .= ' checked';
            }

            $return .= '> ';
            
            if($Data['text_clickable'])
            {
                $return .= '<label for="' . $Data['id'] . '">';
            }

            $return .= $Data['text'][$i];

            if($Data['text_clickable'])
            {
                $return .= '</label>';
            }

            $return .= ' ';
        }

        return $return;
    }

    /**
     * HIDDEN FIELD
     *
     * Values of the Data array:
     * Required:
     * type = hidden
     *
     * Optional:
     * - value
     * - name
     * - id
     * - extras
     * - no_template
     *
     * @param array $Data Hidden field properties
     * @access private
     */
    function Field_hidden(&$Data)
    {
        $this->IncrementCount('hidden');

        $return .= '<input type="hidden"';
        $return .= $this->AddName($Data);
        $return .= $this->AddValue($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';

        return $return;
    }

    /**
     * BUTTONS
     *
     * Values of the Data array:
     * Required:     
     * - type = button
     * - subtype = submit|button|reset (array or string)
     * - value (array or string)
     *
     * Optional (all (array or string)):
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - id
     * - extras
     * - css
     * - css_error
     * - errors (array)
     * - no_template
     *
     * @param array $Data Button properties
     * @access private
     */
    function Field_button(&$Data)
    {
        $this->IncrementCount('button');

        if(is_array($Data['subtype']))
        {
            for($i = 0; $i < sizeof($Data['subtype']); ++$i)
            {
                $return .= '<input type="' . $Data['subtype'][$i] . '"';        
                $return .= $this->AddName($Data, $i);
                $return .= $this->AddValue($Data, $i);
                $return .= $this->AddCss($Data);
                $return .= $this->AddId($Data['id'][$i]);
                $return .= $this->AddExtras($Data['extras'][$i]);
                $return .= '> ';
            }
        }
        else
        {
            $return .= '<input type="' . $Data['subtype'] . '"';        
            $return .= $this->AddName($Data);
            $return .= $this->AddValue($Data);
            $return .= $this->AddCss($Data);
            $return .= $this->AddId($Data['id']);
            $return .= $this->AddExtras($Data['extras']);
            $return .= '>';
        }

        return $return;
    }

    /**
     * IMAGE
     *
     * Values of the Data array:
     * Required:     
     * - type = image
     * - src
     *
     * Optional
     * - size (array(width, height))
     * - value
     * - alone = true|false (if is to use the template template_field_one)
     * - label
     * - name
     * - id
     * - extras
     * - errors (array)
     * - no_template
     *
     * @param array $Data Button properties
     * @access private
     */
    function Field_image(&$Data)
    {
        $this->IncrementCount('image');

        $return .= '<input type="image" src="' . $Data['src'] .  '"';        
        $return .= $this->AddName($Data);
        $return .= $this->AddValue($Data);
        $return .= $this->AddSize($Data);
        $return .= $this->AddId($Data['id']);
        $return .= $this->AddExtras($Data['extras']);
        $return .= '>';

        return $return;
    }

    /**
     * Add property 'name' to a field
     *
     * @param array $Data Field data
     * @param integer $Index In case of Multiple Checkboxes
     * @access private
     */
    function AddName(&$Data, $Index = false)
    {
        // No property name
        if($Data['name'] === false)
        {
            return;
        }

        $return .= ' name="';
    
        // In case of multiple checkboxes
        if(is_array($Data['name']))
        {
            $return .= $Data['name'][$Index];
        }
        elseif($Data['name'])
        {
            // Get name from the given config to this field
            $return .= $Data['name'];
        }
        else
        {
            // Generate a name using the field type
            $return .= $Data['type'] . '_' . $this->Count[$Data['type']];
        }

        $return .= '"';

        return $return;
    }

    /**
     * Add property 'size' to a field
     *
     * @param array $Data Field data
     * @access private
     */
    function AddSize(&$Data)
    {
        // No property size
        if($Data['size'] === false)
        {
            return;
        }

        // Is a selectbox and no size was given, so do not add default size
        if(!$Data['size'] && $Data['type'] == 'selectbox')
        {
            return;
        }

        // Is a textarea
        if($Data['type'] == 'textarea' && isset($Data['size']))
        {
            if(!$Data['size'][0] || !$Data['size'][1])
            {
                $this->ShowError('TEXTAREA - Property SIZE of a textarea must have the values of COLS and ROWS, ex: array(50, 5)');
                return false;
            }

            $return .= ' cols="' . $Data['size'][0] . '" ';
            $return .= 'rows="' . $Data['size'][1] . '"';
        }
        // Is a image
        elseif($Data['type'] == 'image' && isset($Data['size']))
        {
            if(!$Data['size'][0] || !$Data['size'][1])
            {
                $this->ShowError('IMAGE - Property SIZE of a image must have the values of WIDTH and HEIGHT, ex: array(468, 60)');
                return false;
            }

            $return .= ' width="' . $Data['size'][0] . '" ';
            $return .= 'height="' . $Data['size'][1] . '"';
        }
        // Other field type
        elseif(($Data['size'] || $this->Config['size']) && $Data['type'] != 'textarea' && $Data['type'] != 'image')
        {
            $return .= ' size="';

            // Get size from the given config to this field
            if($Data['size'])
            {
                $return .= $Data['size'];
            }
            // Get default size given in the global config
            else
            {
                $return .= $this->Config['size'];
            }

            $return .= '"';
        }

        return $return;
    }

    /**
     * Add property value to a field
     *
     * @param array $Value Field data
     * @param integer $Index In case of Multiple Checkboxes
     * @access private
     */
    function AddValue(&$Data, $Index = false)
    {
        // In case of multiple checkboxes
        if(is_array($Data['value']))
        {
            $return .= ' value="' . $Data['value'][$Index] . '"';
        }
        elseif($Data['type'] !== 'textarea' && $Data['value'])
        {
            $return .= ' value="' . $Data['value'] . '"';
        }
        elseif($Data['type'] == 'textarea' && $Data['value'])
        {
            $return .= $Data['value'];
        }

        return $return;
    }

    /**
     * Add property 'css' to a field
     *
     * @param array $Data Field data
     * @access private
     */
    function AddCss(&$Data)
    {
        // No property css and no errors has occurred
        if( ($Data['css'] === false && !$Data['errors']) || ($Data['errors'] && $Data['css_error'] === false) )
        {
            return;
        }

        $return .= ' class="';

        // Error has occurred and has css especial for errors
        if(sizeof($Data['errors']) && ($this->Config['css_error'] || $Data['css_error']))
        {
            // Get css_error from the given config to this field
            if($Data['css_error'])
            {
                $return .= $Data['css_error'];
            }
            // Get default css_error given in the global config
            else
            {
                $return .= $this->Config['css_error'];
            }
        }

        // - No error has occurred and has css
        // - Or error has occurred, but no css especial for errors, then, use the
        // common css
        elseif($Data['css'] || $this->Config['css'])
        {
            // Get css from the given config to this field
            if($Data['css'])
            {
                $return .= $Data['css'];
            }
            // Get default css given in the global config
            else
            {
                $return .= $this->Config['css'];
            }           
        }

        $return .= '"';

        return $return;
    }

    /**
     * Add property 'id' to a field
     *
     * @param string $Id The id
     * @access private
     */
    function AddId($Id)
    {
        if($Id)
        {
            $return .= ' id="';
            $return .= $Id;
            $return .= '"';
        }

        return $return;
    }

    /**
     * Add property 'maxlength' to a field
     *
     * @param string $Maxlength The maxlength
     * @since Feb 16, 2005
     * @access private
     */
    function AddMaxlength($Maxlength)
    {
        if($Maxlength)
        {
            $return .= ' maxlength="';
            $return .= $Maxlength;
            $return .= '"';
        }

        return $return;
    }

    /**
     * Add extras properties to a field, example:
     * onChange="setValue(this.value)"
     *
     * @param string $Extras The extras
     * @access private
     */
    function AddExtras($Extras)
    {
        if($Extras)
        {
            $return .= ' ' . $Extras;
        }

        return $return;
    }
}

?>
Return current item: AuriumForm