You are here

function mostpopular_blocks_admin_form in Drupal Most Popular 7

@file Provides an admin GUI for configuring most popular blocks.

1 string reference to 'mostpopular_blocks_admin_form'
mostpopular_menu in ./mostpopular.module
Implements hook_menu().

File

./mostpopular.blocks.inc, line 16
Provides an admin GUI for configuring most popular blocks.

Code

function mostpopular_blocks_admin_form($form, &$form_state) {
  if (!empty($form_state['confirm_delete'])) {
    $bid = $form_state['confirm_delete'];
    return confirm_form($form, t('Are you sure you want to delete block %id?', array(
      '%id' => $bid,
    )), 'admin/config/mostpopular/blocks', t('All services and intervals attached to this block will also be deleted.  Are you sure you want to do this?'), t('Yes, delete this block'), t('No, nevermind'));
  }
  $form['blocks'] = array(
    '#tree' => TRUE,
    '#theme' => 'mostpopular_admin_blocks_table',
    '#weight' => 0,
  );
  if (!isset($form_state['blocks'])) {
    $form_state['blocks'] = mostpopular_blocks();
  }
  foreach ($form_state['blocks'] as $bid => $block) {
    $form['blocks'][$bid] = array(
      'block' => array(
        '#type' => 'value',
        '#value' => $block,
      ),
      'id' => array(
        '#type' => 'item',
        '#title' => t('ID'),
        '#markup' => !empty($block->bid) ? check_plain($block->bid) : t('New Block'),
      ),
      'name' => array(
        '#type' => 'textfield',
        '#title' => t('Machine Name'),
        '#default_value' => check_plain($block->name),
        '#required' => TRUE,
        '#size' => 5,
      ),
      'title' => array(
        '#type' => 'textfield',
        '#title' => t('Title'),
        '#default_value' => check_plain($block->title),
        '#required' => TRUE,
      ),
    );
    if (empty($block->remote_bid)) {
      $form['blocks'][$bid]['remote'] = array(
        '#type' => 'markup',
        '#markup' => t('Local'),
      );
      $form['blocks'][$bid]['count'] = array(
        '#type' => 'textfield',
        '#title' => t('Max Results'),
        '#default_value' => check_plain($block->count),
        '#size' => 3,
        '#required' => TRUE,
      );
    }
    else {
      $form['blocks'][$bid]['remote'] = array(
        '#type' => 'markup',
        '#markup' => t('Remote'),
      );
      if (!empty($block->data['remote_database']) && !empty($block->remote_bid)) {
        db_set_active($block->data['remote_database']);
        $count = db_select('mostpopular_block', 'b')
          ->fields('b', array(
          'count',
        ))
          ->condition('bid', $block->remote_bid)
          ->execute()
          ->fetchColumn();
        db_set_active('default');
      }
      $form['blocks'][$bid]['count'] = array(
        '#type' => 'markup',
        '#markup' => isset($count) ? check_plain($count) : t('Unknown'),
      );
    }
    if (!empty($block->bid)) {
      $form['blocks'][$bid]['configure'] = array(
        '#type' => 'link',
        '#title' => t('Configure'),
        '#href' => "admin/structure/block/manage/mostpopular/{$block->bid}/manage",
        '#options' => array(
          'query' => drupal_get_destination(),
        ),
      );
      $form['blocks'][$bid]['delete'] = array(
        '#type' => 'submit',
        '#value' => t('Delete'),
        '#submit' => array(
          'mostpopular_blocks_admin_form_delete_block',
        ),
        '#limit_validation_errors' => array(),
        '#bid' => $block->bid,
        '#prefix' => ' ',
      );
    }
  }
  $form['add_local_block'] = array(
    '#type' => 'submit',
    '#value' => t('Create a new block'),
    '#submit' => array(
      'mostpopular_blocks_admin_form_add_local_block',
    ),
    '#weight' => 20,
    '#limit_validation_errors' => array(),
  );
  $form['add_remote_block'] = array(
    '#type' => 'submit',
    '#value' => t('Create a remote block'),
    '#submit' => array(
      'mostpopular_blocks_admin_form_add_remote_block',
    ),
    '#weight' => 20,
    '#limit_validation_errors' => array(),
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Save'),
    '#submit' => array(
      'mostpopular_blocks_admin_form_submit',
    ),
    '#weight' => 10,
  );
  return $form;
}