Commit | Line | Data |
---|---|---|
19b53e5b C |
1 | <?php |
2 | ||
3 | namespace Civi\Api4\Action; | |
4 | ||
5 | use Civi\API\Exception\NotImplementedException; | |
6 | use Civi\Api4\Generic\BasicGetAction; | |
7 | use Civi\Api4\Utils\ActionUtil; | |
8 | use Civi\Api4\Utils\ReflectionUtils; | |
9 | ||
10 | /** | |
11 | * Get actions for an entity with a list of accepted params | |
12 | */ | |
13 | class GetActions extends BasicGetAction { | |
14 | ||
15 | private $_actions = []; | |
16 | ||
17 | private $_actionsToGet; | |
18 | ||
19 | protected function getRecords() { | |
20 | $this->_actionsToGet = $this->_itemsToGet('name'); | |
21 | ||
22 | $entityReflection = new \ReflectionClass('\Civi\Api4\\' . $this->_entityName); | |
23 | foreach ($entityReflection->getMethods(\ReflectionMethod::IS_STATIC | \ReflectionMethod::IS_PUBLIC) as $method) { | |
24 | $actionName = $method->getName(); | |
25 | if ($actionName != 'permissions' && $actionName[0] != '_') { | |
26 | $this->loadAction($actionName); | |
27 | } | |
28 | } | |
29 | if (!$this->_actionsToGet || count($this->_actionsToGet) > count($this->_actions)) { | |
23c2d07c | 30 | // Search for entity-specific actions in extensions |
19b53e5b C |
31 | foreach (\CRM_Extension_System::singleton()->getMapper()->getActiveModuleFiles() as $ext) { |
32 | $dir = \CRM_Utils_File::addTrailingSlash(dirname($ext['filePath'])); | |
33 | $this->scanDir($dir . 'Civi/Api4/Action/' . $this->_entityName); | |
34 | } | |
23c2d07c | 35 | // Search for entity-specific actions in core |
36 | $this->scanDir(\CRM_Utils_File::addTrailingSlash(__DIR__) . $this->_entityName); | |
19b53e5b C |
37 | } |
38 | ksort($this->_actions); | |
39 | return $this->_actions; | |
40 | } | |
41 | ||
42 | /** | |
43 | * @param $dir | |
44 | */ | |
45 | private function scanDir($dir) { | |
46 | if (is_dir($dir)) { | |
47 | foreach (glob("$dir/*.php") as $file) { | |
48 | $matches = []; | |
49 | preg_match('/(\w*).php/', $file, $matches); | |
50 | $actionName = array_pop($matches); | |
51 | $actionClass = new \ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName . '\\' . $actionName); | |
52 | if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) { | |
53 | $this->loadAction(lcfirst($actionName)); | |
54 | } | |
55 | } | |
56 | } | |
57 | } | |
58 | ||
59 | /** | |
60 | * @param $actionName | |
61 | */ | |
62 | private function loadAction($actionName) { | |
63 | try { | |
64 | if (!isset($this->_actions[$actionName]) && (!$this->_actionsToGet || in_array($actionName, $this->_actionsToGet))) { | |
65 | $action = ActionUtil::getAction($this->getEntityName(), $actionName); | |
66 | if (is_object($action)) { | |
67 | $this->_actions[$actionName] = ['name' => $actionName]; | |
68 | if ($this->_isFieldSelected('description') || $this->_isFieldSelected('comment')) { | |
69 | $actionReflection = new \ReflectionClass($action); | |
70 | $actionInfo = ReflectionUtils::getCodeDocs($actionReflection); | |
71 | unset($actionInfo['method']); | |
72 | $this->_actions[$actionName] += $actionInfo; | |
73 | } | |
74 | if ($this->_isFieldSelected('params')) { | |
75 | $this->_actions[$actionName]['params'] = $action->getParamInfo(); | |
76 | // Language param is only relevant on multilingual sites | |
77 | $languageLimit = (array) \Civi::settings()->get('languageLimit'); | |
78 | if (count($languageLimit) < 2) { | |
79 | unset($this->_actions[$actionName]['params']['language']); | |
80 | } | |
81 | elseif (isset($this->_actions[$actionName]['params']['language'])) { | |
82 | $this->_actions[$actionName]['params']['language']['options'] = array_keys($languageLimit); | |
83 | } | |
84 | } | |
85 | } | |
86 | } | |
87 | } | |
88 | catch (NotImplementedException $e) { | |
89 | } | |
90 | } | |
91 | ||
92 | public function fields() { | |
93 | return [ | |
94 | [ | |
95 | 'name' => 'name', | |
96 | 'data_type' => 'String', | |
97 | ], | |
98 | [ | |
99 | 'name' => 'description', | |
100 | 'data_type' => 'String', | |
101 | ], | |
102 | [ | |
103 | 'name' => 'comment', | |
104 | 'data_type' => 'String', | |
105 | ], | |
106 | [ | |
107 | 'name' => 'params', | |
108 | 'data_type' => 'Array', | |
109 | ], | |
110 | ]; | |
111 | } | |
112 | ||
113 | } |