3 namespace Civi\Api4\Action
;
5 use Civi\API\Exception\NotImplementedException
;
6 use Civi\Api4\Generic\BasicGetAction
;
7 use Civi\Api4\Utils\ActionUtil
;
8 use Civi\Api4\Utils\ReflectionUtils
;
11 * Get actions for an entity with a list of accepted params
13 class GetActions
extends BasicGetAction
{
15 private $_actions = [];
17 private $_actionsToGet;
19 protected function getRecords() {
20 $this->_actionsToGet
= $this->_itemsToGet('name');
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);
29 if (!$this->_actionsToGet ||
count($this->_actionsToGet
) > count($this->_actions
)) {
30 // Search entity-specific actions (including those provided by extensions)
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
);
36 ksort($this->_actions
);
37 return $this->_actions
;
43 private function scanDir($dir) {
45 foreach (glob("$dir/*.php") as $file) {
47 preg_match('/(\w*).php/', $file, $matches);
48 $actionName = array_pop($matches);
49 $actionClass = new \
ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName
. '\\' . $actionName);
50 if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) {
51 $this->loadAction(lcfirst($actionName));
60 private function loadAction($actionName) {
62 if (!isset($this->_actions
[$actionName]) && (!$this->_actionsToGet ||
in_array($actionName, $this->_actionsToGet
))) {
63 $action = ActionUtil
::getAction($this->getEntityName(), $actionName);
64 if (is_object($action)) {
65 $this->_actions
[$actionName] = ['name' => $actionName];
66 if ($this->_isFieldSelected('description') ||
$this->_isFieldSelected('comment')) {
67 $actionReflection = new \
ReflectionClass($action);
68 $actionInfo = ReflectionUtils
::getCodeDocs($actionReflection);
69 unset($actionInfo['method']);
70 $this->_actions
[$actionName] +
= $actionInfo;
72 if ($this->_isFieldSelected('params')) {
73 $this->_actions
[$actionName]['params'] = $action->getParamInfo();
74 // Language param is only relevant on multilingual sites
75 $languageLimit = (array) \Civi
::settings()->get('languageLimit');
76 if (count($languageLimit) < 2) {
77 unset($this->_actions
[$actionName]['params']['language']);
79 elseif (isset($this->_actions
[$actionName]['params']['language'])) {
80 $this->_actions
[$actionName]['params']['language']['options'] = array_keys($languageLimit);
86 catch (NotImplementedException
$e) {
90 public function fields() {
94 'data_type' => 'String',
97 'name' => 'description',
98 'data_type' => 'String',
102 'data_type' => 'String',
106 'data_type' => 'Array',