You are here

twitter_block.admin.inc in Twitter Block 7

Same filename and directory in other branches
  1. 7.2 twitter_block.admin.inc

Admin page callbacks for the Twitter Block module.

File

twitter_block.admin.inc
View source
<?php

/**
 * @file
 * Admin page callbacks for the Twitter Block module.
 */

/**
 * Form constructor for the add block form.
 *
 * @see twitter_block_add_block_form_validate()
 * @see twitter_block_add_block_form_submit()
 * @ingroup forms
 */
function twitter_block_add_block_form($form, &$form_state) {
  module_load_include('inc', 'block', 'block.admin');
  $form = block_admin_configure($form, $form_state, 'twitter_block', NULL);

  // Other modules should be able to use hook_form_block_add_block_form_alter()
  // to modify this form, so add a base form ID
  $form_state['build_info']['base_form_id'] = 'block_add_block_form';

  // Prevent block_add_block_form_validate/submit() from being automatically
  // added because of the base form ID by providing these handlers manually
  $form['#validate'] = array(
    'twitter_block_add_block_form_validate',
  );
  $form['#submit'] = array(
    'twitter_block_add_block_form_submit',
  );
  return $form;
}

/**
 * Form validation handler for twitter_block_add_block_form().
 *
 * @see twitter_block_add_block_form()
 * @see twitter_block_add_block_form_submit()
 */
function twitter_block_add_block_form_validate($form, &$form_state) {
  $twitter_block_exists = (bool) db_query_range('SELECT 1 FROM {twitter_block} WHERE info = :info', 0, 1, array(
    ':info' => $form_state['values']['info'],
  ))
    ->fetchField();
  if (empty($form_state['values']['info']) || $twitter_block_exists) {
    form_set_error('info', t('Ensure that each block description is unique.'));
  }
}

/**
 * Form submission handler for twitter_block_add_block_form().
 *
 * Saves the new custom Twitter block.
 *
 * @see twitter_block_add_block_form()
 * @see twitter_block_add_block_form_validate()
 */
function twitter_block_add_block_form_submit($form, &$form_state) {

  // Save the block configuration
  $delta = db_insert('twitter_block')
    ->fields(array(
    'info' => $form_state['values']['info'],
    'search_type' => $form_state['values']['search_type'],
    'include_rts' => $form_state['values']['include_rts'],
    'search_string' => $form_state['values']['search_string'],
    'results_per_page' => $form_state['values']['results_per_page'],
    'lang' => $form_state['values']['lang'],
  ))
    ->execute();

  // Store block delta to allow other modules to work with new block
  $form_state['values']['delta'] = $delta;

  // Run the normal new block submission (borrowed from block_add_block_form_submit)
  $query = db_insert('block')
    ->fields(array(
    'visibility',
    'pages',
    'custom',
    'title',
    'module',
    'theme',
    'status',
    'weight',
    'delta',
    'cache',
  ));
  foreach (list_themes() as $key => $theme) {
    if ($theme->status) {
      $query
        ->values(array(
        'visibility' => (int) $form_state['values']['visibility'],
        'pages' => trim($form_state['values']['pages']),
        'custom' => (int) $form_state['values']['custom'],
        'title' => $form_state['values']['title'],
        'module' => $form_state['values']['module'],
        'theme' => $theme->name,
        'status' => 0,
        'weight' => 0,
        'delta' => $delta,
        'cache' => DRUPAL_NO_CACHE,
      ));
    }
  }
  $query
    ->execute();
  $query = db_insert('block_role')
    ->fields(array(
    'rid',
    'module',
    'delta',
  ));
  foreach (array_filter($form_state['values']['roles']) as $rid) {
    $query
      ->values(array(
      'rid' => $rid,
      'module' => $form_state['values']['module'],
      'delta' => $delta,
    ));
  }
  $query
    ->execute();

  // Store regions per theme for this block
  foreach ($form_state['values']['regions'] as $theme => $region) {
    db_merge('block')
      ->key(array(
      'theme' => $theme,
      'delta' => $delta,
      'module' => $form_state['values']['module'],
    ))
      ->fields(array(
      'region' => $region == BLOCK_REGION_NONE ? '' : $region,
      'pages' => trim($form_state['values']['pages']),
      'status' => (int) ($region != BLOCK_REGION_NONE),
    ))
      ->execute();
  }
  drupal_set_message(t('The block has been created.'));
  cache_clear_all();
  $form_state['redirect'] = 'admin/structure/block';
}

/**
 * Form constructor for the custom Twitter block deletion form.
 *
 * @param $module
 *   The name of the module that implements the block to be deleted. This
 *   should always equal 'twitter_block' since it only allows custom Twitter
 *   blocks to be deleted.
 * @param $delta
 *   The unique ID of the block within the context of $module.
 *
 * @see twitter_block_delete_submit()
 */
function twitter_block_delete($form, &$form_state, $module, $delta) {
  $block = block_load($module, $delta);
  $twitter_block = twitter_block_block_get($block->delta);
  $form['info'] = array(
    '#type' => 'hidden',
    '#value' => $twitter_block['info'] ? $twitter_block['info'] : $twitter_block['title'],
  );
  $form['bid'] = array(
    '#type' => 'hidden',
    '#value' => $block->delta,
  );
  return confirm_form($form, t('Are you sure you want to delete the block %name?', array(
    '%name' => $twitter_block['info'],
  )), 'admin/structure/block', '', t('Delete'), t('Cancel'));
}

/**
 * Form submission handler for twitter_block_delete().
 *
 * @see twitter_block_delete()
 */
function twitter_block_delete_submit($form, &$form_state) {
  db_delete('twitter_block')
    ->condition('bid', $form_state['values']['bid'])
    ->execute();
  db_delete('block')
    ->condition('module', 'twitter_block')
    ->condition('delta', $form_state['values']['bid'])
    ->execute();
  db_delete('block_role')
    ->condition('module', 'twitter_block')
    ->condition('delta', $form_state['values']['bid'])
    ->execute();
  drupal_set_message(t('The block %name has been removed.', array(
    '%name' => $form_state['values']['info'],
  )));
  cache_clear_all();
  $form_state['redirect'] = 'admin/structure/block';
  return;
}

Functions

Namesort descending Description
twitter_block_add_block_form Form constructor for the add block form.
twitter_block_add_block_form_submit Form submission handler for twitter_block_add_block_form().
twitter_block_add_block_form_validate Form validation handler for twitter_block_add_block_form().
twitter_block_delete Form constructor for the custom Twitter block deletion form.
twitter_block_delete_submit Form submission handler for twitter_block_delete().