33namespace Drupal \os2forms_sync \Controller ;
44
55use Drupal \Core \Controller \ControllerBase ;
6+ use Drupal \Core \Datetime \DrupalDateTime ;
67use Drupal \Core \Http \RequestStack ;
8+ use Drupal \Core \Link ;
9+ use Drupal \Core \Render \Markup ;
710use Drupal \Core \Routing \TrustedRedirectResponse ;
11+ use Drupal \Core \Serialization \Yaml ;
812use Drupal \Core \Url ;
913use Drupal \os2forms_sync \Helper \ImportHelper ;
14+ use Drupal \os2forms_sync \Helper \WebformHelper ;
1015use Symfony \Component \DependencyInjection \ContainerInterface ;
1116use Symfony \Component \HttpKernel \Exception \BadRequestHttpException ;
1217
1318/**
1419 * Webform controller.
1520 */
1621final class WebformController extends ControllerBase {
22+ private const FILTER_QUERY_NAME = 'show ' ;
23+ private const FILTER_WEBFORMS_IMPORTED = 'imported ' ;
24+ private const FILTER_WEBFORMS_NOT_IMPORTED = 'not imported ' ;
25+
1726 /**
1827 * The request stack.
1928 *
@@ -28,12 +37,20 @@ final class WebformController extends ControllerBase {
2837 */
2938 private ImportHelper $ importHelper ;
3039
40+ /**
41+ * The webform helper.
42+ *
43+ * @var \Drupal\os2forms_sync\Helper\WebformHelper
44+ */
45+ private WebformHelper $ webformHelper ;
46+
3147 /**
3248 * Constructor.
3349 */
34- public function __construct (RequestStack $ requestStack , ImportHelper $ importHelper ) {
50+ public function __construct (RequestStack $ requestStack , ImportHelper $ importHelper, WebformHelper $ webformHelper ) {
3551 $ this ->requestStack = $ requestStack ;
3652 $ this ->importHelper = $ importHelper ;
53+ $ this ->webformHelper = $ webformHelper ;
3754 }
3855
3956 /**
@@ -42,7 +59,8 @@ public function __construct(RequestStack $requestStack, ImportHelper $importHelp
4259 public static function create (ContainerInterface $ container ): self {
4360 return new static (
4461 $ container ->get ('request_stack ' ),
45- $ container ->get (ImportHelper::class)
62+ $ container ->get (ImportHelper::class),
63+ $ container ->get (WebformHelper::class)
4664 );
4765 }
4866
@@ -56,15 +74,182 @@ public static function create(ContainerInterface $container): self {
5674 */
5775 public function index (): array {
5876 $ webforms = $ this ->importHelper ->getAvailableWebforms ();
77+ $ importedWebforms = $ this ->importHelper ->loadImportedWebforms ();
78+
79+ // Filter available webforms.
80+ switch ($ this ->requestStack ->getCurrentRequest ()->get (self ::FILTER_QUERY_NAME )) {
81+ case self ::FILTER_WEBFORMS_IMPORTED :
82+ $ webforms = array_filter ($ webforms , static function (array $ webform ) use ($ importedWebforms ) {
83+ return isset ($ importedWebforms [$ webform ['links ' ]['self ' ]]);
84+ });
85+ break ;
86+
87+ case self ::FILTER_WEBFORMS_NOT_IMPORTED :
88+ $ webforms = array_filter ($ webforms , static function (array $ webform ) use ($ importedWebforms ) {
89+ return !isset ($ importedWebforms [$ webform ['links ' ]['self ' ]]);
90+ });
91+ break ;
92+ }
93+
94+ $ elements = [
95+ '#attached ' => ['library ' => ['os2forms_sync/webform-index ' ]],
96+ ];
97+
98+ $ elements ['search ' ] = [
99+ '#type ' => 'container ' ,
100+
101+ 'search ' => [
102+ '#type ' => 'search ' ,
103+ '#title ' => $ this ->t ('Search ' ),
104+ '#title_display ' => 'invisible ' ,
105+ '#attributes ' => [
106+ 'placeholder ' => $ this ->t ('Search title, description and category … ' ),
107+ ],
108+ ],
109+
110+ 'filters ' => [
111+ '#type ' => 'container ' ,
112+ '#attributes ' => [
113+ 'class ' => ['filters ' ],
114+ ],
115+
116+ 'links ' => [
117+ '#theme ' => 'item_list ' ,
118+ '#list_type ' => 'ul ' ,
119+ '#items ' => [
120+ (new Link ($ this ->t ('Show all webforms ' ), Url::fromRoute ('os2forms_sync.webform.index ' )))->toRenderable (),
121+ (new Link ($ this ->t ('Show imported webforms ' ), Url::fromRoute ('os2forms_sync.webform.index ' , ['show ' => self ::FILTER_WEBFORMS_IMPORTED ])))->toRenderable (),
122+ (new Link ($ this ->t ('Show not imported webforms ' ), Url::fromRoute ('os2forms_sync.webform.index ' , ['show ' => self ::FILTER_WEBFORMS_NOT_IMPORTED ])))->toRenderable (),
123+ ],
124+ ],
125+ ],
126+ ];
127+
128+ foreach ($ webforms as $ webform ) {
129+ $ attributes = $ webform ['attributes ' ];
130+ $ form = $ this ->webformHelper ->getSubmissionForm ($ attributes ['elements ' ]);
131+ // Make sure that the form cannot be submitted (hopefully).
132+ $ form ['#attributes ' ]['onsubmit ' ] = 'return false ' ;
133+
134+ $ sourceUrl = $ webform ['links ' ]['self ' ];
135+ $ importedWebform = $ importedWebforms [$ sourceUrl ] ?? NULL ;
136+
137+ $ item = [
138+ '#type ' => 'fieldset ' ,
139+ '#title ' => $ attributes ['title ' ] ?? 'xxx ' ,
140+ '#attributes ' => ['class ' => ['os2forms-sync-webform ' ]],
141+
142+ 'description ' => [
143+ '#type ' => 'container ' ,
144+ '#attributes ' => [
145+ 'class ' => ['description ' ],
146+ 'data-indexed ' => strip_tags ($ attributes ['description ' ]),
147+ ],
148+ '#markup ' => Markup::create ($ attributes ['description ' ]),
149+ ],
150+
151+ 'form_display ' => [
152+ '#type ' => 'details ' ,
153+ '#title ' => $ this ->t ('Form display ' ),
154+ 'form ' => $ form ,
155+ ],
156+
157+ 'elements ' => [
158+ '#type ' => 'details ' ,
159+ '#title ' => $ this ->t ('Elements ' ),
160+ '#markup ' => '<pre> ' . Yaml::encode ($ attributes ['elements ' ]) . '</pre> ' ,
161+ ],
162+
163+ 'metadata ' => [
164+ '#type ' => 'container ' ,
165+ '#attributes ' => [
166+ 'class ' => ['metadata ' ],
167+ ],
168+
169+ 'category ' => [
170+ '#type ' => 'container ' ,
171+ '#attributes ' => [
172+ 'class ' => ['category ' ],
173+ 'data-indexed ' => strip_tags ($ attributes ['category ' ]),
174+ ],
175+
176+ 'label ' => [
177+ '#type ' => 'label ' ,
178+ '#title ' => $ this ->t ('Category ' ),
179+ '#title_display ' => 'above ' ,
180+ ],
181+
182+ 'value ' => [
183+ '#type ' => 'html_tag ' ,
184+ '#tag ' => 'span ' ,
185+ '#attributes ' => [
186+ 'class ' => ['value ' ],
187+ ],
188+ '#value ' => $ attributes ['category ' ],
189+ ],
190+ ],
191+
192+ 'source_url ' => [
193+ '#type ' => 'container ' ,
194+
195+ 'label ' => [
196+ '#type ' => 'label ' ,
197+ '#title ' => $ this ->t ('Source url ' ),
198+ '#title_display ' => 'above ' ,
199+ ],
200+
201+ 'value ' => [
202+ '#type ' => 'html_tag ' ,
203+ '#tag ' => 'span ' ,
204+ '#attributes ' => [
205+ 'class ' => ['value ' ],
206+ ],
207+ 'link ' => (new Link ($ sourceUrl , Url::fromUri ($ sourceUrl )))->toRenderable (),
208+ ],
209+ ],
210+ ],
211+ ];
212+
213+ $ item ['import_form ' ] = [
214+ '#type ' => 'html_tag ' ,
215+ '#tag ' => 'form ' ,
216+ '#attributes ' => [
217+ 'method ' => 'post ' ,
218+ 'action ' => Url::fromRoute ('os2forms_sync.webform.import ' , ['url ' => $ sourceUrl ])->toString (TRUE )->getGeneratedUrl (),
219+ ],
220+
221+ 'button ' => [
222+ '#type ' => 'button ' ,
223+ '#value ' => NULL === $ importedWebform ? $ this ->t ('Import webform ' ) : $ this ->t ('Update webform ' ),
224+ ],
225+ ];
226+
227+ if (NULL !== $ importedWebform ) {
228+ $ item ['import_form ' ]['info ' ] = [
229+ '#markup ' => $ this ->t ('<a href=":webform_url">Webform</a> updated at @updated_at. ' , [
230+ ':webform_url ' => Url::fromRoute ('entity.webform.edit_form ' , ['webform ' => $ importedWebform ->webformId ])->toString (TRUE )->getGeneratedUrl (),
231+ '@updated_at ' => $ importedWebform ->updatedAt ->format (DrupalDateTime::FORMAT ),
232+ ]),
233+ ];
234+ }
235+
236+ $ elements [] = $ item ;
237+ }
238+
59239 $ settingsUrl = Url::fromRoute ('os2forms_sync.admin.settings ' );
60- if (!$ settingsUrl ->access ($ this ->currentUser ())) {
61- $ settingsUrl = NULL ;
240+ if ($ settingsUrl ->access ($ this ->currentUser ())) {
241+ $ elements ['settings ' ] = [
242+ '#type ' => 'container ' ,
243+
244+ 'link ' => (new Link ($ this ->t ('O2Forms sync settings ' ), $ settingsUrl ))->toRenderable (),
245+ ];
62246 }
63247
64248 return [
65- '#theme ' => 'os2forms_sync_webform_index ' ,
66- '#webforms ' => $ webforms ,
67- '#settings_url ' => $ settingsUrl ,
249+ '#type ' => 'container ' ,
250+ '#attributes ' => ['class ' => ['os2forms-sync-webform-index ' ]],
251+
252+ 'elements ' => $ elements ,
68253 ];
69254 }
70255
0 commit comments