1
|
|
|
<?php declare(strict_types=1); |
2
|
|
|
|
3
|
|
|
namespace Modelarium\Frontend\Vue\Renderable; |
4
|
|
|
|
5
|
|
|
use Doctrine\Inflector\InflectorFactory; |
6
|
|
|
use Illuminate\Support\Str; |
7
|
|
|
use Formularium\Exception\ClassNotFoundException; |
8
|
|
|
use Formularium\Field; |
9
|
|
|
use Formularium\Renderable; |
10
|
|
|
use Formularium\HTMLNode; |
11
|
|
|
use Formularium\Frontend\Vue\RenderableVueTrait; |
12
|
|
|
use Formularium\Frontend\Vue\Framework as VueFramework; |
13
|
|
|
use Modelarium\Datatypes\Datatype_relationship; |
14
|
|
|
use Modelarium\Datatypes\RelationshipFactory; |
15
|
|
|
|
16
|
|
|
class Renderable_relationship extends Renderable |
17
|
|
|
{ |
18
|
|
|
use RenderableVueTrait { |
19
|
|
|
RenderableVueTrait::viewable as _viewable; |
20
|
|
|
RenderableVueTrait::editable as _editable; |
21
|
|
|
} |
22
|
|
|
|
23
|
|
|
/** |
24
|
|
|
* Subcall of wrapper editable() |
25
|
|
|
* |
26
|
|
|
* @param mixed $value |
27
|
|
|
* @param Field $field |
28
|
|
|
* @param HTMLNode $previous |
29
|
|
|
* @return HTMLNode |
30
|
|
|
*/ |
31
|
|
|
public function viewable($value, Field $field, HTMLNode $previous): HTMLNode |
32
|
|
|
{ |
33
|
|
|
$previous = $this->_viewable($value, $field, $previous); |
34
|
|
|
// TODO: replace with <Card></Card>, props |
35
|
|
|
return $previous; |
36
|
|
|
} |
37
|
|
|
|
38
|
|
|
/** |
39
|
|
|
* Subcall of wrapper editable() |
40
|
|
|
* |
41
|
|
|
* @param mixed $value |
42
|
|
|
* @param Field $field |
43
|
|
|
* @param HTMLNode $previous |
44
|
|
|
* @return HTMLNode |
45
|
|
|
*/ |
46
|
|
|
public function editable($value, Field $field, HTMLNode $previous): HTMLNode |
47
|
|
|
{ |
48
|
|
|
$previous = $this->_editable($value, $field, $previous); |
49
|
|
|
|
50
|
|
|
/* |
51
|
|
|
* init variables |
52
|
|
|
*/ |
53
|
|
|
$inflector = InflectorFactory::create()->build(); |
54
|
|
|
/** |
55
|
|
|
* @var VueFramework $vue |
56
|
|
|
*/ |
57
|
|
|
$vue = $this->framework; |
58
|
|
|
$mvar = $vue->getFieldModelVariable(); |
59
|
|
|
/** |
60
|
|
|
* @var Datatype_relationship $datatype |
61
|
|
|
*/ |
62
|
|
|
$datatype = $field->getDatatype(); |
63
|
|
|
// @phpstan-ignore-next-line |
64
|
|
|
$targetModel = call_user_func($datatype->getTargetClass() . '::getFormularium'); |
65
|
|
|
if ($targetModel === false) { |
66
|
|
|
throw new ClassNotFoundException("Cannot find model " . $datatype->getTarget()); |
67
|
|
|
} |
68
|
|
|
/** |
69
|
|
|
* @var \Formularium\Model $targetModel |
70
|
|
|
*/ |
71
|
|
|
|
72
|
|
|
// get the title field |
73
|
|
|
$titleField = $targetModel->firstField( |
74
|
|
|
function (Field $field) { |
75
|
|
|
return $field->getRenderable('title', false); |
76
|
|
|
} |
77
|
|
|
); |
78
|
|
|
// import graphql query |
79
|
|
|
$query = 'relationList' . $targetModel->getName() . 'Query'; |
80
|
|
|
$targetStudly = Str::studly($datatype->getTarget()); |
81
|
|
|
$vue->appendImport($query, "raw-loader!../" . $targetStudly . "/queryList.graphql"); |
82
|
|
|
$vue->appendExtraData($query, $query); |
83
|
|
|
|
84
|
|
|
$relationship = $datatype->getRelationship(); |
85
|
|
|
if ($relationship === RelationshipFactory::RELATIONSHIP_MANY_TO_MANY || |
86
|
|
|
$relationship === RelationshipFactory::MORPH_MANY_TO_MANY |
87
|
|
|
// TODO: inverses 1:n? |
88
|
|
|
) { |
89
|
|
|
$component = 'RelationshipMultiple'; |
90
|
|
|
} elseif ($field->getRenderable('relationshipSelect', false)) { // TODO: document |
91
|
|
|
$component = 'RelationshipSelect'; |
92
|
|
|
} else { |
93
|
|
|
$component = 'RelationshipAutocomplete'; |
94
|
|
|
} |
95
|
|
|
|
96
|
|
|
// replace the <select> with our component |
97
|
|
|
foreach (array_merge($previous->get('select'), $previous->get('input')) as $input) { |
98
|
|
|
$classes = $input->getAttribute('class'); |
99
|
|
|
$input->setTag($component) |
100
|
|
|
->setAttributes( |
101
|
|
|
[ |
102
|
|
|
'name' => $field->getName(), |
103
|
|
|
'htmlClass' => $classes, |
104
|
|
|
'titleField' => ($titleField ? $titleField->getName() : 'id'), |
105
|
|
|
':query' => $query, |
106
|
|
|
'targetType' => $datatype->getTarget(), |
107
|
|
|
'targetTypePlural' => $inflector->pluralize(mb_strtolower($datatype->getTarget())), |
108
|
|
|
'v-model' => $mvar . $field->getName() |
109
|
|
|
] |
110
|
|
|
); |
111
|
|
|
} |
112
|
|
|
|
113
|
|
|
return $previous; |
114
|
|
|
} |
115
|
|
|
} |
116
|
|
|
|