boxes.admin.inc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. <?php
  2. /**
  3. * Generate form for creating new boxes.
  4. */
  5. function boxes_add_form($form, $form_state, $plugin_key) {
  6. $form_state['box'] = boxes_factory($plugin_key);
  7. $form = boxes_box_form($form, $form_state);
  8. $form['delta'] = array(
  9. '#type' => 'textfield',
  10. '#title' => t('Machine name'),
  11. '#description' => t('Lowercase letters, numbers and underscores only'),
  12. '#required' => TRUE,
  13. '#element_validate' => array('boxes_validate_delta'),
  14. '#size' => 32,
  15. '#maxlength' => 32,
  16. '#weight' => -20,
  17. );
  18. $form['submit']['#attributes']['class'] = array();
  19. return $form;
  20. }
  21. /**
  22. * Validate handler for box delta.
  23. */
  24. function boxes_validate_delta($element, &$form_state) {
  25. if (!preg_match('!^[a-z0-9_]+$!', $element['#value'])) {
  26. form_error($element, t('The machine-readable name must contain only lowercase letters, numbers, and underscores.'));
  27. }
  28. if ((strpos($element['#value'], 'boxes_add__') === 0) || boxes_box_load($element['#value'])) {
  29. form_error($element, t('The machine-readable name is already taken.'));
  30. }
  31. }
  32. /**
  33. * Submit handler for box_add_form.
  34. */
  35. function boxes_add_form_submit($form, &$form_state) {
  36. $box = boxes_factory($form_state['values']['plugin_key'], $form_state['values']);
  37. $box->save();
  38. drupal_set_message(t('%name has been created.', array('%name' => $box->description)));
  39. $form_state['redirect'] = 'admin/structure/block';
  40. }
  41. /**
  42. * Box deletion form.
  43. */
  44. function boxes_delete_form($form, $form_state, $box) {
  45. $form['delta'] = array(
  46. '#type' => 'hidden',
  47. '#value' => $box->delta,
  48. );
  49. if (($box->export_type & EXPORT_IN_DATABASE) && ($box->export_type & EXPORT_IN_CODE)) {
  50. return confirm_form($form, t('Are you sure you want to revert the block %name?', array('%name' => $box->title)), 'admin/structure/block', '', t('Revert'), t('Cancel'));
  51. }
  52. elseif (!($box->export_type & EXPORT_IN_CODE)) {
  53. return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $box->title)), 'admin/structure/block', '', t('Delete'), t('Cancel'));
  54. }
  55. drupal_not_found();
  56. die;
  57. }
  58. /**
  59. * Submit handler for boxes_delete_form
  60. */
  61. function boxes_delete_form_submit($form, &$form_state) {
  62. boxes_box_load($form_state['values']['delta'])->delete();
  63. $form_state['redirect'] = 'admin/structure/block';
  64. }
  65. /**
  66. * Theme function for the form.
  67. */
  68. function theme_boxes_box($variables) {
  69. $block = $variables['block'];
  70. $empty = '';
  71. // Box is empty
  72. if ((empty($block['title']) || ($block['title'] == '<none>') ) && empty($block['content'])) {
  73. // add a class to mark the box as empty
  74. $empty = ' box-empty';
  75. // show something if the block is empty but the user has the permission to edit it
  76. if (boxes_access_edit()) {
  77. $block['content'] = t('This box appears empty when displayed on this page. This is simply placeholder text.');
  78. }
  79. else {
  80. return;
  81. }
  82. }
  83. $output = "<div id='boxes-box-" . $block['delta'] . "' class='boxes-box" . (!empty($block['editing']) ? ' boxes-box-editing' : '') . $empty . "'>";
  84. $output .= '<div class="boxes-box-content">' . $block['content'] . '</div>';
  85. if (!empty($block['controls'])) {
  86. $output .= '<div class="boxes-box-controls">';
  87. $output .= $block['controls'];
  88. $output .= '</div>';
  89. }
  90. if (!empty($block['editing'])) {
  91. $output .= '<div class="box-editor">' . drupal_render($block['editing']) . '</div>';
  92. }
  93. $output .= '</div>';
  94. return $output;
  95. }
  96. /**
  97. * Menu callback for settings form.
  98. */
  99. function boxes_settings($form, $form_state) {
  100. $form['boxes_edit_location'] = array(
  101. '#title' => t('Edit location'),
  102. '#type' => 'radios',
  103. '#options' => array(
  104. BOXES_EDIT_SEPARATE_PAGE => t('Separate page'),
  105. BOXES_EDIT_IN_PLACE => t('Edit in place'),
  106. ),
  107. '#default_value' => variable_get('boxes_edit_location', BOXES_EDIT_IN_PLACE),
  108. '#description' => t('Boxes can either be edited directly where they are displayed, or on a separate page.')
  109. );
  110. return system_settings_form($form);
  111. }