cms/drupal/modules/block/block.admin.inc
changeset 541 e756a8c72c3d
equal deleted inserted replaced
540:07239de796bb 541:e756a8c72c3d
       
     1 <?php
       
     2 
       
     3 /**
       
     4  * @file
       
     5  * Admin page callbacks for the block module.
       
     6  */
       
     7 
       
     8 /**
       
     9  * Menu callback for admin/structure/block/demo.
       
    10  */
       
    11 function block_admin_demo($theme = NULL) {
       
    12   drupal_add_css(drupal_get_path('module', 'block') . '/block.css');
       
    13   return '';
       
    14 }
       
    15 
       
    16 /**
       
    17  * Menu callback for admin/structure/block.
       
    18  *
       
    19  * @param $theme
       
    20  *   The theme to display the administration page for. If not provided, defaults
       
    21  *   to the currently used theme.
       
    22  */
       
    23 function block_admin_display($theme = NULL) {
       
    24   global $theme_key;
       
    25 
       
    26   drupal_theme_initialize();
       
    27 
       
    28   if (!isset($theme)) {
       
    29     // If theme is not specifically set, rehash for the current theme.
       
    30     $theme = $theme_key;
       
    31   }
       
    32 
       
    33   // Fetch and sort blocks.
       
    34   $blocks = block_admin_display_prepare_blocks($theme);
       
    35 
       
    36   return drupal_get_form('block_admin_display_form', $blocks, $theme);
       
    37 }
       
    38 
       
    39 /**
       
    40  * Prepares a list of blocks for display on the blocks administration page.
       
    41  *
       
    42  * @param $theme
       
    43  *   The machine-readable name of the theme whose blocks should be returned.
       
    44  *
       
    45  * @return
       
    46  *   An array of blocks, as returned by _block_rehash(), sorted by region in
       
    47  *   preparation for display on the blocks administration page.
       
    48  *
       
    49  * @see block_admin_display_form()
       
    50  */
       
    51 function block_admin_display_prepare_blocks($theme) {
       
    52   $blocks = _block_rehash($theme);
       
    53   $compare_theme = &drupal_static('_block_compare:theme');
       
    54   $compare_theme = $theme;
       
    55   usort($blocks, '_block_compare');
       
    56   return $blocks;
       
    57 }
       
    58 
       
    59 /**
       
    60  * Form constructor for the main block administration form.
       
    61  *
       
    62  * @param $blocks
       
    63  *   An array of blocks, as returned by block_admin_display_prepare_blocks().
       
    64  * @param $theme
       
    65  *   A string representing the name of the theme to edit blocks for.
       
    66  * @param $block_regions
       
    67  *   (optional) An array of regions in which the blocks will be allowed to be
       
    68  *   placed. Defaults to all visible regions for the theme whose blocks are
       
    69  *   being configured. In all cases, a dummy region for disabled blocks will
       
    70  *   also be displayed.
       
    71  *
       
    72  * @return
       
    73  *   An array representing the form definition.
       
    74  *
       
    75  * @ingroup forms
       
    76  * @see block_admin_display_form_submit()
       
    77  */
       
    78 function block_admin_display_form($form, &$form_state, $blocks, $theme, $block_regions = NULL) {
       
    79 
       
    80   $form['#attached']['css'] = array(drupal_get_path('module', 'block') . '/block.css');
       
    81 
       
    82   // Get a list of block regions if one was not provided.
       
    83   if (!isset($block_regions)) {
       
    84     $block_regions = system_region_list($theme, REGIONS_VISIBLE);
       
    85   }
       
    86 
       
    87   // Weights range from -delta to +delta, so delta should be at least half
       
    88   // of the amount of blocks present. This makes sure all blocks in the same
       
    89   // region get an unique weight.
       
    90   $weight_delta = round(count($blocks) / 2);
       
    91 
       
    92   // Build the form tree.
       
    93   $form['edited_theme'] = array(
       
    94     '#type' => 'value',
       
    95     '#value' => $theme,
       
    96   );
       
    97   $form['block_regions'] = array(
       
    98     '#type' => 'value',
       
    99     // Add a last region for disabled blocks.
       
   100     '#value' => $block_regions + array(BLOCK_REGION_NONE => BLOCK_REGION_NONE),
       
   101   );
       
   102   $form['blocks'] = array();
       
   103   $form['#tree'] = TRUE;
       
   104 
       
   105   foreach ($blocks as $i => $block) {
       
   106     $key = $block['module'] . '_' . $block['delta'];
       
   107     $form['blocks'][$key]['module'] = array(
       
   108       '#type' => 'value',
       
   109       '#value' => $block['module'],
       
   110     );
       
   111     $form['blocks'][$key]['delta'] = array(
       
   112       '#type' => 'value',
       
   113       '#value' => $block['delta'],
       
   114     );
       
   115     $form['blocks'][$key]['info'] = array(
       
   116       '#markup' => check_plain($block['info']),
       
   117     );
       
   118     $form['blocks'][$key]['theme'] = array(
       
   119       '#type' => 'hidden',
       
   120       '#value' => $theme,
       
   121     );
       
   122     $form['blocks'][$key]['weight'] = array(
       
   123       '#type' => 'weight',
       
   124       '#default_value' => $block['weight'],
       
   125       '#delta' => $weight_delta,
       
   126       '#title_display' => 'invisible',
       
   127       '#title' => t('Weight for @block block', array('@block' => $block['info'])),
       
   128     );
       
   129     $form['blocks'][$key]['region'] = array(
       
   130       '#type' => 'select',
       
   131       '#default_value' => $block['region'] != BLOCK_REGION_NONE ? $block['region'] : NULL,
       
   132       '#empty_value' => BLOCK_REGION_NONE,
       
   133       '#title_display' => 'invisible',
       
   134       '#title' => t('Region for @block block', array('@block' => $block['info'])),
       
   135       '#options' => $block_regions,
       
   136     );
       
   137     $form['blocks'][$key]['configure'] = array(
       
   138       '#type' => 'link',
       
   139       '#title' => t('configure'),
       
   140       '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/configure',
       
   141     );
       
   142     if ($block['module'] == 'block') {
       
   143       $form['blocks'][$key]['delete'] = array(
       
   144         '#type' => 'link',
       
   145         '#title' => t('delete'),
       
   146         '#href' => 'admin/structure/block/manage/' . $block['module'] . '/' . $block['delta'] . '/delete',
       
   147      );
       
   148     }
       
   149   }
       
   150   // Do not allow disabling the main system content block when it is present.
       
   151   if (isset($form['blocks']['system_main']['region'])) {
       
   152     $form['blocks']['system_main']['region']['#required'] = TRUE;
       
   153   }
       
   154 
       
   155   $form['actions'] = array(
       
   156     '#tree' => FALSE,
       
   157     '#type' => 'actions',
       
   158   );
       
   159   $form['actions']['submit'] = array(
       
   160     '#type' => 'submit',
       
   161     '#value' => t('Save blocks'),
       
   162   );
       
   163 
       
   164   return $form;
       
   165 }
       
   166 
       
   167 /**
       
   168  * Form submission handler for block_admin_display_form().
       
   169  *
       
   170  * @see block_admin_display_form()
       
   171  */
       
   172 function block_admin_display_form_submit($form, &$form_state) {
       
   173   $transaction = db_transaction();
       
   174   try {
       
   175     foreach ($form_state['values']['blocks'] as $block) {
       
   176       $block['status'] = (int) ($block['region'] != BLOCK_REGION_NONE);
       
   177       $block['region'] = $block['status'] ? $block['region'] : '';
       
   178       db_update('block')
       
   179         ->fields(array(
       
   180           'status' => $block['status'],
       
   181           'weight' => $block['weight'],
       
   182           'region' => $block['region'],
       
   183         ))
       
   184         ->condition('module', $block['module'])
       
   185         ->condition('delta', $block['delta'])
       
   186         ->condition('theme', $block['theme'])
       
   187         ->execute();
       
   188     }
       
   189   }
       
   190   catch (Exception $e) {
       
   191     $transaction->rollback();
       
   192     watchdog_exception('block', $e);
       
   193     throw $e;
       
   194   }
       
   195   drupal_set_message(t('The block settings have been updated.'));
       
   196   cache_clear_all();
       
   197 }
       
   198 
       
   199 /**
       
   200  * Sorts active blocks by region, then by weight; sorts inactive blocks by name.
       
   201  *
       
   202  * Callback for usort() in block_admin_display_prepare_blocks().
       
   203  */
       
   204 function _block_compare($a, $b) {
       
   205   global $theme_key;
       
   206 
       
   207   // Theme should be set before calling this function, or the current theme
       
   208   // is being used.
       
   209   $theme = &drupal_static(__FUNCTION__ . ':theme');
       
   210   if (!isset($theme)) {
       
   211     $theme = $theme_key;
       
   212   }
       
   213 
       
   214   $regions = &drupal_static(__FUNCTION__ . ':regions');
       
   215   // We need the region list to correctly order by region.
       
   216   if (!isset($regions)) {
       
   217     $regions = array_flip(array_keys(system_region_list($theme)));
       
   218     $regions[BLOCK_REGION_NONE] = count($regions);
       
   219   }
       
   220 
       
   221   // Separate enabled from disabled.
       
   222   $status = $b['status'] - $a['status'];
       
   223   if ($status) {
       
   224     return $status;
       
   225   }
       
   226   // Sort by region (in the order defined by theme .info file).
       
   227   if ((!empty($a['region']) && !empty($b['region'])) && ($place = ($regions[$a['region']] - $regions[$b['region']]))) {
       
   228     return $place;
       
   229   }
       
   230   // Sort by weight, unless disabled.
       
   231   if ($a['region'] != BLOCK_REGION_NONE) {
       
   232     $weight = $a['weight'] - $b['weight'];
       
   233     if ($weight) {
       
   234       return $weight;
       
   235     }
       
   236   }
       
   237   // Sort by title.
       
   238   return strcmp($a['info'], $b['info']);
       
   239 }
       
   240 
       
   241 /**
       
   242  * Form constructor for the block configuration form.
       
   243  *
       
   244  * Also used by block_add_block_form() for adding a new custom block.
       
   245  *
       
   246  * @param $module
       
   247  *   Name of the module that implements the block to be configured.
       
   248  * @param $delta
       
   249  *   Unique ID of the block within the context of $module.
       
   250  *
       
   251  * @see block_admin_configure_validate()
       
   252  * @see block_admin_configure_submit()
       
   253  * @ingroup forms
       
   254  */
       
   255 function block_admin_configure($form, &$form_state, $module, $delta) {
       
   256   $block = block_load($module, $delta);
       
   257   $form['module'] = array(
       
   258     '#type' => 'value',
       
   259     '#value' => $block->module,
       
   260   );
       
   261   $form['delta'] = array(
       
   262     '#type' => 'value',
       
   263     '#value' => $block->delta,
       
   264   );
       
   265 
       
   266   // Get the block subject for the page title.
       
   267   $info = module_invoke($block->module, 'block_info');
       
   268   if (isset($info[$block->delta])) {
       
   269     drupal_set_title(t("'%name' block", array('%name' => $info[$block->delta]['info'])), PASS_THROUGH);
       
   270   }
       
   271 
       
   272   $form['settings']['title'] = array(
       
   273     '#type' => 'textfield',
       
   274     '#title' => t('Block title'),
       
   275     '#maxlength' => 255,
       
   276     '#description' => $block->module == 'block' ? t('The title of the block as shown to the user.') : t('Override the default title for the block. Use <em>!placeholder</em> to display no title, or leave blank to use the default block title.', array('!placeholder' => '&lt;none&gt;')),
       
   277     '#default_value' => isset($block->title) ? $block->title : '',
       
   278     '#weight' => -19,
       
   279   );
       
   280 
       
   281   // Module-specific block configuration.
       
   282   if ($settings = module_invoke($block->module, 'block_configure', $block->delta)) {
       
   283     foreach ($settings as $k => $v) {
       
   284       $form['settings'][$k] = $v;
       
   285     }
       
   286   }
       
   287 
       
   288   // Region settings.
       
   289   $form['regions'] = array(
       
   290     '#type' => 'fieldset',
       
   291     '#title' => t('Region settings'),
       
   292     '#collapsible' => FALSE,
       
   293     '#description' => t('Specify in which themes and regions this block is displayed.'),
       
   294     '#tree' => TRUE,
       
   295   );
       
   296 
       
   297   $theme_default = variable_get('theme_default', 'bartik');
       
   298   $admin_theme = variable_get('admin_theme');
       
   299   foreach (list_themes() as $key => $theme) {
       
   300     // Only display enabled themes
       
   301     if ($theme->status) {
       
   302       $region = db_query("SELECT region FROM {block} WHERE module = :module AND delta = :delta AND theme = :theme", array(
       
   303         ':module' => $block->module,
       
   304         ':delta' => $block->delta,
       
   305         ':theme' => $key,
       
   306       ))->fetchField();
       
   307 
       
   308       // Use a meaningful title for the main site theme and administrative
       
   309       // theme.
       
   310       $theme_title = $theme->info['name'];
       
   311       if ($key == $theme_default) {
       
   312         $theme_title = t('!theme (default theme)', array('!theme' => $theme_title));
       
   313       }
       
   314       elseif ($admin_theme && $key == $admin_theme) {
       
   315         $theme_title = t('!theme (administration theme)', array('!theme' => $theme_title));
       
   316       }
       
   317       $form['regions'][$key] = array(
       
   318         '#type' => 'select',
       
   319         '#title' => $theme_title,
       
   320         '#default_value' => !empty($region) && $region != -1 ? $region : NULL,
       
   321         '#empty_value' => BLOCK_REGION_NONE,
       
   322         '#options' => system_region_list($key, REGIONS_VISIBLE),
       
   323         '#weight' => ($key == $theme_default ? 9 : 10),
       
   324       );
       
   325     }
       
   326   }
       
   327 
       
   328   // Visibility settings.
       
   329   $form['visibility_title'] = array(
       
   330     '#type' => 'item',
       
   331     '#title' => t('Visibility settings'),
       
   332   );
       
   333   $form['visibility'] = array(
       
   334     '#type' => 'vertical_tabs',
       
   335     '#attached' => array(
       
   336       'js' => array(drupal_get_path('module', 'block') . '/block.js'),
       
   337     ),
       
   338   );
       
   339 
       
   340   // Per-path visibility.
       
   341   $form['visibility']['path'] = array(
       
   342     '#type' => 'fieldset',
       
   343     '#title' => t('Pages'),
       
   344     '#collapsible' => TRUE,
       
   345     '#collapsed' => TRUE,
       
   346     '#group' => 'visibility',
       
   347     '#weight' => 0,
       
   348   );
       
   349 
       
   350   $access = user_access('use PHP for settings');
       
   351   if (isset($block->visibility) && $block->visibility == BLOCK_VISIBILITY_PHP && !$access) {
       
   352     $form['visibility']['path']['visibility'] = array(
       
   353       '#type' => 'value',
       
   354       '#value' => BLOCK_VISIBILITY_PHP,
       
   355     );
       
   356     $form['visibility']['path']['pages'] = array(
       
   357       '#type' => 'value',
       
   358       '#value' => isset($block->pages) ? $block->pages : '',
       
   359     );
       
   360   }
       
   361   else {
       
   362     $options = array(
       
   363       BLOCK_VISIBILITY_NOTLISTED => t('All pages except those listed'),
       
   364       BLOCK_VISIBILITY_LISTED => t('Only the listed pages'),
       
   365     );
       
   366     $description = t("Specify pages by using their paths. Enter one path per line. The '*' character is a wildcard. Example paths are %blog for the blog page and %blog-wildcard for every personal blog. %front is the front page.", array('%blog' => 'blog', '%blog-wildcard' => 'blog/*', '%front' => '<front>'));
       
   367 
       
   368     if (module_exists('php') && $access) {
       
   369       $options += array(BLOCK_VISIBILITY_PHP => t('Pages on which this PHP code returns <code>TRUE</code> (experts only)'));
       
   370       $title = t('Pages or PHP code');
       
   371       $description .= ' ' . t('If the PHP option is chosen, enter PHP code between %php. Note that executing incorrect PHP code can break your Drupal site.', array('%php' => '<?php ?>'));
       
   372     }
       
   373     else {
       
   374       $title = t('Pages');
       
   375     }
       
   376     $form['visibility']['path']['visibility'] = array(
       
   377       '#type' => 'radios',
       
   378       '#title' => t('Show block on specific pages'),
       
   379       '#options' => $options,
       
   380       '#default_value' => isset($block->visibility) ? $block->visibility : BLOCK_VISIBILITY_NOTLISTED,
       
   381     );
       
   382     $form['visibility']['path']['pages'] = array(
       
   383       '#type' => 'textarea',
       
   384       '#title' => '<span class="element-invisible">' . $title . '</span>',
       
   385       '#default_value' => isset($block->pages) ? $block->pages : '',
       
   386       '#description' => $description,
       
   387     );
       
   388   }
       
   389 
       
   390   // Per-role visibility.
       
   391   $default_role_options = db_query("SELECT rid FROM {block_role} WHERE module = :module AND delta = :delta", array(
       
   392     ':module' => $block->module,
       
   393     ':delta' => $block->delta,
       
   394   ))->fetchCol();
       
   395   $role_options = array_map('check_plain', user_roles());
       
   396   $form['visibility']['role'] = array(
       
   397     '#type' => 'fieldset',
       
   398     '#title' => t('Roles'),
       
   399     '#collapsible' => TRUE,
       
   400     '#collapsed' => TRUE,
       
   401     '#group' => 'visibility',
       
   402     '#weight' => 10,
       
   403   );
       
   404   $form['visibility']['role']['roles'] = array(
       
   405     '#type' => 'checkboxes',
       
   406     '#title' => t('Show block for specific roles'),
       
   407     '#default_value' => $default_role_options,
       
   408     '#options' => $role_options,
       
   409     '#description' => t('Show this block only for the selected role(s). If you select no roles, the block will be visible to all users.'),
       
   410   );
       
   411 
       
   412   // Per-user visibility.
       
   413   $form['visibility']['user'] = array(
       
   414     '#type' => 'fieldset',
       
   415     '#title' => t('Users'),
       
   416     '#collapsible' => TRUE,
       
   417     '#collapsed' => TRUE,
       
   418     '#group' => 'visibility',
       
   419     '#weight' => 20,
       
   420   );
       
   421   $form['visibility']['user']['custom'] = array(
       
   422     '#type' => 'radios',
       
   423     '#title' => t('Customizable per user'),
       
   424     '#options' => array(
       
   425       BLOCK_CUSTOM_FIXED => t('Not customizable'),
       
   426       BLOCK_CUSTOM_ENABLED => t('Customizable, visible by default'),
       
   427       BLOCK_CUSTOM_DISABLED => t('Customizable, hidden by default'),
       
   428     ),
       
   429     '#description' => t('Allow individual users to customize the visibility of this block in their account settings.'),
       
   430     '#default_value' => isset($block->custom) ? $block->custom : BLOCK_CUSTOM_FIXED,
       
   431   );
       
   432 
       
   433   $form['actions'] = array('#type' => 'actions');
       
   434   $form['actions']['submit'] = array(
       
   435     '#type' => 'submit',
       
   436     '#value' => t('Save block'),
       
   437   );
       
   438 
       
   439   return $form;
       
   440 }
       
   441 
       
   442 /**
       
   443  * Form validation handler for block_admin_configure().
       
   444  *
       
   445  * @see block_admin_configure()
       
   446  * @see block_admin_configure_submit()
       
   447  */
       
   448 function block_admin_configure_validate($form, &$form_state) {
       
   449   if ($form_state['values']['module'] == 'block') {
       
   450     $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE bid <> :bid AND info = :info', 0, 1, array(
       
   451       ':bid' => $form_state['values']['delta'],
       
   452       ':info' => $form_state['values']['info'],
       
   453     ))->fetchField();
       
   454     if (empty($form_state['values']['info']) || $custom_block_exists) {
       
   455       form_set_error('info', t('Ensure that each block description is unique.'));
       
   456     }
       
   457   }
       
   458 }
       
   459 
       
   460 /**
       
   461  * Form submission handler for block_admin_configure().
       
   462  *
       
   463  * @see block_admin_configure()
       
   464  * @see block_admin_configure_validate()
       
   465  */
       
   466 function block_admin_configure_submit($form, &$form_state) {
       
   467   if (!form_get_errors()) {
       
   468     $transaction = db_transaction();
       
   469     try {
       
   470       db_update('block')
       
   471         ->fields(array(
       
   472           'visibility' => (int) $form_state['values']['visibility'],
       
   473           'pages' => trim($form_state['values']['pages']),
       
   474           'custom' => (int) $form_state['values']['custom'],
       
   475           'title' => $form_state['values']['title'],
       
   476         ))
       
   477         ->condition('module', $form_state['values']['module'])
       
   478         ->condition('delta', $form_state['values']['delta'])
       
   479         ->execute();
       
   480 
       
   481       db_delete('block_role')
       
   482         ->condition('module', $form_state['values']['module'])
       
   483         ->condition('delta', $form_state['values']['delta'])
       
   484         ->execute();
       
   485       $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
       
   486       foreach (array_filter($form_state['values']['roles']) as $rid) {
       
   487         $query->values(array(
       
   488           'rid' => $rid,
       
   489           'module' => $form_state['values']['module'],
       
   490           'delta' => $form_state['values']['delta'],
       
   491         ));
       
   492       }
       
   493       $query->execute();
       
   494 
       
   495       // Store regions per theme for this block
       
   496       foreach ($form_state['values']['regions'] as $theme => $region) {
       
   497         db_merge('block')
       
   498           ->key(array('theme' => $theme, 'delta' => $form_state['values']['delta'], 'module' => $form_state['values']['module']))
       
   499           ->fields(array(
       
   500             'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
       
   501             'pages' => trim($form_state['values']['pages']),
       
   502             'status' => (int) ($region != BLOCK_REGION_NONE),
       
   503           ))
       
   504           ->execute();
       
   505       }
       
   506 
       
   507       module_invoke($form_state['values']['module'], 'block_save', $form_state['values']['delta'], $form_state['values']);
       
   508     }
       
   509     catch (Exception $e) {
       
   510       $transaction->rollback();
       
   511       watchdog_exception('block', $e);
       
   512       throw $e;
       
   513     }
       
   514     drupal_set_message(t('The block configuration has been saved.'));
       
   515     cache_clear_all();
       
   516     $form_state['redirect'] = 'admin/structure/block';
       
   517   }
       
   518 }
       
   519 
       
   520 /**
       
   521  * Form constructor for the add block form.
       
   522  *
       
   523  * @see block_add_block_form_validate()
       
   524  * @see block_add_block_form_submit()
       
   525  * @ingroup forms
       
   526  */
       
   527 function block_add_block_form($form, &$form_state) {
       
   528   return block_admin_configure($form, $form_state, 'block', NULL);
       
   529 }
       
   530 
       
   531 /**
       
   532  * Form validation handler for block_add_block_form().
       
   533  *
       
   534  * @see block_add_block_form()
       
   535  * @see block_add_block_form_submit()
       
   536  */
       
   537 function block_add_block_form_validate($form, &$form_state) {
       
   538   $custom_block_exists = (bool) db_query_range('SELECT 1 FROM {block_custom} WHERE info = :info', 0, 1, array(':info' => $form_state['values']['info']))->fetchField();
       
   539 
       
   540   if (empty($form_state['values']['info']) || $custom_block_exists) {
       
   541     form_set_error('info', t('Ensure that each block description is unique.'));
       
   542   }
       
   543 }
       
   544 
       
   545 /**
       
   546  * Form submission handler for block_add_block_form().
       
   547  *
       
   548  * Saves the new custom block.
       
   549  *
       
   550  * @see block_add_block_form()
       
   551  * @see block_add_block_form_validate()
       
   552  */
       
   553 function block_add_block_form_submit($form, &$form_state) {
       
   554   $delta = db_insert('block_custom')
       
   555     ->fields(array(
       
   556       'body' => $form_state['values']['body']['value'],
       
   557       'info' => $form_state['values']['info'],
       
   558       'format' => $form_state['values']['body']['format'],
       
   559     ))
       
   560     ->execute();
       
   561   // Store block delta to allow other modules to work with new block.
       
   562   $form_state['values']['delta'] = $delta;
       
   563 
       
   564   $query = db_insert('block')->fields(array('visibility', 'pages', 'custom', 'title', 'module', 'theme', 'status', 'weight', 'delta', 'cache'));
       
   565   foreach (list_themes() as $key => $theme) {
       
   566     if ($theme->status) {
       
   567       $query->values(array(
       
   568         'visibility' => (int) $form_state['values']['visibility'],
       
   569         'pages' => trim($form_state['values']['pages']),
       
   570         'custom' => (int) $form_state['values']['custom'],
       
   571         'title' => $form_state['values']['title'],
       
   572         'module' => $form_state['values']['module'],
       
   573         'theme' => $theme->name,
       
   574         'status' => 0,
       
   575         'weight' => 0,
       
   576         'delta' => $delta,
       
   577         'cache' => DRUPAL_NO_CACHE,
       
   578       ));
       
   579     }
       
   580   }
       
   581   $query->execute();
       
   582 
       
   583   $query = db_insert('block_role')->fields(array('rid', 'module', 'delta'));
       
   584   foreach (array_filter($form_state['values']['roles']) as $rid) {
       
   585     $query->values(array(
       
   586       'rid' => $rid,
       
   587       'module' => $form_state['values']['module'],
       
   588       'delta' => $delta,
       
   589     ));
       
   590   }
       
   591   $query->execute();
       
   592 
       
   593   // Store regions per theme for this block
       
   594   foreach ($form_state['values']['regions'] as $theme => $region) {
       
   595     db_merge('block')
       
   596       ->key(array('theme' => $theme, 'delta' => $delta, 'module' => $form_state['values']['module']))
       
   597       ->fields(array(
       
   598         'region' => ($region == BLOCK_REGION_NONE ? '' : $region),
       
   599         'pages' => trim($form_state['values']['pages']),
       
   600         'status' => (int) ($region != BLOCK_REGION_NONE),
       
   601       ))
       
   602       ->execute();
       
   603   }
       
   604 
       
   605   drupal_set_message(t('The block has been created.'));
       
   606   cache_clear_all();
       
   607   $form_state['redirect'] = 'admin/structure/block';
       
   608 }
       
   609 
       
   610 /**
       
   611  * Form constructor for the custom block deletion form.
       
   612  *
       
   613  * @param $module
       
   614  *   The name of the module that implements the block to be deleted. This should
       
   615  *   always equal 'block' since it only allows custom blocks to be deleted.
       
   616  * @param $delta
       
   617  *   The unique ID of the block within the context of $module.
       
   618  *
       
   619  * @see block_custom_block_delete_submit()
       
   620  */
       
   621 function block_custom_block_delete($form, &$form_state, $module, $delta) {
       
   622   $block = block_load($module, $delta);
       
   623   $custom_block = block_custom_block_get($block->delta);
       
   624   $form['info'] = array('#type' => 'hidden', '#value' => $custom_block['info'] ? $custom_block['info'] : $custom_block['title']);
       
   625   $form['bid'] = array('#type' => 'hidden', '#value' => $block->delta);
       
   626 
       
   627   return confirm_form($form, t('Are you sure you want to delete the block %name?', array('%name' => $custom_block['info'])), 'admin/structure/block', '', t('Delete'), t('Cancel'));
       
   628 }
       
   629 
       
   630 /**
       
   631  * Form submission handler for block_custom_block_delete().
       
   632  *
       
   633  * @see block_custom_block_delete()
       
   634  */
       
   635 function block_custom_block_delete_submit($form, &$form_state) {
       
   636   db_delete('block_custom')
       
   637     ->condition('bid', $form_state['values']['bid'])
       
   638     ->execute();
       
   639   db_delete('block')
       
   640     ->condition('module', 'block')
       
   641     ->condition('delta', $form_state['values']['bid'])
       
   642     ->execute();
       
   643   db_delete('block_role')
       
   644     ->condition('module', 'block')
       
   645     ->condition('delta', $form_state['values']['bid'])
       
   646     ->execute();
       
   647   drupal_set_message(t('The block %name has been removed.', array('%name' => $form_state['values']['info'])));
       
   648   cache_clear_all();
       
   649   $form_state['redirect'] = 'admin/structure/block';
       
   650   return;
       
   651 }
       
   652 
       
   653 /**
       
   654  * Processes variables for block-admin-display-form.tpl.php.
       
   655  *
       
   656  * The $variables array contains the following arguments:
       
   657  * - $form
       
   658  *
       
   659  * @see block-admin-display.tpl.php
       
   660  * @see theme_block_admin_display()
       
   661  */
       
   662 function template_preprocess_block_admin_display_form(&$variables) {
       
   663   $variables['block_regions'] = $variables['form']['block_regions']['#value'];
       
   664   if (isset($variables['block_regions'][BLOCK_REGION_NONE])) {
       
   665     $variables['block_regions'][BLOCK_REGION_NONE] = t('Disabled');
       
   666   }
       
   667 
       
   668   foreach ($variables['block_regions'] as $key => $value) {
       
   669     // Initialize an empty array for the region.
       
   670     $variables['block_listing'][$key] = array();
       
   671   }
       
   672 
       
   673   // Initialize disabled blocks array.
       
   674   $variables['block_listing'][BLOCK_REGION_NONE] = array();
       
   675 
       
   676   // Add each block in the form to the appropriate place in the block listing.
       
   677   foreach (element_children($variables['form']['blocks']) as $i) {
       
   678     $block = &$variables['form']['blocks'][$i];
       
   679 
       
   680     // Fetch the region for the current block.
       
   681     $region = (isset($block['region']['#default_value']) ? $block['region']['#default_value'] : BLOCK_REGION_NONE);
       
   682 
       
   683     // Set special classes needed for table drag and drop.
       
   684     $block['region']['#attributes']['class'] = array('block-region-select', 'block-region-' . $region);
       
   685     $block['weight']['#attributes']['class'] = array('block-weight', 'block-weight-' . $region);
       
   686 
       
   687     $variables['block_listing'][$region][$i] = new stdClass();
       
   688     $variables['block_listing'][$region][$i]->row_class = !empty($block['#attributes']['class']) ? implode(' ', $block['#attributes']['class']) : '';
       
   689     $variables['block_listing'][$region][$i]->block_modified = !empty($block['#attributes']['class']) && in_array('block-modified', $block['#attributes']['class']);
       
   690     $variables['block_listing'][$region][$i]->block_title = drupal_render($block['info']);
       
   691     $variables['block_listing'][$region][$i]->region_select = drupal_render($block['region']) . drupal_render($block['theme']);
       
   692     $variables['block_listing'][$region][$i]->weight_select = drupal_render($block['weight']);
       
   693     $variables['block_listing'][$region][$i]->configure_link = drupal_render($block['configure']);
       
   694     $variables['block_listing'][$region][$i]->delete_link = !empty($block['delete']) ? drupal_render($block['delete']) : '';
       
   695     $variables['block_listing'][$region][$i]->printed = FALSE;
       
   696   }
       
   697 
       
   698   $variables['form_submit'] = drupal_render_children($variables['form']);
       
   699 }
       
   700