-
Notifications
You must be signed in to change notification settings - Fork 107
/
Copy pathinit-db.php
559 lines (466 loc) · 19.8 KB
/
init-db.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
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
<?php
declare(strict_types=1);
namespace Atk4\Ui\Demos;
use Atk4\Core\Factory;
use Atk4\Data\Field;
use Atk4\Data\Model;
use Atk4\Ui\Exception;
use Atk4\Ui\Form;
use Atk4\Ui\Table;
use Mvorisek\Atk4\Hintable\Data\HintablePropertyDef;
try {
require_once file_exists(__DIR__ . '/db.php')
? __DIR__ . '/db.php'
: __DIR__ . '/db.default.php';
} catch (\PDOException $e) {
// do not show $e unless you can secure DSN!
throw (new Exception('This demo requires access to the database. See "demos/init-db.php"'))
->addMoreInfo('PDO error', $e->getMessage());
}
// a very basic file that sets up Agile Data to be used in some demonstrations
trait ModelPreventModificationTrait
{
public function atomic(\Closure $fx)
{
$eRollback = new \Exception('Prevent modification');
$res = null;
try {
parent::atomic(function () use ($fx, $eRollback, &$res) {
$res = $fx();
throw $eRollback;
});
} catch (\Exception $e) {
if ($e !== $eRollback) {
throw $e;
}
}
return $res;
}
protected function wrapUserActionCallbackPreventModification(Model\UserAction $action, \Closure $outputCallback): void
{
$originalCallback = $action->callback;
$action->callback = function (Model $model, ...$args) use ($action, $originalCallback, $outputCallback) {
if ($model->isEntity()) {
$action = $action->getActionForEntity($model);
$loadedEntity = clone $model;
}
$callbackBackup = $action->callback;
try {
$action->callback = $originalCallback;
$action->execute(...$args);
} finally {
$action->callback = $callbackBackup;
}
return $outputCallback($model->isEntity() && !$model->isLoaded() ? $loadedEntity : $model, ...$args);
};
}
protected function initPreventModification(): void
{
$makeMessageFx = function (string $actionName, Model $model) {
return $model->getModelCaption() . ' action "' . $actionName . '" with "' . $model->getTitle() . '" entity '
. ' was executed. In demo mode, all changes are reverved.';
};
$this->wrapUserActionCallbackPreventModification($this->getUserAction('add'), function (Model $model) use ($makeMessageFx) {
return $makeMessageFx('add', $model);
});
$this->wrapUserActionCallbackPreventModification($this->getUserAction('edit'), function (Model $model) use ($makeMessageFx) {
return $makeMessageFx('edit', $model);
});
$this->getUserAction('delete')->confirmation = 'Please go ahead. Demo mode does not really delete data.';
$this->wrapUserActionCallbackPreventModification($this->getUserAction('delete'), function (Model $model) use ($makeMessageFx) {
return $makeMessageFx('delete', $model);
});
}
}
class ModelWithPrefixedFields extends Model
{
use ModelPreventModificationTrait;
/** @var array<string, string> */
private static $prefixedFieldNames = [];
private function prefixFieldName(string $fieldName, bool $forActualName = false): string
{
$tableShort = $this->table;
if (strlen($tableShort) > 16) {
$tableShort = substr(md5($tableShort), 0, 16);
}
if ($forActualName) {
$fieldName = $this->unprefixFieldName($fieldName);
}
$fieldShort = $fieldName;
$fieldWithIdSuffix = false;
if (str_ends_with($fieldShort, '_id')) {
$fieldShort = substr($fieldShort, 0, -strlen('_id'));
$fieldWithIdSuffix = true;
}
if (strlen($fieldShort) > 16) {
$fieldShort = substr(md5($fieldShort), 0, 16);
}
if ($fieldWithIdSuffix) {
$fieldShort .= '_id';
}
$res = 'atk_' . ($forActualName ? 'a' : '') . 'fp_' . $tableShort . '__' . $fieldShort;
self::$prefixedFieldNames[$res] = $fieldName;
return $res;
}
private function unprefixFieldName(string $name): string
{
if (!str_starts_with($name, 'atk_fp_')) {
return $name;
}
if (isset(self::$prefixedFieldNames[$name . '_id'])) {
return self::$prefixedFieldNames[$name . '_id'];
}
return self::$prefixedFieldNames[$name];
}
protected function createHintablePropsFromClassDoc(string $className): array
{
return array_map(function (HintablePropertyDef $hintableProp) {
$hintableProp->fieldName = $this->prefixFieldName($hintableProp->name);
return $hintableProp;
}, parent::createHintablePropsFromClassDoc($className));
}
protected function init(): void
{
if ($this->idField === 'id') {
$this->idField = $this->prefixFieldName($this->idField);
}
if ($this->titleField === 'name') {
$this->titleField = $this->prefixFieldName($this->titleField);
}
parent::init();
$this->initPreventModification();
}
public function addField(string $name, $seed = []): Field
{
$seed = Factory::mergeSeeds($seed, [
'actual' => $this->prefixFieldName($name, true),
'caption' => $this->readableCaption($this->unprefixFieldName($name)),
]);
return parent::addField($name, $seed);
}
}
/**
* @property string $name @Atk4\Field()
* @property string $sys_name @Atk4\Field()
* @property string $iso @Atk4\Field()
* @property string $iso3 @Atk4\Field()
* @property string $numcode @Atk4\Field()
* @property string $phonecode @Atk4\Field()
*/
class Country extends ModelWithPrefixedFields
{
public $table = 'country';
public $caption = 'Country';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name, ['actual' => 'atk_afp_country__nicename', 'required' => true, 'type' => 'string']);
$this->addField($this->fieldName()->sys_name, ['actual' => 'atk_afp_country__name', 'system' => true]);
$this->addField($this->fieldName()->iso, ['caption' => 'ISO', 'required' => true, 'type' => 'string', 'ui' => ['table' => ['sortable' => false]]]);
$this->addField($this->fieldName()->iso3, ['caption' => 'ISO3', 'required' => true, 'type' => 'string']);
$this->addField($this->fieldName()->numcode, ['caption' => 'ISO Numeric Code', 'type' => 'integer', 'required' => true]);
$this->addField($this->fieldName()->phonecode, ['caption' => 'Phone Prefix', 'type' => 'integer', 'required' => true]);
$this->onHook(Model::HOOK_BEFORE_SAVE, function (self $model) {
if (!$model->sys_name) {
$model->sys_name = mb_strtoupper($model->name);
}
});
}
public function validate(string $intent = null): array
{
$errors = parent::validate($intent);
if (mb_strlen($this->iso ?? '') !== 2) {
$errors[$this->fieldName()->iso] = 'Must be exactly 2 characters';
}
if (mb_strlen($this->iso3 ?? '') !== 3) {
$errors[$this->fieldName()->iso3] = 'Must be exactly 3 characters';
}
// look if name is unique
$c = $this->getModel()->tryLoadBy($this->fieldName()->name, $this->name);
if ($c !== null && !$this->compare($this->idField, $c->getId())) {
$errors[$this->fieldName()->name] = 'Country name must be unique';
}
return $errors;
}
}
/**
* @property string $project_name @Atk4\Field()
* @property string $project_code @Atk4\Field()
* @property string $description @Atk4\Field()
* @property string $client_name @Atk4\Field()
* @property string $client_address @Atk4\Field()
* @property Country $client_country_iso @Atk4\RefOne()
* @property string $client_country @Atk4\Field()
* @property bool $is_commercial @Atk4\Field()
* @property string $currency @Atk4\Field()
* @property string $currency_symbol @Atk4\Field()
* @property float $project_budget @Atk4\Field()
* @property float $project_invoiced @Atk4\Field()
* @property float $project_paid @Atk4\Field()
* @property float $project_hour_cost @Atk4\Field()
* @property int $project_hours_est @Atk4\Field()
* @property int $project_hours_reported @Atk4\Field()
* @property float $project_expenses_est @Atk4\Field()
* @property float $project_expenses @Atk4\Field()
* @property float $project_mgmt_cost_pct @Atk4\Field()
* @property float $project_qa_cost_pct @Atk4\Field()
* @property \DateTime $start_date @Atk4\Field()
* @property \DateTime $finish_date @Atk4\Field()
* @property \DateTime $finish_time @Atk4\Field()
* @property \DateTime $created @Atk4\Field()
* @property \DateTime $updated @Atk4\Field()
*/
class Stat extends ModelWithPrefixedFields
{
public $table = 'stat';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->project_name, ['type' => 'string']);
$this->addField($this->fieldName()->project_code, ['type' => 'string']);
$this->titleField = $this->fieldName()->project_name;
$this->addField($this->fieldName()->description, ['type' => 'text']);
$this->addField($this->fieldName()->client_name, ['type' => 'string']);
$this->addField($this->fieldName()->client_address, ['type' => 'text', 'ui' => ['form' => [Form\Control\Textarea::class, 'rows' => 4]]]);
$this->hasOne($this->fieldName()->client_country_iso, [
'model' => [Country::class],
'theirField' => Country::hinting()->fieldName()->iso,
'type' => 'string',
'ui' => [
'form' => [Form\Control\Line::class],
'table' => [Table\Column\CountryFlag::class],
],
])
->addField($this->fieldName()->client_country, Country::hinting()->fieldName()->name);
$this->addField($this->fieldName()->is_commercial, ['type' => 'boolean']);
$this->addField($this->fieldName()->currency, ['values' => ['EUR' => 'Euro', 'USD' => 'US Dollar', 'GBP' => 'Pound Sterling']]);
$this->addField($this->fieldName()->currency_symbol, ['neverPersist' => true]);
$this->onHook(Model::HOOK_AFTER_LOAD, function (self $model) {
/* implementation for "intl"
$locale = 'en-UK';
$fmt = new \NumberFormatter($locale . '@currency=' . $model->currency, NumberFormatter::CURRENCY);
$model->currency_symbol = $fmt->getSymbol(NumberFormatter::CURRENCY_SYMBOL);
*/
$map = ['EUR' => '€', 'USD' => '$', 'GBP' => '£'];
$model->currency_symbol = $map[$model->currency] ?? '?';
});
$this->addField($this->fieldName()->project_budget, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_invoiced, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_paid, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_hour_cost, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_hours_est, ['type' => 'integer']);
$this->addField($this->fieldName()->project_hours_reported, ['type' => 'integer']);
$this->addField($this->fieldName()->project_expenses_est, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_expenses, ['type' => 'atk4_money']);
$this->addField($this->fieldName()->project_mgmt_cost_pct, new Percent());
$this->addField($this->fieldName()->project_qa_cost_pct, new Percent());
$this->addField($this->fieldName()->start_date, ['type' => 'date']);
$this->addField($this->fieldName()->finish_date, ['type' => 'date']);
$this->addField($this->fieldName()->finish_time, ['type' => 'time']);
$this->addField($this->fieldName()->created, ['type' => 'datetime', 'ui' => ['form' => ['disabled' => true]]]);
$this->addField($this->fieldName()->updated, ['type' => 'datetime', 'ui' => ['form' => ['disabled' => true]]]);
}
}
class Percent extends Field
{
public string $type = 'float';
}
/**
* @property string $name @Atk4\Field()
* @property string $type @Atk4\Field()
* @property bool $is_folder @Atk4\Field()
* @property File $SubFolder @Atk4\RefMany()
* @property int $count @Atk4\Field()
* @property Folder $parent_folder_id @Atk4\RefOne()
*/
class File extends ModelWithPrefixedFields
{
public $table = 'file';
public $caption = 'File';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);
$this->addField($this->fieldName()->type, ['caption' => 'MIME Type']);
$this->addField($this->fieldName()->is_folder, ['type' => 'boolean']);
$this->hasMany($this->fieldName()->SubFolder, [
'model' => [self::class],
'theirField' => self::hinting()->fieldName()->parent_folder_id,
])
->addField($this->fieldName()->count, ['aggregate' => 'count', 'field' => $this->getPersistence()->expr($this, '*')]);
$this->hasOne($this->fieldName()->parent_folder_id, [
'model' => [Folder::class],
])
->addTitle();
}
/**
* Perform import from filesystem.
*/
public function importFromFilesystem(string $path, bool $isSub = null): void
{
if ($isSub === null) {
if ($this->isEntity()) { // TODO should be not needed once UserAction is for non-entity only
$this->getModel()->importFromFilesystem($path);
return;
}
$this->atomic(function () use ($path) {
foreach ($this as $entity) {
$entity->delete();
// skip full/slow import for Behat testing
if ($_ENV['CI'] ?? null) {
break;
}
}
$path = __DIR__ . '/../' . $path;
$this->importFromFilesystem($path, false);
});
return;
}
foreach (new \DirectoryIterator($path) as $fileinfo) {
if ($fileinfo->isDot() || in_array($fileinfo->getFilename(), ['.git', 'vendor', 'js'], true)) {
continue;
}
$entity = $this->createEntity();
$entity->save([
$this->fieldName()->name => $fileinfo->getFilename(),
$this->fieldName()->is_folder => $fileinfo->isDir(),
$this->fieldName()->type => pathinfo($fileinfo->getFilename(), \PATHINFO_EXTENSION),
]);
if ($fileinfo->isDir()) {
$entity->SubFolder->importFromFilesystem($fileinfo->getPath() . '/' . $fileinfo->getFilename(), true);
}
// skip full/slow import for Behat testing
if ($_ENV['CI'] ?? null) {
break;
}
}
}
}
class Folder extends File
{
protected function init(): void
{
parent::init();
$this->addCondition($this->fieldName()->is_folder, true);
}
}
/**
* @property string $name @Atk4\Field()
* @property SubCategory $SubCategories @Atk4\RefMany()
* @property Product $Products @Atk4\RefMany()
*/
class Category extends ModelWithPrefixedFields
{
public $table = 'product_category';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);
$this->hasMany($this->fieldName()->SubCategories, [
'model' => [SubCategory::class],
'theirField' => SubCategory::hinting()->fieldName()->product_category_id,
]);
$this->hasMany($this->fieldName()->Products, [
'model' => [Product::class],
'theirField' => Product::hinting()->fieldName()->product_category_id,
]);
}
}
/**
* @property string $name @Atk4\Field()
* @property Category $product_category_id @Atk4\RefOne()
* @property Product $Products @Atk4\RefMany()
*/
class SubCategory extends ModelWithPrefixedFields
{
public $table = 'product_sub_category';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);
$this->hasOne($this->fieldName()->product_category_id, [
'model' => [Category::class],
]);
$this->hasMany($this->fieldName()->Products, [
'model' => [Product::class],
'theirField' => Product::hinting()->fieldName()->product_sub_category_id,
]);
}
}
/**
* @property string $name @Atk4\Field()
* @property string $brand @Atk4\Field()
* @property Category $product_category_id @Atk4\RefOne()
* @property SubCategory $product_sub_category_id @Atk4\RefOne()
*/
class Product extends ModelWithPrefixedFields
{
public $table = 'product';
public $caption = 'Product';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name);
$this->addField($this->fieldName()->brand);
$this->hasOne($this->fieldName()->product_category_id, [
'model' => [Category::class],
])->addTitle();
$this->hasOne($this->fieldName()->product_sub_category_id, [
'model' => [SubCategory::class],
])->addTitle();
}
}
/**
* @property string $item @Atk4\Field()
* @property \DateTime $inv_date @Atk4\Field()
* @property \DateTime $inv_time @Atk4\Field()
* @property Country $country_id @Atk4\RefOne()
* @property int $qty @Atk4\Field()
* @property int $box @Atk4\Field()
* @property int $total_sql @Atk4\Field()
* @property int $total_php @Atk4\Field()
*/
class MultilineItem extends ModelWithPrefixedFields
{
public $table = 'multiline_item';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->item, ['required' => true]);
$this->addField($this->fieldName()->inv_date, ['type' => 'date']);
$this->addField($this->fieldName()->inv_time, ['type' => 'time']);
$this->hasOne($this->fieldName()->country_id, [
'model' => [Country::class],
]);
$this->addField($this->fieldName()->qty, ['type' => 'integer', 'required' => true]);
$this->addField($this->fieldName()->box, ['type' => 'integer', 'required' => true]);
$this->addExpression($this->fieldName()->total_sql, [
'expr' => function (Model /* TODO self is not working bacause of clone in Multiline */ $row) {
return $row->expr('{' . $this->fieldName()->qty . '} * {' . $this->fieldName()->box . '}'); // @phpstan-ignore-line
},
'type' => 'integer',
]);
$this->addCalculatedField($this->fieldName()->total_php, [
'expr' => function (self $row) {
return $row->qty * $row->box;
},
'type' => 'integer',
]);
}
}
/**
* @property string $name @Atk4\Field()
* @property Country $country @Atk4\RefOne()
* @property MultilineItem $items @Atk4\RefMany()
*/
class MultilineDelivery extends ModelWithPrefixedFields
{
public $table = 'multiline_delivery';
protected function init(): void
{
parent::init();
$this->addField($this->fieldName()->name, ['required' => true]);
$this->containsOne($this->fieldName()->country, ['model' => [Country::class]]);
$this->containsMany($this->fieldName()->items, ['model' => [MultilineItem::class]]);
}
}