/
ElementIndexesController.php
922 lines (794 loc) · 30.5 KB
/
ElementIndexesController.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
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
<?php
/**
* @link https://craftcms.com/
* @copyright Copyright (c) Pixel & Tonic, Inc.
* @license https://craftcms.github.io/license/
*/
namespace craft\controllers;
use Craft;
use craft\base\ElementAction;
use craft\base\ElementActionInterface;
use craft\base\ElementExporterInterface;
use craft\base\ElementInterface;
use craft\elements\actions\DeleteActionInterface;
use craft\elements\actions\Restore;
use craft\elements\conditions\ElementCondition;
use craft\elements\conditions\ElementConditionInterface;
use craft\elements\conditions\ElementConditionRuleInterface;
use craft\elements\db\ElementQueryInterface;
use craft\elements\exporters\Raw;
use craft\events\ElementActionEvent;
use craft\helpers\Component;
use craft\helpers\Cp;
use craft\helpers\ElementHelper;
use craft\services\ElementSources;
use yii\base\InvalidValueException;
use yii\web\BadRequestHttpException;
use yii\web\ForbiddenHttpException;
use yii\web\Response;
/**
* The ElementIndexesController class is a controller that handles various element index related actions.
* Note that all actions in the controller require an authenticated Craft session via [[allowAnonymous]].
*
* @author Pixel & Tonic, Inc. <support@pixelandtonic.com>
* @since 3.0.0
*/
class ElementIndexesController extends BaseElementsController
{
/**
* @var string
* @phpstan-var class-string<ElementInterface>
*/
protected string $elementType;
/**
* @var string
*/
protected string $context;
/**
* @var string|null
*/
protected ?string $sourceKey = null;
/**
* @var array|null
*/
protected ?array $source = null;
/**
* @var ElementConditionInterface|null
* @since 4.0.0
*/
protected ?ElementConditionInterface $condition = null;
/**
* @var array|null
*/
protected ?array $viewState = null;
/**
* @var ElementQueryInterface|null
*/
protected ?ElementQueryInterface $elementQuery = null;
/**
* @var ElementActionInterface[]|null
*/
protected ?array $actions = null;
/**
* @var ElementExporterInterface[]|null
*/
protected ?array $exporters = null;
/**
* @inheritdoc
*/
public function beforeAction($action): bool
{
if (!parent::beforeAction($action)) {
return false;
}
if (!in_array($action->id, ['export', 'perform-action'], true)) {
$this->requireAcceptsJson();
}
$this->elementType = $this->elementType();
$this->context = $this->context();
$this->sourceKey = $this->request->getParam('source') ?: null;
$this->source = $this->source();
$this->condition = $this->condition();
if ($action->id !== 'filter-hud') {
$this->viewState = $this->viewState();
$this->elementQuery = $this->elementQuery();
if (
in_array($action->id, ['get-elements', 'get-more-elements', 'perform-action', 'export']) &&
$this->includeActions() &&
isset($this->sourceKey)
) {
$this->actions = $this->availableActions();
$this->exporters = $this->availableExporters();
}
}
return true;
}
/**
* Returns the element query that’s defining which elements will be returned in the current request.
*
* Other components can fetch this like so:
*
* ```php
* $criteria = Craft::$app->controller->getElementQuery();
* ```
*
* @return ElementQueryInterface
*/
public function getElementQuery(): ElementQueryInterface
{
return $this->elementQuery;
}
/**
* Returns the source path for the given source key, step key, and context.
*
* @since 4.4.12
*/
public function actionSourcePath(): Response
{
/** @var string|ElementInterface $elementType */
$elementType = $this->elementType;
$stepKey = $this->request->getRequiredBodyParam('stepKey');
$sourcePath = $elementType::sourcePath($this->sourceKey, $stepKey, $this->context);
return $this->asJson([
'sourcePath' => $sourcePath,
]);
}
/**
* Renders and returns an element index container, plus its first batch of elements.
*
* @return Response
*/
public function actionGetElements(): Response
{
$responseData = $this->elementResponseData(true, $this->includeActions());
return $this->asJson($responseData);
}
/**
* Renders and returns a subsequent batch of elements for an element index.
*
* @return Response
*/
public function actionGetMoreElements(): Response
{
$responseData = $this->elementResponseData(false, false);
return $this->asJson($responseData);
}
/**
* Returns the total number of elements that match the current criteria.
*
* @return Response
* @since 3.4.6
*/
public function actionCountElements(): Response
{
/** @var string|ElementInterface $elementType */
$elementType = $this->elementType;
return $this->asJson([
'resultSet' => $this->request->getParam('resultSet'),
'count' => $elementType::indexElementCount($this->elementQuery, $this->sourceKey),
]);
}
/**
* Performs an action on one or more selected elements.
*
* @return Response|null
* @throws BadRequestHttpException if the requested element action is not supported by the element type, or its parameters didn’t validate
*/
public function actionPerformAction(): ?Response
{
$this->requirePostRequest();
$elementsService = Craft::$app->getElements();
$actionClass = $this->request->getRequiredBodyParam('elementAction');
$elementIds = $this->request->getRequiredBodyParam('elementIds');
// Find that action from the list of available actions for the source
if (!empty($this->actions)) {
/** @var ElementAction $availableAction */
foreach ($this->actions as $availableAction) {
if ($actionClass === get_class($availableAction)) {
$action = clone $availableAction;
break;
}
}
}
/** @noinspection UnSafeIsSetOverArrayInspection - FP */
if (!isset($action)) {
throw new BadRequestHttpException('Element action is not supported by the element type');
}
// Check for any params in the post data
foreach ($action->settingsAttributes() as $paramName) {
$paramValue = $this->request->getBodyParam($paramName);
if ($paramValue !== null) {
$action->$paramName = $paramValue;
}
}
// Make sure the action validates
if (!$action->validate()) {
throw new BadRequestHttpException('Element action params did not validate');
}
// Perform the action
$actionCriteria = (clone $this->elementQuery)
->offset(0)
->limit(null)
->orderBy([])
->positionedAfter(null)
->positionedBefore(null)
->id($elementIds);
// Fire a 'beforePerformAction' event
$event = new ElementActionEvent([
'action' => $action,
'criteria' => $actionCriteria,
]);
$elementsService->trigger($elementsService::EVENT_BEFORE_PERFORM_ACTION, $event);
if ($event->isValid) {
$success = $action->performAction($actionCriteria);
$message = $action->getMessage();
if ($success) {
// Fire an 'afterPerformAction' event
$elementsService->trigger($elementsService::EVENT_AFTER_PERFORM_ACTION, new ElementActionEvent([
'action' => $action,
'criteria' => $actionCriteria,
]));
}
} else {
$success = false;
$message = $event->message;
}
// Respond
if ($action->isDownload()) {
return $this->response;
}
if (!$success) {
return $this->asFailure($message);
}
// Send a new set of elements
$responseData = $this->elementResponseData(true, true);
// Send updated badge counts
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType;
$formatter = Craft::$app->getFormatter();
foreach (Craft::$app->getElementSources()->getSources($elementType, $this->context) as $source) {
if (isset($source['key'])) {
if (isset($source['badgeCount'])) {
$responseData['badgeCounts'][$source['key']] = $formatter->asDecimal($source['badgeCount'], 0);
} else {
$responseData['badgeCounts'][$source['key']] = null;
}
}
}
return $this->asSuccess($message, data: $responseData);
}
/**
* Returns the source tree HTML for an element index.
*
* @return Response
*/
public function actionGetSourceTreeHtml(): Response
{
$this->requireAcceptsJson();
$sources = Craft::$app->getElementSources()->getSources($this->elementType, $this->context);
return $this->asJson([
'html' => $this->getView()->renderTemplate('_elements/sources.twig', [
'elementType' => $this->elementType,
'sources' => $sources,
]),
]);
}
/**
* Exports element data.
*
* @return Response
* @throws BadRequestHttpException
* @since 3.4.4
*/
public function actionExport(): Response
{
$exporter = $this->_exporter();
$exporter->setElementType($this->elementType);
// Set the filename header before calling export() in case export() starts outputting the data
$filename = $exporter->getFilename();
if ($exporter::isFormattable()) {
$this->response->format = $this->request->getBodyParam('format', 'csv');
$filename .= '.' . $this->response->format;
}
$this->response->setDownloadHeaders($filename);
$export = $exporter->export($this->elementQuery);
if ($exporter::isFormattable()) {
// Handle being passed in a generator function or other callable
if (is_callable($export)) {
$export = $export();
}
if (!is_iterable($export)) {
throw new InvalidValueException(get_class($exporter) . '::export() must return an array or generator function since isFormattable() returns true.');
}
$this->response->data = $export;
switch ($this->response->format) {
case Response::FORMAT_JSON:
$this->response->formatters[Response::FORMAT_JSON]['prettyPrint'] = true;
break;
case Response::FORMAT_XML:
Craft::$app->language = 'en-US';
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType;
$this->response->formatters[Response::FORMAT_XML]['rootTag'] = $elementType::pluralLowerDisplayName();
break;
}
} elseif (
is_callable($export) ||
is_resource($export) ||
(is_array($export) && isset($export[0]) && is_resource($export[0]))
) {
$this->response->stream = $export;
} else {
$this->response->data = $export;
$this->response->format = Response::FORMAT_RAW;
}
return $this->response;
}
/**
* Returns the exporter for the request.
*
* @return ElementExporterInterface
* @throws BadRequestHttpException
*/
private function _exporter(): ElementExporterInterface
{
if (!$this->sourceKey) {
throw new BadRequestHttpException('Request missing required body param');
}
if ($this->context !== 'index') {
throw new BadRequestHttpException('Request missing index context');
}
// Find that exporter from the list of available exporters for the source
$exporterClass = $this->request->getBodyParam('type', Raw::class);
if (!empty($this->exporters)) {
foreach ($this->exporters as $exporter) {
if ($exporterClass === get_class($exporter)) {
return $exporter;
}
}
}
throw new BadRequestHttpException('Element exporter is not supported by the element type');
}
/**
* Creates a filter HUD’s contents.
*
* @since 4.0.0
*/
public function actionFilterHud(): Response
{
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType();
$id = $this->request->getRequiredBodyParam('id');
$conditionConfig = $this->request->getBodyParam('conditionConfig');
$serialized = $this->request->getBodyParam('serialized');
$conditionsService = Craft::$app->getConditions();
if ($conditionConfig) {
$conditionConfig = Component::cleanseConfig($conditionConfig);
/** @var ElementConditionInterface $condition */
$condition = $conditionsService->createCondition($conditionConfig);
} elseif ($serialized) {
parse_str($serialized, $conditionConfig);
/** @var ElementConditionInterface $condition */
$condition = $conditionsService->createCondition($conditionConfig['condition']);
} else {
/** @var ElementConditionInterface $condition */
$condition = $elementType::createCondition();
}
$condition->mainTag = 'div';
$condition->id = $id;
$condition->addRuleLabel = Craft::t('app', 'Add a filter');
// Filter out any condition rules that touch the same query params as the source criteria
if ($this->source['type'] === ElementSources::TYPE_NATIVE) {
$condition->queryParams = array_keys($this->source['criteria'] ?? []);
$condition->sourceKey = $this->sourceKey;
} else {
/** @var ElementConditionInterface $sourceCondition */
$sourceCondition = $conditionsService->createCondition($this->source['condition']);
$condition->queryParams = [];
foreach ($sourceCondition->getConditionRules() as $rule) {
/** @var ElementConditionRuleInterface $rule */
$params = $rule->getExclusiveQueryParams();
foreach ($params as $param) {
$condition->queryParams[] = $param;
}
}
}
if ($this->condition) {
foreach ($this->condition->getConditionRules() as $rule) {
/** @var ElementConditionRuleInterface $rule */
$params = $rule->getExclusiveQueryParams();
foreach ($params as $param) {
$condition->queryParams[] = $param;
}
}
}
$condition->queryParams[] = 'site';
$condition->queryParams[] = 'status';
$html = $condition->getBuilderHtml();
$view = Craft::$app->getView();
return $this->asJson([
'hudHtml' => $html,
'headHtml' => $view->getHeadHtml(),
'bodyHtml' => $view->getBodyHtml(),
]);
}
/**
* Identify whether index actions should be included in the element index
*
* @return bool
*/
protected function includeActions(): bool
{
return $this->context === 'index';
}
/**
* Returns the selected source info.
*
* @return array|null
* @throws ForbiddenHttpException if the user is not permitted to access the requested source
*/
protected function source(): ?array
{
if (!isset($this->sourceKey)) {
return null;
}
$source = ElementHelper::findSource($this->elementType, $this->sourceKey, $this->context);
if ($source === null) {
// That wasn't a valid source, or the user doesn't have access to it in this context
$this->sourceKey = null;
}
return $source;
}
/**
* Returns the condition that should be applied to the element query.
*
* @return ElementConditionInterface|null
* @since 4.0.0
*/
protected function condition(): ?ElementConditionInterface
{
/** @var array|null $conditionConfig */
/** @phpstan-var array{class:class-string<ElementConditionInterface>}|null $conditionConfig */
$conditionConfig = $this->request->getBodyParam('condition');
if (!$conditionConfig) {
return null;
}
$condition = Craft::$app->getConditions()->createCondition($conditionConfig);
if ($condition instanceof ElementCondition) {
$referenceElementId = $this->request->getBodyParam('referenceElementId');
if ($referenceElementId) {
$siteId = $this->request->getBodyParam('referenceElementSiteId');
$condition->referenceElement = Craft::$app->getElements()->getElementById((int)$referenceElementId, siteId: $siteId);
}
}
return $condition;
}
/**
* Returns the current view state.
*
* @return array
*/
protected function viewState(): array
{
$viewState = $this->request->getParam('viewState', []);
if (empty($viewState['mode'])) {
$viewState['mode'] = 'table';
}
return $viewState;
}
/**
* Returns the element query based on the current params.
*
* @return ElementQueryInterface
*/
protected function elementQuery(): ElementQueryInterface
{
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType;
$query = $elementType::find();
$conditionsService = Craft::$app->getConditions();
if (!$this->source) {
$query->id(false);
return $query;
}
// Does the source specify any criteria attributes?
switch ($this->source['type']) {
case ElementSources::TYPE_NATIVE:
if (isset($this->source['criteria'])) {
Craft::configure($query, $this->source['criteria']);
}
break;
case ElementSources::TYPE_CUSTOM:
/** @var ElementConditionInterface $sourceCondition */
$sourceCondition = $conditionsService->createCondition($this->source['condition']);
$sourceCondition->modifyQuery($query);
}
// Was a condition provided?
if (isset($this->condition)) {
$this->condition->modifyQuery($query);
}
// Override with the request's params
if ($criteria = $this->request->getBodyParam('criteria')) {
if (isset($criteria['trashed'])) {
$criteria['trashed'] = filter_var($criteria['trashed'] ?? false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
}
if (isset($criteria['drafts'])) {
$criteria['drafts'] = filter_var($criteria['drafts'] ?? false, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE) ?? false;
}
if (isset($criteria['draftOf'])) {
if (is_numeric($criteria['draftOf']) && $criteria['draftOf'] != 0) {
$criteria['draftOf'] = (int)$criteria['draftOf'];
} else {
$criteria['draftOf'] = filter_var($criteria['draftOf'], FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE);
}
}
Craft::configure($query, Component::cleanseConfig($criteria));
}
// Override with the custom filters
$filterConditionConfig = $this->request->getBodyParam('filterConfig');
if (!$filterConditionConfig) {
$filterConditionStr = $this->request->getBodyParam('filters');
if ($filterConditionStr) {
parse_str($filterConditionStr, $filterConditionConfig);
$filterConditionConfig = $filterConditionConfig['condition'];
}
}
if ($filterConditionConfig) {
/** @var ElementConditionInterface $filterCondition */
$filterCondition = $conditionsService->createCondition(Component::cleanseConfig($filterConditionConfig));
$filterCondition->modifyQuery($query);
}
// Exclude descendants of the collapsed element IDs
$collapsedElementIds = $this->request->getParam('collapsedElementIds');
if ($collapsedElementIds) {
$descendantQuery = (clone $query)
->offset(null)
->limit(null)
->orderBy([])
->positionedAfter(null)
->positionedBefore(null)
->status(null);
// Get the actual elements
$collapsedElements = (clone $descendantQuery)
->id($collapsedElementIds)
->orderBy(['lft' => SORT_ASC])
->all();
if (!empty($collapsedElements)) {
$descendantIds = [];
foreach ($collapsedElements as $element) {
// Make sure we haven't already excluded this one, because its ancestor is collapsed as well
if (in_array($element->id, $descendantIds, false)) {
continue;
}
$elementDescendantIds = (clone $descendantQuery)
->descendantOf($element)
->ids();
$descendantIds = array_merge($descendantIds, $elementDescendantIds);
}
if (!empty($descendantIds)) {
$query->andWhere(['not', ['elements.id' => $descendantIds]]);
}
}
}
return $query;
}
/**
* Returns the element data to be returned to the client.
*
* @param bool $includeContainer Whether the element container should be included in the response data
* @param bool $includeActions Whether info about the available actions should be included in the response data
* @return array
*/
protected function elementResponseData(bool $includeContainer, bool $includeActions): array
{
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType;
$responseData = [];
$view = $this->getView();
// Get the action head/foot HTML before any more is added to it from the element HTML
if ($includeActions) {
$responseData['actions'] = $this->actionData();
$responseData['actionsHeadHtml'] = $view->getHeadHtml();
$responseData['actionsBodyHtml'] = $view->getBodyHtml();
$responseData['exporters'] = $this->exporterData();
}
$disabledElementIds = $this->request->getParam('disabledElementIds', []);
$showCheckboxes = !empty($this->actions);
if ($this->sourceKey) {
$responseData['html'] = $elementType::indexHtml(
$this->elementQuery,
$disabledElementIds,
$this->viewState,
$this->sourceKey,
$this->context,
$includeContainer,
$showCheckboxes
);
$responseData['headHtml'] = $view->getHeadHtml();
$responseData['bodyHtml'] = $view->getBodyHtml();
} else {
$responseData['html'] = '';
}
return $responseData;
}
/**
* Returns the available actions for the current source.
*
* @return ElementActionInterface[]|null
*/
protected function availableActions(): ?array
{
if ($this->request->isMobileBrowser()) {
return null;
}
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType;
$actions = $elementType::actions($this->sourceKey);
foreach ($actions as $i => $action) {
// $action could be a string or config array
if ($action instanceof ElementActionInterface) {
$action->setElementType($elementType);
} else {
if (is_string($action)) {
$action = ['type' => $action];
}
/** @var array $action */
/** @phpstan-var array{type:class-string<ElementActionInterface>} $action */
$action['elementType'] = $elementType;
$actions[$i] = $action = Craft::$app->getElements()->createAction($action);
}
if ($this->elementQuery->trashed) {
if ($action instanceof DeleteActionInterface && $action->canHardDelete()) {
$action->setHardDelete();
} elseif (!$action instanceof Restore) {
unset($actions[$i]);
}
} elseif ($action instanceof Restore) {
unset($actions[$i]);
}
}
if ($this->elementQuery->trashed) {
// Make sure Restore goes first
usort($actions, function($a, $b): int {
if ($a instanceof Restore) {
return -1;
}
if ($b instanceof Restore) {
return 1;
}
return 0;
});
}
return array_values($actions);
}
/**
* Returns the available exporters for the current source.
*
* @return ElementExporterInterface[]|null
* @since 3.4.0
*/
protected function availableExporters(): ?array
{
if ($this->request->isMobileBrowser()) {
return null;
}
/** @var string|ElementInterface $elementType */
/** @phpstan-var class-string<ElementInterface>|ElementInterface $elementType */
$elementType = $this->elementType;
$exporters = $elementType::exporters($this->sourceKey);
foreach ($exporters as $i => $exporter) {
// $action could be a string or config array
if ($exporter instanceof ElementExporterInterface) {
$exporter->setElementType($elementType);
} else {
if (is_string($exporter)) {
$exporter = ['type' => $exporter];
}
$exporter['elementType'] = $elementType;
$exporters[$i] = Craft::$app->getElements()->createExporter($exporter);
}
}
return array_values($exporters);
}
/**
* Returns the data for the available actions.
*
* @return array|null
*/
protected function actionData(): ?array
{
if (empty($this->actions)) {
return null;
}
$actionData = [];
/** @var ElementAction $action */
foreach ($this->actions as $action) {
$actionData[] = [
'type' => get_class($action),
'destructive' => $action->isDestructive(),
'download' => $action->isDownload(),
'name' => $action->getTriggerLabel(),
'trigger' => $action->getTriggerHtml(),
'confirm' => $action->getConfirmationMessage(),
'settings' => $action->getSettings() ?: null,
];
}
return $actionData;
}
/**
* Returns the data for the available exporters.
*
* @return array|null
* @since 3.4.0
*/
protected function exporterData(): ?array
{
if (empty($this->exporters)) {
return null;
}
$exporterData = [];
foreach ($this->exporters as $exporter) {
$exporterData[] = [
'type' => get_class($exporter),
'name' => $exporter::displayName(),
'formattable' => $exporter::isFormattable(),
];
}
return $exporterData;
}
/**
* Returns the updated table attribute HTML for an element.
*
* @return Response
* @throws BadRequestHttpException
* @throws ForbiddenHttpException
*/
public function actionElementTableHtml(): Response
{
$this->requireAcceptsJson();
if (!$this->sourceKey) {
throw new BadRequestHttpException("Request missing required body param");
}
/** @var string|ElementInterface $elementType */
$elementType = $this->elementType;
$id = $this->request->getRequiredBodyParam('id');
$siteId = $this->request->getRequiredBodyParam('siteId');
$site = $siteId ? Craft::$app->getSites()->getSiteById($siteId) : null;
if (!$id || !is_numeric($id)) {
throw new BadRequestHttpException("Invalid element ID: $id");
}
if (!$site) {
throw new BadRequestHttpException("Invalid site ID: $siteId");
}
if (Craft::$app->getIsMultiSite() && !Craft::$app->getUser()->checkPermission("editSite:$site->uid")) {
throw new ForbiddenHttpException('User not authorized to edit content for this site.');
}
/** @var ElementInterface|null $element */
$element = $elementType::find()
->id($id)
->drafts(null)
->provisionalDrafts(null)
->revisions(null)
->siteId($siteId)
->status(null)
->one();
if (!$element) {
throw new BadRequestHttpException("Invalid element ID: $id");
}
$attributes = Craft::$app->getElementSources()->getTableAttributes($this->elementType, $this->sourceKey);
$attributeHtml = [];
foreach ($attributes as [$attribute]) {
$attributeHtml[$attribute] = $element->getTableAttributeHtml($attribute);
}
return $this->asJson([
'elementHtml' => Cp::elementHtml($element, $this->context),
'attributeHtml' => $attributeHtml,
]);
}
}