This repository has been archived on 2024-04-08. You can view files and clone it, but cannot push or open issues or pull requests.
lnapp/classes/lnApp/Form.php

193 lines
5.2 KiB
PHP
Raw Normal View History

2013-04-25 00:22:36 +00:00
<?php defined('SYSPATH') or die('No direct access allowed.');
/**
* This class extends Kohana's HTML
*
2013-10-08 23:24:11 +00:00
* @package lnApp
* @category Modifications
2013-04-25 00:22:36 +00:00
* @author Deon George
* @copyright (c) 2010 Deon George
* @license http://dev.leenooks.net/license.html
*/
abstract class lnApp_Form extends Kohana_Form {
/**
* Render our control group form attributes
*
* @return array((string) control group wrapper,(array) attributes to exclude
*/
private static function _controlgroup($name,array &$attributes=NULL) {
2014-09-08 13:42:05 +00:00
// Bypass this rendering if nocg is set.
2013-04-25 00:22:36 +00:00
if (isset($attributes['nocg'])) {
unset($attributes['nocg']);
return '%s';
}
2014-10-08 12:20:27 +00:00
if (! isset($attributes['class']))
$attributes['class'] = 'form-control';
2013-04-25 00:22:36 +00:00
$output = '';
$output .= '<div class="input-group">';
2013-04-25 00:22:36 +00:00
2014-09-08 13:42:05 +00:00
// Only need col-md for horizonal forms?
2013-04-25 00:22:36 +00:00
if (isset($attributes['label'])) {
$output .= Form::label($name,$attributes['label'],array('class'=>'control-label'));
2013-04-25 00:22:36 +00:00
unset($attributes['label']);
}
if (isset($attributes['help-block'])) {
$help = $attributes['help-block'];
unset($attributes['help-block']);
}
2013-06-20 05:55:47 +00:00
if (isset($attributes['add-on'])) {
$addon = $attributes['add-on'];
unset($attributes['add-on']);
}
2014-09-08 13:42:05 +00:00
$classdiv = FALSE;
2014-10-08 12:20:27 +00:00
if (isset($attributes['divclass'])) {
$output .= sprintf('<div class="%s">',$attributes['divclass']);
unset($attributes['divclass']);
2014-09-08 13:42:05 +00:00
$classdiv = TRUE;
}
2013-04-25 00:22:36 +00:00
$output .= '%s';
2014-09-29 04:47:51 +00:00
if (in_array('required',$attributes))
$output .= '<div class="help-block with-errors"></div>';
2014-09-08 13:42:05 +00:00
if ($classdiv)
$output .= '</div>';
2013-04-25 00:22:36 +00:00
if (isset($help))
$output .= sprintf('<span class="help-block">%s</span>',$help);
2013-06-20 05:55:47 +00:00
if (isset($addon))
2014-09-08 13:42:05 +00:00
$output .= sprintf('<span class="input-group-addon">%s</span>',$addon);
2013-06-20 05:55:47 +00:00
2013-04-25 00:22:36 +00:00
$output .= '</div>';
return $output;
}
public static function button($name,$body,array $attributes=NULL) {
2013-12-26 10:39:49 +00:00
return sprintf(self::_controlgroup($name,$attributes),parent::button($name,$body,$attributes));
}
2013-04-25 00:22:36 +00:00
/**
* Wrap our Form() functions with boostrap HTML
*
* @usedby Form::hidden
* @usedby Form::password
* @usedby Form::file
* @usedby Form::checkbox
* @usedby Form::radio
* @usedby Form::submit
* @usedby Form::image
*/
public static function input($name,$value=NULL,array $attributes=NULL) {
2014-10-08 12:20:27 +00:00
return (isset($attributes['type']) AND $attributes['type'] == 'hidden') ? parent::input($name,$value,$attributes) : sprintf(self::_controlgroup($name,$attributes),parent::input($name,$value,$attributes));
2013-04-25 00:22:36 +00:00
}
public static function select($name,array $options=NULL,$selected=NULL,array $attributes=NULL) {
// If only 1 record, dont show select, but a hidden attribute and a displayed value.
if (isset($attributes['oneonly']) AND $attributes['oneonly']) {
$attributes['disabled'] = 'disabled';
unset($attributes['oneonly']);
}
if (isset($attributes['sort']) AND $attributes['sort']) {
asort($options);
unset($attributes['sort']);
}
2013-12-26 10:39:49 +00:00
return sprintf(self::_controlgroup($name,$attributes),parent::select($name,$options,$selected,$attributes));
2013-04-25 00:22:36 +00:00
}
public static function textarea($name,$body='',array $attributes=NULL,$double_encode=TRUE) {
if (! isset($attributes['id']))
$attributes['id'] = preg_replace('/[\[\]]/','_',$name);
if (isset($attributes['editor'])) {
switch ($attributes['editor']) {
case 'tinymce':
Script::factory()
->type('file')
->data('media/vendor/tinymce/js/tinymce/jquery.tinymce.min.js');
Script::factory()
->type('stdin')
->data('$("#'.$attributes['id'].'").tinymce({
script_url : "'.URL::site('media/vendor/tinymce/js/tinymce/tinymce.min.js').'",
theme : "modern",
plugins: [ "code","link","image","preview","table" ],
menubar: "",
toolbar: "undo redo | styleselect | bold italic | link image | table | preview | code",
});');
break;
case 'wysihtml5':
Style::factory()
->type('file')
->data('media/theme/bootstrap/vendor/bootstrap-wysihtml5-0.0.2/bootstrap-wysihtml5-0.0.2.css');
Script::factory()
->type('file')
->data('media/theme/bootstrap/vendor/bootstrap-wysihtml5-0.0.2/libs/js/wysihtml5-0.3.0_rc2.min.js');
Script::factory()
->type('file')
->data('media/theme/bootstrap/vendor/bootstrap-wysihtml5-0.0.2/bootstrap-wysihtml5-0.0.2.min.js');
Script::factory()
->type('stdin')
->data('$("#'.$attributes['id'].'").wysihtml5({
html: true,
parserRules: {
tags: {
p: {},
strong: {},
table: {},
tbody: {},
thead: {},
tr: {},
td: {
check_attributes: {
colspan: "numbers",
rowspan: "numbers",
}
}
}
}
});');
break;
default:
throw new Kohana_Exception('Unknown editor :editor for textarea',array(':editor'=>$attributes['editor']));
}
unset($attributes['edit']);
}
2013-04-25 00:22:36 +00:00
2013-12-26 10:39:49 +00:00
return sprintf(self::_controlgroup($name,$attributes),parent::textarea($name,$body,$attributes,$double_encode));
2013-04-25 00:22:36 +00:00
}
public static function textarea_rows($textarea,$min=10,$char="\n") {
return ($x=count(explode($char,$textarea))) < $min ? $min : $x;
}
2016-07-25 06:53:19 +00:00
public static function textarea_width($textarea,$min=76,$char="\n") {
$x = $min;
foreach (explode($char,$textarea) as $string)
{
if (strlen($string) > $x)
$x = strlen($string);
}
return $x;
}
2013-04-25 00:22:36 +00:00
}
?>