Skip to content

Commit

Permalink
Prueba
Browse files Browse the repository at this point in the history
  • Loading branch information
alfonsobries committed Jan 29, 2016
1 parent c4563f0 commit 5d84d70
Show file tree
Hide file tree
Showing 3 changed files with 196 additions and 0 deletions.
101 changes: 101 additions & 0 deletions src/Middleware/MenuMiddleware.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,101 @@
<?php

namespace Vexilo\Utilities\Middleware;

use Closure;
use Illuminate\Contracts\Auth\Guard;
use Menu;
use Config;

class MenuMiddleware
{
protected $auth;

public function __construct(Guard $auth)
{
$this->auth = $auth;
}

/**
* Build the menú according to the configuration array
* @param Menu $menu
* @param array $navArray
* @param $id
*
* @return mixed
*/
private function buildMenu($menu, $navArray)
{

foreach ($navArray as $index => $nav) {
$label = '';
$url = false;
$attribs = false;

if (array_key_exists('label', $nav)) {
$label = $nav['label'];
}
if (array_key_exists('url', $nav)) {
$url = $nav['url'];
} else {
$options = [];

if (array_key_exists('route', $nav)) {
$options['route'] = $nav['route'];
}

if (array_key_exists('action', $nav)) {
$options['action'] = $nav['action'];
}
}

if ($url) {
$menuItem = $menu->add($label, $url);
} else {
$menuItem = $menu->add($label, $options);
}

if (array_key_exists('icon', $nav)) {
$menuItem->data('icon', $nav['icon']);
}

if (array_key_exists('hidden', $nav)) {
$menuItem->data('hidden', $nav['hidden']);
}

// If have pages call the same function recursevely with the last inserted menu item
if (array_key_exists('pages', $nav) && count($nav['pages'])) {
$this->buildMenu($menuItem, $nav['pages']);
}
}

return $menu;

}

/**
* Handle an incoming request.
*
* @param \Illuminate\Http\Request $request
* @param \Closure $next
* @param string $menuName Name of the menu
*
* @return mixed
*/
public function handle($request, Closure $next, $menuName = null)
{
$user = $this->auth->user();

$navConfig = Config::get('nav');

// Find if exists a menu configuration for this param
if (!is_null($menuName) && isset($navConfig[$menuName]) && count($navConfig[$menuName])) {
// Build the menu
$navArray = $navConfig[$menuName];
Menu::make($menuName, function ($menu) use ($user, $navArray) {
$menu = $this->buildMenu($menu, $navArray);
});
}
return $next($request);
}
}
11 changes: 11 additions & 0 deletions src/Models/Traits/OrderableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?php
namespace Vexilo\Utilities\Models\Traits;

trait OrderableTrait
{
public function scopeByOrder($query)
{
$query->orderBy('data_order', 'desc');
return $query;
}
}
84 changes: 84 additions & 0 deletions src/Models/Traits/SortableTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
<?php
namespace Vexilo\Utilities\Models\Traits;

trait SortableTrait
{
/**
* Return an array with the sortable columns
*
* @param array $sort the current sort array
* @return array
*/
public static function getSortableColums($sort = null)
{
$instance = new static;
$sortable = $instance->sortable;

// Sort the array based on the input
if (is_array($sort)&&count($sort)) {
foreach ($sort as $item) {
if (isset($sortable[$item])) {
$sortable_aux[$item] = $sortable[$item];
}
}
$sortable = array_merge($sortable_aux, $sortable);
}

return $sortable;
}

/**
* Return an array with the searchable columns
*
* @return array
*/
public static function getSearchableColums()
{
$instance = new static;
return $instance->searchable;
}

/**
* Allow to filter results by the search form
*
* @param Illuminate\Database\Query $query [description]
* @param string $string The search query
* @return lluminate\Database\Query [description]
*/
public function scopeSearchByInput($query, $string = '')
{
if ($string) {
$searchable = self::getSearchableColums();
$query->where(function ($query) use ($searchable, $string) {
$string_parts = explode(" ", $string);
foreach ($string_parts as $part) {
$query->where(function ($query) use ($searchable, $part) {
foreach ($searchable as $column => $value) {
$query->orWhere($column, 'like', '%'.$part.'%');
}
});
}
});
}

return $query;
}

/**
* Sort the results by the order form input
*
* @param Illuminate\Database\Query $query
* @param array $sortBy Order by columns
* @param array $sortByOrder Order by sort colums
* @return Illuminate\Database\Query\Builder
*/
public function scopeSortByInput($query, $sortBy = array(), $sortByOrder = array())
{
if (is_array($sortBy)&&count($sortBy)) {
foreach ($sortBy as $column) {
$query->orderBy($column, ($sortByOrder && in_array($column, $sortByOrder))?'desc':'asc');
}
}
return $query;
}
}

0 comments on commit 5d84d70

Please sign in to comment.