Merge pull request #15830 from eileenmcnaughton/dedupe4
[civicrm-core.git] / Civi / Api4 / Action / GetActions.php
1 <?php
2
3 /*
4 +--------------------------------------------------------------------+
5 | Copyright CiviCRM LLC. All rights reserved. |
6 | |
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 +--------------------------------------------------------------------+
11 */
12
13 /**
14 *
15 * @package CRM
16 * @copyright CiviCRM LLC https://civicrm.org/licensing
17 * $Id$
18 *
19 */
20
21
22 namespace Civi\Api4\Action;
23
24 use Civi\API\Exception\NotImplementedException;
25 use Civi\Api4\Generic\BasicGetAction;
26 use Civi\Api4\Utils\ActionUtil;
27 use Civi\Api4\Utils\ReflectionUtils;
28
29 /**
30 * Get actions for an entity with a list of accepted params
31 */
32 class GetActions extends BasicGetAction {
33
34 private $_actions = [];
35
36 private $_actionsToGet;
37
38 protected function getRecords() {
39 $this->_actionsToGet = $this->_itemsToGet('name');
40
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);
46 }
47 }
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);
53 }
54 // Search for entity-specific actions in core
55 $this->scanDir(\CRM_Utils_File::addTrailingSlash(__DIR__) . $this->_entityName);
56 }
57 ksort($this->_actions);
58 return $this->_actions;
59 }
60
61 /**
62 * @param $dir
63 */
64 private function scanDir($dir) {
65 if (is_dir($dir)) {
66 foreach (glob("$dir/*.php") as $file) {
67 $matches = [];
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));
73 }
74 }
75 }
76 }
77
78 /**
79 * @param $actionName
80 */
81 private function loadAction($actionName) {
82 try {
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;
92 }
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']);
99 }
100 elseif (isset($this->_actions[$actionName]['params']['language'])) {
101 $this->_actions[$actionName]['params']['language']['options'] = array_keys($languageLimit);
102 }
103 }
104 }
105 }
106 }
107 catch (NotImplementedException $e) {
108 }
109 }
110
111 public function fields() {
112 return [
113 [
114 'name' => 'name',
115 'data_type' => 'String',
116 ],
117 [
118 'name' => 'description',
119 'data_type' => 'String',
120 ],
121 [
122 'name' => 'comment',
123 'data_type' => 'String',
124 ],
125 [
126 'name' => 'params',
127 'data_type' => 'Array',
128 ],
129 ];
130 }
131
132 }