Skip to content

Commit

Permalink
Setup defaults and get queries working
Browse files Browse the repository at this point in the history
  • Loading branch information
ryanmitchell committed Jun 15, 2021
1 parent ff2aa25 commit 20be83e
Show file tree
Hide file tree
Showing 5 changed files with 134 additions and 11 deletions.
18 changes: 17 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ Event::listen('admin.form.extendFieldsBefore', function (Form $form) {
});
```

### Extending queries
### Extending where fields

Through your extension add rules by listening for the core `thoughtco.reports.fieldToQuery` event, and apply logic on the basis of the $field and $controller . For example:

Expand All @@ -63,3 +63,19 @@ Event::listen('thoughtco.reports.fieldToQuery', function ($controller, $query, $

});
```

### Extending overall query

Through your extension add rules by listening for the core `extendQuery` event, and apply logic on the basis of the $query, $modelName and $controller . For example:

```php
Event::listen('thoughtco.reports.extendQuery', function($controller, $query, $modelName) {

// we only care about certain models by default - this allows others to extend
if (!in_array($modelName, ['\Admin\Models\Orders_model', '\Admin\Models\Customers_model']))
return;

$query->selectRaw('*, CONCAT(first_name, " ", last_name) as customer_name');

});
```
36 changes: 32 additions & 4 deletions controllers/Builder.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public function view($context, $id)
if (!($model = QueryBuilder::find($id)))
abort(404);

// some fields require extending query
// some fields require extending the where
Event::listen('thoughtco.reports.fieldToQuery', function ($controller, $query, $field, $operator, $value, $condition)
{
// we only care about certain models by default - this allows others to extend
Expand All @@ -85,6 +85,16 @@ public function view($context, $id)
}, $condition);
}

if ($field == 'date_added_relative') {
$value = strtotime('-'.$value.' days');
return $query->where('date_added', $operator, date('Y-m-d H:i:s', $value), $condition);
}

if ($field == 'order_date_relative') {
$value = strtotime('-'.$value.' days');
return $query->where('order_date', $operator, date('Y-m-d', $value), $condition);
}

if ($field == 'orders.categories') {

$value = \Admin\Models\Menus_model::whereHas('categories', function($query) use ($value) {
Expand Down Expand Up @@ -120,10 +130,28 @@ public function view($context, $id)
return $query->where('order_menus.menu_id', $operator, $value, $condition);
}

// catch-all
return $query->where($field, $operator, $value, $condition);

});

// some require extending the overall query
Event::listen('thoughtco.reports.extendQuery', function($controller, $query, $modelName) {

// we only care about certain models by default - this allows others to extend
if (!in_array($modelName, ['\Admin\Models\Orders_model', '\Admin\Models\Customers_model']))
return;

$query->selectRaw('*, CONCAT(first_name, " ", last_name) as customer_name');

if ($modelName == '\Admin\Models\Customers_model') {

$query->leftJoin('addresses', 'addresses.address_id', 'customers.address_id');
$query->selectRaw('CONCAT(address_1, " ", address_2, " ", city, " ", state, " ", postcode) as customer_address');

}

});

// default sort
// $sort = ['orders.order_id', 'desc'];

// csv export
if ($csv = request()->input('csv')) {
Expand Down
24 changes: 24 additions & 0 deletions database/migrations/2021_06_02_setup_report_tables.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,34 @@ public function up()
$table->mediumText('csv_columns');
$table->timestamps();
});

$this->seedReports();

}

public function down()
{
Schema::dropIfExists('thoughtco_reportbuilder');
}

protected function seedReports()
{

DB::table('thoughtco_reportbuilder')->insert(json_decode('[
{
"title": "Orders in the last 30 days",
"builder_json": "'.addslashes('{"model":"\\Admin\\Models\\Orders_model","rules":{"condition":"AND","rules":[{"id":"order_date_relative","field":"order_date_relative","type":"string","input":"select","operator":"greater_or_equal","value":"30"}],"valid":true}}').'",
"list_columns": "'.addslashes('[{"priority":0,"column":"{\"key\":\"email\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Email"},{"priority":1,"column":"{\"key\":\"customer_name\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Name"},{"priority":2,"column":"{\"key\":\"order_total\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Order Total"},{"priority":3,"column":"{\"key\":\"order_date\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Date"}]').'",
"csv_columns": "'.addslashes('[{"priority":0,"column":"{\"key\":\"email\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Email"},{"priority":1,"column":"{\"key\":\"customer_name\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Name"},{"priority":2,"column":"{\"key\":\"order_total\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Order Total"},{"priority":3,"column":"{\"key\":\"order_date\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Date"}]').'"
},
{
"title": "Customers who registered in the last 90 days",
"builder_json": "'.addslashes('{"model":"\\Admin\\Models\\Customers_model","rules":{"condition":"AND","rules":[{"id":"date_added_relative","field":"date_added_relative","type":"string","input":"select","operator":"greater_or_equal","value":"90"}],"valid":true}}').'",
"list_columns": "'.addslashes('[{"priority":0,"column":"{\"key\":\"customer_name\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Name"},{"priority":1,"column":"{\"key\":\"email\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Email"},{"priority":2,"column":"{\"key\":\"customer_address\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\"]}","label":"Address"}]').'",
"csv_columns": "'.addslashes('[{"priority":0,"column":"{\"key\":\"customer_name\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Name"},{"priority":1,"column":"{\"key\":\"email\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\",\"\\\\Admin\\\\Models\\\\Orders_model\"]}","label":"Email"},{"priority":2,"column":"{\"key\":\"customer_address\",\"contexts\":[\"\\\\Admin\\\\Models\\\\Customers_model\"]}","label":"Address"}]').'"
},
]'));


}
}
6 changes: 2 additions & 4 deletions formwidgets/ReportsTable.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,7 @@ public function getDataTableRecords($offset, $limit, $search)
$table = $klass->newQuery();
$query = $parser->parse(json_encode($model->builderjson['rules']), $table);

if ($csv = request()->input('csv')) {
echo 'Use Excel Lib'; exit();
}
$this->fireSystemEvent('thoughtco.reports.extendQuery', [$query, $model->builderjson['model']] );

if (strlen($search)) {
$query->search($search, $this->searchableFields);
Expand All @@ -33,7 +31,7 @@ public function getDataTableRecords($offset, $limit, $search)
[$sortColumn, $sortBy] = $this->defaultSort;
$query->orderBy($sortColumn, $sortBy);
}

$page = ($offset / $limit) + 1;

return $query->paginate($limit, ['*'], 'page', $page);
Expand Down
61 changes: 59 additions & 2 deletions models/config/querybuilder.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,30 @@
'\Admin\Models\Orders_model',
],
]) => 'lang:thoughtco.reports::default.qb.label_customer_email',
json_encode([
'key' => 'order_total',
'contexts' => [
'\Admin\Models\Orders_model',
],
]) => 'lang:admin::lang.orders.label_order_total',
json_encode([
'key' => 'order_date',
'contexts' => [
'\Admin\Models\Orders_model',
],
]) => 'lang:admin::lang.orders.label_order_date',
json_encode([
'key' => 'order_type',
'contexts' => [
'\Admin\Models\Orders_model',
],
]) => 'lang:admin::lang.orders.label_order_type',
json_encode([
'key' => 'customer_address',
'contexts' => [
'\Admin\Models\Customers_model',
],
]) => 'lang:admin::lang.orders.label_delivery_address',
];

return [
Expand Down Expand Up @@ -142,7 +166,23 @@
'greater', 'greater_or_equal',
],
],

[
'id' => 'date_added_relative',
'label' => lang('thoughtco.reports::default.qb.label_customer_joined_relative'),
'input' => 'select',
'values' => [
'7' => lang('thoughtco.reports::default.qb.value_date_relative_7'),
'14' => lang('thoughtco.reports::default.qb.value_date_relative_14'),
'30' => lang('thoughtco.reports::default.qb.value_date_relative_30'),
'90' => lang('thoughtco.reports::default.qb.value_date_relative_90'),
'365' => lang('thoughtco.reports::default.qb.value_date_relative_365'),
],
'operators' => [
'equal', 'not_equal',
'less', 'less_or_equal',
'greater', 'greater_or_equal',
],
],
]
],
'\Admin\Models\Orders_model' => [
Expand Down Expand Up @@ -186,6 +226,23 @@
'todayHighlight' => true,
'autoclose' => true,
],
'operators' => [
'equal', 'not_equal',
'less', 'less_or_equal',
'greater', 'greater_or_equal',
],
],
[
'id' => 'order_date_relative',
'label' => lang('thoughtco.reports::default.qb.label_orders_date_relative'),
'input' => 'select',
'values' => [
'7' => lang('thoughtco.reports::default.qb.value_date_relative_7'),
'14' => lang('thoughtco.reports::default.qb.value_date_relative_14'),
'30' => lang('thoughtco.reports::default.qb.value_date_relative_30'),
'90' => lang('thoughtco.reports::default.qb.value_date_relative_90'),
'365' => lang('thoughtco.reports::default.qb.value_date_relative_365'),
],
'operators' => [
'equal', 'not_equal',
'less', 'less_or_equal',
Expand Down Expand Up @@ -217,7 +274,7 @@
'label' => lang('thoughtco.reports::default.qb.label_orders_type'),
'type' => 'string',
'input' => 'select',
'options' => ['Admin\Models\Locations_model', 'getOrderTypeOptions'],
'values' => ['Admin\Models\Locations_model', 'getOrderTypeOptions'],
],
[
'id' => 'orders.delivery_address',
Expand Down

0 comments on commit 20be83e

Please sign in to comment.