Skip to content

Commit b6b7609

Browse files
committed
fixa bug with boolean when creating resource from-database
1 parent 4589a69 commit b6b7609

File tree

9 files changed

+410
-279
lines changed

9 files changed

+410
-279
lines changed

config/codegenerator.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -215,6 +215,8 @@
215215
'name',
216216
'label',
217217
'header',
218+
'subject',
219+
'head',
218220
],
219221

220222
/*
@@ -279,6 +281,12 @@
279281
|
280282
*/
281283
'common_definitions' => [
284+
[
285+
'match' => '*',
286+
'set' => [
287+
'labels' => '[% field_name_title %]',
288+
],
289+
],
282290
[
283291
'match' => 'id',
284292
'set' => [
@@ -372,6 +380,7 @@
372380
'data-type' => 'boolean',
373381
'html-type' => 'checkbox',
374382
'is-nullable' => false,
383+
'options' => ["No", "Yes"],
375384
],
376385
],
377386
[

src/Commands/Bases/ResourceFileCommandBase.php

Lines changed: 3 additions & 81 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,7 @@
44

55
use CrestApps\CodeGenerator\Models\Resource;
66
use CrestApps\CodeGenerator\Support\Config;
7-
use CrestApps\CodeGenerator\Support\FieldTransformer;
8-
use CrestApps\CodeGenerator\Support\Helpers;
97
use CrestApps\CodeGenerator\Traits\CommonCommand;
10-
use Exception;
118
use Illuminate\Console\Command;
129

1310
class ResourceFileCommandBase extends Command
@@ -18,12 +15,13 @@ class ResourceFileCommandBase extends Command
1815
* Gets the resource from the giving file
1916
*
2017
* @param string $file
18+
* @param array $languages
2119
*
2220
* @return CrestApps\CodeGenerator\Models\Resource
2321
*/
24-
protected function getResources($file)
22+
protected function getResources($file, array $languages = [])
2523
{
26-
return Resource::fromJson($this->getFileContent($file), 'crestapps');
24+
return Resource::fromJson($this->getFileContent($file), 'crestapps', $languages);
2725
}
2826

2927
/**
@@ -40,82 +38,6 @@ protected function getFilename($name)
4038
return $path . $name;
4139
}
4240

43-
/**
44-
* Get primary key properties.
45-
*
46-
* @param object $input
47-
*
48-
* @return array
49-
*/
50-
protected function getFields(array $fieldNames, array $transaltionFor)
51-
{
52-
//a,b,c
53-
//OR
54-
//name:a;html-type:select;options:first|second|third|fourth
55-
$fields = [];
56-
57-
foreach ($fieldNames as $fieldName) {
58-
$properties = [];
59-
60-
if (str_contains($fieldName, ':')) {
61-
// Handle the following format
62-
// name:a;html-type:select;options:first|second|third|fourth
63-
if (!str_contains($fieldName, 'name:')) {
64-
throw new Exception('The "name:" property is not present and is required!');
65-
}
66-
67-
$parts = explode(';', $fieldName);
68-
69-
foreach ($parts as $part) {
70-
71-
if (str_contains($part, ':')) {
72-
list($key, $value) = explode(':', $part);
73-
$properties[$key] = $value;
74-
75-
if ($key == 'options') {
76-
$options = explode('|', $value);
77-
78-
$properties['options'] = [];
79-
80-
foreach ($options as $option) {
81-
$optionTitle = Helpers::convertNameToLabel($option);
82-
83-
if (count($transaltionFor) == 0) {
84-
$properties['options'][$option] = $optionTitle;
85-
}
86-
87-
foreach ($transaltionFor as $lang) {
88-
if (!isset($properties['options'][$lang])) {
89-
$properties['options'][$lang] = [];
90-
}
91-
$properties['options'][$lang][$option] = $optionTitle;
92-
}
93-
}
94-
} else {
95-
$properties[$key] = $value;
96-
}
97-
} else {
98-
$properties['name'] = $part;
99-
}
100-
}
101-
} else {
102-
// handle the following format
103-
// a,b,c
104-
$properties['name'] = $fieldName;
105-
}
106-
107-
$label = Helpers::convertNameToLabel($properties['name']);
108-
109-
foreach ($transaltionFor as $lang) {
110-
$properties['label'][$lang] = $label;
111-
}
112-
113-
$fields[] = $properties;
114-
}
115-
116-
return FieldTransformer::fromArray($fields, 'generic');
117-
}
118-
11941
/**
12042
* Display a common error
12143
*

src/Commands/ResourceFileAppendCommand.php

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use CrestApps\CodeGenerator\Models\ForeignRelationship;
77
use CrestApps\CodeGenerator\Models\Index;
88
use CrestApps\CodeGenerator\Models\Resource;
9+
use CrestApps\CodeGenerator\Support\FieldTransformer;
910
use CrestApps\CodeGenerator\Support\Helpers;
1011

1112
class ResourceFileAppendCommand extends ResourceFileCreatorCommandBase
@@ -53,7 +54,7 @@ public function handle()
5354
return false;
5455
}
5556

56-
$resource = $this->getResources($file);
57+
$resource = $this->getResources($file, $input->translationFor);
5758

5859
$totalAddedFields = $this->mergeFields($resource, $input);
5960
$totalAddedRelations = $this->mergeRelations($resource, $input);
@@ -89,7 +90,7 @@ public function handle()
8990
protected function mergeFields(&$resource, $input)
9091
{
9192
$existingNames = Collect($resource->fields)->pluck('name')->all();
92-
$fields = $this->getFields($input->fieldNames, $input->translationFor);
93+
$fields = FieldTransformer::fromString($this->option('fields'), 'generic', $input->translationFor);
9394
$mergeFields = 0;
9495
foreach ($fields as $field) {
9596
if (in_array($field->name, $existingNames)) {

src/Commands/ResourceFileCreateCommand.php

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
use CrestApps\CodeGenerator\Models\Index;
88
use CrestApps\CodeGenerator\Models\Resource;
99
use CrestApps\CodeGenerator\Support\Config;
10+
use CrestApps\CodeGenerator\Support\FieldTransformer;
1011
use CrestApps\CodeGenerator\Support\Helpers;
1112
use CrestApps\CodeGenerator\Support\ResourceMapper;
1213

@@ -59,7 +60,7 @@ public function handle()
5960
$mapper->append($input->modelName, $input->file);
6061
}
6162

62-
$fields = $this->getFields($input->fieldNames, $input->translationFor);
63+
$fields = FieldTransformer::fromString($this->option('fields'), 'generic', $input->translationFor);
6364
$relations = $this->getRelations($input->relations);
6465
$indexes = $this->getIndexes($input->indexes);
6566

src/DatabaseParsers/MysqlParser.php

Lines changed: 2 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -273,8 +273,8 @@ protected function getTransfredFields(array $columns)
273273

274274
$collection[] = $properties;
275275
}
276-
$localeGroup = Str::plural(strtolower($this->tableName));
277-
$fields = FieldTransformer::fromArray($collection, $localeGroup);
276+
$localeGroup = Helpers::makeLocaleGroup($this->tableName);
277+
$fields = FieldTransformer::fromArray($collection, $localeGroup, $this->languages);
278278

279279
return $fields;
280280
}
@@ -366,38 +366,13 @@ protected function getForeignConstraint($name)
366366
*/
367367
protected function getHtmlOptions($dataType, $columnType)
368368
{
369-
if ($columnType == 'tinyint(1)') {
370-
return $this->getBooleanOptions();
371-
}
372-
373369
if (($options = $this->getEnumOptions($columnType)) !== null) {
374370
return $options;
375371
}
376372

377373
return [];
378374
}
379375

380-
/**
381-
* Get boolean options
382-
*
383-
* @return array
384-
*/
385-
protected function getBooleanOptions()
386-
{
387-
$options = [];
388-
if (!$this->hasLanguages()) {
389-
return $this->booleanOptions;
390-
}
391-
392-
foreach ($this->booleanOptions as $key => $title) {
393-
foreach ($this->languages as $language) {
394-
$options[$key][$language] = $title;
395-
}
396-
}
397-
398-
return $options;
399-
}
400-
401376
/**
402377
* Parses out the options from a giving type
403378
*
@@ -422,13 +397,6 @@ protected function getEnumOptions($type)
422397
$finals = [];
423398

424399
foreach ($options as $option) {
425-
if ($this->hasLanguages()) {
426-
foreach ($this->languages as $language) {
427-
$finals[$language][$option] = $option;
428-
}
429-
continue;
430-
}
431-
432400
$finals[$option] = $option;
433401
}
434402

0 commit comments

Comments
 (0)