-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmicro.pages.inc
More file actions
268 lines (236 loc) · 9.05 KB
/
micro.pages.inc
File metadata and controls
268 lines (236 loc) · 9.05 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
<?php
// $Id$
/**
* @file
* Page callbacks for adding, editing, and deleting micro items.
*/
/**
* Menu callback; presents the micro editing form, or redirects to delete confirmation.
*/
function micro_page_edit($micro) {
$type = micro_type_get_type($micro);
drupal_set_title(t('Edit @type @mid', array('@type' => $type->name, '@mid' => $micro->mid)));
return drupal_get_form($type->machine_name . '_micro_form', $micro);
}
function micro_add_page() {
$item = menu_get_item();
$content = system_admin_menu_block($item);
// Bypass the micro/add listing if only one content type is available.
if (count($content) == 1) {
$item = array_shift($content);
drupal_goto($item['href']);
}
return theme('micro_add_list', array('content' => $content));
}
/**
* Returns HTML for a list of available micro types for micro creation.
*
* @param $variables
* An associative array containing:
* - content: An array of content types.
*
* @ingroup themeable
*/
function theme_micro_add_list($variables) {
$content = $variables['content'];
$output = '';
if ($content) {
$output = '<dl class="micro-type-list">';
foreach ($content as $item) {
$output .= '<dt>' . l($item['title'], $item['href'], $item['localized_options']) . '</dt>';
$output .= '<dd>' . filter_xss_admin($item['description']) . '</dd>';
}
$output .= '</dl>';
}
else {
$output = '<p>' . t('You have not created any micro types yet. Go to the <a href="@create-micro">micro type creation page</a> to add a new micro type.', array('@create-micro' => url('admin/structure/micro/add'))) . '</p>';
}
return $output;
}
/**
* Present a micro submission form or a set of links to such forms.
*/
function micro_add($micro_type, $id, $is_page = TRUE) {
global $user;
$type = micro_type_get_type($micro_type);
// If a micro type has been specified, validate its existence.
if (isset($type)) {
// Initialize settings:
$micro = micro_create(array('uid' => $user->uid, 'mtid' => $type->mtid, 'eid' => $id, 'type' => $type->machine_name, 'name' => $user->name));
if ($is_page) {
drupal_set_title(t('Create @name', array('@name' => $type->name)), PASS_THROUGH);
}
$output = drupal_get_form($type->machine_name . '_micro_form', $micro);
}
return $output;
}
function micro_form_validate($form, &$form_state) {
// $form_state['micro'] contains the actual entity being edited, but we must
// not update it with form values that have not yet been validated, so we
// create a pseudo-entity to use during validation.
$micro = (object) $form_state['values'];
micro_validate($micro, $form, $form_state);
entity_form_field_validate('micro', $form, $form_state);
}
/**
* Generate the micro add/edit form array.
*/
function micro_form($form, &$form_state, $micro) {
global $user;
// During initial form build, add the micro entity to the form state for use
// during form building and processing. During a rebuild, use what is in the
// form state.
if (!isset($form_state['micro'])) {
micro_object_prepare($micro);
$form_state['micro'] = $micro;
}
else {
$micro = $form_state['micro'];
}
$micro_type = micro_type_get_type($micro);
$form['#micro'] = $micro;
$form['#micro_type'] = $micro_type;
$form['#micro_edit_form'] = TRUE;
$form['#attributes']['class'][] = 'micro-form';
if (!empty($micro->mtid)) {
$form['#attributes']['class'][] = 'micro-' . $micro_type->machine_name . '-form';
}
// Basic micro information.
// These elements are just values so they are not even sent to the client.
foreach (array('mid', 'uid', 'eid', 'created', 'type', 'mtid') as $key) {
$form[$key] = array(
'#type' => 'value',
'#value' => isset($micro->$key) ? $micro->$key : NULL,
);
}
// Invoke hook_form() to get the micro-specific bits. Can't use micro_invoke(),
// because hook_form() needs to be able to receive $form_state by reference.
// @todo hook_form() implementations are unable to add #validate or #submit
// handlers to the form buttons below. Remove hook_form() entirely.
$function = micro_type_get_module($micro) . '_micro_form';
if (function_exists($function) && ($extra = $function($micro, $form_state))) {
$form = array_merge_recursive($form, $extra);
}
// Add the buttons.
$form['actions'] = array('#type' => 'actions');
$form['actions']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save'),
'#weight' => 5,
'#submit' => array('micro_form_submit'),
);
if (!empty($micro->mid) && micro_access('delete', $micro)) {
$form['actions']['delete'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
'#weight' => 15,
'#submit' => array('micro_form_delete_submit'),
);
}
// This form uses a button-level #submit handler for the form's main submit
// action. micro_form_submit() manually invokes all form-level #submit handlers
// of the form. Without explicitly setting #submit, Form API would auto-detect
// micro_form_submit() as submit handler, but that is the button-level #submit
// handler for the 'Save' action.
$form['#validate'][] = 'micro_form_validate';
if (!isset($form['#submit']) && function_exists($micro_type->machine_name . '_micro_form_submit')) {
$form['#submit'][] = $micro_type->machine_name . '_micro_form_submit';
}
$form += array('#submit' => array());
$form['#builder_function'] = 'micro_form_submit_build_micro';
field_attach_form('micro', $micro, $form, $form_state);
return $form;
}
/**
* Button submit function: handle the 'Delete' button on the micro form.
*/
function micro_form_delete_submit($form, &$form_state) {
$destination = array();
if (isset($_GET['destination'])) {
$destination = drupal_get_destination();
unset($_GET['destination']);
}
$micro = $form['#micro'];
$form_state['redirect'] = array('micro/' . $micro->mid . '/delete', array('query' => $destination));
}
function micro_form_submit($form, &$form_state) {
$micro = $form['#builder_function']($form, $form_state);
$micro_type = $form['#micro_type'];
$insert = empty($micro->mid);
micro_save($micro);
$micro_link = l(t('view'), 'micro/' . $micro->mid);
$watchdog_args = array('@type' => $micro_type->machine_name, '%mid' => $micro->mid);
$t_args = array('@type' => micro_type_get_name($micro), '%mid' => $micro->mid);
if ($insert) {
watchdog('content', '@type: added %mid.', $watchdog_args, WATCHDOG_NOTICE, $micro_link);
drupal_set_message(t('@type %mid has been created.', $t_args));
}
else {
watchdog('content', '@type: updated %mid.', $watchdog_args, WATCHDOG_NOTICE, $micro_link);
drupal_set_message(t('@type %mid has been updated.', $t_args));
}
if ($micro->mid) {
$form_state['values']['mid'] = $micro->mid;
$form_state['mid'] = $micro->mid;
$form_state['redirect'] = 'micro/' . $micro->mid;
}
else {
// In the unlikely case something went wrong on save, the micro will be rebuilt
drupal_set_message(t('The post could not be saved.'), 'error');
$form_state['rebuild'] = TRUE;
}
// Clear the page and block caches.
cache_clear_all();
}
/**
* Updates the form state's micro entity by processing this submission's values.
*
* This is the default #builder_function for the micro form. It is called
* during the "Save" submit handler to retrieve the entity to
* save or preview. This function can also be called by a "Next" button of a
* wizard to update the form state's entity with the current step's values
* before proceeding to the next step.
*
* @see micro_form()
*/
function micro_form_submit_build_micro($form, &$form_state) {
unset($form_state['submit_handlers']);
form_execute_handlers('submit', $form, $form_state);
$micro = $form_state['micro'];
entity_form_submit_build_entity('micro', $micro, $form, $form_state);
micro_submit($micro);
foreach (module_implements('micro_submit') as $module) {
$function = $module . '_micro_submit';
$function($micro, $form, $form_state);
}
return $micro;
}
/**
* Menu callback -- ask for confirmation of micro deletion
*/
function micro_delete_confirm($form, &$form_state, $micro) {
$form['#micro'] = $micro;
$type = micro_type_get_type($micro);
// Always provide entity id in the same form key as in the entity edit form.
$form['mid'] = array('#type' => 'value', '#value' => $micro->mid);
return confirm_form($form,
t('Are you sure you want to delete @type %mid?', array('@type' => $type->name, '%mid' => $micro->mid)),
'micro/' . $micro->mid,
t('This action cannot be undone.'),
t('Delete'),
t('Cancel')
);
}
/**
* Execute micro deletion
*/
function micro_delete_confirm_submit($form, &$form_state) {
if ($form_state['values']['confirm']) {
$micro = micro_load($form_state['values']['mid']);
$micro_type = micro_type_get_type($micro);
micro_delete($form_state['values']['mid']);
watchdog('content', '@type: deleted %mid.', array('@type' => $micro_type->machine_name, '%mid' => $micro->mid));
drupal_set_message(t('@type %mid has been deleted.', array('@type' => $micro_type->name, '%mid' => $micro->mid)));
}
$form_state['redirect'] = '<front>';
}