* @copyright Copyright (c) 2010, ConsultorPC * @license http://www.gnu.org/licenses/lgpl-3.0-standalone.html * @link http://smartclientphp.com/ * @since Version 0.1b * @filesource */ /** * Smart Client Form Helper * */ class ScForm extends ScGeneral { /** * @var array */ protected $_fields; /** * @var string Default title orientation for the form fields */ protected $_defaultTitleOrientation = 'left'; /** * * Add Button * * @param mixed $data If string, it must be the title of the field. If array, them we will loop on it * @param array $options Additional options * */ public function addButton( $data , $click = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['title'] = $data; $newField['click'] = $click; $newField['type'] = 'button'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Checkbox Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param boolean $checked Default value * @param array $options Additional options * */ public function addCheckboxField( $data , $title = '' , $checked = false , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['defaultValue'] = $checked; $newField['type'] = 'checkbox'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Custom Field * * @param array $data Array containing the field information * */ public function addCustomField( $data ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; // Add field to the array $this->_fields[] = $newField; } } /** * * Add Date Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param string $defaultValue Default value * @param boolean $useTextField Use a text field * @param string $displayFormat Display format * @param string $startDate Start date * @param string $endDate End date * @param array $options Additional options * */ public function addDateField( $data , $title = '' , $defaultValue = '' , $useTextField = false , $displayFormat = '' , $startDate = '' , $endDate = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['defaultValue'] = $defaultValue; $newField['useTextField'] = $useTextField; $newField['displayFormat'] = $displayFormat; $newField['startDate'] = $startDate; $newField['endDate'] = $endDate; $newField['type'] = 'date'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Header * * @param mixed $data If string, it must be the value of the field. If array, them we will loop on it * @param array $options Additional options * */ public function addHeader( $data , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['defaultValue'] = $data; $newField['type'] = 'header'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Hidden Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $defaultValue Default value for the hidden field * @param array $options Additional options * */ public function addHiddenField( $data , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'hidden'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Radio Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param array $valueMap Array containing the values for this select field * @param mixed $defaultValue Default value * @param array $options Additional options * */ public function addRadioField( $data , $title = '' , $valueMap = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['valueMap'] = $valueMap; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'radioGroup'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Spinner Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param float $min Min value * @param float $max Max value * @param float $step Step value * @param float $defaultValue Default value * @param array $options Additional options * */ public function addSpinnerField( $data , $title = '' , $min = '' , $max = '' , $step = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['min'] = $min; $newField['max'] = $max; $newField['step'] = $step; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'spinner'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Password Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param string $defaultValue Default value for the text area * @param array $options Additional options * */ public function addPasswordField( $data , $title = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'password'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Slider Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param float $min Min value * @param float $max Max value * @param float $step Step value * @param float $defaultValue Default value * @param array $options Additional options * */ public function addSliderField( $data , $title = '' , $minValue = '' , $maxValue = '' , $numValues = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['minValue'] = $minValue; $newField['maxValue'] = $maxValue; $newField['numValues'] = $numValues; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'slider'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Section * * @param mixed $data If string, it must be a unique value for this section. If array, them we will loop on it * @param string $defaultValue Title of the section * @param array $itemIds Fields on this section * @param boolean $sectionExpanded True if section is expanded * @param array $options Additional options * */ public function addSection( $data , $defaultValue = '' , $itemIds = '' , $sectionExpanded = true , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['ID'] = $data; $newField['defaultValue'] = $defaultValue; $newField['itemIds'] = $itemIds; $newField['sectionExpanded'] = $sectionExpanded; $newField['type'] = 'section'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Set $_currentSection $this->_currentSection = $newField['ID']; // Add field to the array $this->_fields[] = $newField; } /** * * Add Select Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param array $valueMap Array containing the values for this select field * @param mixed $defaultValue Default value * @param array $options Additional options * */ public function addSelectField( $data , $title = '' , $valueMap = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['valueMap'] = $valueMap; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'select'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Submit Button * * @param mixed $data If string, it must be the value of the field. If array, them we will loop on it * @param array $options Additional options * */ public function addSubmitButton( $data , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['title'] = $data; $newField['type'] = 'submit'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Text Area Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param string $defaultValue Default value for the text area * @param array $options Additional options * */ public function addTextArea( $data , $title = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'textArea'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Text Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param string $defaultValue Default value for the text area * @param array $options Additional options * */ public function addTextField( $data , $title = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'text'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Time Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param string $defaultValue Default value * @param array $options Additional options * */ public function addTimeField( $data , $title = '' , $defaultValue = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['defaultValue'] = $defaultValue; $newField['type'] = 'time'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Toolbar Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param array $buttons Array containing all buttons for this toolbar * @param array $options Additional options * */ public function addToolbarField( $data , $buttons , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['buttons'] = $buttons; $newField['type'] = 'toolbar'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * * Add Upload Field * * @param mixed $data If string, it must be the name of the field. If array, them we will loop on it * @param string $title Title of the field * @param array $options Additional options * */ public function addUploadField( $data , $title = '' , $options = '' ) { $newField = array(); // Check if it is an array if ( is_array( $data ) ) { $newField = $data; } else { // Add vars to the array $newField['name'] = $data; $newField['title'] = $title; $newField['type'] = 'upload'; // Set default value for titleOrientation if it is not set if ( empty( $options['titleOrientation'] ) ) { $options['titleOrientation'] = $this->_defaultTitleOrientation; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newField[$name] = $value; } } } // Add field to the array $this->_fields[] = $newField; } /** * Get Fields * * Return the current fields array * * @return array * */ public function getFields() { return $this->_fields; } /** * * Clear fields * * Clear Fields array * */ public function clearFields() { unset( $this->_fields ); } /** * * Create * * Create the dynamic form * * @param mixed $data If string, it must be the id of the form. If array, them we will loop through it * @param string $action Action of the form * @param array $options Additional options * @return string * */ public function create( $data , $action = '' , $options = '' ) { $newForm = array(); // Check if it is an array if ( is_array( $data ) ) { $newForm = $data; } else { // Add vars to the new form $newForm['ID'] = $data; $newForm['action'] = $action; $newForm['fields'] = $this->_fields; // Set the default value for canSubmit if ( !isset( $options['canSubmit'] ) ) { $options['canSubmit'] = true; } // Set the default value for position if ( !isset( $options['position'] ) ) { $options['position'] = 'relative'; } // Set the default value for saveOnEnter if ( !isset( $options['saveOnEnter'] ) ) { $options['saveOnEnter'] = true; } // Check if options is array and do the loop if ( is_array( $options ) ) { foreach ( $options as $name => $value ) { $newForm[$name] = $value; } } } // Encode data $dataEncoded = $this->encode( $newForm ); // Generate js code $js = 'isc.DynamicForm.create( '. $dataEncoded .' );'; // Add script tag $js = $this->addScriptTags( $js ); return $js; } } ?>