813e32ef |
1 | <?php |
2 | /* |
3 | +--------------------------------------------------------------------+ |
4 | | Copyright CiviCRM LLC. All rights reserved. | |
5 | | | |
6 | | This work is published under the GNU AGPLv3 license with some | |
7 | | permitted exceptions and without any warranty. For full license | |
8 | | and copyright information, see https://civicrm.org/licensing | |
9 | +--------------------------------------------------------------------+ |
10 | */ |
11 | |
12 | /** |
13 | * Class CRM_Export_Controller_Standalone |
14 | */ |
15 | abstract class CRM_Core_Controller_Task extends CRM_Core_Controller { |
16 | |
17 | /** |
18 | * Class constructor. |
19 | * |
20 | * @param string $title |
21 | * @param bool|int $action |
22 | * @param bool $modal |
23 | * |
24 | * @throws \CRM_Core_Exception |
25 | * @throws \CiviCRM_API3_Exception |
26 | */ |
27 | public function __construct($title = NULL, $action = CRM_Core_Action::NONE, $modal = TRUE) { |
28 | |
29 | parent::__construct($title, $modal); |
30 | $id = explode(',', CRM_Utils_Request::retrieve('id', 'CommaSeparatedIntegers', $this, TRUE)); |
31 | |
32 | // Check permissions |
33 | $perm = civicrm_api3($this->getEntity(), 'get', [ |
34 | 'return' => 'id', |
35 | 'options' => ['limit' => 0], |
36 | 'check_permissions' => 1, |
37 | 'id' => ['IN' => $id], |
38 | ])['values']; |
39 | if (empty($perm)) { |
40 | throw new CRM_Core_Exception(ts('No records available')); |
41 | } |
42 | $this->set('id', implode(',', array_keys($perm))); |
43 | $pages = array_fill_keys($this->getTaskClass(), NULL); |
44 | |
45 | $this->_stateMachine = new CRM_Core_StateMachine($this); |
46 | $this->_stateMachine->addSequentialPages($pages); |
47 | // create and instantiate the pages |
48 | $this->addPages($this->_stateMachine, $action); |
49 | // add all the actions |
50 | $this->addActions(); |
51 | } |
52 | |
53 | /** |
54 | * Get the name used to construct the class. |
55 | * |
56 | * @return string |
57 | */ |
58 | abstract public function getEntity():string; |
59 | |
60 | /** |
61 | * Get the available tasks for the entity. |
62 | * |
63 | * @return array |
64 | */ |
65 | abstract public function getAvailableTasks():array; |
66 | |
67 | /** |
68 | * Get the class for the action. |
69 | * |
70 | * @return array Array of the classes for the form controlle. |
71 | * |
72 | * @throws \CRM_Core_Exception |
73 | */ |
74 | protected function getTaskClass(): array { |
75 | $task = CRM_Utils_Request::retrieve('task', 'Alphanumeric', $this, TRUE); |
76 | foreach ($this->getAvailableTasks() as $taskAction) { |
77 | if (($taskAction['key'] ?? '') === $task) { |
78 | return (array) $taskAction['class']; |
79 | } |
80 | } |
81 | throw new CRM_Core_Exception(ts('Invalid task')); |
82 | } |
83 | |
84 | } |