Cart Bug Fixes and Category items fixes for visible in menu only
This commit is contained in:
parent
802c737a61
commit
683b18e424
|
|
@ -76,31 +76,35 @@ class CategoryDataGrid
|
|||
'columns' => [
|
||||
[
|
||||
'name' => 'cat.id',
|
||||
'alias' => 'catID',
|
||||
'alias' => 'cat_id',
|
||||
'type' => 'number',
|
||||
'label' => 'Category ID',
|
||||
'sortable' => true,
|
||||
'filter' => [
|
||||
'function' => 'where',
|
||||
'condition' => ['cta.locale', app()->getLocale()]
|
||||
],
|
||||
], [
|
||||
'name' => 'ct.name',
|
||||
'alias' => 'catName',
|
||||
'alias' => 'cat_name',
|
||||
'type' => 'string',
|
||||
'label' => 'Category Name',
|
||||
'sortable' => false,
|
||||
], [
|
||||
'name' => 'cat.position',
|
||||
'alias' => 'catPosition',
|
||||
'alias' => 'cat_position',
|
||||
'type' => 'string',
|
||||
'label' => 'Category Position',
|
||||
'sortable' => false,
|
||||
], [
|
||||
'name' => 'cta.name',
|
||||
'alias' => 'parentName',
|
||||
'alias' => 'parent_name',
|
||||
'type' => 'string',
|
||||
'label' => 'Parent Name',
|
||||
'sortable' => true,
|
||||
], [
|
||||
'name' => 'cat.status',
|
||||
'alias' => 'catStatus',
|
||||
'alias' => 'cat_status',
|
||||
'type' => 'string',
|
||||
'label' => 'Visible in Menu',
|
||||
'sortable' => true,
|
||||
|
|
@ -110,8 +114,7 @@ class CategoryDataGrid
|
|||
else
|
||||
return "True";
|
||||
},
|
||||
],
|
||||
|
||||
]
|
||||
],
|
||||
|
||||
'filterable' => [
|
||||
|
|
|
|||
|
|
@ -165,7 +165,7 @@ class EventServiceProvider extends ServiceProvider
|
|||
'icon-class' => '',
|
||||
], [
|
||||
'key' => 'settings.sliders',
|
||||
'name' => 'Create Sliders',
|
||||
'name' => 'Sliders',
|
||||
'route' => 'admin.sliders.index',
|
||||
'sort' => 7,
|
||||
'icon-class' => '',
|
||||
|
|
|
|||
|
|
@ -74,6 +74,19 @@ class CategoryRepository extends Repository
|
|||
: Category::orderBy('position', 'ASC')->get()->toTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* get visible category tree
|
||||
*
|
||||
* @param integer $id
|
||||
* @return mixed
|
||||
*/
|
||||
public function getVisibleCategoryTree($id = null)
|
||||
{
|
||||
return $id
|
||||
? Category::orderBy('position', 'ASC')->where('id', '!=', $id)->where('status', '=', '1')->get()->toTree()
|
||||
: Category::orderBy('position', 'ASC')->where('status', '=', '1')->get()->toTree();
|
||||
}
|
||||
|
||||
/**
|
||||
* Checks slug is unique or not based on locale
|
||||
*
|
||||
|
|
|
|||
|
|
@ -509,25 +509,51 @@ class Cart {
|
|||
if($id == $item->id) {
|
||||
if($item->type == "configurable") {
|
||||
$canBe = $this->canAddOrUpdate($item->child->id, $quantity);
|
||||
|
||||
if($canBe == false) {
|
||||
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
||||
|
||||
return $cart;
|
||||
}
|
||||
|
||||
$item->update([
|
||||
'quantity' => $quantity,
|
||||
'total' => core()->convertPrice($item->price * ($quantity)),
|
||||
'base_total' => $item->price * ($quantity),
|
||||
'total_weight' => $item->weight * ($quantity),
|
||||
'base_total_weight' => $item->weight * ($quantity)
|
||||
]);
|
||||
} else {
|
||||
$canBe = $this->canAddOrUpdate($id, $quantity);
|
||||
|
||||
if($canBe == false) {
|
||||
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
||||
|
||||
return $cart;
|
||||
}
|
||||
$prevQty = $item->quantity;
|
||||
|
||||
$item->update([
|
||||
'quantity' => $quantity,
|
||||
'total' => core()->convertPrice($item->price * ($quantity)),
|
||||
'base_total' => $item->price * ($quantity),
|
||||
'total_weight' => $item->weight * ($quantity),
|
||||
'base_total_weight' => $item->weight * ($quantity)
|
||||
]);
|
||||
}
|
||||
|
||||
if($canBe == false) {
|
||||
session()->flash('warning', trans('shop::app.checkout.cart.quantity.inventory_warning'));
|
||||
|
||||
return $cart;
|
||||
}
|
||||
|
||||
$item->update(['quantity' => $quantity]);
|
||||
}
|
||||
}
|
||||
}
|
||||
$this->collectTotals();
|
||||
|
||||
session()->flash('success', trans('shop::app.checkout.cart.quantity.success'));
|
||||
}
|
||||
|
||||
return $cart;
|
||||
return $cart;
|
||||
} else {
|
||||
session()->flash('warning', trans('shop::app.checkout.cart.integrity.missing_fields'));
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
|
|
@ -758,6 +784,8 @@ class Cart {
|
|||
if(!$cart = $this->getCart())
|
||||
return false;
|
||||
|
||||
$this->validateItems();
|
||||
|
||||
$this->calculateItemsTax();
|
||||
|
||||
$cart->grand_total = $cart->base_grand_total = 0;
|
||||
|
|
@ -792,6 +820,60 @@ class Cart {
|
|||
$cart->save();
|
||||
}
|
||||
|
||||
/**
|
||||
* To validate if the product information is changed by admin and the items have
|
||||
* been added to the cart before it.
|
||||
*
|
||||
* @return boolean
|
||||
*/
|
||||
public function validateItems() {
|
||||
$cart = $this->getCart();
|
||||
|
||||
if(count($cart->items) == 0) {
|
||||
$this->cart->delete($cart->id);
|
||||
|
||||
return redirect()->route('shop.home.index');
|
||||
} else {
|
||||
$items = $cart->items;
|
||||
|
||||
foreach($items as $item) {
|
||||
if($item->product->type == 'configurable') {
|
||||
if($item->product->sku != $item->sku) {
|
||||
$item->update(['sku' => $item->product->sku]);
|
||||
|
||||
} else if($item->product->name != $item->name) {
|
||||
$item->update(['name' => $item->product->name]);
|
||||
|
||||
} else if($item->child->product->price != $item->price) {
|
||||
$item->update([
|
||||
'price' => $item->child->product->price,
|
||||
'base_price' => $item->child->product->price,
|
||||
'total' => core()->convertPrice($item->child->product->price * ($item->quantity)),
|
||||
'base_total' => $item->child->product->price * ($item->quantity),
|
||||
]);
|
||||
}
|
||||
|
||||
} else if($item->product->type == 'simple') {
|
||||
if($item->product->sku != $item->sku) {
|
||||
$item->update(['sku' => $item->product->sku]);
|
||||
|
||||
} else if($item->product->name != $item->name) {
|
||||
$item->update(['name' => $item->product->name]);
|
||||
|
||||
} else if($item->product->price != $item->price) {
|
||||
$item->update([
|
||||
'price' => $item->product->price,
|
||||
'base_price' => $item->product->price,
|
||||
'total' => core()->convertPrice($item->child->product->price * ($item->quantity)),
|
||||
'base_total' => $item->child->product->price * ($item->quantity),
|
||||
]);
|
||||
}
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Calculates cart items tax
|
||||
*
|
||||
|
|
|
|||
|
|
@ -67,12 +67,12 @@ class RegistrationController extends Controller
|
|||
|
||||
if ($this->customer->create($data)) {
|
||||
|
||||
session()->flash('success', 'Account created successfully.');
|
||||
session()->flash('success', 'Account Created Successfully');
|
||||
|
||||
return redirect()->route($this->_config['redirect']);
|
||||
|
||||
} else {
|
||||
session()->flash('error', 'Cannot Create Your Account.');
|
||||
session()->flash('error', 'Cannot Create Your Account');
|
||||
|
||||
return redirect()->back();
|
||||
}
|
||||
|
|
|
|||
|
|
@ -44,7 +44,7 @@ class CategoryComposer
|
|||
{
|
||||
$categories = [];
|
||||
|
||||
foreach ($this->category->getCategoryTree() as $category) {
|
||||
foreach ($this->category->getVisibleCategoryTree() as $category) {
|
||||
array_push($categories, collect($category));
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1,4 +1,3 @@
|
|||
|
||||
<template>
|
||||
<li>
|
||||
<a :href="url+'/categories/'+this.item['translations'][0].slug">{{ this.item['translations'][0].name }} <i class="icon dropdown-right-icon"
|
||||
|
|
@ -17,6 +16,7 @@
|
|||
</ul>
|
||||
</li>
|
||||
</template>
|
||||
|
||||
<script>
|
||||
// define the item component
|
||||
export default {
|
||||
|
|
|
|||
|
|
@ -446,7 +446,6 @@ class DataGrid
|
|||
* Adds expressional verbs to be used
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setOperators(array $operators)
|
||||
{
|
||||
$this->operators = $operators ?: [];
|
||||
|
|
@ -458,7 +457,6 @@ class DataGrid
|
|||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function addPagination($pagination = [])
|
||||
{
|
||||
if ($pagination instanceof Pagination) {
|
||||
|
|
@ -477,7 +475,6 @@ class DataGrid
|
|||
* make from controller.
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
private function getSelect()
|
||||
{
|
||||
$select = [];
|
||||
|
|
@ -501,7 +498,6 @@ class DataGrid
|
|||
* name.
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function findAlias($column_alias) {
|
||||
foreach($this->columns as $column) {
|
||||
if($column->alias == $column_alias) {
|
||||
|
|
@ -515,7 +511,6 @@ class DataGrid
|
|||
* and get it ready
|
||||
* to be used.
|
||||
*/
|
||||
|
||||
private function parse()
|
||||
{
|
||||
$parsed = [];
|
||||
|
|
@ -534,7 +529,6 @@ class DataGrid
|
|||
* ->join('contacts', 'users.id', '=', 'contacts.user_id')
|
||||
* @return $this->query
|
||||
*/
|
||||
|
||||
private function getQueryWithJoin()
|
||||
{
|
||||
foreach ($this->join as $join) {
|
||||
|
|
@ -546,7 +540,8 @@ class DataGrid
|
|||
{
|
||||
foreach ($this->columns as $column) {
|
||||
if ($column->filter) { // if the filter bag in array exists then these will be applied.
|
||||
if (count($column->filter['condition']) == count($column->filter['condition'], COUNT_RECURSIVE)) {
|
||||
// if (count($column->filter['condition']) == count($column->filter['condition'], COUNT_RECURSIVE)) {
|
||||
if (count($column->filter['condition']) == 2) {
|
||||
$this->query->{$column->filter['function']}(...$column->filter['condition']);
|
||||
} else {
|
||||
if (count($column->filter['condition']) == 3) {
|
||||
|
|
@ -570,10 +565,9 @@ class DataGrid
|
|||
}
|
||||
|
||||
/**
|
||||
* Function runs when
|
||||
* filters, sort, search
|
||||
* any of it is applied
|
||||
* @return $this->query
|
||||
* Function runs when filters, sort, search any of it is applied
|
||||
*
|
||||
* @return QueryBuilder object
|
||||
*/
|
||||
|
||||
private function getQueryWithFilters()
|
||||
|
|
@ -753,7 +747,6 @@ class DataGrid
|
|||
$alias_proper_secondary = true;
|
||||
} else {
|
||||
throw new \Exception('Aliases of Join table and the secondary key columns do not match');
|
||||
|
||||
}
|
||||
} else {
|
||||
throw new \Exception('Improper aliasing on secondary/join column for join');
|
||||
|
|
|
|||
|
|
@ -1,968 +0,0 @@
|
|||
<?php
|
||||
namespace Webkul\Ui\DataGrid;
|
||||
|
||||
use Illuminate\Http\Request;
|
||||
use Illuminate\Support\Collection;
|
||||
use Illuminate\Support\Validate;
|
||||
use Illuminate\Pagination\Paginator;
|
||||
use Illuminate\Support\Facades\DB;
|
||||
use Webkul\Ui\DataGrid\Helpers\Column;
|
||||
use Webkul\Ui\DataGrid\Helpers\Pagination;
|
||||
use Webkul\Ui\DataGrid\Helpers\Css;
|
||||
use Webkul\Attribute\Repositories\AttributeRepository;
|
||||
use Webkul\Product\Repositories\ProductAttributeValueRepository;
|
||||
use Webkul\Product\Models\ProductAttributeValue;
|
||||
use URL;
|
||||
|
||||
/**
|
||||
* DataGrid controller
|
||||
*
|
||||
* @author Nikhil Malik <nikhil@webkul.com> @ysmnikhil
|
||||
* &
|
||||
* @author Prashant Singh <prashant.singh852@webkul.com> @prashant-webkul
|
||||
*
|
||||
* @copyright 2018 Webkul Software Pvt Ltd (http://www.webkul.com)
|
||||
*/
|
||||
|
||||
|
||||
class ProductGrid
|
||||
{
|
||||
|
||||
/**
|
||||
* Name of DataGrid
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
|
||||
protected $name;
|
||||
|
||||
|
||||
/**
|
||||
* select from table(s)
|
||||
*
|
||||
* @var string
|
||||
*/
|
||||
|
||||
protected $select;
|
||||
|
||||
|
||||
/**
|
||||
* Table
|
||||
* @var Boolean for aliasing
|
||||
*/
|
||||
|
||||
protected $aliased;
|
||||
|
||||
|
||||
/**
|
||||
* Pagination variable
|
||||
* @var String
|
||||
*/
|
||||
|
||||
protected $perpage;
|
||||
|
||||
|
||||
/**
|
||||
* Table
|
||||
*
|
||||
* @var String Classs name $table
|
||||
*/
|
||||
|
||||
protected $table;
|
||||
|
||||
|
||||
/**
|
||||
* Join
|
||||
*
|
||||
* @var Array name $join
|
||||
*
|
||||
* [
|
||||
* 'join' => 'left',
|
||||
* 'table' => 'posts',
|
||||
* 'primaryKey' => 'user.id',
|
||||
* 'condition' => '=',
|
||||
* 'secondryKey' => 'posts.user_id',
|
||||
* 'callback' => 'not supported yet'
|
||||
* ]
|
||||
*/
|
||||
|
||||
protected $join;
|
||||
|
||||
|
||||
/**
|
||||
* Collection Object of Column $columns
|
||||
*
|
||||
* @var Collection
|
||||
*/
|
||||
|
||||
protected $columns;
|
||||
|
||||
|
||||
/**
|
||||
* array of columns
|
||||
* to be filtered
|
||||
* @var Array
|
||||
*/
|
||||
|
||||
protected $filterable;
|
||||
|
||||
|
||||
/**
|
||||
* array of columns
|
||||
* to be searched
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
|
||||
protected $searchable;
|
||||
|
||||
|
||||
/**
|
||||
* mass operations
|
||||
*
|
||||
* @var Array
|
||||
*/
|
||||
|
||||
protected $massoperations;
|
||||
|
||||
|
||||
/**
|
||||
* Pagination $pagination
|
||||
*
|
||||
* @var Pagination
|
||||
*/
|
||||
|
||||
|
||||
protected $pagination;
|
||||
|
||||
|
||||
/**
|
||||
* Css $css
|
||||
*
|
||||
* @var Css
|
||||
*/
|
||||
|
||||
protected $css;
|
||||
|
||||
|
||||
/**
|
||||
* Actions $action
|
||||
* @var action
|
||||
*/
|
||||
|
||||
protected $actions;
|
||||
|
||||
/**
|
||||
* Attribute Columns
|
||||
* for columns which
|
||||
* are attributes.
|
||||
*
|
||||
* @var $attributeColumns
|
||||
*/
|
||||
|
||||
protected $attributeColumns;
|
||||
|
||||
/**
|
||||
* URL parse $parsed
|
||||
* @var parse
|
||||
*/
|
||||
|
||||
protected $parsed;
|
||||
|
||||
/**
|
||||
* To store the
|
||||
* attribute columns aliases
|
||||
* for search, filter and
|
||||
* sort.
|
||||
*
|
||||
* @var attributeAliases
|
||||
*/
|
||||
|
||||
private $attributeAliases = [];
|
||||
|
||||
//Prepares the input parameters passed as the configuration for datagrid.
|
||||
public function make($args)
|
||||
{
|
||||
// list($name, $select, $table, $join, $columns) = array_values($args);
|
||||
$name = $select = $aliased = $table = false;
|
||||
$join = $columns = $filterable = $searchable =
|
||||
$massoperations = $attributeColumns = $css = $operators = $actions = [];
|
||||
extract($args);
|
||||
return $this->build($name, $select, $filterable, $searchable, $massoperations, $attributeColumns , $aliased, $perpage, $table, $join, $columns, $css, $operators,$actions);
|
||||
}
|
||||
|
||||
//contructor for getting the current locale and channel
|
||||
|
||||
private $locale;
|
||||
private $channel;
|
||||
private $attributes;
|
||||
private $allAttributes;
|
||||
|
||||
public function __construct(AttributeRepository $attributes) {
|
||||
|
||||
$this->channel = request()->get('channel') ?: core()->getDefaultChannelCode();
|
||||
$this->locale = request()->get('locale') ?: app()->getLocale();
|
||||
$this->attributes = $attributes;
|
||||
|
||||
}
|
||||
|
||||
//This assigns the private and public properties of the datagrid classes from make functions
|
||||
public function build(
|
||||
$name = null,
|
||||
$select = false,
|
||||
array $filterable = [],
|
||||
array $searchable = [],
|
||||
array $massoperations = [],
|
||||
array $attributeColumns = [],
|
||||
bool $aliased = false,
|
||||
$perpage = 0,
|
||||
$table = null,
|
||||
array $join = [],
|
||||
array $columns = null,
|
||||
array $css = [],
|
||||
array $operators = [],
|
||||
array $actions = [],
|
||||
Pagination $pagination = null
|
||||
) {
|
||||
$this->request = Request::capture();
|
||||
$this->setName($name);
|
||||
$this->setSelect($select);
|
||||
$this->setFilterable($filterable);
|
||||
$this->setSearchable($filterable);
|
||||
$this->setMassOperations($massoperations);
|
||||
$this->setAttributeColumns($attributeColumns);
|
||||
$this->setAlias($aliased);
|
||||
$this->setPerPage($perpage);
|
||||
$this->setTable($table);
|
||||
$this->setJoin($join);
|
||||
$this->addColumns($columns, true);
|
||||
$this->setCss($css);
|
||||
$this->setOperators($operators);
|
||||
$this->setActions($actions);
|
||||
// $this->addPagination($pagination);
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Name.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setName(string $name)
|
||||
{
|
||||
$this->name = $name ?: 'Default' . time();
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Select.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setSelect($select)
|
||||
{
|
||||
$this->select = $select ? : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Filterable
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setFilterable(array $filterable)
|
||||
{
|
||||
$this->filterable = $filterable ? : [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set Searchable columns
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setSearchable($searchable)
|
||||
{
|
||||
$this->searchable = $searchable ? : [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set mass operations
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setMassOperations($massops)
|
||||
{
|
||||
$this->massoperations = $massops ? : [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set alias parameter
|
||||
* to know whether
|
||||
* aliasing is true or not.
|
||||
*
|
||||
* @return $this.
|
||||
*/
|
||||
|
||||
public function setAlias(bool $aliased)
|
||||
{
|
||||
$this->aliased = $aliased ? : false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set the default
|
||||
* pagination for
|
||||
* data grid.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setPerPage($perpage)
|
||||
{
|
||||
$this->perpage = $perpage ? : 5;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set table name in front
|
||||
* of query scope.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setTable(string $table)
|
||||
{
|
||||
$this->table = $table ?: false;
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Set join bag if
|
||||
* present.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setJoin(array $join)
|
||||
{
|
||||
$this->join = $join ?: [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds the custom css rules
|
||||
* @retun $this
|
||||
*/
|
||||
|
||||
private function setCss($css = [])
|
||||
{
|
||||
$this->css = new Css($css);
|
||||
return $this->css;
|
||||
}
|
||||
|
||||
/**
|
||||
* To set the attributes
|
||||
* bag on product datagrid.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
private function setAttributeColumns($attributes = []) {
|
||||
$this->attributeColumns = $attributes;
|
||||
return $this->attributeColumns;
|
||||
}
|
||||
|
||||
/**
|
||||
* setFilterableColumns
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
// public function setFilterableColumns($filterable_columns = [])
|
||||
// {
|
||||
// $this->join = $filterable_columns ?: [];
|
||||
// return $this;
|
||||
// }
|
||||
|
||||
/**
|
||||
* Section actions bag
|
||||
* here.
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setActions($actions = []) {
|
||||
$this->actions = $actions ?: [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Columns.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function addColumns($columns = [], $reCreate = false)
|
||||
{
|
||||
if ($reCreate) {
|
||||
$this->columns = new Collection();
|
||||
}
|
||||
if ($columns) {
|
||||
foreach ($columns as $column) {
|
||||
$this->addColumn($column);
|
||||
}
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Column.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function addColumn($column = [])
|
||||
{
|
||||
if ($column instanceof Column) {
|
||||
$this->columns->push($column);
|
||||
} elseif (gettype($column) == 'array' && $column) {
|
||||
$this->columns->push(new Column($column, $this->request));
|
||||
} else {
|
||||
throw new \Exception("DataGrid: Add Column argument is not valid!");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add ColumnMultiple.
|
||||
* Currently is not
|
||||
* of any use.
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
private function addColumnMultiple($column = [], $multiple = false)
|
||||
{
|
||||
if ($column instanceof Column) {
|
||||
if ($multiple) {
|
||||
if ($this->columns->offsetExists($column->getName())) {
|
||||
$this->columns->offsetSet($column->getName(). time(), $column);
|
||||
} else {
|
||||
$this->columns->offsetSet($column->getName(), $column);
|
||||
}
|
||||
} else {
|
||||
$this->columns->offsetSet($column->getName(), $column);
|
||||
}
|
||||
} elseif (gettype($column) == 'array' && $column) {
|
||||
$columnObj = new Column($column);
|
||||
if ($multiple) {
|
||||
if ($this->columns->offsetExists($columnObj->getName())) {
|
||||
$this->columns->offsetSet($columnObj->getName(). time(), $columnObj);
|
||||
} else {
|
||||
$this->columns->offsetSet($columnObj->getName(), $columnObj);
|
||||
}
|
||||
} else {
|
||||
$this->columns->offsetSet($columnObj->getName(), $columnObj);
|
||||
}
|
||||
} else {
|
||||
throw new \Exception("DataGrid: Add Column argument is not valid!");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Adds expressional verbs to be used
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function setOperators(array $operators)
|
||||
{
|
||||
$this->operators = $operators ?: [];
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Add Pagination.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
public function addPagination($pagination = [])
|
||||
{
|
||||
if ($pagination instanceof Pagination) {
|
||||
$this->pagination = $pagination;
|
||||
} elseif (gettype($pagination) == 'array' && $pagination) {
|
||||
$this->pagination = new Pagination($pagination);
|
||||
} else {
|
||||
throw new \Exception("DataGrid: Pagination argument is not valid!");
|
||||
}
|
||||
return $this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Used for selecting
|
||||
* the columns got in
|
||||
* make from controller.
|
||||
* @return $this
|
||||
*/
|
||||
|
||||
private function getSelect()
|
||||
{
|
||||
$select = [];
|
||||
foreach ($this->columns as $column) {
|
||||
$select[] = $column->name.' as '.$column->alias;
|
||||
}
|
||||
|
||||
$this->query->select(...$select);
|
||||
|
||||
if ($this->select) {
|
||||
$this->query->addselect($this->select);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* This function will
|
||||
* map and resolve the
|
||||
* product attributes
|
||||
* and thier values
|
||||
* per channel and per
|
||||
* locale.
|
||||
*
|
||||
* @return Array
|
||||
*/
|
||||
|
||||
private function resolveOrMapAttributes () {
|
||||
return $this->query->get();
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* To find the alias
|
||||
* of the column and
|
||||
* by taking the column
|
||||
* name.
|
||||
* @return string
|
||||
*/
|
||||
|
||||
public function findAlias($column_alias) {
|
||||
foreach($this->columns as $column) {
|
||||
if($column->alias == $column_alias) {
|
||||
return $column->name;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Parse the URL
|
||||
* and get it ready
|
||||
* to be used.
|
||||
*
|
||||
* @return String
|
||||
*/
|
||||
|
||||
private function parse()
|
||||
{
|
||||
$parsed = [];
|
||||
|
||||
$unparsed = url()->full();
|
||||
|
||||
if (count(explode('?', $unparsed))>1) {
|
||||
|
||||
$to_be_parsed = explode('?', $unparsed)[1];
|
||||
|
||||
parse_str($to_be_parsed, $parsed);
|
||||
|
||||
unset($parsed['page']);
|
||||
|
||||
return $parsed;
|
||||
} else {
|
||||
|
||||
return $parsed;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Getting all attributes from the repository instance
|
||||
* type hinted in the contructor of product grid.
|
||||
*
|
||||
* @return $this
|
||||
*/
|
||||
public function getAttributes() {
|
||||
|
||||
return $this->attributes->all();
|
||||
}
|
||||
|
||||
public function filterProductByAttributes($attributes) {
|
||||
|
||||
foreach($attributes as $key => $value) {
|
||||
|
||||
$filterAlias = 'filter_' . $attribute->code;
|
||||
|
||||
$qb->leftJoin('product_attribute_values as ' . $filterAlias, 'products.id', '=', $filterAlias . '.product_id');
|
||||
|
||||
$qb->where($filterAlias . '.' . ProductAttributeValue::$attributeTypeFields[$attribute->type], 'Product Name');
|
||||
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
* ->join('contacts', 'users.id', '=', 'contacts.user_id')
|
||||
* @return $this->query
|
||||
*/
|
||||
|
||||
private function getQueryWithJoin()
|
||||
{
|
||||
foreach ($this->join as $join) {
|
||||
// if(array_key_exists('withAttributes',$join)) {
|
||||
// $qb = $this->query;
|
||||
|
||||
// $channel = $this->channel;
|
||||
|
||||
// $locale = $this->locale;
|
||||
|
||||
// foreach ($this->attributeColumns as $code) {
|
||||
|
||||
// $attribute = $this->attributes->findOneByField('code', $code);
|
||||
|
||||
// $productValueAlias = 'pav' . $attribute->code;
|
||||
|
||||
// array_push($this->attributeAliases, $productValueAlias);
|
||||
|
||||
// $qb->leftJoin('product_attribute_values as ' . $productValueAlias, function ($leftJoin) use ($channel, $locale, $attribute, $productValueAlias) {
|
||||
|
||||
// $leftJoin->on('prods.id', $productValueAlias . '.product_id');
|
||||
|
||||
// if ($attribute->value_per_channel) {
|
||||
|
||||
// if ($attribute->value_per_locale) {
|
||||
|
||||
// $leftJoin->where($productValueAlias . '.channel', $channel)->where($productValueAlias . '.locale', $locale);
|
||||
|
||||
// } else {
|
||||
// $leftJoin->where($productValueAlias . '.channel', $channel);
|
||||
// }
|
||||
// } else {
|
||||
|
||||
// if ($attribute->value_per_locale) {
|
||||
|
||||
// $leftJoin->where($productValueAlias . '.locale', $locale);
|
||||
// }
|
||||
// }
|
||||
|
||||
// $leftJoin->where($productValueAlias . '.attribute_id', $attribute->id);
|
||||
// });
|
||||
|
||||
// $qb->addSelect($productValueAlias . '.' . ProductAttributeValue::$attributeTypeFields[$attribute->type] . ' as ' . $code);
|
||||
// }
|
||||
// }
|
||||
// else
|
||||
if(array_key_exists('withAttributes', $join)) {
|
||||
$this->query->{$join['join']}($join['table'], $join['primaryKey'], $join['condition'], $join['secondaryKey'])->where('attribute_id','=',2)->addSelect('name as product_name');
|
||||
}
|
||||
else
|
||||
$this->query->{$join['join']}($join['table'], $join['primaryKey'], $join['condition'], $join['secondaryKey']);
|
||||
}
|
||||
|
||||
// dd($this->query->toSql());
|
||||
$this->query->get();
|
||||
}
|
||||
|
||||
/**
|
||||
* Use this function
|
||||
* when taking filters
|
||||
* on columns while
|
||||
* fetching columns
|
||||
*
|
||||
* @return array
|
||||
*/
|
||||
|
||||
private function getQueryWithColumnFilters()
|
||||
{
|
||||
foreach ($this->columns as $column) {
|
||||
if ($column->filter) { // if the filter bag in array exists then these will be applied.
|
||||
if (count($column->filter['condition']) == count($column->filter['condition'], COUNT_RECURSIVE)) {
|
||||
$this->query->{$column->filter['function']}(...$column->filter['condition']);
|
||||
} else {
|
||||
if (count($column->filter['condition']) == 3) {
|
||||
$this->query->{$column->filter['function']}(
|
||||
extract(
|
||||
array_combine(
|
||||
// ['key', 'condition', 'value'],
|
||||
array_fill( //will work with all kind of where conditions
|
||||
0,
|
||||
(count($column->filter['condition']) - 1),
|
||||
'array_fill_nikhil'.time()
|
||||
),
|
||||
$column->filter['condition']
|
||||
)
|
||||
)
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Function runs when
|
||||
* filters, sort, search
|
||||
* any of it is applied
|
||||
* @return $this->query
|
||||
*/
|
||||
|
||||
private function getQueryWithFilters()
|
||||
{
|
||||
$parsed = $this->parse();
|
||||
|
||||
if ($this->aliased) { //aliasing is expected in this case or it will be changed to presence of join bag
|
||||
foreach ($parsed as $key=>$value) {
|
||||
if ($key=="sort") {
|
||||
//resolve the case with the column helper class
|
||||
if(substr_count($key,'_') >= 1)
|
||||
$column_name = $this->findAlias($key);
|
||||
|
||||
//case that don't need any resolving
|
||||
$count_keys = count(array_keys($value));
|
||||
if ($count_keys==1) {
|
||||
$this->query->orderBy(
|
||||
str_replace('_', '.', array_keys($value)[0]),
|
||||
array_values($value)[0]
|
||||
);
|
||||
} else {
|
||||
throw new \Exception('Multiple Sort keys Found, Please Resolve the URL Manually.');
|
||||
}
|
||||
} elseif ($key=="search") {
|
||||
|
||||
$count_keys = count(array_keys($value));
|
||||
if($count_keys==1)
|
||||
$this->query->where(function ($query) use ($parsed) {
|
||||
foreach ($this->searchable as $search) {
|
||||
$query->orWhere($search['column'], 'like', '%'.$parsed['search']['all'].'%');
|
||||
}
|
||||
});
|
||||
} else {
|
||||
$column_name = $this->findAlias($key);
|
||||
if (array_keys($value)[0]=="like" || array_keys($value)[0]=="nlike") {
|
||||
foreach ($value as $condition => $filter_value) {
|
||||
$this->query->where(
|
||||
$column_name,
|
||||
$this->operators[$condition],
|
||||
'%'.$filter_value.'%'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
foreach ($value as $condition => $filter_value) {
|
||||
$this->query->where(
|
||||
$column_name,
|
||||
$this->operators[$condition],
|
||||
$filter_value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
} else {
|
||||
//this is the case for the non aliasing.
|
||||
foreach ($parsed as $key=>$value) {
|
||||
|
||||
if ($key=="sort") {
|
||||
|
||||
//case that don't need any resolving
|
||||
$count_keys = count(array_keys($value));
|
||||
if ($count_keys==1) {
|
||||
|
||||
$this->query->orderBy(
|
||||
array_keys($value)[0],
|
||||
array_values($value)[0]
|
||||
);
|
||||
|
||||
} else {
|
||||
throw new \Exception('Multiple Sort keys Found, Please Resolve the URL Manually.');
|
||||
}
|
||||
} elseif ($key=="search") {
|
||||
|
||||
$count_keys = count(array_keys($value));
|
||||
if($count_keys==1)
|
||||
$this->query->where(function ($query) use ($parsed) {
|
||||
foreach ($this->searchable as $search) {
|
||||
$query->orWhere($search['column'], 'like', '%'.$parsed['search']['all'].'%');
|
||||
}
|
||||
});
|
||||
else
|
||||
throw new \Exception('Multiple Search keys Found, Please Resolve the URL Manually.');
|
||||
|
||||
} else {
|
||||
// $column_name = $key;
|
||||
$column_name = $this->findAlias($key);
|
||||
if (array_keys($value)[0]=="like" || array_keys($value)[0]=="nlike") {
|
||||
foreach ($value as $condition => $filter_value) {
|
||||
$this->query->where(
|
||||
$column_name,
|
||||
$this->operators[$condition],
|
||||
'%'.$filter_value.'%'
|
||||
);
|
||||
}
|
||||
} else {
|
||||
foreach ($value as $condition => $filter_value) {
|
||||
$this->query->where(
|
||||
$column_name,
|
||||
$this->operators[$condition],
|
||||
$filter_value
|
||||
);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private function getDbQueryResults()
|
||||
{
|
||||
$parsed = $this->parse();
|
||||
|
||||
if ($this->aliased==true) {
|
||||
|
||||
//flags
|
||||
$table_alias = false;
|
||||
$join_table_alias = false;
|
||||
$allowed_joins = false;
|
||||
$other_joins = false;
|
||||
$join_good = false;
|
||||
|
||||
//prepare query object
|
||||
$this->query = DB::table($this->table);
|
||||
|
||||
//explode if alias is available
|
||||
if (strpos('.', $this->table)) {
|
||||
throw new \Exception("Dot(s) cannot be used in table names in Database.");
|
||||
} else {
|
||||
$exploded = explode('as', $this->table);
|
||||
}
|
||||
|
||||
//check whether exploded string still has same table name
|
||||
if ($exploded[0]==$this->table) {
|
||||
$table_alias = false;
|
||||
} else { // (isset($exploded))
|
||||
$table_alias = true;
|
||||
$table_name = trim($exploded[0]);
|
||||
$table_alias = trim($exploded[1]);
|
||||
}
|
||||
|
||||
//Run this if there are any selects priorly.
|
||||
if (!empty($this->select)) {
|
||||
$this->getSelect();
|
||||
}
|
||||
|
||||
//Run this if there are joins
|
||||
if (!empty($this->join)) {
|
||||
|
||||
foreach ($this->join as $index => $join) {
|
||||
|
||||
$name = strtolower($join['join']);
|
||||
|
||||
//Allow joins i.e left or right
|
||||
if ($name=='leftjoin' || $name=='rightjoin') {
|
||||
|
||||
//check if the aliasing on the primary table and primaryKey in join is also the same
|
||||
$primary_key_alias = trim(explode('.', $join['primaryKey'])[0]);
|
||||
|
||||
if ($primary_key_alias == $table_alias) {
|
||||
|
||||
$join_table_alias = explode('as', $join['table']);
|
||||
|
||||
if (isset($join_table_alias)) {
|
||||
|
||||
$alias1 = trim($join_table_alias[1]); //important!!!!!
|
||||
|
||||
//check if the secondary table match column is not having '.' and has proper alias
|
||||
$secondary_join_column = $join['secondaryKey'];
|
||||
if (isset($secondary_join_column)) {
|
||||
|
||||
$exploded_secondary = explode('.', $secondary_join_column);
|
||||
$alias2 = trim($exploded_secondary[0]);
|
||||
|
||||
if ($alias1 == $alias2) {
|
||||
if($index==count($this->join)-1)
|
||||
$this->getQueryWithJoin();
|
||||
$alias_proper_secondary = true;
|
||||
} else {
|
||||
throw new \Exception('Aliases of Join table and the secondary key columns do not match.');
|
||||
|
||||
}
|
||||
} else {
|
||||
throw new \Exception('Improper aliasing on secondary/join column for join.');
|
||||
}
|
||||
|
||||
} else {
|
||||
throw new \Exception('Join/Secondary table alias is not found for join.');
|
||||
}
|
||||
} else {
|
||||
throw new \Exception('Primary key and primary table aliases do not match for join.');
|
||||
}
|
||||
} else {
|
||||
$other_joins = true;
|
||||
throw new \Exception('Please check if there is some fault in your aliasing and do not use as in column names or you might have been using a join that is not allowed i.e cross, inner, etc use left and right join only.');
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
//Check for column filter bags and resolve aliasing
|
||||
$this->getQueryWithColumnFilters();
|
||||
|
||||
if (!empty($parsed)) {
|
||||
$this->getQueryWithFilters();
|
||||
}
|
||||
|
||||
$this->results = $this->query->paginate($this->perpage)->appends(request()->input());
|
||||
return $this->results;
|
||||
|
||||
} else {
|
||||
|
||||
$this->query = DB::table($this->table);
|
||||
|
||||
$this->getSelect();
|
||||
|
||||
$this->getQueryWithColumnFilters();
|
||||
|
||||
if (!empty($parsed)) {
|
||||
$this->getQueryWithFilters();
|
||||
}
|
||||
|
||||
$this->results = $this->query->distinct()->paginate($this->perpage)->appends(request()->input());
|
||||
return $this->results;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Main Render Function,
|
||||
* it renders views responsible
|
||||
* for loading datagrid.
|
||||
*
|
||||
* @return view
|
||||
*/
|
||||
|
||||
public function render()
|
||||
{
|
||||
|
||||
$this->allAttributes = $this->getAttributes();
|
||||
$this->getDbQueryResults();
|
||||
// dd($this->results);
|
||||
return view('ui::datagrid.index', [
|
||||
'css' => $this->css,
|
||||
'results' => $this->results,
|
||||
'columns' => $this->columns,
|
||||
'attribute_columns' => $this->attributeColumns,
|
||||
'attributeAliases' => $this->attributeAliases,
|
||||
'filterable' =>$this->filterable,
|
||||
'operators' => $this->operators,
|
||||
'massoperations' => $this->massoperations,
|
||||
'actions' => $this->actions,
|
||||
]);
|
||||
}
|
||||
}
|
||||
File diff suppressed because one or more lines are too long
|
|
@ -1,4 +1,4 @@
|
|||
{
|
||||
"/js/shop.js": "/js/shop.js",
|
||||
"/css/shop.css": "/css/shop.css"
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue