'process','class'=>'form-horizontal'))?> 'abc', '2'=>'def'))?> 'abc', '2'=>'def'))?> 'sal_gt_20k', 'display'=>'GT 20K', 'value'=>'1', 'checked'=>false), array('name'=>'sal_lt_3k', 'display'=>'Blah', 'value'=>'1', 'checked'=>false) )); ?> 'lang_c', 'display'=>'C', 'value'=>'1', 'checked'=>false), array('name'=>'langc_plus', 'display'=>'C++', 'value'=>'1', 'checked'=>false), array('name'=>'lang_java', 'display'=>'Java', 'value'=>'1', 'checked'=>false), array('name'=>'lang_php', 'display'=>'PHP', 'value'=>'1', 'checked'=>false), array('name'=>'lang_sql', 'display'=>'SQL', 'value'=>'1', 'checked'=>false), )); ?> 'sex', 'display'=>'Male','value'=>'m', 'checked'=>false), array('name'=>'sex', 'display'=>'Female','value'=>'f', 'checked'=>false) )); ?> 'option', 'display'=>'a','value'=>'a', 'checked'=>false), array('name'=>'option', 'display'=>'b','value'=>'b', 'checked'=>false), array('name'=>'option', 'display'=>'c','value'=>'c', 'checked'=>false), array('name'=>'option', 'display'=>'d','value'=>'d', 'checked'=>false), array('name'=>'option', 'display'=>'e','value'=>'e', 'checked'=>false), array('name'=>'option', 'display'=>'f','value'=>'f', 'checked'=>false), array('name'=>'option', 'display'=>'g','value'=>'g', 'checked'=>false), )); ?> 'col-sm-offset-2 btn btn-primary'))?> 'Extreme', 'rightAnchor'=>'Minimal', 'count'=>6, 'name'=>'radioSlide1', ))?> 'Minimal', 'rightAnchor'=>'Extreme', 'count'=>6, 'name'=>'radioSlide2', ), 'asc')?> 'Too little', 'rightAnchor'=>'Too much', 'min'=>0, 'max'=>100, 'name'=>'slider1', ))?> ****HTML MACROS ****************** * ********************************************************************************************/ Form::macro ( 'textField', function ($name, $label = null, $value = null, $attributes = array()) { $element = Form::text ( $name, $value, fieldAttributes ( $name, $attributes ) ); return fieldWrapper ( $name, $element, $label ); } ); Form::macro ( 'emailField', function ($name, $label = null, $value = null, $attributes = array()) { $element = Form::email ( $name, $value, fieldAttributes ( $name, $attributes ) ); return fieldWrapper ( $name, $element, $label ); } ); Form::macro ( 'passwordField', function ($name, $label = null, $value = null, $attributes = array()) { $element = Form::password ( $name, fieldAttributes ( $name, $attributes ) ); return fieldWrapper ( $name, $element, $label ); } ); Form::macro ( 'textareaField', function ($name, $label = null, $value = null, $attributes = array()) { if (! isset ( $attributes ['rows'] )) { $attributes = array_merge ( $attributes, array ( 'rows' => 3 ) ); } $element = Form::textarea ( $name, $value, fieldAttributes ( $name, $attributes ) ); return fieldWrapper ( $name, $element, $label ); } ); Form::macro ( 'selectField', function ($name, $options, $label = null, $value = null, $attributes = array()) { $options = array('' => 'Select')+$options; $element = Form::select ( $name, $options, $value, fieldAttributes ( $name, $attributes ) ); return fieldWrapper ( $name, $element, $label ); } ); Form::macro ( 'selectMultipleField', function ($name, $options, $label = null, $value = null, $attributes = array()) { $attributes = array_merge ( $attributes, array ( 'multiple' => true ) ); $element = Form::select ( $name . '[]', $options, $value, fieldAttributes ( $name, $attributes ) ); return fieldWrapper ( $name, $element, $label ); } ); // fieldName, text, value, checked, attribs for each entry // checkboxGroup('enter name', array( // array(name=>'sal_gt_20k', display=>'GT 20K', value=>'1', checked=>true) // )); Form::macro ( 'checkboxGroup', function ($label, $options = array()) { $grp = ''; $class = ''; $field = ''; foreach ( $options as $option ) { $grp .= checkbox ( $option ['name'], $option ['display'], $option ['value'], $option ['checked'], $option ['attribs'] = array () ); $grp .= '  '; if (fieldError ( $option ['name'] )) { $class = 'has-error'; $field = $option ['name']; } } return '
' . fieldWrapper ( $field, $grp, $label ) . '
'; } ); // fieldName, text, value, checked, attribs for each entry // radioGroup('enter name', array( // array(name=>'sal_gt_20k', display=>'GT 20K', value=>'1', checked=>true) // )); Form::macro ( 'radioGroup', function ($label, $options = array()) { $grp = ''; foreach ( $options as $option ) { $fieldName = $option ['name']; $grp .= radio ( $option ['name'], $option ['display'], $option ['value'], $option ['checked'], $option ['attribs'] = array () ); $grp .= '  '; } return '
' . fieldWrapper ( $fieldName, $grp, $label ) . '
'; } ); Form::macro ( 'checkboxGroupTable', function ($label, $rows, $columns, $options = array()) { $getData = renderTabular ( $rows, $columns, $options ); return '
' . fieldWrapper ( $getData->field, $getData->html, $label ) . '
'; } ); Form::macro ( 'radioGroupTable', function ($label, $rows, $columns, $options = array()) { $getData = renderTabular ( $rows, $columns, $options, 'radio' ); return '
' . fieldWrapper ( $getData->field, $getData->html, $label ) . '
'; } ); Form::macro ( 'checkboxField', function ($name, $value = 1, $checked = null, $attributes = array()) { return checkBox ( $name, $displayName, $value, $checked, $attributes ); } ); Form::macro ( 'radioField', function ($name, $displayName, $value = 1, $checked = null, $attributes = array()) { return radio ( $name, $displayName, $value, $checked, $attributes ); } ); /** * Helper function to handle tabulating radios and checkbox * * @param int $rows * - number of rows (horizontal) in array * @param int $columns * - number of columns (vertical) in array * @param array $options * - options array entered by the user for this set of radios/checkboxes * @param string $type * - checkbox or radio.. default to checkbox * @return \stdClass - return an stdclass object with fields error, field and html */ if (! function_exists ( 'renderTabular' )) { function renderTabular($rows, $cols, $options, $type = "checkbox") { $grp = ''; $offset = 0; $retObj = new \stdClass (); $retObj->error = ''; $retObj->field = ''; for($r = 0; $r < $rows; $r ++) { $grp .= ""; for($c = 0; $c < $cols; $c ++) { $offset = $r * $cols + $c; if (isset ( $options [$offset] )) { $grp .= ""; } else { // empty cell to maintain symmetry $grp .= ""; } } $grp .= ''; } $grp .= '
"; if ($type == 'checkbox') { $grp .= checkbox ( $options [$offset] ['name'], $options [$offset] ['display'], $options [$offset] ['value'], $options [$offset] ['checked'], $options [$offset] ['attribs'] = array () ); if (fieldError ( $options [$offset] ['name'] )) { $retObj->error = 'has-error'; $retObj->field = $options [$offset] ['name']; } } else if ($type == 'radio') { $retObj->field = $options [$offset] ['name']; $grp .= radio ( $options [$offset] ['name'], $options [$offset] ['display'], $options [$offset] ['value'], $options [$offset] ['checked'], $options [$offset] ['attribs'] = array () ); } $grp .= " 
'; $retObj->html = $grp; return $retObj; } } /** * display a star rating (jquery plugin) * RATY (http://wbotelhos.com/raty) * Make sure you include the jquery and raty js files. * Note that the big png files are stored in js/img path */ Form::macro ( 'star', function ($name, $value = '') { $markup = '
'; $markup .= '
' . errorMessage ( $name ); // scoreName does not help with storing data between submits.. so use another hidden $markup .= Form::hidden ( $name, $value, array ( 'id' => "$name-field" ) ); $markup .= "
"; $markup .= ""; return $markup; } ); /** * render a linear scale using radio buttons * the data array must contain the following keys * leftAnchor : text to be displayed on left * rightAnchor: text to be displayed on right * count : the number of radio buttons to be displayed * name : the name of the control * The default order is descending.. passing in 'asc' as the second parameter * displays radios in ascending order */ Form::macro ( 'radioScale', function ($data, $value = 0, $order = 'desc') { $leftAnchor = $data ['leftAnchor']; $rightAnchor = $data ['rightAnchor']; $count = $data ['count']; $name = $data ['name']; // generate the scale class='.fieldError($name). $str = '

' . $leftAnchor . '

'; if ($order == 'desc') { for($index = $count; $index >= 1; $index --) { $str .= radio ( $name, $index, $index, $index == $value ? true : false ); } } else if ($order = 'asc') { for($index = 1; $index <= $count; $index ++) { $str .= radio ( $name, $index, $index, $index == $value ? true : false ); } } $str .= '

' . $rightAnchor . '

' . errorMessage ( $name ); return $str; } ); /** * Displays a slider using Jquery ui * http://jqueryui.com/slider/ * the data array must contain the following keys * leftAnchor : text to be displayed on left * rightAnchor: text to be displayed on right * min : minimum value of slider * max: maximum value of slider * name : the name of the control */ Form::macro ( 'slider', function ($data, $value = '') { $leftAnchor = $data ['leftAnchor']; $rightAnchor = $data ['rightAnchor']; $min = $data ['min']; $max = $data ['max']; $name = $data ['name']; $sliderid = $name . '-slider'; // generate the slider $str = '
' . $leftAnchor . '
' . $rightAnchor . '
'; $str .= Form::hidden ( $name, $value, array ( 'id' => $name ) ); $str .= '
' . errorMessage ( $name ); $str .= ""; return $str; } ); /** * Return the html to render an individual radio control * * @param string $name * - name of the radio * @param string $displayName * - display name of the radio * @param string $value * - value of control if selected * @param string $checked * - is the radio selected? * @param array $attributes * - other attributes (class, id etc) * @return string - The html rendering of the radio control */ if (! function_exists ( 'radio' )) { function radio($name, $displayName, $value, $checked = null, $attributes = array()) { $out = ''; $attributes = array_merge ( array ( 'id' => 'id-field-' . $name.'-'.$displayName ), $attributes ); $out .= ''; return $out; } } /** * Return the html to render an individual checkbox control * * @param string $name * - Name of the checkbox * @param string $displayName * - Display name of the checkbox * @param string $value * - Value if control is checked * @param string $checked * - Is the checkbox checked by default * @param array $attributes * - other attributes (class, id etc) * @return string - html rendering of the checkbox control. Note that * it includes a hidden field. This simplifies form processing when checkbox is unchecked */ if (! function_exists ( 'checkBox' )) { function checkBox($name, $displayName, $value = 1, $checked = null, $attributes = array()) { $out = ''; $attributes = array_merge ( array ( 'id' => 'id-field-' . $name ), $attributes ); $out .= ''; return $out; } } /** * Wrap an element with twitter bootstrap 3.0 specific code for proper rendering * * @param string $field * - field name * @param string $element * - html rendering of internal form element to be output * @param string $label * - label that is displayed to the left * @return string - formatted html with all divs etc for final display on screen */ if (! function_exists ( 'fieldWrapper' )) { function fieldWrapper($field, $element, $label = null) { $out = '
'; // set error class $out .= fieldLabel ( $field, $label ); // gen label $out .= '
'; $out .= $element; $out .= errorMessage ( $field ); // display error message $out .= '
'; $out .= '
'; return $out; } } /** * return formatted error message associated with a field * * @param string $field * - name of the field to be checked for errors * @return string - TBS 3.0 formatted span tag that is to be displayed alongside the field */ if (! function_exists ( 'errorMessage' )) { function errorMessage($field) { if ($errors = Session::get ( 'errors' )) { return '' . $errors->first ( $field ) . ''; } } } /** * Return string 'has-error' that can be tagged to element div to signal erroneous entry * * @param string $field * - the field name * @return string - 'has-error' in case the field has a validation error */ if (! function_exists ( 'fieldError' )) { function fieldError($field) { $error = ''; if ($errors = Session::get ( 'errors' )) { $error = $errors->first ( $field ) ? ' has-error' : ''; } return $error; } } /** * html required for displaying the field label. * In case an explicit label is not passed, * generate one * * @param unknown $name * - field name * @param unknown $label * - label to be used * @return string - html for the label (TBS 3.0 formatted) */ if (! function_exists ( 'fieldLabel' )) { function fieldLabel($name, $label) { $out = ''; return $out; } } /** * helper function to add required classes (TBS 3.0) for "text input" fields * * @param string $name * - field name * @param array $attributes * - control attribs passed by user * @return array - attributes array merged with TBS specific classes */ if (! function_exists ( 'fieldAttributes' )) { function fieldAttributes($name, $attributes = array()) { return array_merge ( array ( 'class' => 'form-control', 'id' => 'id-field-' . $name ), $attributes ); } } /** * ************************************************************** * HTML MACROS * ************************************************************** */ /** * Display a progressbar using jquery ui * http://jqueryui.com/progressbar/ * $current is the current step of the slider * $total is the total number of steps */ HTML::macro ( 'progress', function ($current, $total) { $pctValue = number_format ( $current / $total * 100, 0 ); $str = ''; $str .= '
' . $pctValue . '% Complete
'; $str .= ''; return $str; } ); /** * Generate content required to display swf videos on page * http://code.google.com/p/swfobject/ * It is assumed that an swf encoded file named $name is in public subdir swf/ */ HTML::macro ( 'swfobject', function ($name) { $swfPath = URL::to("/swf/$name"); $expressInstallPath = URL::to( "js/expressInstall.swf"); $version = "9.0.0"; $headScript = << var flashvars = {}; var params = {}; params.play = "true"; params.loop = "true"; params.allowfullscreen = "true"; var attributes = {}; swfobject.embedSWF("$swfPath", "myAlternativeContent", "640", "480", "$version", "$expressInstallPath", flashvars, params, attributes);
Get Adobe Flash player
HEADSCRIPT; return $headScript; } );