$block,
'ops' => $ops,
);
}
return theme('relevant_content_admin_overview', $rows);
}
/**
* Preprocess handler for the overview rows
*/
function template_preprocess_relevant_content_admin_overview(&$vars) {
drupal_add_css(drupal_get_path('module', 'relevant_content') .'/relevant_content.admin.css');
foreach ($vars['rows'] as $block_id => $row) {
// Render the Types and Vocabs
$row['types_rendered'] = implode(', ', $row['block']['types']);
$row['vocabs_rendered'] = implode(', ', $row['block']['vocabs']);
// Render the "ops"
$row['ops_rendered'] = implode(' | ', $row['ops']);
// Render the "absolute links" boolean
$row['absolute_links_rendered'] = $row['block']['absolute_links'] ? t('Absolute') : t('Relative');
// Render the header
$row['header_rendered'] = check_markup($row['block']['header_text'], $row['block']['header_format'], FALSE);
// Define the classes
$row['row_classes'] = array();
$row['row_classes'][] = 'relevant-content-block';
switch ($row['block']['storage']) {
case RELEVANT_CONTENT_STORAGE_NORMAL :
$row['row_classes'][] = 'relevant-content-block-normal';
$row['status'] = t('Normal');
break;
case RELEVANT_CONTENT_STORAGE_DEFAULT :
$row['row_classes'][] = 'relevant-content-block-default';
$row['status'] = t('Default');
break;
case RELEVANT_CONTENT_STORAGE_OVERRIDDEN :
$row['row_classes'][] = 'relevant-content-block-overriden';
$row['status'] = t('Overriden');
break;
}
$row['row_classes'][] = 'relevant-content-block-'. ($row['block']['status'] ? 'enabled' : 'disabled');
// Output a 'rendered' set of classes
$row['row_classes_rendered'] = implode(' ', $row['row_classes']);
$vars['rows'][$block_id] = $row;
}
}
/**
* Private function used as a callback for array_map which sets the item to the value of $value using $key as either an array offset or an object property.
*
* @param $item
* This is a variable reference to the item being mapped to. Chaning this value will change the value in the array being mapped using array_map
* @param $key
* They key of $item in the array being mapped
* @param $values
* A user defined array passed in. In this case, it us used for reference purposes
*/
function _relevant_content_array_map_key_to_values(&$item, $key, $values) {
if (isset($values[$key])) {
if (is_object($values[$key])) {
$item = $values[$key]->name;
}
else {
$item = $values[$key];
}
}
else {
$item = t('Error: Item Missing');
}
}
/**
* Form API based function which generates the block Add AND Edit form.
* The form is an 'edit' form if a block settngs array is passed in.
* @param $block
* The block settings array of the block being editted.
*/
function relevant_content_admin_block_form($form_state, $block = NULL) {
// Get the defaults for the form - ensures a common place for defining
$defaults = _relevant_content_santize_block();
// Init the form
$form = array();
// Need a psuedo contains for the settings
$form['settings'] = array(
'#tree' => TRUE,
);
if (isset($block)) {
$form['settings']['id'] = array(
'#type' => 'value',
'#value' => $block['id'],
);
$form['settings']['id_field'] = array(
'#type' => 'item',
'#title' => t('Block ID'),
'#description' => t('The ID is used to uniquely identify a block'),
'#value' => check_plain($block['id']),
);
}
else {
$form['settings']['id'] = array(
'#type' => 'textfield',
'#title' => t('Block ID'),
'#description' => t('The ID is used to uniquely identify a block. Please user uppercase & lowercase characters, numbers and underscores only'),
'#required' => TRUE,
'#maxlength' => 255,
'#size' => 12,
);
}
$form['settings']['types'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled Content Types'),
'#description' => t('Check the content types you would like to search for.'),
'#options' => node_get_types('names'),
'#default_value' => isset($block) ? $block['types'] : $defaults['types'],
);
$vocabs = array();
foreach (taxonomy_get_vocabularies() as $vid => $voc) {
$vocabs[$vid] = $voc->name;
}
$form['settings']['vocabs'] = array(
'#type' => 'checkboxes',
'#title' => t('Enabled Vocabularies'),
'#description' => t('Check the vocabularies you would like to search for'),
'#options' => $vocabs,
'#default_value' => isset($block) ? $block['vocabs'] : $defaults['vocabs'],
);
$form['settings']['max_items'] = array(
'#type' => 'textfield',
'#title' => t('Limit'),
'#description' => t('What is the maximum number of results would you like returned?'),
'#size' => 3,
'#maxlength' => 3,
'#required' => TRUE,
'#default_value' => isset($block) ? $block['max_items'] : $defaults['max_items'],
);
$form['settings']['header'] = array(
'#type' => 'fieldset',
'#title' => t('Header'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
);
$form['settings']['header']['header_text'] = array(
'#type' => 'textarea',
'#title' => t('Header Text'),
'#description' => t('Optionally provide some text to appear above the listing'),
'#rows' => 3,
'#default_value' => isset($block) ? $block['header_text'] : $defaults['header_text'],
'#parents' => array('settings', 'header_text'),
);
$format = isset($block) ? $block['header_format'] : $defaults['header_format'];
$form['settings']['header']['header_format'] = filter_form($format, NULL, array('settings', 'header_format'));
if (module_exists('token')) {
$form['settings']['token_settings_wrapper'] = array(
'#type' => 'fieldset',
'#title' => T('Token Settings'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#parents' => array('settings'),
);
$form['settings']['token_settings_wrapper']['token_settings'] = array(
'#type' => 'textfield',
'#title' => t('Token pattern'),
'#description' => t('Optionally define a token pattern here to override the default output. Please use plain text only.'),
'#default_value' => isset($block) ? $block['token_settings'] : $defaults['token_settings'],
'#parents' => array('settings', 'token_settings'),
);
$form['settings']['token_settings_wrapper']['token_help'] = array(
'#type' => 'markup',
'#value' => theme('token_tree', array('global', 'node')),
);
}
$form['settings']['options'] = array(
'#type' => 'fieldset',
'#title' => T('Options'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#parents' => array('settings'),
);
$form['settings']['options']['absolute_links'] = array(
'#type' => 'checkbox',
'#title' => t('Absolute Links'),
'#description' => t('If enabled, the generated URL will be absolute rather than relative.'),
'#default_value' => isset($block) ? $block['absolute_links'] : $defaults['absolute_links'],
);
$form['settings']['options']['status'] = array(
'#type' => 'checkbox',
'#title' => t('Status'),
'#description' => t('If enabled, the block will appear in the lists. If disabled, the settings will be stored but the block will not appear in the block listings or anywhere on the site'),
'#default_value' => isset($block) ? $block['status'] : $defaults['status'],
);
$form['settings']['op'] = array(
'#type' => 'value',
'#value' => isset($block) ? 'edit' : 'add',
);
$form['settings']['submit'] = array(
'#type' => 'submit',
'#value' => t('Save Settings'),
);
return $form;
}
/**
* Validate function for the above form
*/
function relevant_content_admin_block_form_validate($form, &$form_state) {
if ($form_state['values']['settings']['op'] == 'add') {
if (preg_match('#[^a-zA-Z0-9_]#', $form_state['values']['settings']['id'])) {
form_set_error('settings][id', t('Invalid character detected. Please only use uppercase and lowercase characters, numbers and underscores'));
return;
}
// Try to load the block - if it loads, it exists already... Fail validaton
if (relevant_content_get_settings($form_state['values']['settings']['id'])) {
form_set_error('settings][id', t('This ID has already been used, please try another'));
}
}
// Is the max_items numeric and is it more than zero?
if (!is_numeric($form_state['values']['settings']['max_items']) || $form_state['values']['settings']['max_items'] <= 0) {
form_set_error('settings][max_items', t('The limit must be a positive numeric value, eg 5.'));
}
// Check we have selected some types
if (count(array_filter($form_state['values']['settings']['types'])) == 0) {
form_set_error('settings][types', t('You must select at least one content type to display.'));
}
// Check we have selected some vocabs
if (count(array_filter($form_state['values']['settings']['vocabs'])) == 0) {
form_set_error('settings][vocabs', t('You must select at least one vocabulary to limit your term searching to.'));
}
}
/**
* Submit handler for the block add/edit form.
*/
function relevant_content_admin_block_form_submit($form, &$form_state) {
// Pull the block settings from the form
$block = $form_state['values']['settings'];
// Sanitize some of the values/arrays
$block['token_settings'] = isset($block['token_settings']) ? trim($block['token_settings']) : '';
$block['types'] = array_filter($block['types']);
$block['vocabs'] = array_filter($block['vocabs']);
// Save it
relevant_content_save_block($block);
// Set a redirect
$form_state['redirect'] = 'admin/settings/relevant_content';
}
/**
* Operator Page Callback - this dispatches to the appropriate confirm form
*/
function relevant_content_admin_block_operator_callback($block, $op) {
return drupal_get_form($op['callback'], $block);
}
/**
* Form API based function for the confirmation page for deleting a block from the settings.
*/
function relevant_content_admin_delete_confirm($form_state, $block) {
$form = array();
$form['#block'] = $block;
return confirm_form(
$form,
t('Are you sure you want to delete block "%block"?', array('%block' => $block['id'])),
'admin/settings/relevant_content',
t('Note: This action cannot be undone')
);
}
/**
* Delete confirmation form submission handler.
*/
function relevant_content_admin_delete_confirm_submit($form, &$form_state) {
relevant_content_delete_block($form['#block']['id']);
$form_state['redirect'] = 'admin/settings/relevant_content';
}
/**
* Form API based function for the confirmation page for reverting a block from the settings.
*/
function relevant_content_admin_revert_confirm($form_state, $block) {
$form = array();
$form['#block'] = $block;
return confirm_form(
$form,
t('Are you sure you want to revert block "%block"?', array('%block' => $block['id'])),
'admin/settings/relevant_content',
t('Note: This action cannot be undone. Any changes made to this block will be lost and the block will revert to the settings provided by the its module.')
);
}
/**
* Revert confirmation form submission handler.
*/
function relevant_content_admin_revert_confirm_submit($form, &$form_state) {
// A revert is basically a delete...
relevant_content_delete_block($form['#block']['id']);
$form_state['redirect'] = 'admin/settings/relevant_content';
}
/**
* Form API based function for the confirmation page for disableing a block from the settings.
*/
function relevant_content_admin_disable_confirm($form_state, $block) {
$form = array();
$form['#block'] = $block;
return confirm_form(
$form,
t('Are you sure you want to disable block "%block"?', array('%block' => $block['id'])),
'admin/settings/relevant_content',
t('Note: Disabled blocks are not deleted, however they do not appear on the block listing page.')
);
}
/**
* Revert confirmation form submission handler.
*/
function relevant_content_admin_disable_confirm_submit($form, &$form_state) {
// A disable is basically a delete...
relevant_content_set_block_status($form['#block']['id'], 0);
$form_state['redirect'] = 'admin/settings/relevant_content';
}
/**
* Form API based function for the confirmation page for disableing a block from the settings.
*/
function relevant_content_admin_enable_confirm($form_state, $block) {
$form = array();
$form['#block'] = $block;
return confirm_form(
$form,
t('Are you sure you want to enable block "%block"?', array('%block' => $block['id'])),
'admin/settings/relevant_content',
t('Note: Once enabled, this block will appear on the block listing page. You will still need to assign it to a region.')
);
}
/**
* Revert confirmation form submission handler.
*/
function relevant_content_admin_enable_confirm_submit($form, &$form_state) {
// A disable is basically a delete...
relevant_content_set_block_status($form['#block']['id'], 1);
$form_state['redirect'] = 'admin/settings/relevant_content';
}