1
1
<?php
2
+
2
3
namespace verbb \formie \elements ;
3
4
4
5
use verbb \formie \Formie ;
21
22
use verbb \formie \models \FormTemplate ;
22
23
use verbb \formie \models \HtmlTag ;
23
24
use verbb \formie \models \Notification ;
24
- use verbb \formie \models \Settings ;
25
25
use verbb \formie \models \Status ;
26
26
use verbb \formie \records \Form as FormRecord ;
27
27
use verbb \formie \services \Statuses ;
28
28
29
29
use Craft ;
30
30
use craft \base \Element ;
31
- use craft \db \Query ;
32
- use craft \db \Table ;
33
31
use craft \elements \Entry ;
34
32
use craft \elements \User ;
35
33
use craft \elements \actions \Delete ;
55
53
56
54
use Twig \Error \SyntaxError ;
57
55
use Twig \Error \LoaderError ;
56
+ use verbb \formie \elements \actions \SetFormStatus ;
58
57
59
58
class Form extends Element
60
59
{
61
60
// Constants
62
61
// =========================================================================
63
62
64
63
public const EVENT_MODIFY_HTML_TAG = 'modifyHtmlTag ' ;
64
+ public const STATUS_ACTIVE = 'active ' ;
65
+ public const STATUS_INACTIVE = 'inactive ' ;
66
+ public const STATUSES = [
67
+ self ::STATUS_ACTIVE ,
68
+ self ::STATUS_INACTIVE ,
69
+ ];
65
70
66
71
67
72
// Static Methods
@@ -99,6 +104,19 @@ public static function hasContent(): bool
99
104
return true ;
100
105
}
101
106
107
+ public static function hasStatuses (): bool
108
+ {
109
+ return true ;
110
+ }
111
+
112
+ public static function statuses (): array
113
+ {
114
+ return [
115
+ self ::STATUS_ACTIVE => Craft::t ('app ' , 'Active ' ),
116
+ self ::STATUS_INACTIVE => Craft::t ('app ' , 'Inactive ' ),
117
+ ];
118
+ }
119
+
102
120
/**
103
121
* @inheritDoc
104
122
*/
@@ -155,7 +173,7 @@ public static function defineSources(string $context = null): array
155
173
156
174
return $ sources ;
157
175
}
158
-
176
+
159
177
/**
160
178
* @inheritDoc
161
179
*/
@@ -196,7 +214,6 @@ protected static function defineFieldLayouts(string $source): array
196
214
protected static function defineActions (string $ source = null ): array
197
215
{
198
216
$ actions = [];
199
-
200
217
$ canDeleteForms = Craft::$ app ->getUser ()->checkPermission ('formie-deleteForms ' );
201
218
202
219
$ actions [] = DuplicateForm::class;
@@ -215,6 +232,10 @@ protected static function defineActions(string $source = null): array
215
232
'partialSuccessMessage ' => Craft::t ('formie ' , 'Some forms restored. ' ),
216
233
'failMessage ' => Craft::t ('formie ' , 'Forms not restored. ' ),
217
234
];
235
+ $ actions [] = [
236
+ 'type ' => SetFormStatus::class,
237
+ 'statuses ' => self ::STATUSES ,
238
+ ];
218
239
219
240
return $ actions ;
220
241
}
@@ -228,6 +249,7 @@ protected static function defineTableAttributes(): array
228
249
'title ' => ['label ' => Craft::t ('app ' , 'Title ' )],
229
250
'id ' => ['label ' => Craft::t ('app ' , 'ID ' )],
230
251
'handle ' => ['label ' => Craft::t ('app ' , 'Handle ' )],
252
+ 'formStatus ' => ['label ' => Craft::t ('formie ' , 'Status ' )],
231
253
'template ' => ['label ' => Craft::t ('app ' , 'Template ' )],
232
254
'usageCount ' => ['label ' => Craft::t ('formie ' , 'Usage Count ' )],
233
255
'dateCreated ' => ['label ' => Craft::t ('app ' , 'Date Created ' )],
@@ -288,6 +310,7 @@ protected static function defineSortOptions(): array
288
310
// Properties
289
311
// =========================================================================
290
312
313
+ private ?string $ formStatus = self ::STATUS_ACTIVE ;
291
314
public ?string $ handle = null ;
292
315
public ?string $ oldHandle = null ;
293
316
public ?string $ fieldContentTable = null ;
@@ -385,7 +408,7 @@ public function behaviors(): array
385
408
386
409
return $ behaviors ;
387
410
}
388
-
411
+
389
412
/**
390
413
* @inheritdoc
391
414
*/
@@ -555,6 +578,23 @@ public function getDefaultStatus(): ?Status
555
578
return $ this ->_defaultStatus ;
556
579
}
557
580
581
+ public function setFormStatus (string $ status = null ): void
582
+ {
583
+ if ($ status !== null ) {
584
+ $ this ->formStatus = $ status ;
585
+ }
586
+ }
587
+
588
+ public function getFormStatus (): string
589
+ {
590
+ return $ this ->formStatus ?? self ::STATUS_ACTIVE ;
591
+ }
592
+
593
+ public function getStatus (): string
594
+ {
595
+ return $ this ->getFormStatus ();
596
+ }
597
+
558
598
/**
559
599
* Sets the default status.
560
600
*
@@ -1296,7 +1336,7 @@ public function getPageFieldErrors($submission): array
1296
1336
public function renderTemplate (array |string $ components , array $ variables = []): string
1297
1337
{
1298
1338
$ view = Craft::$ app ->getView ();
1299
-
1339
+
1300
1340
// Normalise the components to allow for a single component
1301
1341
if (!is_array ($ components )) {
1302
1342
$ components = [$ components ];
@@ -1579,7 +1619,7 @@ public function defineHtmlTag(string $key, array $context = []): ?HtmlTag
1579
1619
$ page = $ context ['page ' ] ?? null ;
1580
1620
$ inputAttributes = $ page ->settings ->getInputAttributes () ?? [];
1581
1621
$ saveButtonStyle = $ page ->settings ->saveButtonStyle ?? 'link ' ;
1582
-
1622
+
1583
1623
return new HtmlTag ('button ' , [
1584
1624
'class ' => [
1585
1625
'fui-btn fui-save ' ,
@@ -1899,7 +1939,7 @@ public function setSettings($settings, $updateSnapshot = true): void
1899
1939
public function setFieldSettings ($ handle , $ settings , $ updateSnapshot = true ): void
1900
1940
{
1901
1941
$ field = null ;
1902
-
1942
+
1903
1943
// Check for nested fields so we can use `group.dropdown` or `dropdown`.
1904
1944
$ handles = explode ('. ' , $ handle );
1905
1945
@@ -1930,7 +1970,7 @@ public function setIntegrationSettings(string $handle, array $settings, $updateS
1930
1970
{
1931
1971
// Get the integration settings so we only override what we want
1932
1972
$ integrationSettings = $ this ->settings ->integrations [$ handle ] ?? [];
1933
-
1973
+
1934
1974
// Update the integration settings
1935
1975
$ this ->settings ->integrations [$ handle ] = array_merge ($ integrationSettings , $ settings );
1936
1976
@@ -2018,7 +2058,7 @@ public function isBeforeSchedule(): bool
2018
2058
if ($ this ->settings ->scheduleForm && $ this ->settings ->scheduleFormStart ) {
2019
2059
return !DateTimeHelper::isInThePast ($ this ->settings ->scheduleFormStart );
2020
2060
}
2021
-
2061
+
2022
2062
return false ;
2023
2063
}
2024
2064
@@ -2027,7 +2067,7 @@ public function isAfterSchedule(): bool
2027
2067
if ($ this ->settings ->scheduleForm && $ this ->settings ->scheduleFormEnd ) {
2028
2068
return DateTimeHelper::isInThePast ($ this ->settings ->scheduleFormEnd );
2029
2069
}
2030
-
2070
+
2031
2071
return false ;
2032
2072
}
2033
2073
@@ -2065,7 +2105,7 @@ public function isWithinSubmissionsLimit(): bool
2065
2105
return false ;
2066
2106
}
2067
2107
}
2068
-
2108
+
2069
2109
return true ;
2070
2110
}
2071
2111
@@ -2149,6 +2189,7 @@ public function afterSave(bool $isNew): void
2149
2189
2150
2190
$ record ->handle = $ this ->handle ;
2151
2191
$ record ->fieldContentTable = $ this ->fieldContentTable ;
2192
+ $ record ->formStatus = $ this ->formStatus ;
2152
2193
$ record ->settings = $ this ->settings ;
2153
2194
$ record ->templateId = $ this ->templateId ;
2154
2195
$ record ->submitActionEntryId = $ this ->submitActionEntryId ;
@@ -2286,7 +2327,7 @@ protected function defineRules(): array
2286
2327
];
2287
2328
2288
2329
$ rules [] = [
2289
- 'handle ' , function ($ attribute , $ params , Validator $ validator ): void {
2330
+ 'handle ' , function ($ attribute , $ params , Validator $ validator ): void {
2290
2331
$ query = static ::find ()->handle ($ this ->$ attribute );
2291
2332
if ($ this ->id ) {
2292
2333
$ query = $ query ->id ("not {$ this ->id }" );
0 commit comments