4 +--------------------------------------------------------------------+
6 +--------------------------------------------------------------------+
7 | Copyright CiviCRM LLC (c) 2004-2019 |
8 +--------------------------------------------------------------------+
9 | This file is a part of CiviCRM. |
11 | CiviCRM is free software; you can copy, modify, and distribute it |
12 | under the terms of the GNU Affero General Public License |
13 | Version 3, 19 November 2007 and the CiviCRM Licensing Exception. |
15 | CiviCRM is distributed in the hope that it will be useful, but |
16 | WITHOUT ANY WARRANTY; without even the implied warranty of |
17 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. |
18 | See the GNU Affero General Public License for more details. |
20 | You should have received a copy of the GNU Affero General Public |
21 | License and the CiviCRM Licensing Exception along |
22 | with this program; if not, contact CiviCRM LLC |
23 | at info[AT]civicrm[DOT]org. If you have questions about the |
24 | GNU Affero General Public License or the licensing of CiviCRM, |
25 | see the CiviCRM license FAQ at http://civicrm.org/licensing |
26 +--------------------------------------------------------------------+
32 * @copyright CiviCRM LLC (c) 2004-2019
38 namespace Civi\Api4\Action
;
40 use Civi\API\Exception\NotImplementedException
;
41 use Civi\Api4\Generic\BasicGetAction
;
42 use Civi\Api4\Utils\ActionUtil
;
43 use Civi\Api4\Utils\ReflectionUtils
;
46 * Get actions for an entity with a list of accepted params
48 class GetActions
extends BasicGetAction
{
50 private $_actions = [];
52 private $_actionsToGet;
54 protected function getRecords() {
55 $this->_actionsToGet
= $this->_itemsToGet('name');
57 $entityReflection = new \
ReflectionClass('\Civi\Api4\\' . $this->_entityName
);
58 foreach ($entityReflection->getMethods(\ReflectionMethod
::IS_STATIC | \ReflectionMethod
::IS_PUBLIC
) as $method) {
59 $actionName = $method->getName();
60 if ($actionName != 'permissions' && $actionName[0] != '_') {
61 $this->loadAction($actionName);
64 if (!$this->_actionsToGet ||
count($this->_actionsToGet
) > count($this->_actions
)) {
65 // Search for entity-specific actions in extensions
66 foreach (\CRM_Extension_System
::singleton()->getMapper()->getActiveModuleFiles() as $ext) {
67 $dir = \CRM_Utils_File
::addTrailingSlash(dirname($ext['filePath']));
68 $this->scanDir($dir . 'Civi/Api4/Action/' . $this->_entityName
);
70 // Search for entity-specific actions in core
71 $this->scanDir(\CRM_Utils_File
::addTrailingSlash(__DIR__
) . $this->_entityName
);
73 ksort($this->_actions
);
74 return $this->_actions
;
80 private function scanDir($dir) {
82 foreach (glob("$dir/*.php") as $file) {
84 preg_match('/(\w*).php/', $file, $matches);
85 $actionName = array_pop($matches);
86 $actionClass = new \
ReflectionClass('\\Civi\\Api4\\Action\\' . $this->_entityName
. '\\' . $actionName);
87 if ($actionClass->isInstantiable() && $actionClass->isSubclassOf('\\Civi\\Api4\\Generic\\AbstractAction')) {
88 $this->loadAction(lcfirst($actionName));
97 private function loadAction($actionName) {
99 if (!isset($this->_actions
[$actionName]) && (!$this->_actionsToGet ||
in_array($actionName, $this->_actionsToGet
))) {
100 $action = ActionUtil
::getAction($this->getEntityName(), $actionName);
101 if (is_object($action)) {
102 $this->_actions
[$actionName] = ['name' => $actionName];
103 if ($this->_isFieldSelected('description') ||
$this->_isFieldSelected('comment')) {
104 $actionReflection = new \
ReflectionClass($action);
105 $actionInfo = ReflectionUtils
::getCodeDocs($actionReflection);
106 unset($actionInfo['method']);
107 $this->_actions
[$actionName] +
= $actionInfo;
109 if ($this->_isFieldSelected('params')) {
110 $this->_actions
[$actionName]['params'] = $action->getParamInfo();
111 // Language param is only relevant on multilingual sites
112 $languageLimit = (array) \Civi
::settings()->get('languageLimit');
113 if (count($languageLimit) < 2) {
114 unset($this->_actions
[$actionName]['params']['language']);
116 elseif (isset($this->_actions
[$actionName]['params']['language'])) {
117 $this->_actions
[$actionName]['params']['language']['options'] = array_keys($languageLimit);
123 catch (NotImplementedException
$e) {
127 public function fields() {
131 'data_type' => 'String',
134 'name' => 'description',
135 'data_type' => 'String',
139 'data_type' => 'String',
143 'data_type' => 'Array',