-
-
Save bato3/5500824 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* Twitter Bootstrap Kohana helper | |
* | |
* example of use: | |
* <?=Bootstrap::grid($grid, Bootstrap::FLUID);?> | |
* <?=$grid->span($view, 4);?> | |
* <?=Bootstrap::icon('download', TRUE);?> | |
* <?=Bootstrap::button('Button'); ?> | |
* <?=Bootstrap::button(array('Google', 'http://www.google.com')); ?> | |
* | |
* @copyright Birkir Rafn Gudjonsson | |
* @licence kohanaframework.org/license | |
*/ | |
class Bootstrap { | |
/** | |
* @const LAYOUTS | |
*/ | |
const FIXED = 100; | |
const FLUID = 101; | |
/** | |
* @const DIRECTION | |
*/ | |
const HORIZONTAL = 200; | |
const VERTICAL = 201; | |
/** | |
* @const STATES | |
*/ | |
const PRIMARY = 300; | |
const INFO = 301; | |
const SUCCESS = 302; | |
const WARNING = 303; | |
const ERROR = 304; | |
const INVERSE = 305; | |
const LINK = 306; | |
const BLOCK = 307; | |
const DISABLED 308; | |
/** | |
* @const SIZES | |
*/ | |
const MINI = 400; | |
const SMALL = 401; | |
const MEDIUM = 402; | |
const LARGE = 403; | |
/** | |
* @var Grid sum | |
*/ | |
private $_grid_sum = 0; | |
/** | |
* Get class attribute from attributes and attach more classes to it | |
* | |
* @param array $attributes HTML anchor attributes | |
* @param array $classes Class(es) wanted | |
* @return string | |
*/ | |
public static function class($attributes, $classes) | |
{ | |
// extract class from attributes (if any) | |
$classes = (isset($attributes['class']) ? explode(' ', $attributes['class']) : array()); | |
// split classes by whitespace | |
foreach (explode(' ', $classes) as $class) | |
{ | |
// check if not already exists | |
if ( ! in_array($class, $classes)) | |
{ | |
// attach class to its context | |
$classes[] = $class; | |
} | |
} | |
// re-attach class attribute to attributes | |
$attributes['class'] = implode(' ', $classes); | |
// return attributes | |
return $attributes; | |
} | |
/** | |
* Extract HTML node name and attributes | |
* | |
* @param string $html Element in html string | |
* @return array | |
*/ | |
public static function extract($html = NULL) | |
{ | |
$xml = new SimpleXMLElement((string) $html); | |
return array($xml->getName(), $xml->attributes()); | |
} | |
/** | |
* Bootstrap flag parser | |
* | |
* @param integer $flag Flag to parse | |
* @param string $prefix Prefix to attach | |
* @return string | |
*/ | |
public static function parse($flag, $prefix) | |
{ | |
// array mapper | |
$conv = array( | |
100 => 'fixed', | |
101 => 'fluid', | |
200 => 'horizontal', | |
201 => 'vertical', | |
300 => 'primary', | |
301 => 'info', | |
302 => 'success', | |
303 => 'warning', | |
304 => 'success', | |
305 => 'inverse', | |
306 => 'link', | |
307 => 'block', | |
308 => 'disabled', | |
400 => 'mini', | |
401 => 'small', | |
402 => 'medium', | |
403 => 'large' | |
); | |
// no prefix for disabled flag | |
if ($flag === 308) | |
return 'disabled'; | |
// return prefix-flagname | |
return $prefix.'-'.$conv[$flag]; | |
} | |
/** | |
* Create grid row instance | |
* | |
* @param variable $grid Output new Bootstrap object | |
* @param integer $layout Fixed or fluid | |
* @param array $attributes HTML anchor attributes | |
* @return string | |
* @uses Bootstrap::class | |
* @uses HTML::attributes | |
*/ | |
public static function grid(&$grid, $layout = 0, $attributes = array()) | |
{ | |
// add class | |
$attributes = Bootstrap::class($attributes, $type === Bootstrap::FIXED ? 'row' : 'row-fluid'); | |
// return new Bootstrap | |
$grid = new Bootstrap; | |
// echo new row | |
return '<div'.HTML::attributes($attributes).'>'; | |
} | |
/** | |
* Create grid span in row | |
* | |
* @param mixed $view View object or string | |
* @param integer $width Width number | |
* @param integer $offset Offset number | |
* @param array $attributes HTML anchor attributes | |
* @return string | |
* @uses Bootstrap::class | |
* @uses HTML::attributes | |
*/ | |
public static function span($view, $width = 0, $offset = 0, $attributes = array()) | |
{ | |
// add class | |
$attributes = Bootstrap::class($attributes, 'span'.$width.' offset'.$offset); | |
// set grid sum | |
if (isset($this)) | |
{ | |
$this->_grid_sum += $width + $offset; | |
} | |
// echo span div | |
return '<div'.HTML::attributes($attributes).'>'.$view.'</div>' | |
. (isset($this->_grid_sum) AND $this->_grid_sum > 12) ? '</div>' : NULL; | |
} | |
/** | |
* Close any open div element | |
* | |
* @return string | |
*/ | |
public static function close() | |
{ | |
return '</div>'; | |
} | |
/** | |
* Build icon with italic element | |
* | |
* @param string $icon Bootstrap icons name | |
* @param boolean $white Use white icons | |
* @return string | |
* @uses HTML::attributes | |
*/ | |
public static function icon($icon = NULL, $white = FALSE) | |
{ | |
// setup class attribute | |
$attributes = array( | |
'class' => 'icon-'.$icon.($white ? ' icon-white' : NULL) | |
); | |
// return HTML element | |
return '<i'.HTML::attributes($attributes).'></i>'; | |
} | |
/** | |
* Group buttons or form controls | |
* | |
* @param string $type Group type prefix | |
* @param array $items Items to append | |
* @param array $attributes HTML anchor attributes | |
* @return string | |
* @uses Bootstrap::class | |
* @uses HTML::attributes | |
*/ | |
public static function group($type, $items = array(), $attributes = array()) | |
{ | |
// add class | |
$attributes = Bootstrap::class($attributes, $type.'-group'); | |
// return div | |
return '<div'.HTML::attributes($attributes).'>'.implode(NULL, $items).'</div>'; | |
} | |
/** | |
* Generate buttons by the use of flags and attributes | |
* | |
* @param mixed $text Title as string, HTML::anchor, Form::button or Form::submit | |
* @param array $flags Flags for button | |
* @param array $attributes HTML anchor attributes | |
* @return string | |
* @uses HTML::anchor | |
* @uses HTML::attributes | |
* @uses Bootstrap::extract | |
* @uses Bootstrap::parse | |
* @uses Arr::merge | |
*/ | |
public static function button($text, $flags = array(), $attributes = array()) | |
{ | |
// set element type | |
$el = 'button'; | |
// check if text is array | |
if (is_array($text)) | |
{ | |
$text = HTML::anchor($text[0], $text[1]); | |
} | |
// check if text is instance of HTML::anchor or Form::button | |
if ($text instanceof HTML OR $text instanceof Form) | |
{ | |
// list element type and attributes | |
list($el, $attrs) = Bootstrap::extract($text); | |
// merge attributes | |
$attributes = Arr:merge($attributes, $attrs); | |
} | |
// set classes array | |
$classes = (isset($attributes['class']) ? explode(' ', $attributes['class']) : array()); | |
// loop through set flags | |
foreach ($flags as $item) | |
{ | |
// check if item is button and disabled | |
if ($item === Bootstrap::DISABLED AND ($el === 'button' OR $el === 'input')) | |
{ | |
// set disabled attribute | |
$attributes['disabled'] = 'disabled'; | |
} | |
// attach class | |
$classes[] = Bootstrap::parse($item, 'btn'); | |
} | |
// set attribute classes | |
$attributes['class'] = implode(' ', $classes); | |
return '<'.$el.HTML::attributes($attributes).($el === 'input' ? ' /' : '></'.$el).'>'; | |
} | |
/** | |
* Breadcrumbs anchor list | |
* | |
* @param array $items Breadcrumb items as [[URL, TITLE], ...] | |
* @param array $attributes HTML anchor attributes | |
* @return string | |
* @uses Bootstrap::class | |
* @uses HTML::anchor | |
*/ | |
public static function breadcrumb(array $items, $attributes = array()) | |
{ | |
// setup attributes with class | |
$attributes = Bootstrap::class($attributes, 'breadcrumb'); | |
// begin return | |
$ret = '<ul'.HTML::attributes($attributes).'>'; | |
// loop breadcrumbs items | |
foreach ($items as $i =>$item) | |
{ | |
// check if last item | |
if ($i === count($items)) | |
{ | |
// return active list item | |
$ret .= '<li class="active">'.$item[1].'</li>'; | |
} | |
else | |
{ | |
// return list item with anchor | |
$ret .= '<li>'.HTML::anchor($item[0], $item[1], isset($item[2]) ? $item[2] : NULL).'<span class="divider">/</span></li>'; | |
} | |
} | |
// return list, items and end of list | |
return $ret.'</ul>'; | |
} | |
/** | |
* Progress bars for loading, redirecting or action status. | |
* | |
* @param mixed $percentage One or many percentage as integers | |
* @param array $attributes HTML anchor attributes | |
* @return string | |
* @uses Bootstrap::parse | |
* @uses Bootstrap::class | |
* @uses HTML::attributes | |
*/ | |
public static function progress(array $percentage, $attributes = array()) | |
{ | |
// setup attributes with class | |
$attributes = Bootstrap::class($attributes, 'progress'); | |
// begin return | |
$ret = '<div'.HTML::attributes($attributes).'>'; | |
// check if percentages are not many | |
if ( ! is_array($percentage)) | |
{ | |
// act as many percentages | |
$percentage = array($percentage); | |
} | |
// loop through percentages | |
foreach ($percentage as $item) | |
{ | |
// check if item is array | |
if ( ! is_array($item)) | |
{ | |
// set default info state | |
$item = array($item, Bootstrap::INFO); | |
} | |
// attach attributes | |
$ret .= '<div'.HTML::attributes(array( | |
'style' => 'width: '.$item[0].'%;', | |
'class' => 'bar '.Bootstrap::parse($item[1], 'bar') | |
)).'></div>'; | |
} | |
// echo progress bars | |
return $ret.'</div>'; | |
} | |
} | |
class BS extends Bootstrap { | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment