',
);
$form['submit'] = array(
'#type' => 'submit',
'#value' => t('Delete'),
);
$form['cancel'] = array(
'#value' => "".t('Cancel')."",
);
return $form;
}
function tabbed_block_delete_submit( $form_id , $form_values ) {
$list = variable_get( 'tabbed_block_list' , NULL );
unset($list[arg(4)]);
variable_set( 'tabbed_block_list' , $list );
variable_del( 'tabbed_block_settings'.$form_values['id'] );
return 'admin/settings/tabbed_block';
}
/**
* core function to config wht is inside the tabbed block.
* the function will not do anything to DB until save is pressed
* so there is hidden fields to save the current settings until 'save'
*
* callback from tabbed_block_menu
*
* use non-drupal process flow.
*
* @return $form
*/
function tabbed_block_admin_settings( $form_values = NULL ){
global $tabbed_block_validation;
drupal_add_css(drupal_get_path('module', 'tabbed_block') .'/tabbed_block.css');
//get args passed in, to find the delta of this editing block
$module_delta = arg(4);
if ( $form_values['exists'] ){
for( $i=0 ; $i < count($form_values['exists']) ; $i++ ){
$block_list[] = $form_values['exists'][$i]['hidden'];
}
}
//process ops, before validation only
if ( $tabbed_block_validation ){
if ( empty($op) ){
for( $i=0 ; $i < count($form_values['exists']) ; $i++ ){
if ( $form_values['exists'][$i]['delete'] ){
$op = 'Delete';
$key = $i;
}elseif ( $form_values['exists'][$i]['up'] ){
$op = 'up';
$key = $i;
}elseif ( $form_values['exists'][$i]['down'] ){
$op = 'down';
$key = $i;
}
}
if ( $op == "Delete" ){
array_splice( $block_list , $key , 1 );
drupal_set_message("Deleted successfully. Note that this configuration will not be saved until you press the save buttom");
}elseif ( $op == "up" ){
//find the block need to move
$move_block = array_slice( $block_list , $key , 1 );
//delete the moved block from array
array_splice( $block_list , $key , 1 );
//a self defined function to perform re-index and insertion of $move_block
$block_list = array_move( $block_list , $move_block[0] , $key-1 );
drupal_set_message("Move up successfully. Note that this configuration will not be saved until you press the save buttom");
}elseif ( $op == "down" ){
//find the block need to move
$move_block = array_slice( $block_list , $key , 1 );
//delete the moved block from array
array_splice( $block_list , $key , 1 );
//a self defined function to perform re-index and insertion of $move_block
$block_list = array_move( $block_list , $move_block[0] , $key+1 );
drupal_set_message("Move down successfully. Note that this configuration will not be saved until you press the save buttom");
}
}
//process add block buttom
if ( $form_values['op'] == t('Add block') ){
//insert the selected block into settings
$block_list[] = $form_values['block'];
drupal_set_message("Added successfully. Note that this configuration will not be saved until you press the save buttom");
}elseif ($form_values['op'] == t('Delete all block') ){
$block_list = array();
drupal_set_message("Deleted successfully. Note that this configuration will not be saved until you press the save buttom");
}elseif ( $op == t("Save") ){
//dummy, never reach here
tabbed_block_admin_settings_submit( 'tabbed_block_admin_settings' , $form_values );
}elseif ( empty($op) ){
//save if and only if after this function is the second time arrive
tabbed_block_admin_settings_submit( 'tabbed_block_admin_settings' , $form_values );
}
$tabbed_block_validation = FALSE;
}else{
//if not POST data, get current setting from DB
$block_list = variable_get('tabbed_block_settings'.$module_delta, array());
}
$form['help'] = array(
'#type' => 'fieldset',
'#title' => t('Help'),
'#collapsible' => false,
'#weight' => -7,
);
$form['help']['text'] = array(
'#value' =>
t(
"Use the add buttom below to add block, in order
Or use the trash bin buttom to delete blocks.
"),
);
//generate an exists block list, with their names, position, and operation buttoms
$form['exists'] = array(
'#type' => 'fieldset',
'#title' => t('Existing block'),
'#collapsible' => true,
'#weight' => -6,
'#tree' => TRUE,
);
if ( $block_list ){
//init count of blocks
$max_i = count( $block_list )-1;
//generate form for each $block_list
for ( $i = 0 ; $i < count($block_list) ; $i++ ){
$block = $block_list[$i];
list( $name , $delta ) = explode( "-" , $block , 2 );
$blocks_name = module_invoke( $name, 'block', 'list');
$module_path = base_path() . drupal_get_path('module', 'tabbed_block');
//delete buttom
$form['exists'][$i]['delete'] = array(
'#type' => 'tabbed_block_imagebutton',
'#image' => base_path().drupal_get_path('module','tabbed_block').'/user-trash.png',
'#default_value' => 'Delete#'.$i, // original value of button text
'#prefix' => '
',
);
//move up buttom
if ( $i != 0 ){
$form['exists'][$i]['up'] = array(
'#type' => 'tabbed_block_imagebutton',
'#image' => base_path().drupal_get_path('module','tabbed_block').'/go-up.png',
'#default_value' => 'up#'.$i, // original value of button text
);
}else{
$form['exists'][$i]['up'] = array(
'#value' => '',
);
}
//move down buttom
if ( $i != $max_i ){
$form['exists'][$i]['down'] = array(
'#type' => 'tabbed_block_imagebutton',
'#image' => base_path().drupal_get_path('module','tabbed_block').'/go-down.png',
'#default_value' => 'down#'.$i, // original value of button text
);
}else{
$form['exists'][$i]['down'] = array(
'#value' => '',
);
}
//hidden field for records position on operations other than save
$form['exists'][$i]['hidden'] = array(
'#type' => 'hidden',
'#value' => $block,
);
//name of blocks for display
$form['exists'][$i]['name'] = array(
'#type' => 'item',
'#value' => $blocks_name[$delta]['info'],
'#suffix' => '
',
);
}
$form['reset'] = array(
'#type' => 'button',
'#value' => t('Delete all block'),
);
}else{
//message for no block configured
$form['exists']['message'] = array(
'#type' =>'markup',
'#value' => "There is no block added currently",
);
}
$form['add'] = array(
'#type' => 'fieldset',
'#title' => t('Add block to list'),
'#collapsible' => true,
'#weight' => -5,
);
//generate the select list of all blocks from all modules
foreach (module_list() as $module) {
$module_blocks = module_invoke($module, 'block', 'list');
// do not display blocks that is from tabbed_block, which means, do not support recursive blocks
if ($module_blocks && $module != 'tabbed_block') {
$array = array();
foreach ($module_blocks as $delta => $block) {
// strip_tags used because it goes through check_plain and that just looks bad.
$array["$module-$delta"] = strip_tags($block['info']);
}
$options[$module] = $array;
}
}
$form['add']['block'] = array(
'#type' => 'select',
'#options' => $options,
'#title' => t('Please select a block that you want to add'),
'#width' => 30,
);
$form['add']['submit'] = array(
'#type' => 'button',
'#value' => t('Add block'),
);
$form['delta'] = array(
'#type' => 'hidden',
'#value' => $module_delta,
);
$form['save']['submit'] = array(
'#type' => 'button',
'#value' => t('Save'),
);
$form['#multistep'] = TRUE;
$form['#redirect'] = FALSE;
return $form;
}
/**
* validate
* it is used to prevent double process of thr form, if op is not save
*
*/
function tabbed_block_admin_settings_validate( $form_id , $form_values ){
global $tabbed_block_validation;
$tabbed_block_validation = TRUE;
}
/**
* submit
* this function will not do anything unless $form_values['exists'] is set to
* which means, hook_submit do not process anything, as ['exists'] is build by tabbed_block_admin_settings(), if validation is true
*
* it is only called by tabbed_block_admin_setting(), as a normal function call.
*/
function tabbed_block_admin_settings_submit( $form_id , $form_values ){
//process submit
$variable = 'tabbed_block_settings'.arg(4);
//init exists blocks from POST instead of database settings
if ( $form_values['exists'] ){
for( $i=0 ; $i < count($form_values['exists']) ; $i++ ){
$block_list[] = $form_values['exists'][$i]['hidden'];
}
if ($form_values['op'] == t('Save') ){
variable_set( $variable , $block_list );
drupal_set_message( "Blocks saved sucessfully." );
}
}else{
//nothing here. delete all blocks will arrive true here.
variable_del( $variable );
drupal_set_message( "All Blocks deleted sucessfully." );
}
return 'admin/settings/tabbed_block';
}
/**
* define a function that move values to a specific index inside array
*
* @param $in_array: the array that need to be inserted
* @param $insert: the insert element
* @param $new_key: the position of the insert element
*
* @return $array: the sortted array
*/
function array_move( $in_array , $insert , $new_key ){
$new_array = array();
reset( $in_array );
//as there is one more element have to insert, so $i<= count
for ( $i = 0 ; $i <= count( $in_array ) ; $i++ ){
if ( $i == $new_key ){
$new_array[] = $insert;
}else{
$new_array[] = current( $in_array );
next( $in_array );
}
}
return $new_array;
}
function tabbed_block_elements() { // Change this line
$type['tabbed_block_imagebutton'] = array(
'#submit' => TRUE,
'#input' => TRUE,
'#button_type' => 'submit',
'#executes_submit_callback' => TRUE,
);
return $type;
}
function theme_tabbed_block_imagebutton($element) {
return '";
}
function tabbed_block_imagebutton_value($var) {
// null function guarantees default_value doesn't get moved to #value.
}