You are here

radioactivity_arbitrary_source.module in Radioactivity 5

File

plugins/radioactivity_arbitrary_source.module
View source
<?php

function radioactivity_arbitrary_source_help($section = '') {
  $output = '';
  switch ($section) {
    case "admin/help#radioactivity_arbitrary_source":
      $output = '<p>' . t('Provides arbitrary configurable radioactivity sources for any radioactivity ' . 'target. Note that this module does not contain triggers for the sources. ' . 'To utilize these sources, use <code>radioactivity_add_energy()</code> in ' . 'custom code or modules such as <code>radioactivity_http</code>.') . '</p>';
      break;
  }
  return $output;
}
function _radioactivity_get_arbitrary_sources() {
  return variable_get('radioactivity_arbitrary_sources', array());
}
function radioactivity_arbitrary_source_radioactivity_info() {
  global $radioactivity_arbitrary_source_skip_radioactivity_info;
  if ($radioactivity_arbitrary_source_skip_radioactivity_info) {
    return array();
  }
  return array(
    'sources' => _radioactivity_get_arbitrary_sources(),
  );
}
function radioactivity_arbitrary_source_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/radioactivity/arbitrary_sources',
      'title' => t('Arbitrary sources'),
      'description' => t('Arbitrary sources configuration'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'radioactivity_arbitrary_source_admin_list',
      ),
      'access' => user_access(RADIOACTIVITY_PERM_ADMIN),
      'weight' => 20,
      'type' => MENU_LOCAL_TASK,
    );
    $items[] = array(
      'path' => 'admin/settings/radioactivity/arbitrary_source',
      'title' => t('Arbitrary source edit'),
      'description' => t('Edit arbitrary source'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'radioactivity_arbitrary_source_admin_source',
      ),
      'access' => user_access(RADIOACTIVITY_PERM_ADMIN),
      'type' => MENU_CALLBACK,
    );
    $items[] = array(
      'path' => 'admin/settings/radioactivity/arbitrary_source_delete',
      'title' => t('Confirm delete arbitrary source'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array(
        'radioactivity_arbitrary_source_admin_delete',
      ),
      'access' => user_access(RADIOACTIVITY_PERM_ADMIN),
      'type' => MENU_CALLBACK,
    );
  }
  return $items;
}
function radioactivity_arbitrary_source_admin_list() {
  global $radioactivity_arbitrary_source_skip_radioactivity_info;
  $radioactivity_arbitrary_source_skip_radioactivity_info = TRUE;
  $sources = _radioactivity_get_arbitrary_sources();
  $info = radioactivity_get_radioactivity_info();
  $form = array();
  foreach (array_keys($info['targets']) as $target) {
    $form[$target] = array(
      '#type' => 'fieldset',
      '#title' => t('Sources for #target', array(
        '#target' => $target,
      )),
      '#description' => t('Configure arbitrary radioactivity energy sources for #target', array(
        '#target' => $target,
      )),
    );
    $rows = array();
    foreach ($info['sources'][$target] as $id => $data) {
      $rows[] = array(
        'data' => array(
          $id,
          $data['title_placeholder'],
          t('Non-configurable'),
        ),
      );
    }
    if (count($sources[$target])) {
      foreach ($sources[$target] as $id => $data) {
        $row = array(
          'data' => array(
            $id,
            $data['title_placeholder'],
            l(t('Edit'), 'admin/settings/radioactivity/arbitrary_source/' . $target . '/' . $id) . ' ' . l(t('Delete'), 'admin/settings/radioactivity/arbitrary_source_delete/' . $target . '/' . $id),
          ),
        );

        // check for conflicting row
        if (isset($info['sources'][$target][$id])) {
          $row['class'] = 'error';
          drupal_set_message(t('Conflicting source definition detected. Please remove arbitrary source %s for target %t. ' . 'It is already provided by another plugin.', array(
            '%s' => $id,
            '%t' => $target,
          )), 'error');
        }
        $rows[] = $row;
      }
    }
    $table = theme('table', array(
      t('Source identifier'),
      t('Source label'),
      t('Actions'),
    ), $rows);
    $form[$target]['arb_sources'] = array(
      '#value' => $table,
    );
    if (count($sources[$target]) == 0) {
      $form[$target]['no_sources'] = array(
        '#type' => 'item',
        '#value' => t('No arbitrary sources configured.'),
      );
    }
    $form[$target]['new_source'] = array(
      '#type' => 'item',
      '#value' => l(t('New source'), 'admin/settings/radioactivity/arbitrary_source/' . $target),
    );
  }
  return $form;
}
function radioactivity_arbitrary_source_admin_source($target = NULL, $source = NULL) {
  $sources = _radioactivity_get_arbitrary_sources();
  $form = array();
  $form['#action'] = url('admin/settings/radioactivity/arbitrary_source');
  $form['target_hidden'] = array(
    '#type' => 'hidden',
    '#default_value' => $target,
  );
  $form['source_hidden'] = array(
    '#type' => 'hidden',
    '#default_value' => $source,
  );
  $form['target'] = array(
    '#type' => 'item',
    '#title' => t('Target name'),
    '#value' => check_plain($target),
  );
  $form['identifier'] = array(
    '#type' => 'textfield',
    '#title' => t('Source identifier'),
    '#default_value' => $source,
    '#required' => TRUE,
    '#description' => t('This is the source identifier. You may use lower case alphabets a-z, numbers, and underscore.'),
  );
  $form['title'] = array(
    '#type' => 'textfield',
    '#title' => t('Source label'),
    '#default_value' => $sources[$target][$source]['title_placeholder'],
    '#description' => t('This is the human readable name of the source.'),
  );
  $form['submit'] = array(
    '#type' => 'submit',
  );
  if ($source) {
    $form['submit']['#value'] = t('Edit');
  }
  else {
    $form['submit']['#value'] = t('Create');
  }
  $form['cancel']['#value'] = l(t('Cancel'), 'admin/settings/radioactivity/arbitrary_sources');
  return $form;
}
function radioactivity_arbitrary_source_admin_source_validate($form_id, $form) {
  $info = radioactivity_get_radioactivity_info();
  $sources = _radioactivity_get_arbitrary_sources();

  // check that target is valid
  if (!isset($info['targets'][$form['target_hidden']])) {
    drupal_set_message(t('Invalid target: "%t"', array(
      '%t' => ${$form}['target_hidden'],
    )));
    drupal_goto('admin/settings/radioactivity/arbitrary_sources');
  }

  // check that source is valid
  if ($form['source_hidden'] && !isset($sources[$form['target_hidden']][$form['source_hidden']])) {
    drupal_set_message(t('Invalid source: "%t"', array(
      '%t' => $form['source_hidden'],
    )));
    drupal_goto('admin/settings/radioactivity/arbitrary_sources');
  }

  // validate that identifier uses only a-z, 0-9, _
  if (!preg_match('@^[a-z0-9_]+$@', $form['identifier'])) {
    form_set_error('identifier', t('Malformed identifier'));
  }
  else {
    global $radioactivity_arbitrary_source_skip_radioactivity_info;
    $radioactivity_arbitrary_source_skip_radioactivity_info = TRUE;
    $info = radioactivity_get_radioactivity_info(TRUE);

    // check that we are not overriding any source provided by another plugin
    if (isset($info['sources'][$form['target_hidden']][$form['identifier']])) {
      form_set_error('identifier', t('Identifier already provided by another plugin'));
    }

    // check that we are not overriding any source that is already configured
    if ($form['identifier'] != $form['source_hidden'] && isset($sources[$form['target_hidden']][$form['identifier']])) {
      form_set_error('identifier', t('Identifier already configured'));
    }
  }
}
function radioactivity_arbitrary_source_admin_source_submit($form_id, $form) {
  $identifier = $form['identifier'];
  $source = $form['source_hidden'];
  $target = $form['target_hidden'];
  drupal_set_message(t('Saved arbitrary source %s for target %t', array(
    '%s' => $identifier,
    '%t' => $target,
  )));
  $sources = _radioactivity_get_arbitrary_sources();
  $title = $form['title'];
  if (!$title) {
    $title = $identifier;
  }
  if ($source) {
    unset($sources[$target][$source]);
  }
  $sources[$target][$identifier] = array(
    'title_placeholder' => $title,
  );
  variable_set('radioactivity_arbitrary_sources', $sources);
  drupal_goto('admin/settings/radioactivity/arbitrary_sources');
}
function radioactivity_arbitrary_source_admin_delete($target, $source) {
  $form = array();
  drupal_set_title(t('Confirm delete arbitrary source %s for target %t', array(
    '%s' => $source,
    '%t' => $target,
  )));
  $form['source'] = array(
    '#type' => 'hidden',
    '#default_value' => $source,
  );
  $form['target'] = array(
    '#type' => 'hidden',
    '#default_value' => $target,
  );
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Delete'),
  );
  $form['cancel'] = array(
    '#value' => l(t('Cancel'), 'admin/settings/radioactivity/arbitrary_sources'),
  );
  return $form;
}
function radioactivity_arbitrary_source_admin_delete_submit($form_id, $form) {
  $source = $form['source'];
  $target = $form['target'];
  drupal_set_message(t('Deleted arbitrary source %s for target %t', array(
    '%s' => $source,
    '%t' => $target,
  )));
  $sources = _radioactivity_get_arbitrary_sources();
  unset($sources[$target][$source]);
  variable_set('radioactivity_arbitrary_sources', $sources);
  drupal_goto('admin/settings/radioactivity/arbitrary_sources');
}