/[drupal]/contributions/modules/flexinode/flexinode_admin.module
ViewVC logotype

Contents of /contributions/modules/flexinode/flexinode_admin.module

Parent Directory Parent Directory | Revision Log Revision Log | View Revision Graph Revision Graph


Revision 1.5 - (show annotations) (download) (as text)
Sat Jan 27 14:46:39 2007 UTC (2 years, 10 months ago) by ber
Branch: MAIN
CVS Tags: DRUPAL-4-7--0-1, DRUPAL-4-7--0-3, DRUPAL-4-7--0-2, HEAD
Branch point for: DRUPAL-4-7
Changes since 1.4: +0 -14 lines
File MIME type: text/x-php
Moved view/listing settings from admin to miniview.
1 <?php
2 // $Id: flexinode_admin.module,v 1.3 2006/12/25 17:25:36 ber Exp $
3
4 /**
5 * @file Administration interface for flexinode.
6 **/
7
8 /**
9 * Implementation of hook_help().
10 */
11 function flexinode_admin_help($section) {
12 switch ($section) {
13 case 'admin/help#flexinode':
14 $output = '<p>'.
15 t('The flexinode module allows administrators to create simple new content types. This is the administration part, which allows you to manage creation of new types of content without having to program a new content module.') .'</p>';
16 $output .= '<p>'.
17 t('When creating a new flexinode, administrators are presented with a flexinode form to create their new content type. Once administrators have created their flexinode they can accept the format or choose to theme the content type to change it\'s presentation. For users, creating content that is a flexinode is just like adding other content. The flexinode content type will show up alongside all normal content.') .'</p>';
18 $output .= t('<p>You can</p>
19 <ul>
20 <li>create a flexinode content type at <a href="%admin-node-types" title="administer content types to add a flexinode"> administer &gt;&gt; content &gt;&gt; content types</a> and select <strong>add content type</strong>.</li>
21 <li><a href="%node-add" title="create a new flexinode type">create content &gt;&gt; add type.</a></li>
22 <li>administer flexinode at <a href="%admin-settings-flexinode">administer &gt;&gt; settings &gt;&gt; flexinode</a>.</li>
23 ', array('%admin-node-types' => url('admin/node/types'), '%node-add' => url('node/add'), '%admin-settings-flexinode' => url('admin/settings/flexinode'))) .'</ul>';
24 $output .= '<p>'. t('For more information please read the configuration and customization handbook <a href="%flexinode">Flexinode page</a>.', array('%flexinode' => 'http://www.drupal.org/handbook/modules/flexinode/')) .'</p>';
25 return $output;
26 case 'admin/modules#description':
27 return t('Allows administrators to define their own content types.');
28 case 'admin/node/types':
29 return t('You may manage your own content types here. These will all have a "title" field to start off, and contain additional fields that you specify. Choose the "add content type" tab to add a new type. Make a selection below to edit an existing type or field. To delete a field or entire content type, first open the editing form for that field or type.');
30 case 'admin/node/type':
31 return t('Once you create a content type here you will be able to add additional fields to it on the "content types" tab.');
32 }
33
34 $output = '';
35
36 return $output;
37 }
38
39 /**
40 * Implementation of hook_menu().
41 */
42 function flexinode_admin_menu($may_cache) {
43 $items = array();
44
45 if ($may_cache) {
46 $admin_access = user_access('administer content types');
47 foreach (flexinode_content_types() as $ctype) {
48 $items[] = array(
49 'path' => 'node/add/flexinode-'. $ctype->ctype_id,
50 'title' => t($ctype->name),
51 'access' => user_access('create '. $ctype->name .' content') or $admin_access
52 );
53 }
54
55 // admin menu items
56 $items[] = array(
57 'path' => 'admin/node/types',
58 'title' => t('content types'),
59 'callback' => 'flexinode_admin_page_admin',
60 'access' => $admin_access,
61 'type' => MENU_LOCAL_TASK,
62 );
63 $items[] = array(
64 'path' => 'admin/node/type',
65 'title' => t('add content type'),
66 'callback' => 'flexinode_admin_content_type_form',
67 'access' => $admin_access,
68 'type' => MENU_LOCAL_TASK,
69 );
70 $items[] = array(
71 'path' => 'admin/node/type/delete',
72 'title' => t('delete content type'),
73 'callback' => 'flexinode_admin_confirm_delete_content_type',
74 'access' => $admin_access,
75 'type' => MENU_CALLBACK,
76 );
77 $items[] = array(
78 'path' => 'admin/node/field',
79 'title' => t('edit fields'),
80 'callback' => 'flexinode_admin_admin_field',
81 'access' => $admin_access,
82 'type' => MENU_CALLBACK,
83 );
84 $items[] = array(
85 'path' => 'flexinode/update',
86 'title' => t('update'),
87 'callback' => 'flexinode_admin_page_admin_update',
88 'access' => $admin_access,
89 'type' => MENU_CALLBACK,
90 );
91 }
92
93 return $items;
94 }
95
96 /**
97 * MENU CALLBACKS
98 */
99 /**
100 * Menu callback; presents an overview of all admin-defined content types.
101 */
102 function flexinode_admin_page_admin($ctype_id = NULL) {
103 $content_types = flexinode_content_types();
104
105 $output = '';
106
107 foreach ($content_types as $ctype) {
108 $ctype = flexinode_load_content_type($ctype->ctype_id);
109 $ctype->links[] = l(t('edit'), 'admin/node/type/'. $ctype->ctype_id);
110 $ctype->links[] = l(t('settings'), 'admin/settings/content-types/flexinode-'. $ctype->ctype_id);
111
112
113 $ctype->controls = flexinode_admin_field_select($ctype->ctype_id);
114
115 foreach ($ctype->fields as $field) {
116 $ctype->fieldlist[] = $field->label . ' (' . l(t('edit field'), 'admin/node/field/' . $field->field_id) .')';
117 }
118
119 $output .= theme('flexinode_type', $ctype);
120 $first = FALSE;
121 }
122
123 if (strlen($output) == 0) {
124 $output = '<p>'. t('No flexinode content types have been defined. You can <a href="%url">add a new content type</a>.', array('%url' => url('admin/node/type'))) .'</p>';
125 }
126
127 drupal_add_js('misc/collapse.js');
128 return $output;
129 }
130
131 /**
132 * Menu callback; presents a form to edit an existing field in a content type.
133 */
134 function flexinode_admin_admin_field($field_id = NULL) {
135 $op = $_POST['op'];
136
137 $crumbs = drupal_get_breadcrumb();
138 $crumbs[] = l('content types', 'admin/node/types');
139 drupal_set_breadcrumb($crumbs);
140
141 switch ($op) {
142 case t('Add field'):
143 $field_id = $_POST['edit']['field_type'];
144 $output = flexinode_admin_field_form($field_id);
145 break;
146 case t('Confirm'):
147 $field = flexinode_load_field($field_id);
148 flexinode_admin_delete_field($field);
149 drupal_goto('admin/node/types/'. $field->ctype_id);
150 break;
151 case t('Delete'):
152 $field = flexinode_load_field($field_id);
153 $output = flexinode_admin_confirm_delete_field($field);
154 break;
155 case t('More'):
156 default:
157 $output = flexinode_admin_field_form($field_id);
158 break;
159 }
160
161 return $output;
162 }
163
164 /**
165 * Perform database updates from older versions.
166 *
167 * This is a temporary solution until the install system becomes part of
168 * the Drupal core.
169 */
170 function flexinode_admin_page_admin_update($update_num = 0) {
171 switch ($update_num) {
172 case 1:
173 db_query("ALTER TABLE {flexinode_field} MODIFY default_value MEDIUMTEXT NOT NULL");
174 db_query("ALTER TABLE {flexinode_field} ADD show_teaser INT(1) UNSIGNED DEFAULT '0' NOT NULL");
175 db_query("ALTER TABLE {flexinode_field} ADD show_table INT(1) UNSIGNED DEFAULT '0' NOT NULL");
176
177 foreach (flexinode_content_types() as $type) {
178 $field_id = flexinode_amin_save_field(array('label' => 'Description', 'default_value' => '', 'rows' => 10, 'required' => 0, 'weight' => 0, 'ctype_id' => $type->ctype_id, 'field_type' => 'textarea', 'options' => NULL, 'description' => ''));
179 $result = db_query("SELECT body, nid FROM {node} WHERE type = 'flexinode-%d'", $type->ctype_id);
180 while ($node = db_fetch_object($result)) {
181 db_query("INSERT INTO {flexinode_data} (nid, field_id, textual_data) VALUES (%d, %d, '%s')", $node->nid, $field_id, $node->body);
182 }
183 }
184
185 $output = 'Update complete.';
186 break;
187
188 default:
189 $output = '<p>Which update do you wish to perform?</p>';
190 $output .= '<dl>';
191 $output .= '<dt>'. l('6/16/04', 'flexinode/update/1') .'</dt>';
192 $output .= '<dd>Allows multiline default values for text areas. Allows administrator to configure which fields appear in the teaser and the tabular view. Deprecates special "Description" field in favor of using only basic textarea fields.</dd>';
193 $output .= '</dl>';
194 }
195 return $output;
196 }
197
198 /**
199 * CORE HOOKS
200 */
201
202
203 /**
204 * Implementation of hook_access().
205 */
206 function flexinode_admin_access($op, $node) {
207 global $user;
208
209 if (!is_object($node)) {
210 $type = $node;
211 $node = new StdClass();
212 $node->type = $type;
213 }
214
215 if ($op == 'create') {
216 return user_access('create '. node_get_name($node) .' content');
217 }
218
219 if ($op == 'update') {
220 foreach ($node as $fieldname => $field) {
221 if (preg_match('!flexinode_[0-9]+_format!', $fieldname) && !filter_access($field)) {
222 return FALSE;
223 }
224 }
225 }
226
227 if ($op == 'update' || $op == 'delete') {
228 if (user_access('edit any '. node_get_name($node) .' content')) {
229 return TRUE;
230 }
231 elseif (user_access('edit own '. node_get_name($node) .' content') && ($user->uid == $node->uid)) {
232 return TRUE;
233 }
234 }
235 }
236
237 /**
238 * Render a form for the editing of a content type.
239 */
240 function flexinode_admin_content_type_form($ctype_id = NULL) {
241 if ($ctype_id) {
242 $ctype = flexinode_load_content_type($ctype_id);
243 }
244
245 $form['name'] = array(
246 '#type' => 'textfield',
247 '#title' => t('Content type name'),
248 '#default_value' => $ctype->name,
249 '#size' => 60,
250 '#maxlength' => 128,
251 '#required' => TRUE,
252 );
253 $form['description'] = array(
254 '#type' => 'textfield',
255 '#title' => t('Description'),
256 '#default_value' => $ctype->description,
257 '#size' => 60,
258 '#maxlength' => 128,
259 '#description' => t('A one-line description of the content type.'),
260 );
261 $form['help'] = array(
262 '#type' => 'textarea',
263 '#title' => t('Help text'),
264 '#default_value' => $ctype->help,
265 '#size' => 60,
266 '#maxlength' => 5,
267 '#description' => t('Instructions to present to the user when adding new content of this type.'),
268 );
269 $form['ctype_id'] = array('#type' => 'hidden', '#value' => $ctype_id);
270 $form['submit'] = array('#type' => 'submit', '#value' => t('Submit'));
271
272 if ($ctype_id) {
273 $form['delete'] = array('#type' => 'submit', '#value' => t('Delete'));
274 }
275
276 return drupal_get_form('flexinode_admin_content_type_form', $form);
277 }
278
279 /**
280 * flexinode_content_type_form hook_validate callback function.
281 */
282 function flexinode_admin_content_type_form_validate($form_id, $edit) {
283 if (!$edit['name']) {
284 form_set_error('name', t('You must give this content type a name.'));
285 }
286 }
287
288 /**
289 * flexinode_content_type_form hook_submit callback function.
290 */
291 function flexinode_admin_content_type_form_submit($form_id, $edit) {
292 if($_POST['op'] == t('Delete')) {
293 drupal_goto('admin/node/type/delete/'. $edit['ctype_id']);
294 }
295 if ($edit['ctype_id']) {
296 $ctype_id = $edit['ctype_id'];
297
298 $former_ctype = flexinode_load_content_type($ctype_id);
299
300 db_query("UPDATE {flexinode_type} SET name = '%s', description = '%s', help = '%s' WHERE ctype_id = %d", $edit['name'], $edit['description'], $edit['help'], $ctype_id);
301
302 drupal_set_message(t('updated content type "%name".', array('%name' => $edit['name'])));
303 }
304 else {
305 $ctype_id = db_next_id('{flexinode_ctype}');
306
307 db_query("INSERT INTO {flexinode_type} (name, description, help, ctype_id) VALUES ('%s', '%s', '%s', %d)", $edit['name'], $edit['description'], $edit['help'], $ctype_id);
308
309 drupal_set_message(t('created new content type "%name".', array('%name' => $edit['name'])));
310 }
311
312 // update the cached "create content" menu
313 menu_rebuild();
314 drupal_goto('admin/node/types/'. $ctype_id);
315 }
316
317 /**
318 * Generate a confirmation page for the deletion of a custom content type.
319 */
320 function flexinode_admin_confirm_delete_content_type($ctype_id) {
321 if($_POST['op'] == t('Confirm')) {
322 flexinode_admin_delete_content_type($ctype_id);
323 drupal_goto('admin/node/types');
324 }
325
326 $ctype = flexinode_load_content_type($ctype_id);
327 $form['ctype_id'] = array(
328 '#type' => 'hidden',
329 '#value' => $ctype_id,
330 );
331 $form['name'] = array(
332 '#type' => 'hidden',
333 '#value' => $ctype->name,
334 );
335 return confirm_form('flexinode_confirm_delete_content_type', $form, t('Are you sure you want to delete the content type "%name"? All nodes of this type will be lost.', array('%name' => $ctype->name)), 'admin/node/type/'. $ctype_id);
336 }
337
338 /**
339 * Delete a custom content type from the database.
340 */
341 function flexinode_admin_delete_content_type($ctype_id) {
342 // TODO: Delete files as appropriate.
343 db_query('DELETE FROM {flexinode_type} WHERE ctype_id = %d', $ctype_id);
344 db_query('DELETE FROM {flexinode_field} WHERE ctype_id = %d', $ctype_id);
345 $result = db_query("SELECT nid FROM {node} WHERE type = '%s'", 'flexinode-' . $ctype_id);
346 while ($node = db_fetch_object($result)) {
347 db_query('DELETE FROM {flexinode_data} WHERE nid = %d', $node->nid);
348 }
349 db_query("DELETE FROM {node} WHERE type = '%s'", 'flexinode-' . $ctype_id);
350
351 drupal_set_message(t('deleted content type'));
352
353 // update the cached "create content" menu
354 menu_rebuild();
355 }
356
357 /**
358 * FLEXINODE FIELD FUNCTIONS
359 */
360 /**
361 * Generate a form for the editing of a content type field.
362 * @param field_id can be either a field_id, or string containing the field type for new fields
363 */
364 function flexinode_admin_field_form($field_id, $ctype_id = NULL) {
365 if(is_numeric($field_id)) {
366 $field = flexinode_load_field($field_id);
367 }
368 else {
369 $field->field_type = $field_id;
370 $ctype_id = $_POST['edit']['ctype_id'];
371 }
372 $form['label'] = array(
373 '#type' => 'textfield',
374 '#title' => t('Field label'),
375 '#default_value' => $field->label,
376 '#required' => TRUE,
377 );
378 $form['description'] = array(
379 '#type' => 'textfield',
380 '#title' => t('Description'),
381 '#default_value' => $field->description,
382 '#description' => t('A brief description of the field, to be displayed on the content submission form.'),
383 );
384
385 $form[] = flexinode_invoke('config', $field);
386
387 $form['required'] = array(
388 '#type' => 'checkbox',
389 '#title' => t('Required field'),
390 '#default_value' => $field->required,
391 '#description' => t('Whether the user must fill in the field when creating content.'),
392 );
393 $form['show_teaser'] = array(
394 '#type' => 'checkbox',
395 '#title' => t('Show in teaser'),
396 '#default_value' => $field->show_teaser,
397 '#description' => t('Whether this field should be shown as part of the teaser.'),
398 );
399
400 $form['weight'] = array(
401 '#type' => 'weight',
402 '#title' => t('Weight'),
403 '#default_value' => $field->weight,
404 '#delta' => 10,
405 '#description' => t('Optional. On the content editing form, the heavier fields will sink and the lighter fields will be positioned nearer the top.'),
406 );
407 $form['submit'] = array(
408 '#type' => 'submit',
409 '#value' => t('Submit'),
410 );
411
412 $form['ctype_id'] = array(
413 '#type' => 'hidden',
414 '#value' => $ctype_id,
415 );
416 $form['field_type'] = array(
417 '#type' => 'hidden',
418 '#value' => $field->field_type,
419 );
420
421 $form['#action'] = url('admin/node/field/'. $field_id);
422
423 if ($field->field_id) {
424 $form['delete'] = array(
425 '#type' => 'submit',
426 '#value' => t('Delete'),
427 );
428 $form['field_id'] = array(
429 '#type' => 'hidden',
430 '#value' => $field->field_id,
431 );
432 $form['ctype_id'] = array(
433 '#type' => 'hidden',
434 '#value' => $field->ctype_id,
435 );
436 }
437
438 return drupal_get_form('flexinode_admin_field_form', $form);
439 }
440
441 /**
442 * flexinode_admin_field_form hook_submit callback function.
443 */
444 function flexinode_admin_field_form_submit($form_id, $edit) {
445 flexinode_admin_save_field($edit);
446 drupal_goto('admin/node/types/'. $edit['ctype_id']);
447 }
448
449 /**
450 * Builds and returns the field select form.
451 */
452 function flexinode_admin_field_select($ctype_id = NULL) {
453 $form = $options = array();
454
455 $form['#action'] = url('admin/node/field/');
456 $form['#redirect'] = FALSE;
457 $form['field_type'] = array(
458 '#type' => 'select',
459 '#options' => flexinode_admin_field_select_options(),
460 );
461 $form['ctype_id'] = array(
462 '#type' => 'hidden',
463 '#default_value' => $ctype_id,
464 );
465 $form['submit'] = array(
466 '#type' => 'submit',
467 '#value' => t('Add field'),
468 );
469 return drupal_get_form('flexinode_admin_field_select', $form);
470 }
471
472 /**
473 * Build the options for the selectfield.
474 **/
475 function flexinode_admin_field_select_options() {
476 foreach (flexinode_field_types() as $field) {
477 $options[$field] = t('add %fieldtype', array('%fieldtype' => flexinode_invoke('name', $field)));
478 }
479 return $options;
480 }
481
482 /**
483 * Save a custom field to the database.
484 */
485 function flexinode_admin_save_field($edit) {
486 if(is_array($edit['options'])) {
487 $options = $edit['options'];
488 }
489 else {
490 $options = array_merge(array(0 => 0), explode('|', $edit['options']));
491 unset($options[0]);
492 }
493
494 if ($edit['field_id']) {
495 $field_id = $edit['field_id'];
496
497 db_query("UPDATE {flexinode_field} SET label = '%s', default_value = '%s', rows = %d, required = %d, show_teaser = %d, show_table = %d, weight = %d, ctype_id = %d, field_type = '%s', options = '%s', description = '%s' WHERE field_id = %d", $edit['label'], $edit['default_value'], $edit['rows'], $edit['required'], $edit['show_teaser'], $edit['show_table'], $edit['weight'], $edit['ctype_id'], $edit['field_type'], serialize($options), $edit['description'], $field_id);
498
499 drupal_set_message(t('updated field "%name".', array('%name' => $edit['label'])));
500 }
501 else {
502 $field_id = db_next_id('{flexinode_field}');
503
504 db_query("INSERT INTO {flexinode_field} (label, default_value, rows, required, show_teaser, show_table, weight, ctype_id, field_type, options, description, field_id) VALUES ('%s', '%s', %d, %d, %d, %d, %d, %d, '%s', '%s', '%s', %d)", $edit['label'], $edit['default_value'], $edit['rows'], $edit['required'], $edit['show_teaser'], $edit['show_table'], $edit['weight'], $edit['ctype_id'], $edit['field_type'], serialize($options), $edit['description'], $field_id);
505
506 drupal_set_message(t('created new field "%name".', array('%name' => $edit['label'])));
507 }
508
509 return $field_id;
510 }
511
512 /**
513 * Generate a confirmation page prior to deleting a custom field.
514 */
515 function flexinode_admin_confirm_delete_field($field) {
516 return confirm_form('delete_field', array(), t('Are you sure you want to delete field "%name"? All data in this field will be lost.', array('%name' => $field->label)), 'admin/node/field/'. $field->field_id);
517 }
518
519 /**
520 * Delete a custom field from the database.
521 */
522 function flexinode_admin_delete_field($field) {
523 // TODO: Delete files as appropriate.
524 db_query('DELETE FROM {flexinode_field} WHERE field_id = %d', $field->field_id);
525 db_query('DELETE FROM {flexinode_data} WHERE field_id = %d', $field->field_id);
526
527 drupal_set_message(t('deleted field %name', array('%name' => $field->label)));
528 }
529
530
531 /**
532 * THEME FUNCTIONS
533 **/
534 function theme_flexinode_type($type) {
535 $output = "<div><fieldset class=\"collapsible\"><legend>". $type->name ."</legend>\n";
536 $output .= " <p class=\"description\">". $type->description ." (". theme('links', $type->links) .")</p>\n";
537 $output .= " <p class=\"fields\">\n";
538 $output .= theme('item_list', $type->fieldlist, t('Field list'));
539 $output .= $type->controls ."</p>\n";
540 $output .= "</fieldset></div>\n";
541 return $output;
542 }

  ViewVC Help
Powered by ViewVC 1.1.2