Merge pull request #17376 from artfulrobot/artfulrobot-public-status-messages
[civicrm-core.git] / api / v3 / Constant.php
1 <?php
2 /*
3 +--------------------------------------------------------------------+
4 | Copyright CiviCRM LLC. All rights reserved. |
5 | |
6 | This work is published under the GNU AGPLv3 license with some |
7 | permitted exceptions and without any warranty. For full license |
8 | and copyright information, see https://civicrm.org/licensing |
9 +--------------------------------------------------------------------+
10 */
11
12 /**
13 * CiviCRM APIv3 pseudoconstants
14 *
15 * @deprecated
16 * The Constant api is deprecated as of CiviCRM 4.4. Please use the getoptions api action instead.
17 * @package CiviCRM_APIv3
18 */
19
20 /**
21 * Declare deprecated api entity.
22 *
23 * @deprecated api notice
24 * @return string
25 * to indicate this entire api entity is deprecated
26 */
27 function _civicrm_api3_constant_deprecation() {
28 return 'The Constant api is deprecated as of CiviCRM 4.4. Please use the getoptions api action instead.';
29 }
30
31 /**
32 * Get constant values (deprecated).
33 *
34 * @deprecated as of CiviCRM 4.4.
35 * It's recommended to use the api getoptions action instead
36 *
37 * @param array $params
38 * Name of a public static method of
39 * CRM_Core_PseudoConstant: one of
40 * activityStatus
41 * activityType
42 * addressee
43 * allGroup
44 * country
45 * countryIsoCode
46 * county
47 * currencyCode
48 * currencySymbols
49 * customGroup
50 * emailGreeting
51 * fromEmailAddress
52 * gender
53 * group
54 * groupIterator
55 * honor
56 * IMProvider
57 * individualPrefix
58 * individualSuffix
59 * locationType
60 * locationVcardName
61 * mailProtocol
62 * mappingTypes
63 * paymentProcessor
64 * paymentProcessorType
65 * pcm
66 * phoneType
67 * postalGreeting
68 * priority
69 * relationshipType
70 * stateProvince
71 * stateProvinceAbbreviation
72 * stateProvinceForCountry
73 * staticGroup
74 * tag
75 * tasks
76 * ufGroup
77 * visibility
78 * worldRegion
79 * wysiwygEditor
80 *
81 * @return array
82 */
83 function civicrm_api3_constant_get($params) {
84
85 $name = $params['name'];
86 // all the stuff about classes should be adequately replaced by the bit in the 'else'
87 //ie $values = call_user_func(array('CRM_Utils_PseudoConstant', 'getConstant'), $name);
88 // once tests are 100% can try removing the first block & a similar block from Generic:getoptions
89
90 // Whitelist approach is safer
91 $allowedClasses = [
92 'CRM_Core_PseudoConstant',
93 'CRM_Event_PseudoConstant',
94 'CRM_Contribute_PseudoConstant',
95 'CRM_Member_PseudoConstant',
96 ];
97 $className = $allowedClasses[0];
98 if (!empty($params['class']) && in_array($params['class'], $allowedClasses)) {
99 $className = $params['class'];
100 }
101 $callable = "$className::$name";
102 if (is_callable($callable)) {
103 if (empty($params)) {
104 $values = call_user_func([$className, $name]);
105 }
106 else {
107 $values = call_user_func([$className, $name]);
108 //@TODO XAV take out the param the COOKIE, Entity, Action and so there are only the "real param" in it
109 //$values = call_user_func_array( array( $className, $name ), $params );
110 }
111 return civicrm_api3_create_success($values, $params, 'Constant');
112 }
113 else {
114 $values = call_user_func(['CRM_Utils_PseudoConstant', 'getConstant'], $name);
115 if (!empty($values)) {
116 return civicrm_api3_create_success($values, $params, 'Constant');
117 }
118 }
119 return civicrm_api3_create_error('Unknown civicrm constant or method not callable');
120 }
121
122 /**
123 * Adjust metadata for constant get action.
124 *
125 * @param array $params
126 */
127 function _civicrm_api3_constant_get_spec(&$params) {
128 $options = [
129 'activityStatus',
130 'activityType',
131 'addressee',
132 'allGroup',
133 'country',
134 'countryIsoCode',
135 'county',
136 'currencyCode',
137 'currencySymbols',
138 'customGroup',
139 'emailGreeting',
140 'fromEmailAddress',
141 'gender',
142 'group',
143 'honor',
144 'IMProvider',
145 'individualPrefix',
146 'individualSuffix',
147 'locationType',
148 'locationVcardName',
149 'mailProtocol',
150 'mappingTypes',
151 'paymentInstrument',
152 'paymentProcessor',
153 'paymentProcessorType',
154 'pcm',
155 'phoneType',
156 'postalGreeting',
157 'priority',
158 'relationshipType',
159 'stateProvince',
160 'stateProvinceAbbreviation',
161 'stateProvinceForCountry',
162 'staticGroup',
163 'tag',
164 'tasks',
165 'ufGroup',
166 'visibility',
167 'worldRegion',
168 'wysiwygEditor',
169 ];
170 $params = [
171 'name' => [
172 'title' => 'Constant Name',
173 'name' => 'name',
174 'api.required' => 1,
175 'options' => array_combine($options, $options),
176 'type' => CRM_Utils_Type::T_STRING,
177 ],
178 ];
179 }