44
55use Illuminate \Support \Facades \Cache ;
66use Illuminate \View \Component ;
7+ use Ominity \Api \Resources \Modules \Forms \Form as OminityForm ;
78use Ominity \Laravel \Facades \Ominity ;
89
910class Form extends Component
1011{
11- public int $ formId ;
12+ public OminityForm $ form ;
1213
13- public string $ class ;
14-
15- public bool $ ajax ;
14+ public array $ rows = [];
1615
1716 /**
1817 * Create a new component instance.
1918 *
2019 * @return void
2120 */
22- public function __construct (int $ form , string $ class = '' , bool $ ajax = false )
23- {
24- $ this ->formId = $ form ;
25- $ this ->class = $ class ;
26- $ this ->ajax = $ ajax ;
21+ public function __construct (
22+ int |OminityForm $ form ,
23+ public string $ class = '' ,
24+ public bool $ ajax = false
25+ ) {
26+ if ($ form instanceof OminityForm) {
27+ $ this ->form = $ form ;
28+ } else {
29+ $ config = config ('ominity.forms.cache ' );
30+ if ($ config ['enabled ' ]) {
31+ $ this ->form = Cache::store ($ config ['store ' ])->remember ('forms-data- ' .$ form .'- ' .app ()->getLocale (), $ config ['expiration ' ], function () use ($ form ) {
32+ return Ominity::api ()->modules ->forms ->forms ->get ($ form , [
33+ 'include ' => 'fields ' ,
34+ ]);
35+ });
36+ } else {
37+ $ this ->form = Ominity::api ()->modules ->forms ->forms ->get ($ form , [
38+ 'include ' => 'fields ' ,
39+ ]);
40+ }
41+ }
42+
43+ $ this ->rows = $ this ->buildFieldRows ();
2744 }
2845
2946 /**
@@ -33,41 +50,35 @@ public function __construct(int $form, string $class = '', bool $ajax = false)
3350 */
3451 public function render ()
3552 {
36- $ config = config ('ominity.forms.cache ' );
37- $ cacheKey = 'forms-data- ' .$ this ->formId .'- ' .app ()->getLocale ();
38-
39- if ($ config ['enabled ' ]) {
40- $ form = Cache::store ($ config ['store ' ])->remember (
41- $ cacheKey ,
42- $ config ['expiration ' ],
43- function () {
44- return $ this ->fetchFormData ();
45- }
46- );
47- } else {
48- $ form = $ this ->fetchFormData ();
49- }
50-
51- $ class = $ this ->class ;
52-
5353 return view ('ominity::components.form ' , [
54- 'form ' => $ form ,
55- 'class ' => $ class ,
54+ 'form ' => $ this ->form ,
55+ 'rows ' => $ this ->rows ,
56+ 'class ' => $ this ->class ,
5657 'ajax ' => $ this ->ajax ,
5758 ])->render ();
5859 }
5960
60- /**
61- * Fetch form data from the API.
62- *
63- * @return array
64- */
65- protected function fetchFormData ()
61+ protected function buildFieldRows (): array
6662 {
67- $ form = Ominity::api ()->modules ->forms ->forms ->get ($ this ->formId , [
68- 'include ' => 'fields ' ,
69- ]);
63+ $ rows = [];
64+ $ currentRow = [];
65+
66+ foreach ($ this ->form ->fields () as $ field ) {
67+ if (!$ field ->isInline ) {
68+ if (!empty ($ currentRow )) {
69+ $ rows [] = $ currentRow ;
70+ $ currentRow = [];
71+ }
72+ $ rows [] = [$ field ];
73+ } else {
74+ $ currentRow [] = $ field ;
75+ }
76+ }
77+
78+ if (!empty ($ currentRow )) {
79+ $ rows [] = $ currentRow ;
80+ }
7081
71- return $ form ;
82+ return $ rows ;
7283 }
7384}
0 commit comments