Created
March 21, 2015 18:34
-
-
Save vikky410/22fcca2d2b07c58d45d7 to your computer and use it in GitHub Desktop.
Laravel4: macros by Vik
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 | |
| /** | |
| * Back office table headers | |
| */ | |
| HTML::macro('th', function ($field = '', $defaultOrder = null, $sortable = true, $label = true) { | |
| $inputs = Input::all(); | |
| $order = Input::get('order'); | |
| if ($defaultOrder && ! $order) { // set default column active | |
| $order = $field; | |
| } | |
| $direction = Input::get('direction', $defaultOrder); | |
| $th = array(); | |
| $th[] = '<th class="' . $field . '">'; | |
| if ($sortable) { | |
| $inputs['direction'] = 'asc'; | |
| $iconDir = ' text-muted'; // column not active, gray icons | |
| if ($order == $field) { // if this column is active | |
| $iconDir = '-' . $direction; | |
| if ($direction == 'asc') { // reverse direction | |
| $inputs['direction'] = 'desc'; | |
| } | |
| } | |
| $inputs['order'] = $field; | |
| $th[] = '<a href="?' . http_build_query($inputs) . '">'; | |
| $th[] = '<i class="fa fa-sort' . $iconDir . '"></i> '; | |
| } | |
| if ($label) { | |
| $th[] = trans('validation.attributes.' . $field); | |
| } | |
| if ($sortable) { | |
| $th[] = '</a>'; | |
| } | |
| $th[] = '</th>'; | |
| $th[] = "\r\n"; | |
| return implode('', $th); | |
| }); | |
| /** | |
| * Back office buttons in view list | |
| */ | |
| HTML::macro('langButton', function ($locale = null, $attributes = []) { | |
| $inputs = Input::except('locale'); | |
| $inputs['locale'] = $locale; | |
| $attributes['class'] = 'btn btn-default btn-xs'; | |
| if ($locale == Config::get('app.locale')) { | |
| $attributes['class'] .= ' active'; | |
| } | |
| $label = trans('global.languages.' . $locale); | |
| $attributes['href'] = '?' . http_build_query($inputs); | |
| return '<a ' . HTML::attributes($attributes) . '>' . $label . '</a>'; | |
| }); | |
| /** | |
| * Front office lang switcher | |
| */ | |
| HTML::macro('languagesMenu', function (array $langsArray = array(), array $attributes = array()) { | |
| $attributes['role'] = 'menu'; | |
| $attributes = HTML::attributes($attributes); | |
| $html = array(); | |
| $html[] = '<ul ' . $attributes . '>'; | |
| foreach ($langsArray as $item) { | |
| $html[] = '<li class="' . $item->class . '" role="menuitem">'; | |
| $html[] = '<a href="' . $item->url . '">' . $item->lang . '</a>'; | |
| $html[] = '</li>'; | |
| } | |
| $html[] = '</ul>'; | |
| return implode("\r\n", $html); | |
| }); | |
| /** | |
| * Front office menu | |
| */ | |
| HTML::macro('menu', $builtMenu = function ($items = array(), $ulAttr = array()) use (&$builtMenu) { | |
| $menuList = array('<ul ' . HTML::attributes($ulAttr) . '>'); | |
| foreach ($items as $item) { | |
| $liAttr = array(); | |
| $item->class && $liAttr['class'] = $item->class; | |
| $liAttr['role'] = 'menuitem'; | |
| // item | |
| $menuList[] = '<li ' . HTML::attributes($liAttr) . '>'; | |
| $aAttr = array(); | |
| if ($item->items && $item->items->count()) { | |
| $aAttr['class'] = 'dropdown-toggle'; | |
| $aAttr['data-toggle'] = 'dropdown'; | |
| } | |
| $item->target && $aAttr['target'] = $item->target; | |
| $aAttr['href'] = $item->uri; | |
| $menuList[] = '<a ' . HTML::attributes($aAttr) . '>'; | |
| if ($item->icon_class) { | |
| $menuList[] = '<span class="'.$item->icon_class.'"></span>'; | |
| } | |
| $menuList[] = $item->title; | |
| if ($item->items && $item->items->count()) { | |
| $menuList[] = '<span class="caret"></span>'; | |
| } | |
| $menuList[] = '</a>'; | |
| // nested list | |
| if ($item->items && $item->items->count()) { | |
| $menuList[] = $builtMenu($item->items, array('class' => 'dropdown-menu')); | |
| } | |
| $menuList[] = '</li>'; | |
| } | |
| $menuList[] = '</ul>'; | |
| return implode("\r\n", $menuList); | |
| }); | |
| /** | |
| * bs3 form macros | |
| * @params $options [array] | |
| * @type [type of the field text, password, checkbox, dropdown, textarea] | |
| * @labeWrapper [class to wrap label, default: col-sm-2] | |
| * @valueWrapper [class to wrap value, default: col-sm-10] | |
| * @name [name of the value field] | |
| * @label [label to be dispalyed] | |
| * @placeholder [placeholder to be displayed in field] | |
| * @validations [array] [parameters of form validator] | |
| * @dropdownParams [array] [parameters of dropdown] | |
| * @htmlOptions [array] [extra param to form fields] | |
| */ | |
| Form::macro('field', function($options){ | |
| $markup = ''; | |
| $type = 'text'; | |
| $labelWrapper = 'col-sm-2'; | |
| $valueWrapper = 'col-sm-10'; | |
| $required = ""; | |
| $validatons = ""; | |
| $list = [null => 'Select']; | |
| $selected = null; | |
| if(!empty($options['labelWrapper'])){ | |
| $labelWrapper = $options['labelWrapper']; | |
| } | |
| if(!empty($options['valueWrapper'])){ | |
| $valueWrapper = $options['valueWrapper']; | |
| } | |
| if(!empty($options['type'])){ | |
| $type = $options['type']; | |
| } | |
| if(empty($options['name'])){ | |
| return; | |
| } | |
| $name = $options['name']; | |
| if(!empty($options['required'])){ | |
| $required = $options['required']; | |
| } | |
| $label = ""; | |
| if(!empty($options['label'])){ | |
| if(!empty($required) && $required != "" ){ | |
| $label = $options['label']."<span class = 'asterisk'>*</span>"; | |
| } | |
| else{ | |
| $label = $options['label']; | |
| } | |
| } | |
| $value = Input::old($name); | |
| if( !empty( $options['value'] ) ){ | |
| $value = Input::old($name, $options["value"]); | |
| } | |
| $placeholder = ""; | |
| if (!empty($options["placeholder"])){ | |
| $placeholder = $options["placeholder"]; | |
| } | |
| $class = ""; | |
| if (!empty($options["class"])){ | |
| $class = " ".$options['class']; | |
| } | |
| $parameters = array( | |
| "class" => 'form-control' . $class, | |
| "placeholder" => $placeholder | |
| ); | |
| $error = ""; | |
| if( !empty($options['form']) ){ | |
| $error = $options['form']->getError('name'); | |
| } | |
| if ($type !== "hidden"){ | |
| $markup .= "<div class='form-group"; | |
| $markup .= ($error ? " has-error" : ""); | |
| $markup .= "'>"; | |
| } | |
| /** | |
| * formvalidator.net | |
| * form validaor validation | |
| */ | |
| if(!empty($options['validations']) && is_array($options['validations']) ){ | |
| $validators = $options['validations']; | |
| foreach($validators as $k => $v){ | |
| $parameters += array($k => $v); | |
| } | |
| } | |
| /** | |
| * dropdown | |
| */ | |
| if(isset($options['dropdownParams'])){ | |
| if(!empty($options['dropdownParams']) && is_array($options['dropdownParams']) ){ | |
| $dropdownParams = $options['dropdownParams']; | |
| if(isset($dropdownParams['list'])){ | |
| $list = $dropdownParams['list']; | |
| } | |
| if(isset($dropdownParams['selected'])){ | |
| $selected = $dropdownParams['selected']; | |
| } | |
| } | |
| } | |
| /** | |
| * htmlOptions | |
| */ | |
| if(isset($options['htmlOptions'])){ | |
| if(!empty($options['htmlOptions']) && is_array($options['htmlOptions']) ){ | |
| $htmlOptions = $options['htmlOptions']; | |
| foreach($htmlOptions as $k => $v){ | |
| $parameters += array($k => $v); | |
| } | |
| } | |
| } | |
| switch ($type) { | |
| case "text": | |
| { | |
| if(!empty($required) && $required != "" ){ | |
| $markup .= HTML::decode(Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| ))); | |
| } | |
| else{ | |
| $markup .= Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| )); | |
| } | |
| $markup .= '<div class="'.$valueWrapper.'">'; | |
| $markup .= Form::text($name, $value, $parameters); | |
| $markup .= '</div>'; | |
| break; | |
| } | |
| case "dropdown": | |
| { | |
| if(!empty($required) && $required != "" ){ | |
| $markup .= HTML::decode(Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| ))); | |
| } | |
| else{ | |
| $markup .= Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| )); | |
| } | |
| $markup .= '<div class="'.$valueWrapper.'">'; | |
| $markup .= Form::select($name, $list, $selected, $parameters); | |
| $markup .= '</div>'; | |
| break; | |
| } | |
| case "password": | |
| { | |
| if(!empty($required) && $required != "" ){ | |
| $markup .= HTML::decode(Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| ))); | |
| } | |
| else{ | |
| $markup .= Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| )); | |
| } | |
| $markup .= '<div class="'.$valueWrapper.'">'; | |
| $markup .= Form::password($name, $parameters); | |
| $markup .= '</div>'; | |
| break; | |
| } | |
| case "checkbox"; | |
| { | |
| $markup .= "<div class = 'checkbox'>"; | |
| $markup .= "<label>"; | |
| $markup .= Form::checkbox($name, 1, !!$value); | |
| $markup .= " " . $label; | |
| $markup .= "</label>"; | |
| $markup .= "</div>"; | |
| break; | |
| } | |
| case "textarea"; | |
| { | |
| if(!empty($required) && $required != "" ){ | |
| $markup .= HTML::decode(Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| ))); | |
| } | |
| else{ | |
| $markup .= Form::label($name, $label, array( | |
| 'class' => "$labelWrapper control-label" | |
| )); | |
| } | |
| $markup .= '<div class="'.$valueWrapper.'">'; | |
| $markup .= Form::textarea($name, $value, $parameters); | |
| $markup .= '</div>'; | |
| break; | |
| } | |
| case "hidden": | |
| { | |
| $markup .= Form::hidden($name, $value); | |
| break; | |
| } | |
| } | |
| if($error){ | |
| $markup .= "<span class='help-block'>"; | |
| $markup .= $error; | |
| $markup .= "</span>"; | |
| } | |
| if ($type !== "hidden"){ | |
| $markup .= "</div>"; | |
| } | |
| return $markup; | |
| }); | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment