Commit | Line | Data |
---|---|---|
6bc5e930 TO |
1 | <?php |
2 | ||
3 | // AUTO-GENERATED FILE -- Civix may overwrite any changes made to this file | |
4 | ||
5 | /** | |
6 | * The ExtensionUtil class provides small stubs for accessing resources of this | |
7 | * extension. | |
8 | */ | |
5e4accea | 9 | class CRM_AfformAdmin_ExtensionUtil { |
6f1b20a6 CW |
10 | const SHORT_NAME = 'afform_admin'; |
11 | const LONG_NAME = 'org.civicrm.afform_admin'; | |
12 | const CLASS_PREFIX = 'CRM_AfformAdmin'; | |
6bc5e930 TO |
13 | |
14 | /** | |
15 | * Translate a string using the extension's domain. | |
16 | * | |
17 | * If the extension doesn't have a specific translation | |
18 | * for the string, fallback to the default translations. | |
19 | * | |
20 | * @param string $text | |
21 | * Canonical message text (generally en_US). | |
22 | * @param array $params | |
23 | * @return string | |
24 | * Translated text. | |
25 | * @see ts | |
26 | */ | |
7620445e | 27 | public static function ts($text, $params = []) { |
6bc5e930 | 28 | if (!array_key_exists('domain', $params)) { |
7620445e | 29 | $params['domain'] = [self::LONG_NAME, NULL]; |
6bc5e930 TO |
30 | } |
31 | return ts($text, $params); | |
32 | } | |
33 | ||
34 | /** | |
35 | * Get the URL of a resource file (in this extension). | |
36 | * | |
37 | * @param string|NULL $file | |
38 | * Ex: NULL. | |
39 | * Ex: 'css/foo.css'. | |
40 | * @return string | |
41 | * Ex: 'http://example.org/sites/default/ext/org.example.foo'. | |
42 | * Ex: 'http://example.org/sites/default/ext/org.example.foo/css/foo.css'. | |
43 | */ | |
44 | public static function url($file = NULL) { | |
45 | if ($file === NULL) { | |
46 | return rtrim(CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME), '/'); | |
47 | } | |
48 | return CRM_Core_Resources::singleton()->getUrl(self::LONG_NAME, $file); | |
49 | } | |
50 | ||
51 | /** | |
52 | * Get the path of a resource file (in this extension). | |
53 | * | |
54 | * @param string|NULL $file | |
55 | * Ex: NULL. | |
56 | * Ex: 'css/foo.css'. | |
57 | * @return string | |
58 | * Ex: '/var/www/example.org/sites/default/ext/org.example.foo'. | |
59 | * Ex: '/var/www/example.org/sites/default/ext/org.example.foo/css/foo.css'. | |
60 | */ | |
61 | public static function path($file = NULL) { | |
62 | // return CRM_Core_Resources::singleton()->getPath(self::LONG_NAME, $file); | |
63 | return __DIR__ . ($file === NULL ? '' : (DIRECTORY_SEPARATOR . $file)); | |
64 | } | |
65 | ||
66 | /** | |
67 | * Get the name of a class within this extension. | |
68 | * | |
69 | * @param string $suffix | |
70 | * Ex: 'Page_HelloWorld' or 'Page\\HelloWorld'. | |
71 | * @return string | |
72 | * Ex: 'CRM_Foo_Page_HelloWorld'. | |
73 | */ | |
74 | public static function findClass($suffix) { | |
75 | return self::CLASS_PREFIX . '_' . str_replace('\\', '_', $suffix); | |
76 | } | |
77 | ||
78 | } | |
79 | ||
5e4accea | 80 | use CRM_AfformAdmin_ExtensionUtil as E; |
6bc5e930 TO |
81 | |
82 | /** | |
83 | * (Delegated) Implements hook_civicrm_config(). | |
84 | * | |
7620445e | 85 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_config |
6bc5e930 | 86 | */ |
5e4accea | 87 | function _afform_admin_civix_civicrm_config(&$config = NULL) { |
6bc5e930 TO |
88 | static $configured = FALSE; |
89 | if ($configured) { | |
90 | return; | |
91 | } | |
92 | $configured = TRUE; | |
93 | ||
94 | $template =& CRM_Core_Smarty::singleton(); | |
95 | ||
96 | $extRoot = dirname(__FILE__) . DIRECTORY_SEPARATOR; | |
97 | $extDir = $extRoot . 'templates'; | |
98 | ||
99 | if (is_array($template->template_dir)) { | |
100 | array_unshift($template->template_dir, $extDir); | |
101 | } | |
102 | else { | |
7620445e | 103 | $template->template_dir = [$extDir, $template->template_dir]; |
6bc5e930 TO |
104 | } |
105 | ||
106 | $include_path = $extRoot . PATH_SEPARATOR . get_include_path(); | |
107 | set_include_path($include_path); | |
108 | } | |
109 | ||
6bc5e930 TO |
110 | /** |
111 | * Implements hook_civicrm_install(). | |
112 | * | |
7620445e | 113 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_install |
6bc5e930 | 114 | */ |
5e4accea CW |
115 | function _afform_admin_civix_civicrm_install() { |
116 | _afform_admin_civix_civicrm_config(); | |
117 | if ($upgrader = _afform_admin_civix_upgrader()) { | |
6bc5e930 TO |
118 | $upgrader->onInstall(); |
119 | } | |
120 | } | |
121 | ||
122 | /** | |
123 | * Implements hook_civicrm_postInstall(). | |
124 | * | |
7620445e | 125 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_postInstall |
6bc5e930 | 126 | */ |
5e4accea CW |
127 | function _afform_admin_civix_civicrm_postInstall() { |
128 | _afform_admin_civix_civicrm_config(); | |
129 | if ($upgrader = _afform_admin_civix_upgrader()) { | |
7620445e | 130 | if (is_callable([$upgrader, 'onPostInstall'])) { |
6bc5e930 TO |
131 | $upgrader->onPostInstall(); |
132 | } | |
133 | } | |
134 | } | |
135 | ||
136 | /** | |
137 | * Implements hook_civicrm_uninstall(). | |
138 | * | |
7620445e | 139 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_uninstall |
6bc5e930 | 140 | */ |
5e4accea CW |
141 | function _afform_admin_civix_civicrm_uninstall() { |
142 | _afform_admin_civix_civicrm_config(); | |
143 | if ($upgrader = _afform_admin_civix_upgrader()) { | |
6bc5e930 TO |
144 | $upgrader->onUninstall(); |
145 | } | |
146 | } | |
147 | ||
148 | /** | |
149 | * (Delegated) Implements hook_civicrm_enable(). | |
150 | * | |
7620445e | 151 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_enable |
6bc5e930 | 152 | */ |
5e4accea CW |
153 | function _afform_admin_civix_civicrm_enable() { |
154 | _afform_admin_civix_civicrm_config(); | |
155 | if ($upgrader = _afform_admin_civix_upgrader()) { | |
7620445e | 156 | if (is_callable([$upgrader, 'onEnable'])) { |
6bc5e930 TO |
157 | $upgrader->onEnable(); |
158 | } | |
159 | } | |
160 | } | |
161 | ||
162 | /** | |
163 | * (Delegated) Implements hook_civicrm_disable(). | |
164 | * | |
7620445e | 165 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_disable |
6bc5e930 TO |
166 | * @return mixed |
167 | */ | |
5e4accea CW |
168 | function _afform_admin_civix_civicrm_disable() { |
169 | _afform_admin_civix_civicrm_config(); | |
170 | if ($upgrader = _afform_admin_civix_upgrader()) { | |
7620445e | 171 | if (is_callable([$upgrader, 'onDisable'])) { |
6bc5e930 TO |
172 | $upgrader->onDisable(); |
173 | } | |
174 | } | |
175 | } | |
176 | ||
177 | /** | |
178 | * (Delegated) Implements hook_civicrm_upgrade(). | |
179 | * | |
180 | * @param $op string, the type of operation being performed; 'check' or 'enqueue' | |
181 | * @param $queue CRM_Queue_Queue, (for 'enqueue') the modifiable list of pending up upgrade tasks | |
182 | * | |
d9cc2e0d | 183 | * @return mixed |
184 | * based on op. for 'check', returns array(boolean) (TRUE if upgrades are pending) | |
185 | * for 'enqueue', returns void | |
6bc5e930 | 186 | * |
7620445e | 187 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_upgrade |
6bc5e930 | 188 | */ |
5e4accea CW |
189 | function _afform_admin_civix_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) { |
190 | if ($upgrader = _afform_admin_civix_upgrader()) { | |
6bc5e930 TO |
191 | return $upgrader->onUpgrade($op, $queue); |
192 | } | |
193 | } | |
194 | ||
195 | /** | |
5e4accea | 196 | * @return CRM_AfformAdmin_Upgrader |
6bc5e930 | 197 | */ |
5e4accea CW |
198 | function _afform_admin_civix_upgrader() { |
199 | if (!file_exists(__DIR__ . '/CRM/AfformAdmin/Upgrader.php')) { | |
6bc5e930 TO |
200 | return NULL; |
201 | } | |
202 | else { | |
5e4accea | 203 | return CRM_AfformAdmin_Upgrader_Base::instance(); |
6bc5e930 TO |
204 | } |
205 | } | |
206 | ||
6bc5e930 TO |
207 | /** |
208 | * Glob wrapper which is guaranteed to return an array. | |
209 | * | |
210 | * The documentation for glob() says, "On some systems it is impossible to | |
211 | * distinguish between empty match and an error." Anecdotally, the return | |
212 | * result for an empty match is sometimes array() and sometimes FALSE. | |
213 | * This wrapper provides consistency. | |
214 | * | |
215 | * @link http://php.net/glob | |
216 | * @param string $pattern | |
7620445e | 217 | * |
d9cc2e0d | 218 | * @return array |
6bc5e930 | 219 | */ |
5e4accea | 220 | function _afform_admin_civix_glob($pattern) { |
6bc5e930 | 221 | $result = glob($pattern); |
7620445e | 222 | return is_array($result) ? $result : []; |
6bc5e930 TO |
223 | } |
224 | ||
225 | /** | |
226 | * Inserts a navigation menu item at a given place in the hierarchy. | |
227 | * | |
228 | * @param array $menu - menu hierarchy | |
229 | * @param string $path - path to parent of this item, e.g. 'my_extension/submenu' | |
230 | * 'Mailing', or 'Administer/System Settings' | |
231 | * @param array $item - the item to insert (parent/child attributes will be | |
232 | * filled for you) | |
7620445e TO |
233 | * |
234 | * @return bool | |
6bc5e930 | 235 | */ |
5e4accea | 236 | function _afform_admin_civix_insert_navigation_menu(&$menu, $path, $item) { |
6bc5e930 TO |
237 | // If we are done going down the path, insert menu |
238 | if (empty($path)) { | |
7620445e TO |
239 | $menu[] = [ |
240 | 'attributes' => array_merge([ | |
6bc5e930 TO |
241 | 'label' => CRM_Utils_Array::value('name', $item), |
242 | 'active' => 1, | |
7620445e TO |
243 | ], $item), |
244 | ]; | |
6bc5e930 TO |
245 | return TRUE; |
246 | } | |
247 | else { | |
248 | // Find an recurse into the next level down | |
249 | $found = FALSE; | |
250 | $path = explode('/', $path); | |
251 | $first = array_shift($path); | |
252 | foreach ($menu as $key => &$entry) { | |
253 | if ($entry['attributes']['name'] == $first) { | |
254 | if (!isset($entry['child'])) { | |
7620445e | 255 | $entry['child'] = []; |
6bc5e930 | 256 | } |
5e4accea | 257 | $found = _afform_admin_civix_insert_navigation_menu($entry['child'], implode('/', $path), $item); |
6bc5e930 TO |
258 | } |
259 | } | |
260 | return $found; | |
261 | } | |
262 | } | |
263 | ||
264 | /** | |
265 | * (Delegated) Implements hook_civicrm_navigationMenu(). | |
266 | */ | |
5e4accea | 267 | function _afform_admin_civix_navigationMenu(&$nodes) { |
7620445e | 268 | if (!is_callable(['CRM_Core_BAO_Navigation', 'fixNavigationMenu'])) { |
5e4accea | 269 | _afform_admin_civix_fixNavigationMenu($nodes); |
6bc5e930 TO |
270 | } |
271 | } | |
272 | ||
273 | /** | |
274 | * Given a navigation menu, generate navIDs for any items which are | |
275 | * missing them. | |
276 | */ | |
5e4accea | 277 | function _afform_admin_civix_fixNavigationMenu(&$nodes) { |
6bc5e930 TO |
278 | $maxNavID = 1; |
279 | array_walk_recursive($nodes, function($item, $key) use (&$maxNavID) { | |
280 | if ($key === 'navID') { | |
281 | $maxNavID = max($maxNavID, $item); | |
282 | } | |
283 | }); | |
5e4accea | 284 | _afform_admin_civix_fixNavigationMenuItems($nodes, $maxNavID, NULL); |
6bc5e930 TO |
285 | } |
286 | ||
5e4accea | 287 | function _afform_admin_civix_fixNavigationMenuItems(&$nodes, &$maxNavID, $parentID) { |
6bc5e930 TO |
288 | $origKeys = array_keys($nodes); |
289 | foreach ($origKeys as $origKey) { | |
290 | if (!isset($nodes[$origKey]['attributes']['parentID']) && $parentID !== NULL) { | |
291 | $nodes[$origKey]['attributes']['parentID'] = $parentID; | |
292 | } | |
293 | // If no navID, then assign navID and fix key. | |
294 | if (!isset($nodes[$origKey]['attributes']['navID'])) { | |
295 | $newKey = ++$maxNavID; | |
296 | $nodes[$origKey]['attributes']['navID'] = $newKey; | |
297 | $nodes[$newKey] = $nodes[$origKey]; | |
298 | unset($nodes[$origKey]); | |
299 | $origKey = $newKey; | |
300 | } | |
301 | if (isset($nodes[$origKey]['child']) && is_array($nodes[$origKey]['child'])) { | |
5e4accea | 302 | _afform_admin_civix_fixNavigationMenuItems($nodes[$origKey]['child'], $maxNavID, $nodes[$origKey]['attributes']['navID']); |
6bc5e930 TO |
303 | } |
304 | } | |
305 | } | |
306 | ||
6bc5e930 TO |
307 | /** |
308 | * (Delegated) Implements hook_civicrm_entityTypes(). | |
309 | * | |
310 | * Find any *.entityType.php files, merge their content, and return. | |
311 | * | |
7620445e | 312 | * @link https://docs.civicrm.org/dev/en/latest/hooks/hook_civicrm_entityTypes |
6bc5e930 | 313 | */ |
5e4accea | 314 | function _afform_admin_civix_civicrm_entityTypes(&$entityTypes) { |
d9cc2e0d | 315 | $entityTypes = array_merge($entityTypes, []); |
6bc5e930 | 316 | } |