README.md
[civicrm-core.git] / ext / afform / afform.php
1 <?php
2
3 require_once 'afform.civix.php';
4 use CRM_Afform_ExtensionUtil as E;
5
6 function _afform_fields() {
7 return ['name', 'title', 'description', 'requires', 'layout', 'server_route', 'client_route'];
8 }
9
10 /**
11 * Filter the content of $params to only have supported afform fields.
12 *
13 * @param array $params
14 * @return array
15 */
16 function _afform_fields_filter($params) {
17 $result = array();
18 foreach (_afform_fields() as $field) {
19 if (isset($params[$field])) {
20 $result[$field] = $params[$field];
21 }
22 }
23 return $result;
24 }
25
26 /**
27 * Implements hook_civicrm_config().
28 *
29 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_config
30 */
31 function afform_civicrm_config(&$config) {
32 _afform_civix_civicrm_config($config);
33 }
34
35 /**
36 * Implements hook_civicrm_xmlMenu().
37 *
38 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_xmlMenu
39 */
40 function afform_civicrm_xmlMenu(&$files) {
41 _afform_civix_civicrm_xmlMenu($files);
42 }
43
44 /**
45 * Implements hook_civicrm_install().
46 *
47 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_install
48 */
49 function afform_civicrm_install() {
50 _afform_civix_civicrm_install();
51 }
52
53 /**
54 * Implements hook_civicrm_postInstall().
55 *
56 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_postInstall
57 */
58 function afform_civicrm_postInstall() {
59 _afform_civix_civicrm_postInstall();
60 }
61
62 /**
63 * Implements hook_civicrm_uninstall().
64 *
65 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_uninstall
66 */
67 function afform_civicrm_uninstall() {
68 _afform_civix_civicrm_uninstall();
69 }
70
71 /**
72 * Implements hook_civicrm_enable().
73 *
74 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_enable
75 */
76 function afform_civicrm_enable() {
77 _afform_civix_civicrm_enable();
78 }
79
80 /**
81 * Implements hook_civicrm_disable().
82 *
83 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_disable
84 */
85 function afform_civicrm_disable() {
86 _afform_civix_civicrm_disable();
87 }
88
89 /**
90 * Implements hook_civicrm_upgrade().
91 *
92 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_upgrade
93 */
94 function afform_civicrm_upgrade($op, CRM_Queue_Queue $queue = NULL) {
95 return _afform_civix_civicrm_upgrade($op, $queue);
96 }
97
98 /**
99 * Implements hook_civicrm_managed().
100 *
101 * Generate a list of entities to create/deactivate/delete when this module
102 * is installed, disabled, uninstalled.
103 *
104 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_managed
105 */
106 function afform_civicrm_managed(&$entities) {
107 _afform_civix_civicrm_managed($entities);
108 }
109
110 /**
111 * Implements hook_civicrm_caseTypes().
112 *
113 * Generate a list of case-types.
114 *
115 * Note: This hook only runs in CiviCRM 4.4+.
116 *
117 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_caseTypes
118 */
119 function afform_civicrm_caseTypes(&$caseTypes) {
120 _afform_civix_civicrm_caseTypes($caseTypes);
121 }
122
123 /**
124 * Implements hook_civicrm_angularModules().
125 *
126 * Generate a list of Angular modules.
127 *
128 * Note: This hook only runs in CiviCRM 4.5+. It may
129 * use features only available in v4.6+.
130 *
131 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_angularModules
132 */
133 function afform_civicrm_angularModules(&$angularModules) {
134 _afform_civix_civicrm_angularModules($angularModules);
135
136 $scanner = new CRM_Afform_AfformScanner();
137 $names = array_keys($scanner->findFilePaths());
138 foreach ($names as $name) {
139 $meta = $scanner->getMeta($name);
140 $angularModules[_afform_angular_module_name($name)] = [
141 'ext' => E::LONG_NAME,
142 'js' => ['assetBuilder://afform.js?name=' . urlencode($name)],
143 'requires' => $meta['requires'],
144 'basePages' => [],
145 ];
146
147 // FIXME: The HTML layout template is embedded in the JS asset.
148 // This works at runtime for basic usage, but it bypasses
149 // the hook_alterAngular infrastructure, and I'm not sure translation works.
150 // We should update core so that 'partials' can be specified more dynamically.
151 }
152 }
153
154 /**
155 * Implements hook_civicrm_alterSettingsFolders().
156 *
157 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_alterSettingsFolders
158 */
159 function afform_civicrm_alterSettingsFolders(&$metaDataFolders = NULL) {
160 _afform_civix_civicrm_alterSettingsFolders($metaDataFolders);
161 }
162
163 /**
164 * Implements hook_civicrm_entityTypes().
165 *
166 * Declare entity types provided by this module.
167 *
168 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_entityTypes
169 */
170 function afform_civicrm_entityTypes(&$entityTypes) {
171 _afform_civix_civicrm_entityTypes($entityTypes);
172 }
173
174 // --- Functions below this ship commented out. Uncomment as required. ---
175
176 /**
177 * Implements hook_civicrm_buildAsset().
178 */
179 function afform_civicrm_buildAsset($asset, $params, &$mimeType, &$content) {
180 if ($asset !== 'afform.js') {
181 return;
182 }
183
184 if (empty($params['name'])) {
185 throw new RuntimeException("Missing required parameter: afform.js?name=NAME");
186 }
187
188 $name = $params['name'];
189 $meta = civicrm_api3('Afform', 'getsingle', ['name' => $name]);
190 $scanner = new CRM_Afform_AfformScanner();
191
192 $smarty = CRM_Core_Smarty::singleton();
193 $smarty->assign('afform', [
194 'camel' => _afform_angular_module_name($name),
195 'meta' => $meta,
196 'layout' => file_get_contents($scanner->findFilePath($name, 'layout.html'))
197 ]);
198 $mimeType = 'text/javascript';
199 $content = $smarty->fetch('afform/FormAsDirective.tpl');
200 }
201
202 /**
203 * Implements hook_civicrm_alterMenu().
204 */
205 function afform_civicrm_alterMenu(&$items) {
206 $scanner = new CRM_Afform_AfformScanner();
207 foreach ($scanner->getMetas() as $name => $meta) {
208 if (!empty($meta['server_route'])) {
209 $items[$meta['server_route']] = [
210 'page_callback' => 'CRM_Afform_Page_AfformBase',
211 'page_arguments' => 'afform=' . urlencode($name),
212 'title' => CRM_Utils_Array::value('title', $meta, ''),
213 'access_arguments' => [['access CiviCRM'], 'and'] // FIXME
214 ];
215 }
216 }
217 }
218
219 /**
220 * @param string $name
221 * Ex: fooBar
222 * @param string $format
223 * 'camel' or 'dash'.
224 * @return string
225 * Ex: 'FooBar' or 'foo-bar'.
226 */
227 function _afform_angular_module_name($name, $format = 'camel') {
228 switch ($format) {
229 case 'camel':
230 return 'afform' . strtoupper($name{0}) . substr($name, 1);
231
232 case 'dash':
233 $camel = _afform_angular_module_name($name, 'camel');
234 return strtolower(implode('-', array_filter(preg_split('/(?=[A-Z])/', $camel))));
235
236 default:
237 throw new \Exception("Unrecognized format");
238 }
239 }
240
241 /**
242 * Implements hook_civicrm_preProcess().
243 *
244 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_preProcess
245 *
246 function afform_civicrm_preProcess($formName, &$form) {
247
248 } // */
249
250 /**
251 * Implements hook_civicrm_navigationMenu().
252 *
253 * @link http://wiki.civicrm.org/confluence/display/CRMDOC/hook_civicrm_navigationMenu
254 *
255 function afform_civicrm_navigationMenu(&$menu) {
256 _afform_civix_insert_navigation_menu($menu, 'Mailings', array(
257 'label' => E::ts('New subliminal message'),
258 'name' => 'mailing_subliminal_message',
259 'url' => 'civicrm/mailing/subliminal',
260 'permission' => 'access CiviMail',
261 'operator' => 'OR',
262 'separator' => 0,
263 ));
264 _afform_civix_navigationMenu($menu);
265 } // */