Skip to content

Commit 3f2fd7e

Browse files
committed
New version
1 parent e93ff99 commit 3f2fd7e

40 files changed

+938
-186
lines changed

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
"email": "[email protected]"
1212
}
1313
],
14-
"version": "0.1.2",
14+
"version": "0.2.0",
1515
"require": {
1616
"illuminate/html": "5.0.*@dev"
1717
},

readme.md

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
1. Install the package via `composer require laraveldaily/quickadmin`.
33
2. Add `Laraveldaily\Quickadmin\QuickadminServiceProvider::class,` to your `\config\app.php` providers.
44
3. Run `php artisan quickadmin:install` and fill the required information.
5-
4. Access QuickAdmin panel by visiting `http://yourdomain/qa`.
5+
4. Register middleware `'role' => \Laraveldaily\Quickadmin\Middleware\HasPermissions::class,` in your `App\Http\Kernel.php` at `$routeMiddleware`
6+
5. Access QuickAdmin panel by visiting `http://yourdomain/qa`.

src/Builders/ControllerBuilder.php

Lines changed: 168 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -3,31 +3,37 @@
33

44
use Illuminate\Support\Str;
55
use Laraveldaily\Quickadmin\Cache\QuickCache;
6+
use Laraveldaily\Quickadmin\Models\Crud;
67

78
class ControllerBuilder
89
{
910
// Controller namespace
10-
private $namespace = 'App\Http\Controllers';
11+
private $namespace = 'App\Http\Controllers\Admin';
1112
// Template
1213
private $template;
1314
// Global names
1415
private $name;
1516
private $className;
1617
private $modelName;
17-
private $requestName;
18+
private $createRequestName;
19+
private $updateRequestName;
1820
private $fileName;
21+
private $fields;
22+
private $relationships;
23+
private $files;
1924

2025
/**
2126
* Build our controller file
2227
*/
2328
public function build()
2429
{
25-
$cache = new QuickCache();
26-
$cached = $cache->get('fieldsinfo');
27-
$this->template = __DIR__ . '/../Templates/controller';
28-
$this->name = $cached['name'];
29-
$this->fields = $cached['fields'];
30-
$this->soft = $cached['soft_delete'];
30+
$cache = new QuickCache();
31+
$cached = $cache->get('fieldsinfo');
32+
$this->template = __DIR__ . '/../Templates/controller';
33+
$this->name = $cached['name'];
34+
$this->fields = $cached['fields'];
35+
$this->relationships = $cached['relationships'];
36+
$this->files = $cached['files'];
3137
$this->names();
3238
$template = (string) $this->loadTemplate();
3339
$template = $this->buildParts($template);
@@ -54,31 +60,175 @@ private function buildParts($template)
5460
$template = str_replace([
5561
'$NAMESPACE$',
5662
'$MODEL$',
57-
'$REQUESTNAME$',
63+
'$CREATEREQUESTNAME$',
64+
'$UPDATEREQUESTNAME$',
5865
'$CLASS$',
5966
'$COLLECTION$',
6067
'$RESOURCE$',
68+
'$INDEXGET$',
69+
'$RELATIONSHIPS$',
70+
'$RELATIONSHIP_COMPACT$',
71+
'$RELATIONSHIP_COMPACT_EDIT$',
72+
'$RELATIONSHIP_NAMESPACES$',
73+
'$FILETRAIT$',
74+
'$FILESAVING$'
6175
], [
6276
$this->namespace,
6377
$this->modelName,
64-
$this->requestName,
78+
$this->createRequestName,
79+
$this->updateRequestName,
6580
$this->className,
6681
strtolower($this->modelName),
6782
strtolower($this->modelName),
83+
$this->indexBuilder(),
84+
$this->relationshipsBuilder(),
85+
$this->compactBuilder(),
86+
$this->compactEditBuilder(),
87+
$this->relationshipsNamespaces(),
88+
$this->files > 0 ? 'use App\Http\Controllers\Traits\FileUploadTrait;' : '',
89+
$this->files > 0 ? '$this->saveFiles($request);' : ''
6890
], $template);
6991

7092
return $template;
7193
}
7294

95+
/**
96+
* Build our index template
97+
* @return mixed|string
98+
*/
99+
public function indexBuilder()
100+
{
101+
if ($this->relationships == 0) {
102+
return '$' . strtolower($this->modelName) . ' = ' . $this->modelName . '::all();';
103+
} else {
104+
$relationship = '$' . strtolower($this->modelName) . ' = ' . $this->modelName . '::$WITH$get();';
105+
foreach ($this->fields as $field) {
106+
if ($field->type == 'relationship') {
107+
$relationship = str_replace([
108+
'$WITH$'
109+
], [
110+
'with("' . $field->relationship_name . '")->$WITH$'
111+
], $relationship);
112+
}
113+
}
114+
$relationship = str_replace('$WITH$', '', $relationship);
115+
116+
return $relationship;
117+
}
118+
}
119+
120+
public function relationshipsNamespaces()
121+
{
122+
if ($this->relationships == 0) {
123+
return '';
124+
} else {
125+
$cruds = Crud::all()->keyBy('id');
126+
$relationships = '';
127+
$first = true;
128+
foreach ($this->fields as $field) {
129+
if ($field->type == 'relationship') {
130+
$crud = $cruds[$field->relationship_id];
131+
$relationships .= 'use App\\' . ucfirst(Str::camel($crud->name)) . ";\r\n";
132+
}
133+
}
134+
135+
return $relationships;
136+
}
137+
}
138+
139+
/**
140+
* Build relationships for forms
141+
* @return string
142+
*/
143+
public function relationshipsBuilder()
144+
{
145+
if ($this->relationships == 0) {
146+
return '';
147+
} else {
148+
$cruds = Crud::all()->keyBy('id');
149+
$relationships = '';
150+
$first = true;
151+
foreach ($this->fields as $field) {
152+
if ($field->type == 'relationship') {
153+
// Formatting fix if the relationship is not first
154+
if (!$first) {
155+
$relationships .= ' ';
156+
}
157+
$crud = $cruds[$field->relationship_id];
158+
$relationships .= '$'
159+
. $field->relationship_name
160+
. ' = '
161+
. ucfirst(Str::camel($crud->name))
162+
. '::lists("'
163+
. $field->relationship_field
164+
. '", "id");'
165+
. "\r\n";
166+
}
167+
}
168+
169+
return $relationships;
170+
}
171+
}
172+
173+
/**
174+
* Build compact for create form
175+
* @return mixed|string
176+
*/
177+
public function compactBuilder()
178+
{
179+
if ($this->relationships == 0) {
180+
return '';
181+
} else {
182+
$compact = ', compact($RELATIONS$)';
183+
$first = true;
184+
foreach ($this->fields as $field) {
185+
if ($field->type == 'relationship') {
186+
$toReplace = '';
187+
if ($first != true) {
188+
$toReplace .= ', ';
189+
} else {
190+
$first = false;
191+
}
192+
$toReplace .= '"' . $field->relationship_name . '"$RELATIONS$';
193+
$compact = str_replace('$RELATIONS$', $toReplace, $compact);
194+
}
195+
}
196+
$compact = str_replace('$RELATIONS$', '', $compact);
197+
198+
return $compact;
199+
}
200+
}
201+
202+
/**
203+
* Build compact for edit form
204+
* @return string
205+
*/
206+
public function compactEditBuilder()
207+
{
208+
if ($this->relationships == 0) {
209+
return '';
210+
} else {
211+
$compact = '';
212+
foreach ($this->fields as $field) {
213+
if ($field->type == 'relationship') {
214+
$compact .= ', "' . $field->relationship_name . '"';
215+
}
216+
}
217+
218+
return $compact;
219+
}
220+
}
221+
73222
/**
74223
* Generate names
75224
*/
76225
private function names()
77226
{
78-
$camelName = ucfirst(Str::camel($this->name));
79-
$this->className = $camelName . 'Controller';
80-
$this->modelName = $camelName;
81-
$this->requestName = $camelName . 'Request';
227+
$camelName = ucfirst(Str::camel($this->name));
228+
$this->className = $camelName . 'Controller';
229+
$this->modelName = $camelName;
230+
$this->createRequestName = 'Create' . $camelName . 'Request';
231+
$this->updateRequestName = 'Update' . $camelName . 'Request';
82232

83233
$fileName = $this->className . '.php';
84234
$this->fileName = $fileName;
@@ -89,7 +239,10 @@ private function names()
89239
*/
90240
private function publish($template)
91241
{
92-
file_put_contents(app_path('Http/Controllers/' . $this->fileName), $template);
242+
if (!file_exists(app_path('Http\Controllers\Admin'))) {
243+
mkdir(app_path('Http\Controllers\Admin'));
244+
}
245+
file_put_contents(app_path('Http\Controllers\Admin\\' . $this->fileName), $template);
93246
}
94247

95248
}

src/Builders/MigrationBuilder.php

Lines changed: 26 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33

44
use Illuminate\Support\Str;
55
use Laraveldaily\Quickadmin\Cache\QuickCache;
6+
use Laraveldaily\Quickadmin\Fields\FieldsDescriber;
67

78
class MigrationBuilder
89
{
@@ -53,15 +54,14 @@ private function buildParts($template)
5354
{
5455
$camelName = Str::camel($this->name);
5556
$tableName = strtolower($camelName);
56-
$fields = $this->buildFields();
5757
$template = str_replace([
5858
'$TABLENAME$',
5959
'$CLASS$',
6060
'$FIELDS$'
6161
], [
6262
$tableName,
6363
$this->className,
64-
$fields
64+
$this->buildFields()
6565
], $template);
6666

6767
return $template;
@@ -73,19 +73,38 @@ private function buildParts($template)
7373
*/
7474
private function buildFields()
7575
{
76-
$used = [];
77-
$fields = '$table->increments("id");' . "\r\n";
76+
$migrationTypes = FieldsDescriber::migration();
77+
$used = [];
78+
$fields = '$table->increments("id");' . "\r\n";
7879
foreach ($this->fields as $field) {
7980
// Check if there is no duplication for radio and checkbox
8081
if (!in_array($field->title, $used)) {
81-
$fields .= '$table->string("' . $field->title . '");' . "\r\n";
82-
$used[$field->title] = $field->title;
82+
// Generate our migration line
83+
$migrationLine = str_replace([
84+
'$FIELDNAME$',
85+
'$STATE$',
86+
'$RELATIONSHIP$',
87+
], [
88+
$field->title,
89+
$field->default == 'true' ? 1 : 0,
90+
$field->relationship_name
91+
], $migrationTypes[$field->type]);
92+
$fields .= ' '; // Add formatting space to the migration
93+
$fields .= '$table->' . $migrationLine . ";\r\n";
94+
if ($field->type == 'relationship') {
95+
$used[$field->relationship_name] = $field->relationship_name;
96+
} else {
97+
$used[$field->title] = $field->title;
98+
}
8399
}
84100
}
101+
$fields .= ' '; // Add formatting space to the migration
102+
$fields .= '$table->timestamps();';
85103
if ($this->soft == 1) {
104+
$fields .= "\r\n";
105+
$fields .= ' '; // Add formatting space to the migration
86106
$fields .= '$table->softDeletes();';
87107
}
88-
$fields .= '$table->timestamps();';
89108

90109
return $fields;
91110
}

0 commit comments

Comments
 (0)