4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
7 | This work is published under the GNU AGPLv3 license with some |
8 | permitted exceptions and without any warranty. For full license |
9 | and copyright information, see https://civicrm.org/licensing |
10 +--------------------------------------------------------------------+
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
22 namespace Civi\Api4\Action
;
24 use Civi\API\Exception\NotImplementedException
;
25 use Civi\Api4\Generic\BasicGetAction
;
26 use Civi\Api4\Utils\ActionUtil
;
27 use Civi\Api4\Utils\ReflectionUtils
;
30 * Get actions for an entity with a list of accepted params
32 class GetActions
extends BasicGetAction
{
34 private $_actions = [];
36 private $_actionsToGet;
38 protected function getRecords() {
39 $this->_actionsToGet
= $this->_itemsToGet('name');
41 $entityReflection = new \
ReflectionClass('\Civi\Api4\\' . $this->_entityName
);
42 foreach ($entityReflection->getMethods(\ReflectionMethod
::IS_STATIC | \ReflectionMethod
::IS_PUBLIC
) as $method) {
43 $actionName = $method->getName();
44 if ($actionName != 'permissions' && $actionName[0] != '_') {
45 $this->loadAction($actionName);
48 if (!$this->_actionsToGet ||
count($this->_actionsToGet
) > count($this->_actions
)) {
49 // Search for entity-specific actions in extensions
50 foreach (\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
51 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($ext['filePath']));
52 $this->scanDir($dir . 'Civi/Api4/Action/' . $this->_entityName
);
54 // Search for entity-specific actions in core
55 $this->scanDir(\CRM_Utils_File
::addTrailingSlash(__DIR__
) . $this->_entityName
);
57 ksort($this->_actions
);
58 return $this->_actions
;
64 private function scanDir($dir) {
66 foreach (glob("$dir/*.php") as $file) {
68 preg_match('/(\w*).php/', $file, $matches);
69 $actionName = array_pop($matches);
70 $actionClass = new \
ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName
. '\\' . $actionName);
71 if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) {
72 $this->loadAction(lcfirst($actionName));
81 private function loadAction($actionName) {
83 if (!isset($this->_actions
[$actionName]) && (!$this->_actionsToGet ||
in_array($actionName, $this->_actionsToGet
))) {
84 $action = ActionUtil
::getAction($this->getEntityName(), $actionName);
85 if (is_object($action)) {
86 $this->_actions
[$actionName] = ['name' => $actionName];
87 if ($this->_isFieldSelected('description') ||
$this->_isFieldSelected('comment')) {
88 $actionReflection = new \
ReflectionClass($action);
89 $actionInfo = ReflectionUtils
::getCodeDocs($actionReflection);
90 unset($actionInfo['method']);
91 $this->_actions
[$actionName] +
= $actionInfo;
93 if ($this->_isFieldSelected('params')) {
94 $this->_actions
[$actionName]['params'] = $action->getParamInfo();
95 // Language param is only relevant on multilingual sites
96 $languageLimit = (array) \Civi
::settings()->get('languageLimit');
97 if (count($languageLimit) < 2) {
98 unset($this->_actions
[$actionName]['params']['language']);
100 elseif (isset($this->_actions
[$actionName]['params']['language'])) {
101 $this->_actions
[$actionName]['params']['language']['options'] = array_keys($languageLimit);
107 catch (NotImplementedException
$e) {
111 public function fields() {
115 'data_type' => 'String',
118 'name' => 'description',
119 'data_type' => 'String',
123 'data_type' => 'String',
127 'data_type' => 'Array',