| 1 |
<?php |
<?php |
| 2 |
|
// $Id$ |
| 3 |
|
|
| 4 |
/** |
/** |
| 5 |
* @file Block CPR Admin Functions |
* @file |
| 6 |
|
* Block CPR Admin Functions |
| 7 |
*/ |
*/ |
| 8 |
|
|
| 9 |
|
|
| 10 |
|
/** |
| 11 |
|
* Overview form |
| 12 |
|
*/ |
| 13 |
function block_cpr_overview_form() { |
function block_cpr_overview_form() { |
| 14 |
$form = array(); |
// Get the settings and roles |
| 15 |
$settings = variable_get('block_cpr_settings', array()); |
$settings = variable_get('block_cpr_settings', array()); |
| 16 |
|
|
|
foreach ($settings as $delta => $settings) { |
|
|
$ops = array(); |
|
|
$ops[] = l(t('Edit'), 'admin/build/block/cpr/edit/'. $delta); |
|
|
$ops[] = l(t('Delete'), 'admin/build/block/cpr/delete/'. $delta); |
|
|
|
|
|
$form['delta'][$delta] = array('#type' => 'markup', '#value' => check_markup($delta)); |
|
|
$form['ops'][$delta] = array('#type' => 'markup', '#value' => implode(' | ', $ops)); |
|
|
|
|
|
} |
|
|
|
|
|
$form = system_settings_form($form); |
|
|
return $form; |
|
|
} |
|
|
|
|
|
|
|
|
function theme_block_cpr_overview_form($form) { |
|
| 17 |
$rows = array(); |
$rows = array(); |
| 18 |
foreach (element_children($form['delta']) as $delta) { |
|
| 19 |
$rows[] = array( |
// If there are no settings, one full row informing the user there are no blocks |
| 20 |
drupal_render($form['delta'][$delta]), |
if (empty($settings)) { |
|
drupal_render($form['ops'][$delta]), |
|
|
); |
|
|
} |
|
|
if (empty($rows)) { |
|
| 21 |
$rows[] = array( |
$rows[] = array( |
| 22 |
array('data' => t('No CPR Blocks configured'), 'colspan' => 2), |
array('data' => t('No CPR Blocks configured'), 'colspan' => 3), |
| 23 |
); |
); |
| 24 |
} |
} |
| 25 |
|
else { |
| 26 |
|
// Get the system roles |
| 27 |
|
$roles = user_roles(); |
| 28 |
|
|
| 29 |
$output = theme('table', array(t('Delta'), t('Ops')), $rows); |
// For each setting, display the delta, the enabled roles as an item list and finally some operations. |
| 30 |
$output .= drupal_render($form); |
foreach ($settings as $delta => $s) { |
| 31 |
|
$enabled_roles = array(); |
| 32 |
|
foreach ($s as $role_id => $rs) { |
| 33 |
|
$enabled_roles[] = check_plain($roles[$role_id]); |
| 34 |
|
} |
| 35 |
|
|
| 36 |
|
$ops = array(); |
| 37 |
|
$ops[] = l(t('Edit'), 'admin/build/block/cpr/edit/'. $delta); |
| 38 |
|
$ops[] = l(t('Delete'), 'admin/build/block/cpr/delete/'. $delta); |
| 39 |
|
|
| 40 |
|
$rows[] = array( |
| 41 |
|
check_plain($delta), |
| 42 |
|
theme('item_list', $enabled_roles), |
| 43 |
|
implode(' | ', $ops), |
| 44 |
|
); |
| 45 |
|
} |
| 46 |
|
} |
| 47 |
|
|
| 48 |
return $output; |
// Return the resulting rows as a table |
| 49 |
|
return theme('table', array(t('Delta'), t('Roles Enabled'), t('Ops')), $rows, array('id' => 'block_cpr_overview')); |
| 50 |
} |
} |
| 51 |
|
|
| 52 |
|
|
| 53 |
|
/** |
| 54 |
|
* The individual block settings form. This isused for adding AND editing |
| 55 |
|
*/ |
| 56 |
function block_cpr_block_settings_form($delta = NULL) { |
function block_cpr_block_settings_form($delta = NULL) { |
| 57 |
if ($delta) { |
if ($delta) { |
| 58 |
// Get the settings |
// Get the settings |
| 64 |
drupal_set_breadcrumb($breadcrumb); |
drupal_set_breadcrumb($breadcrumb); |
| 65 |
} |
} |
| 66 |
|
|
| 67 |
|
// Get the site roles |
| 68 |
|
|
| 69 |
|
// If we have any settings for this delta (eg edit rather then add)... |
| 70 |
$roles = user_roles(); |
$roles = user_roles(); |
| 71 |
if (isset($settings[$delta])) { |
if (isset($settings[$delta])) { |
| 72 |
|
// ... then generate the list of roles using the existing sorted roles first and append any other roles onto the end (+ does not overwrite existing keys) |
| 73 |
$ordered_roles = array_keys($settings[$delta]) + array_keys($roles); |
$ordered_roles = array_keys($settings[$delta]) + array_keys($roles); |
| 74 |
} |
} |
| 75 |
else { |
else { |
| 76 |
|
// ... otherwise just make a list of site roles |
| 77 |
$ordered_roles = array_keys($roles); |
$ordered_roles = array_keys($roles); |
| 78 |
} |
} |
| 79 |
|
|
| 104 |
'#title' => t('Role Content'), |
'#title' => t('Role Content'), |
| 105 |
); |
); |
| 106 |
|
|
| 107 |
|
// Each role gets its own row to determin if it isenabled, its weight and its content. |
| 108 |
foreach ($ordered_roles as $rid) { |
foreach ($ordered_roles as $rid) { |
| 109 |
$form['roles']['enabled'][$rid] = array('#type' => 'checkbox', '#default_value' => isset($settings[$delta][$rid])); |
$form['roles']['enabled'][$rid] = array('#type' => 'checkbox', '#default_value' => isset($settings[$delta][$rid])); |
| 110 |
$form['roles']['role'][$rid] = array('#type' => 'markup', '#value' => $roles[$rid]); |
$form['roles']['role'][$rid] = array('#type' => 'markup', '#value' => $roles[$rid]); |
| 113 |
$form['roles']['content'][$rid]['format'] = filter_form(isset($settings[$delta][$rid]['content']['format']) ? $settings[$delta][$rid]['content']['format'] : FILTER_FORMAT_DEFAULT, NULL, array('roles', 'content', $rid, 'format')); |
$form['roles']['content'][$rid]['format'] = filter_form(isset($settings[$delta][$rid]['content']['format']) ? $settings[$delta][$rid]['content']['format'] : FILTER_FORMAT_DEFAULT, NULL, array('roles', 'content', $rid, 'format')); |
| 114 |
} |
} |
| 115 |
|
|
|
$form['token_help'] = array( |
|
|
'#type' => 'fieldset', |
|
|
'#title' => t('Token Help'), |
|
|
'#collapsed' => TRUE, |
|
|
'#collapsible' => TRUE, |
|
|
); |
|
|
|
|
|
$form['token_help']['help'] = array( |
|
|
'#type' => 'markup', '#value' => theme('token_help', 'global'), |
|
|
); |
|
|
|
|
|
|
|
| 116 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save')); |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save')); |
| 117 |
return $form; |
return $form; |
| 118 |
} |
} |
| 119 |
|
|
| 120 |
|
|
| 121 |
|
/** |
| 122 |
|
* Theme handler for the block settings |
| 123 |
|
*/ |
| 124 |
function theme_block_cpr_block_settings_form($form) { |
function theme_block_cpr_block_settings_form($form) { |
| 125 |
$output = ''; |
$output = ''; |
| 126 |
|
|
| 127 |
|
// Get a reference to the roles section - cleaner code |
| 128 |
$roles = &$form['roles']; |
$roles = &$form['roles']; |
| 129 |
$rows = array(); |
$rows = array(); |
| 130 |
|
|
| 131 |
|
// For each role element, render a table row for the checkbox, title, weight and content fieldset |
| 132 |
foreach (element_children($roles['role']) as $rid) { |
foreach (element_children($roles['role']) as $rid) { |
| 133 |
$rows[] = array( |
$rows[] = array( |
| 134 |
drupal_render($roles['enabled'][$rid]), |
drupal_render($roles['enabled'][$rid]), |
| 138 |
); |
); |
| 139 |
} |
} |
| 140 |
|
|
| 141 |
|
// Render the table into the roles fieldset value |
| 142 |
$roles['#value'] = theme('table', array(t('Enabled'), t('Role'), t('Weight'), t('Content')), $rows); |
$roles['#value'] = theme('table', array(t('Enabled'), t('Role'), t('Weight'), t('Content')), $rows); |
| 143 |
|
|
| 144 |
|
// Render the entire form out |
| 145 |
return drupal_render($form); |
return drupal_render($form); |
| 146 |
} |
} |
| 147 |
|
|
| 148 |
|
|
| 149 |
|
/** |
| 150 |
|
* Validation handler for the block settings form. |
| 151 |
|
*/ |
| 152 |
function block_cpr_block_settings_form_validate($form_id, $form_values, $delta = NULL) { |
function block_cpr_block_settings_form_validate($form_id, $form_values, $delta = NULL) { |
| 153 |
|
// Does the delta contain any non lowercase alpha, numberic or underscore characters? If so - tell the user not to use them |
| 154 |
if (preg_match('|[^a-z0-9_]|', $form_values['delta'])) { |
if (preg_match('|[^a-z0-9_]|', $form_values['delta'])) { |
| 155 |
form_set_error('delta', t('The delta must be made of lowercase letters, numbers and underscores only.')); |
form_set_error('delta', t('The delta must be made of lowercase letters, numbers and underscores only.')); |
| 156 |
} |
} |
| 157 |
|
|
| 158 |
|
// Has the user enabled any roles? We need at least one for this block to have a point. |
| 159 |
if (count(array_filter($form_values['roles']['enabled'])) == 0) { |
if (count(array_filter($form_values['roles']['enabled'])) == 0) { |
| 160 |
form_set_error('roles][enabled', t('You must enable at least one role')); |
form_set_error('roles][enabled', t('You must enable at least one role')); |
| 161 |
} |
} |
|
|
|
|
form_set_error(NULL, 'test'); |
|
| 162 |
} |
} |
| 163 |
|
|
| 164 |
|
|
| 165 |
|
/** |
| 166 |
|
* Submit handler for the block settings form |
| 167 |
|
*/ |
| 168 |
function block_cpr_block_settings_form_submit($form_id, $form_values, $delta = NULL) { |
function block_cpr_block_settings_form_submit($form_id, $form_values, $delta = NULL) { |
| 169 |
|
// Get the existing settings |
| 170 |
$settings = variable_get('block_cpr_settings', array()); |
$settings = variable_get('block_cpr_settings', array()); |
| 171 |
|
|
| 172 |
|
// Prepare a new sub-settings variable |
| 173 |
$s = array(); |
$s = array(); |
| 174 |
|
|
| 175 |
|
// Foreach enabled role, create a sub-settins entry containing the weight and the content (body & format) settings |
| 176 |
foreach ($form_values['roles']['enabled'] as $rid => $enabled) { |
foreach ($form_values['roles']['enabled'] as $rid => $enabled) { |
| 177 |
if ($enabled) { |
if ($enabled) { |
| 178 |
$s[$rid] = array( |
$s[$rid] = array( |
| 181 |
); |
); |
| 182 |
} |
} |
| 183 |
} |
} |
| 184 |
|
|
| 185 |
|
// Sort the sub-settings using the custom function (sorts by weight and then an alpha sort on the role name) |
| 186 |
uasort($s, '_block_cpr_sort_blocks'); |
uasort($s, '_block_cpr_sort_blocks'); |
| 187 |
|
|
| 188 |
|
// Place the new sub-settings into the settings table |
| 189 |
$settings[$form_values['delta']] = $s; |
$settings[$form_values['delta']] = $s; |
| 190 |
|
|
| 191 |
|
// Save settings |
| 192 |
variable_set('block_cpr_settings', $settings); |
variable_set('block_cpr_settings', $settings); |
| 193 |
|
|
| 194 |
|
// Return to the CPR Overview page |
| 195 |
return 'admin/build/block/cpr'; |
return 'admin/build/block/cpr'; |
| 196 |
} |
} |
| 197 |
|
|
| 198 |
|
|
| 199 |
|
/** |
| 200 |
|
* Custom sort function for storing the block's enabled role settings in the correct order. |
| 201 |
|
*/ |
| 202 |
function _block_cpr_sort_blocks($a, $b) { |
function _block_cpr_sort_blocks($a, $b) { |
| 203 |
if ($a['weight'] > $b['weight']) { return 1; } |
if ($a['weight'] > $b['weight']) return 1; |
| 204 |
elseif ($a['weight'] < $b['weight']) {return -1; } |
elseif ($a['weight'] < $b['weight']) return -1; |
| 205 |
|
|
| 206 |
return strcasecmp($a, $b); |
return strcasecmp($a, $b); |
| 207 |
} |
} |
| 208 |
|
|
| 209 |
|
|
| 210 |
|
/** |
| 211 |
|
* Delete confirm form |
| 212 |
|
*/ |
| 213 |
function block_cpr_block_delete_confirm_form($delta) { |
function block_cpr_block_delete_confirm_form($delta) { |
| 214 |
|
// Get the settings |
| 215 |
$settings = variable_get('block_cpr_settings', array()); |
$settings = variable_get('block_cpr_settings', array()); |
| 216 |
|
|
| 217 |
|
// Make sure the delta being deleted exists |
| 218 |
if (!isset($settings[$delta])) { |
if (!isset($settings[$delta])) { |
| 219 |
drupal_not_found(); |
drupal_not_found(); |
| 220 |
exit; |
exit; |
| 227 |
} |
} |
| 228 |
|
|
| 229 |
|
|
| 230 |
|
/** |
| 231 |
|
* Submit handler for the confirm delete form |
| 232 |
|
*/ |
| 233 |
function block_cpr_block_delete_confirm_form_submit($form_id, $form_values) { |
function block_cpr_block_delete_confirm_form_submit($form_id, $form_values) { |
| 234 |
$settings = variable_get('block_cpr_settings', array()); |
$settings = variable_get('block_cpr_settings', array()); |
| 235 |
unset($settings[$form_values['delta']]); |
unset($settings[$form_values['delta']]); |