Index: multiblock.module =================================================================== RCS file: /cvs/drupal-contrib/contributions/modules/multiblock/multiblock.module,v retrieving revision 1.1.2.1 diff -u -p -r1.1.2.1 multiblock.module --- multiblock.module 18 May 2008 00:39:13 -0000 1.1.2.1 +++ multiblock.module 28 May 2008 21:14:11 -0000 @@ -1,5 +1,5 @@ MENU_NORMAL_ITEM, ); $items[] = array( - 'path' => 'multiblock/delete', + 'path' => 'admin/build/multiblock/delete', 'title' => t('Delete Block Instance'), - 'callback' => 'multiblock_delete', + 'callback' => 'drupal_get_form', + 'callback arguments' => array('multiblock_delete_form'), 'access' => user_access('administer blocks'), 'type' => MENU_CALLBACK, ); } - else { - } return $items; } @@ -34,13 +33,12 @@ function multiblock_menu($may_cache) { function multiblock_block($op = 'list', $delta = 0, $edit = array()) { if ($op == 'list') { // Get all of the block instances that exist. - $result = db_query("SELECT delta, title FROM {multiblock}"); - $blocks = array(); - // Escape the titles and return an array of them keyed by their NEW deltas. - while ($row = db_fetch_object($result)) { - $blocks[$row->delta] = array( 'info' => check_plain($row->title), ); + $blocks = multiblock_get_block(NULL); + $list = array(); + foreach ($blocks as $block) { + $list[$block->delta] = array('info' => check_plain($block->title)); } - return $blocks; + return $list; } // Any op besides list we want to dispatch the call to its respective module. else if ($op == 'view' || $op == 'configure' || $op == 'save') { @@ -49,6 +47,29 @@ function multiblock_block($op = 'list', } /** + * Fetch a given block from the multiblock database table. + * + * @param $delta + * Optional. Retreive a single block based on this delta. If none specified, + * all multiblock instances are returned. + * @param $reset + * Optional. Boolean value to reset the interal cache of this function. + */ +function multiblock_get_block($delta = NULL, $reset = FALSE) { + static $blocks; + + if (!isset($blocks) || $reset) { + $blocks = array(); + $result = db_query("SELECT * FROM {multiblock}"); + while ($row = db_fetch_object($result)) { + $blocks[$row->delta] = $row; + } + } + + return is_numeric($delta) ? $blocks[$delta] : $blocks; +} + +/** * Dispatch a hook_block call to it's respective module. Paramater $delta * is the new multiblock delta that we're using and $op is the op we are * dispatching. @@ -82,12 +103,7 @@ function multiblock_general() { $form = drupal_get_form('multiblock_add_block', $blocks); // Get an array of existing blocks. - $result = db_query('SELECT * FROM {multiblock} ORDER BY title'); - $multiblocks = array(); - while ($multiblock = db_fetch_object($result)) { - $multiblocks[] = $multiblock; - } - + $multiblocks = multiblock_get_block(NULL, TRUE); return theme('multiblock_general', $form, $multiblocks); } @@ -127,23 +143,44 @@ function multiblock_add_block($blocks) { return $form; } +function multiblock_delete_form($delta) { + $block = multiblock_get_block($delta); + + if (empty($block)) { + drupal_set_message(t('The multiblock with the delta @delta was not found.', array('@delta' => $delta)), 'error'); + return array(); + } + + $form['delta'] = array('#type' => 'value', '#value' => $delta); + return confirm_form($form, + t('Delete the block instance %title?', array('%title' => $block->title)), + 'admin/build/multiblock', + t('This will delete the instance of the block %title.', array('%title' => $block->title)), + t('Delete'), t('Cancel')); +} + +function multiblock_delete_form_submit($form_id, $form_values) { + if (multiblock_delete($form_values['delta'])) { + drupal_set_message(t('Block successfully deleted!')); + } + else { + drupal_set_message(t('There was a problem deleting the block')); + } + return 'admin/build/multiblock'; +} + /** - * Callback that deletes the block instance with a delta of arg(2) - * and redirects the user back to the "Manage Block Instances" form. + * Delete a multiblock instance. */ -function multiblock_delete() { - $multiblock_delta = arg(2); - $result = db_query('DELETE FROM {multiblock} WHERE delta=%d', (int)$multiblock_delta); - // If we actually deleted something. +function multiblock_delete($multiblock_delta) { + $result = db_query('DELETE FROM {multiblock} WHERE delta = %d', (int)$multiblock_delta); if (ctype_digit($multiblock_delta) && db_affected_rows() == 1) { - drupal_set_message(t('Block successfully deleted!')); _block_rehash(); + return TRUE; } else { - drupal_set_message(t('There was a problem deleting the block')); + return FALSE; } - drupal_goto('admin/build/multiblock'); - exit; } /** @@ -215,7 +252,7 @@ function theme_multiblock_general($add_b $header = array(t('Title'), t('Original Block Title'), t('Original Module'), t('Original Delta'), t('Action')); foreach ($multiblocks as $row) { - $delete_link = l(t('Delete'), 'multiblock/delete/'. $row->delta); + $delete_link = l(t('Delete'), 'admin/build/multiblock/delete/'. $row->delta); $title = multiblock_get_block_title($row->module, $row->orig_delta); $rows[] = array(check_plain($row->title), $title, $row->module, $row->orig_delta, $delete_link); }