-
Notifications
You must be signed in to change notification settings - Fork 2
/
Extension.php
executable file
·154 lines (128 loc) · 4.31 KB
/
Extension.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
<?php
namespace CupNoodles\TaxClasses;
use System\Classes\BaseExtension;
use System\Classes\ExtensionManager;
// Admin-UI
use Event;
use Admin\Models\Menus_model;
use Admin\Widgets\Form;
use Admin\Classes\AdminController;
use Illuminate\Database\Eloquent\Relations\Relation;
use Illuminate\Support\Facades\DB;
/**
* Butcher Extension Information File
*/
class Extension extends BaseExtension
{
/**
* Returns information about this extension.
*
* @return array
*/
public function extensionMeta()
{
return [
'name' => 'Tax Classes',
'author' => 'CupNoodles',
'description' => 'Allow for different Tax classes per item',
'icon' => 'fas fa-percentage',
'version' => '2.0.0'
];
}
/**
* Register method, called when the extension is first registered.
*
* @return void
*/
public function register()
{
}
/**
* Boot method, called right before the request route.
*
* @return void
*/
public function boot()
{
Menus_Model::extend(function ($model) {
$model->relation['belongsToMany']['tax_classes'] = ['CupNoodles\TaxClasses\Models\TaxClasses', 'table' => 'menu_tax_classes'];
});
Event::listen('admin.form.extendFieldsBefore', function (Form $form) {
if($form->model instanceof Menus_model){
$tax_classes = ['tax_classes' => [
'label' => 'lang:cupnoodles.taxclasses::default.label_tax_classes',
'type' => 'relation',
'span' => 'right'
]];
$form->tabs['fields'] = $this->array_insert_after($form->tabs['fields'], 'menu_priority', $tax_classes);
}
});
}
function array_insert_after( array $array, $key, array $new ) {
$keys = array_keys( $array );
$index = array_search( $key, $keys );
$pos = false === $index ? count( $array ) : $index + 1;
return array_merge( array_slice( $array, 0, $pos ), $new, array_slice( $array, $pos ) );
}
/**
* Registers any front-end components implemented in this extension.
*
* @return array
*/
public function registerComponents()
{
return [
];
}
/**
* Registers any admin permissions used by this extension.
*
* @return array
*/
public function registerPermissions()
{
return [
'Admin.TaxClasses' => [
'label' => 'cupnoodles.taxclasses::default.permissions',
'group' => 'admin::lang.permissions.name',
],
];
}
public function registerCartConditions()
{
$cartConditions = [];
$taxClasses = \CupNoodles\TaxClasses\Models\TaxClasses::all();
$tax_classes = [];
foreach($taxClasses as $taxClass){
// leaving a documentation link here since class_alias() is not a very well-known PHP function
// https://www.php.net/manual/en/function.class-alias.php
$dynamic_class_name = \CupNoodles\TaxClasses\CartConditions\VariableTax::class . '_' . $taxClass->tax_class_id;
class_alias(\CupNoodles\TaxClasses\CartConditions\VariableTax::class, $dynamic_class_name);
$tax_classes[$dynamic_class_name] = [
'name' => $taxClass->name,
'label' => $taxClass->label,
'description' => $taxClass->description,
'tax_class_id' => $taxClass->tax_class_id,
'include_in_price' => $taxClass->include_in_price,
'limit_to_order_type' => $taxClass->limit_to_order_type
];
}
return $tax_classes;
}
public function registerNavigation()
{
return [
'localisation' => [
'child' => [
'taxclasses' => [
'priority' => 90,
'class' => 'TaxClasses',
'href' => admin_url('cupnoodles/taxclasses/taxclasses'),
'title' => lang('cupnoodles.taxclasses::default.side_menu'),
'permission' => 'Admin.TaxClasses',
],
],
],
];
}
}