-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro.admin.inc
More file actions
506 lines (445 loc) · 18 KB
/
micro.admin.inc
File metadata and controls
506 lines (445 loc) · 18 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
<?php
// $Id$
/**
* @file
* Micro type editing UI.
*/
/**
* Displays the micro type admin overview page.
*/
function micro_overview_types() {
$types = micro_type_get_types();
$names = micro_type_get_names();
$field_ui = module_exists('field_ui');
$header = array(t('Name'), array('data' => t('Operations'), 'colspan' => $field_ui ? '4' : '2'));
$rows = array();
foreach ($names as $key => $name) {
$type = $types[$key];
$type_url_str = str_replace('_', '-', $type->machine_name);
$row = array(theme('micro_admin_overview', array('name' => $name, 'machine_name' => $type)));
if (micro_hook($type->machine_name, 'form')) {
// Set the edit column.
$row[] = array('data' => l(t('edit'), 'admin/structure/micro/manage/' . $type_url_str));
if ($field_ui) {
// Manage fields.
$row[] = array('data' => l(t('manage fields'), 'admin/structure/micro/manage/' . $type_url_str . '/fields'));
// Display fields.
$row[] = array('data' => l(t('manage display'), 'admin/structure/micro/manage/' . $type_url_str . '/display'));
}
// Set the delete column.
if ($type->module == 'micro') {
$row[] = array('data' => l(t('delete'), 'admin/structure/micro/manage/' . $type_url_str . '/delete'));
}
else {
$row[] = array('data' => '');
}
}
else {
// For those micro types managed elsewhere
for ($i=0; $i<4; $i++) {
$row[] = array('data' => '');
}
}
$rows[] = $row;
}
$build['micro_table'] = array(
'#theme' => 'table',
'#header' => $header,
'#rows' => $rows,
'#empty' => t('No micro types available. <a href="@link">Add micro type</a>.', array('@link' => url('admin/structure/micro/add'))),
);
return $build;
}
/**
* Returns HTML for a micro type description for the micro type admin overview page.
*
* @param $variables
* An associative array containing:
* - name: The human-readable name of the micro type.
* - type: An object containing the 'type' (machine name) and 'description' of
* the micro type.
*
* @ingroup themeable
*/
function theme_micro_admin_overview($variables) {
$name = $variables['name'];
$type = $variables['machine_name'];
$output = check_plain($name);
$output .= ' <small> (Machine name: ' . check_plain($type->machine_name) . ')</small>';
$output .= '<div class="description">' . filter_xss_admin($type->description) . '</div>';
return $output;
}
/**
* Generates the micro type editing form.
*/
function micro_type_form($form, &$form_state, $type = NULL) {
if (!isset($type->mtid)) {
// This is a new type, managed by micro module.
$type = micro_type_set_defaults(array('module' => 'micro'));
}
// Make the type object available to implementations of hook_form_alter.
$form['#micro_type'] = $type;
$form['name'] = array(
'#title' => t('Name'),
'#type' => 'textfield',
'#default_value' => $type->name,
'#description' => t('The human-readable name of this micro type. This text will be displayed as part of the list on the <em>Add new micro</em> page. It is recommended that this name begin with a capital letter and contain only letters, numbers, and spaces. This name must be unique.'),
'#required' => TRUE,
'#size' => 30,
);
$form['machine_name'] = array(
'#type' => 'machine_name',
'#default_value' => $type->machine_name,
'#maxlength' => 32,
'#disabled' => !isset($type->machine_name),
'#machine_name' => array(
'exists' => 'micro_type_load',
),
'#description' => t('A unique machine-readable name for this micro type. It must only contain lowercase letters, numbers, and underscores. This name will be used for constructing the URL of the %micro-add page, in which underscores will be converted into hyphens.', array(
'%micro-add' => t('Add new micro'),
)),
);
$form['description'] = array(
'#title' => t('Description'),
'#type' => 'textarea',
'#default_value' => $type->description,
'#description' => t('Describe this micro type. The text will be displayed on the <em>Add new micro</em> page.'),
);
$entities = micro_get_entities();
$options_entity = micro_get_entities(FALSE);
$selected = isset($type->entity) ? $type->entity : (isset($form_state['values']['entity']) ? $form_state['values']['entity'] : key($options_entity));
$form['entity'] = array(
'#type' => 'select',
'#title' => t('Entity'),
'#options' => $options_entity,
'#default_value' => $selected,
'#disabled' => isset($type->entity),
'#ajax' => array(
'callback' => 'micro_dependent_dropdown_callback',
'wrapper' => 'ajax-replace',
),
);
//TODO - set error if no bundle / hide if site / if reload, why does second have wrong values?
$form['bundles'] = array(
'#type' => 'checkboxes',
'#title' => t('Bundles'),
'#prefix' => '<div id="ajax-replace">',
'#options' => $entities[isset($type->entity) ? $type->entity : (isset($form_state['values']['entity']) ? $form_state['values']['entity'] : $selected)]['bundles'],
'#default_value' => isset($type->bundles) ? $type->bundles : (isset($form_state['values']['bundles']) ? $form_state['values']['bundles'] : $entities['site']['bundles']),
);
$form['format'] = array(
'#title' => t("Display types"),
'#suffix' => '</div>',
'#type' => 'fieldset',
'#description' => t('How the micro form and items will be displayed on the attached entity.'),
);
//TODO - check type if set (after first submit)
//TODO - force NOT simple page if other than site
if ((isset($type->entity) && $type->entity != 'site') || (isset($form_state['values']['entity']) && $form_state['values']['entity'] != 'site')) {
$format = variable_get('micro_format_' . $type->machine_name);
$strings = variable_get('micro_format_strings_' . $type->machine_name);
$form['format']['string_create'] = array(
'#title' => t('Create'),
'#type' => 'textfield',
'#default_value' => isset($strings['string_create']) ? $strings['string_create'] : '',
'#description' => t('The label used for creating items of this micro type, if the display format requires it.'),
'#required' => TRUE,
'#size' => 30,
);
$form['format']['string_delete'] = array(
'#title' => t('Delete'),
'#type' => 'textfield',
'#default_value' => isset($strings['string_delete']) ? $strings['string_delete'] : '',
'#description' => t('The label used for deleting items of this micro type, if the display format requires it.'),
'#required' => TRUE,
'#size' => 30,
);
foreach ($entities[isset($type->entity) ? $type->entity : $form_state['values']['entity']]['modes'] as $mode => $name) {
$form['format'][$mode] = array(
'#title' => t('Format for view mode "@name"', array('@name' => $name)),
'#type' => 'fieldset',
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#description' => t('How the micro form and items will be displayed on the attached entity.'),
);
$form['format'][$mode]['display_' . $mode] = array(
'#type' => 'select',
'#title' => t('Display type'),
'#options' => _micro_get_display_options($entities, $selected, isset($type->machine_name) ? $type->machine_name : (isset($form_state['values']['machine_name']) ? $form['values']['machine_name'] : NULL)),
'#default_value' => isset($format['display_' . $mode]) ? $format['display_' . $mode] : 'simple_page_display',
);
//TODO - check type if set (after first submit)
//TODO - force NOT simple page if other than site
$form['format'][$mode]['input_' . $mode] = array(
'#type' => 'select',
'#title' => t('Input type'),
'#options' => _micro_get_input_options($entities, $selected, isset($type->machine_name) ? $type->machine_name : (isset($form_state['values']['machine_name']) ? $form['values']['machine_name'] : NULL)),
'#default_value' => isset($format['input_' . $mode]) ? $format['input_' . $mode] : 'simple_page_input',
);
}
}
else {
$form['format']['display'] = array(
'#type' => 'select',
'#title' => t('Display type'),
'#options' => array('simple_page_display' => t('Simple page display')),
'#default_value' => 'simple_page_display',
'#disabled' => TRUE,
);
//TODO - check type if set (after first submit)
//TODO - force NOT simple page if other than site
$form['format']['input'] = array(
'#type' => 'select',
'#title' => t('Input type'),
'#options' => array('simple_page_input' => t('Simple page input')),
'#default_value' => 'simple_page_input',
'#disabled' => TRUE,
);
}
$form['additional_settings'] = array(
'#type' => 'vertical_tabs',
);
$form['workflow'] = array(
'#type' => 'fieldset',
'#title' => t('Workflow settings'),
'#collapsible' => TRUE,
'#group' => 'additional_settings',
);
// Disable if 'site'
$form['workflow']['micro_view_page'] = array(
'#type' => 'checkbox',
'#title' => t('View page'),
'#description' => t('Whether items from this micro type will have their own view page (uri). If not, they will redirect to their parent entity. Micro items attached to the site will always have a view page.'),
'#options' => array('0' => FALSE, '1' => TRUE),
/*
*'#disabled' => (isset($form_state['values']['entity']) && $form_state['values']['entity'] == 'site') ? TRUE : FALSE,
*/
'#default_value' => variable_get('micro_view_page_' . $type->machine_name, TRUE)
);
$form['workflow']['micro_delete_confirmation'] = array(
'#type' => 'checkbox',
'#title' => t('Delete confirmation'),
'#description' => t('Whether items from this micro type have a \'delete confirmation\' page.'),
'#options' => array('0' => FALSE, '1' => TRUE),
'#default_value' => variable_get('micro_delete_confirm_' . $type->machine_name, FALSE),
);
$form['module'] = array(
'#type' => 'value',
'#value' => $type->module,
);
$form['locked'] = array(
'#type' => 'value',
'#value' => $type->isLocked(),
);
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save micro type'),
'#weight' => 40,
);
if ($type->module == 'micro') {
if (!empty($type->mtid)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete micro type'),
'#weight' => 45,
);
}
}
return $form;
}
/**
* Selects just the second dropdown to be returned for re-rendering
*
* @return renderable array
*/
function micro_dependent_dropdown_callback($form, $form_state) {
return array($form['bundles'], $form['format']);
}
/**
* Helper function to populate the display options
*
* @param key. This will determine which set of options is returned.
*
* @return array of options
*/
function _micro_get_display_options($entities, $selected, $type) {
$options = array();
$options['none'] = 'None';
$displays = micro_display_types();
foreach ($displays as $key => $value) {
if (empty($value['micro_types']) || in_array($type, $value['micro_types'])) {
$options[$key] = $value['label'];
}
}
return $options;
}
/**
* Helper function to populate the input options
*
* @param key. This will determine which set of options is returned.
*
* @return array of options
*/
function _micro_get_input_options($entities, $selected, $type) {
$options = array();
$options['none'] = 'None';
$inputs = micro_input_types();
foreach ($inputs as $key => $value) {
if (empty($value['micro_types']) || in_array($type, $value['micro_types'])) {
$options[$key] = $value['label'];
}
}
return $options;
}
/**
* Validates the micro type submission form generated by micro_type_form().
*/
function micro_type_form_validate($form, &$form_state) {
$type = new stdClass();
$type->machine_name = trim($form_state['values']['machine_name']);
$type->name = trim($form_state['values']['name']);
$types = micro_type_get_names();
// 'theme' conflicts with theme_micro_form().
// '0' is invalid, since elsewhere we check it using empty().
if (in_array($type->machine_name, array('0', 'theme'))) {
form_set_error('machine_name', t("Invalid machine-readable name. Enter a name other than %invalid.", array('%invalid' => $type->machine_name)));
}
$names = array_flip($types);
//TODO - validate whether there is an existing name
if (isset($names[$type->name]) && $names[$type->name] != $type->machine_name) {
form_set_error('name', t('The human-readable name %name is already taken.', array('%name' => $type->name)));
}
if (isset($form_state['expiration']) && !isset($form_state['expiration_time'])) {
form_set_error('expiration_time', t('You must specify an expiration time.'));
}
}
/**
* Implements hook_form_submit().
*/
function micro_type_form_submit($form, &$form_state) {
$op = isset($form_state['values']['op']) ? $form_state['values']['op'] : '';
$type = $form['#micro_type'];
if (isset($type->mtid)) {
$type->is_new = 0;
}
$type->machine_name = trim($form_state['values']['machine_name']);
$type->name = trim($form_state['values']['name']);
$type->description = $form_state['values']['description'];
$type->entity = $form_state['values']['entity'];
foreach ($form_state['values']['bundles'] as $key => $value) {
if (empty($value)) {
unset($form_state['values']['bundles'][$key]);
}
}
$type->bundles = $form_state['values']['bundles'];
foreach ($form_state['values'] as $key => $value) {
if (substr($key, 0, 5) == 'input' || substr($key, 0, 7) == 'display') {
$format[$key] = $value;
unset($form_state['values'][$key]);
}
}
if (isset($format)) {
variable_set('micro_format_' . $type->machine_name, $format);
}
foreach ($form_state['values'] as $key => $value) {
if (substr($key, 0, 6) == 'string') {
$strings[$key] = $value;
unset($form_state['values'][$key]);
}
}
if (isset($strings)) {
variable_set('micro_format_strings_' . $type->machine_name, $strings);
}
// TODO - force this in form
if ($form_state['values']['entity'] == 'site') {
$form_state['values']['micro_view_page'] = TRUE;
}
$type->module = !empty($form_state['values']['module']) ? $form_state['values']['module'] : 'micro';
if (isset($form['#micro_type']->module)) {
$type->module = $form['#micro_type']->module;
}
if ($op == t('Delete micro type')) {
$form_state['redirect'] = 'admin/structure/micro/manage/' . str_replace('_', '-', $type->old_type) . '/delete';
return;
}
$variables = $form_state['values'];
// Remove everything that's been saved already - whatever's left is assumed
// to be a persistent variable.
foreach ($variables as $key => $value) {
if (isset($type->$key)) {
unset($variables[$key]);
}
}
unset($variables['form_token'], $variables['op'], $variables['submit'], $variables['delete'], $variables['reset'], $variables['form_id'], $variables['form_build_id']);
// Save or reset persistent variable values.
foreach ($variables as $key => $value) {
$variable = $key . '_' . $type->machine_name;
if (is_array($value)) {
$value = array_keys(array_filter($value));
}
variable_set($variable, $value);
}
// Saving the micro type after saving the variables allows modules to act
// on those variables via hook_micro_type_insert().
$status = micro_type_save($type);
micro_types_rebuild();
menu_rebuild();
$t_args = array('%name' => $type->name);
if ($status == SAVED_UPDATED) {
drupal_set_message(t('The micro type %name has been updated.', $t_args));
}
elseif ($status == SAVED_NEW) {
drupal_set_message(t('The micro type %name has been added.', $t_args));
watchdog('micro', 'Added micro type %name.', $t_args, WATCHDOG_NOTICE, l(t('view'), 'admin/structure/micro'));
}
$form_state['redirect'] = 'admin/structure/micro';
return;
}
/**
* Resets all of the relevant fields of a module-defined micro type to their
* default values.
*
* @param $type
* The micro type to reset. The micro type is passed back by reference with its
* resetted values. If there is no module-defined info for this micro type,
* then nothing happens.
*/
function micro_type_reset($type) {
$info_array = module_invoke_all('micro_info');
if (isset($info_array[$type->machine_name])) {
$info_array[$type->machine_name]['machine_name'] = $type->machine_name;
$info = micro_type_set_defaults($info_array[$type->machine_name]);
foreach ($info as $field => $value) {
$type->$field = $value;
}
}
}
/**
* Menu callback; delete a single micro type.
*/
function micro_type_delete_confirm($form, &$form_state, $type) {
$form['machine_name'] = array('#type' => 'value', '#value' => $type->machine_name);
$form['name'] = array('#type' => 'value', '#value' => $type->name);
$message = t('Are you sure you want to delete the micro type %type?', array('%type' => $type->name));
$caption = '';
$num_micros = db_query("SELECT COUNT(*) FROM {micro} WHERE mtid = :id", array(':id' => $type->mtid))->fetchField();
if ($num_micros) {
$caption .= '<p>' . format_plural($num_micros, '%type is used by 1 piece of content on your site. If you remove this micro type, you will not be able to edit the %type micro and it may not display correctly.', '%type is used by @count pieces of content on your site. If you remove %type, you will not be able to edit the %type micro and it may not display correctly.', array('%type' => $type->name)) . '</p>';
}
$caption .= '<p>' . t('This action cannot be undone.') . '</p>';
return confirm_form($form, $message, 'admin/structure/micro', $caption, t('Delete'));
}
/**
* Process micro type delete confirm submissions.
*/
function micro_type_delete_confirm_submit($form, &$form_state) {
micro_type_delete($form_state['values']['machine_name']);
$t_args = array('%name' => $form_state['values']['name']);
drupal_set_message(t('The micro type %name has been deleted.', $t_args));
watchdog('menu', 'Deleted micro type %name.', $t_args, WATCHDOG_NOTICE);
micro_types_rebuild();
menu_rebuild();
$form_state['redirect'] = 'admin/structure/micro';
return;
}