| 1 |
|
<?php |
| 2 |
|
//$Id$ |
| 3 |
|
|
| 4 |
|
/** |
| 5 |
|
* @file |
| 6 |
|
* The Value List module, which allows end users to manage value lists through Drupal. |
| 7 |
|
* |
| 8 |
|
* Helpful for cases where you are NOT using CCK to define input forms, yet need the |
| 9 |
|
* ability to change select menu options without changing module code. |
| 10 |
|
*/ |
| 11 |
|
|
| 12 |
|
/** |
| 13 |
|
* Implementation of hook_perm(). |
| 14 |
|
* |
| 15 |
|
* @return array |
| 16 |
|
*/ |
| 17 |
|
function valuelist_perm() { |
| 18 |
|
return array( |
| 19 |
|
'create value lists', |
| 20 |
|
'edit value lists', |
| 21 |
|
'delete value lists' |
| 22 |
|
); |
| 23 |
|
} |
| 24 |
|
|
| 25 |
|
/** |
| 26 |
|
* Implementation of hook_menu(). |
| 27 |
|
* |
| 28 |
|
* @param boolean $may_cache |
| 29 |
|
* @return array |
| 30 |
|
*/ |
| 31 |
|
function valuelist_menu($may_cache) { |
| 32 |
|
|
| 33 |
|
$items = array(); |
| 34 |
|
|
| 35 |
|
if ($may_cache) { |
| 36 |
|
$items[] = array( |
| 37 |
|
'path' => 'admin/build/valuelist', |
| 38 |
|
'title' =>t ('Value Lists'), |
| 39 |
|
'access' => user_access('access administration pages'), |
| 40 |
|
'description' => t('Manage custom value lists that can be used by the Form API.'), |
| 41 |
|
'callback' => 'valuelist_dashboard', |
| 42 |
|
//'type' => MENU_VISIBLE_IN_TREE |
| 43 |
|
); |
| 44 |
|
} |
| 45 |
|
else { |
| 46 |
|
|
| 47 |
|
$items[] = array( |
| 48 |
|
'path' => 'valuelist/'. arg(1) .'/edit', |
| 49 |
|
'access' => user_access('edit value lists'), |
| 50 |
|
'callback' => 'drupal_get_form', |
| 51 |
|
'callback arguments' => array('valuelist_edit_form', check_plain(arg(1))), |
| 52 |
|
'type' => MENU_CALLBACK_ITEM |
| 53 |
|
); |
| 54 |
|
|
| 55 |
|
$items[] = array( |
| 56 |
|
'path' => 'valuelist/add', |
| 57 |
|
'access' => user_access('create value lists'), |
| 58 |
|
'callback' => 'drupal_get_form', |
| 59 |
|
'callback arguments' => 'valuelist_edit_form', |
| 60 |
|
'type' => MENU_CALLBACK_ITEM |
| 61 |
|
); |
| 62 |
|
} |
| 63 |
|
|
| 64 |
|
return $items; |
| 65 |
|
|
| 66 |
|
} |
| 67 |
|
|
| 68 |
|
/** |
| 69 |
|
* FAPI definition for the value list editing form. |
| 70 |
|
* |
| 71 |
|
* @param string $vl_name |
| 72 |
|
* The name of the value list being edited. If no name is supplied, |
| 73 |
|
* a new value list will be created. |
| 74 |
|
* @return array |
| 75 |
|
*/ |
| 76 |
|
function valuelist_edit_form($vl_name = '') { |
| 77 |
|
|
| 78 |
|
/* If a value list name is provided, attempt to load the designated VL */ |
| 79 |
|
if (empty($vl_name)) { |
| 80 |
|
drupal_set_title(t('Create Value List')); |
| 81 |
|
$value = ''; |
| 82 |
|
} |
| 83 |
|
else { |
| 84 |
|
drupal_set_title(t('Editing Value List %vl_name', array('%vl_name' => $vl_name))); |
| 85 |
|
$value = variable_get(_valuelist_sys_list_name($vl_name), ''); |
| 86 |
|
} |
| 87 |
|
|
| 88 |
|
$form = array(); |
| 89 |
|
|
| 90 |
|
$form['name'] = array( |
| 91 |
|
'#type' => 'textfield', |
| 92 |
|
'#title' => t('Value List Name'), |
| 93 |
|
'#default_value' => $vl_name |
| 94 |
|
); |
| 95 |
|
|
| 96 |
|
$form['name_old'] = array( |
| 97 |
|
'#type' => 'value', |
| 98 |
|
'#value' => $vl_name |
| 99 |
|
); |
| 100 |
|
|
| 101 |
|
$form['value'] = array( |
| 102 |
|
'#type' => 'textarea', |
| 103 |
|
'#title' => t('Values'), |
| 104 |
|
'#default_value' => $value, |
| 105 |
|
'#description' => t("List one value per line, in the format <i>value</i>|<i>label</i>, or simply <i>value</i> if value and label are the same."), |
| 106 |
|
'#rows' => 10 |
| 107 |
|
); |
| 108 |
|
|
| 109 |
|
$form['submit'] = array( |
| 110 |
|
'#type' => 'submit', |
| 111 |
|
'#value' => t('Save'), |
| 112 |
|
|
| 113 |
|
); |
| 114 |
|
|
| 115 |
|
if (user_access('delete value lists')) { |
| 116 |
|
$form['delete'] = array( |
| 117 |
|
'#type' => 'submit', |
| 118 |
|
'#value' => t('Delete'), |
| 119 |
|
'#attributes' => array('onclick' => t("return confirm('Are you sure you want to delete this value list?')")) |
| 120 |
|
); |
| 121 |
|
} |
| 122 |
|
|
| 123 |
|
$form['#redirect'] = 'admin/build/valuelist'; |
| 124 |
|
|
| 125 |
|
return $form; |
| 126 |
|
} |
| 127 |
|
|
| 128 |
|
/** |
| 129 |
|
* Implementation of hook_submit(). |
| 130 |
|
* |
| 131 |
|
* @param string $form_id |
| 132 |
|
* @param array $form_values |
| 133 |
|
*/ |
| 134 |
|
function valuelist_edit_form_submit($form_id, $form_values) { |
| 135 |
|
|
| 136 |
|
if ($form_id == 'valuelist_edit_form') { |
| 137 |
|
switch ($form_values['op']) { |
| 138 |
|
|
| 139 |
|
case 'Save': |
| 140 |
|
$name = _valuelist_sys_list_name(check_plain($form_values['name'])); |
| 141 |
|
//Strip tags, but don't run check_plain() here or we wind up with |
| 142 |
|
//double-encoded entities by the time they're rendered by FAPI |
| 143 |
|
$value = strip_tags($form_values['value']); |
| 144 |
|
variable_set($name, $value); |
| 145 |
|
|
| 146 |
|
//If the variable name was changed, delete the old one |
| 147 |
|
if (!empty($form_values['name_old']) && $form_values['name_old'] != check_plain($form_values['name'])) { |
| 148 |
|
variable_del(_valuelist_sys_list_name($form_values['name_old'])); |
| 149 |
|
} |
| 150 |
|
|
| 151 |
|
drupal_set_message(t("Value List %name saved.", array('%name' => check_plain($form_values['name'])))); |
| 152 |
|
|
| 153 |
|
break; |
| 154 |
|
|
| 155 |
|
case 'Delete': |
| 156 |
|
|
| 157 |
|
variable_del(_valuelist_sys_list_name(check_plain($form_values['name']))); |
| 158 |
|
drupal_set_message(t("Value List %name deleted.", array('%name' => check_plain($form_values['name'])))); |
| 159 |
|
|
| 160 |
|
} |
| 161 |
|
} |
| 162 |
|
|
| 163 |
|
} |
| 164 |
|
|
| 165 |
|
/** |
| 166 |
|
* Displays an administrator dashboard for the management of value lists. |
| 167 |
|
* Edit access is required. Create/Delete access optional. |
| 168 |
|
*/ |
| 169 |
|
function valuelist_dashboard() { |
| 170 |
|
if (!user_access('create value lists')) { |
| 171 |
|
drupal_access_denied(); |
| 172 |
|
} |
| 173 |
|
|
| 174 |
|
$content = ''; |
| 175 |
|
|
| 176 |
|
/* Retrieve a list of all items in the variable table whose `name` begins with 'valuelist_': */ |
| 177 |
|
$result = db_query("SELECT `name` FROM {variable} WHERE `name` like 'valuelist_%' ORDER BY `name` ASC"); |
| 178 |
|
|
| 179 |
|
|
| 180 |
|
|
| 181 |
|
if (db_num_rows($result) == 0) { |
| 182 |
|
$content .= t("No value lists found."); |
| 183 |
|
} |
| 184 |
|
else { |
| 185 |
|
//Build table rows |
| 186 |
|
$rows = array(); |
| 187 |
|
while ($row = db_fetch_array($result)) { |
| 188 |
|
$rows[] = array( |
| 189 |
|
_valuelist_user_list_name($row['name']), |
| 190 |
|
'<pre>'. variable_get($row['name'], '') .'</pre>', |
| 191 |
|
l('Edit', 'valuelist/'. _valuelist_user_list_name($row['name']) .'/edit') |
| 192 |
|
); |
| 193 |
|
} |
| 194 |
|
|
| 195 |
|
$header = array( |
| 196 |
|
t('Name'), |
| 197 |
|
t('Value List'), |
| 198 |
|
t(' ') |
| 199 |
|
); |
| 200 |
|
|
| 201 |
|
$content .= theme('table', $header, $rows); |
| 202 |
|
|
| 203 |
|
} |
| 204 |
|
|
| 205 |
|
/* If the user has permission to create new value lists, link to the create form here */ |
| 206 |
|
if (user_access('create value lists')) { |
| 207 |
|
$content .= t('<hr/>'); |
| 208 |
|
$content .= l('Create a new Value List', 'valuelist/add'); |
| 209 |
|
} |
| 210 |
|
|
| 211 |
|
return $content; |
| 212 |
|
} |
| 213 |
|
|
| 214 |
|
/** |
| 215 |
|
* Retrieve a value list as an associative array. |
| 216 |
|
* |
| 217 |
|
* @param string $vl_name |
| 218 |
|
* The name of the value list being retrieved. |
| 219 |
|
* @return array |
| 220 |
|
*/ |
| 221 |
|
function valuelist_get_array($vl_name) { |
| 222 |
|
$options = array(); |
| 223 |
|
$list = variable_get(_valuelist_sys_list_name($vl_name), array()); |
| 224 |
|
$lines = explode("\n", $list); |
| 225 |
|
|
| 226 |
|
foreach ($lines as $line) { |
| 227 |
|
if (strpos($line, '|')) { |
| 228 |
|
$parts = explode('|', $line); |
| 229 |
|
$value = $parts[0]; |
| 230 |
|
$label = $parts[1]; |
| 231 |
|
} |
| 232 |
|
else{ |
| 233 |
|
$value = $line; |
| 234 |
|
$label = $line; |
| 235 |
|
} |
| 236 |
|
$options[$value] = t($label); |
| 237 |
|
} |
| 238 |
|
|
| 239 |
|
return $options; |
| 240 |
|
} |
| 241 |
|
|
| 242 |
|
/** |
| 243 |
|
* Implementation of hook_help(). |
| 244 |
|
* |
| 245 |
|
* @param string $section |
| 246 |
|
*/ |
| 247 |
|
function valuelist_help($section) { |
| 248 |
|
switch ($section) { |
| 249 |
|
|
| 250 |
|
case 'admin/help#valuelist': |
| 251 |
|
$output = '<p>'. t("Value lists can be used to populate the <code>#options</code> array of a Form API <em>select</em> or <em>checkboxes</em> element.") .'</p>'; |
| 252 |
|
|
| 253 |
|
$output .= '<p>'. t("For example:") .'</p>'; |
| 254 |
|
$output .= '<pre>'. t("\$form['my_select'] = array( |
| 255 |
|
'#type' => 'select', |
| 256 |
|
'#title' => t('My Select List'), |
| 257 |
|
'#options' => valuelist_get('my_custom_list'); |
| 258 |
|
);") .'</pre>'; |
| 259 |
|
$output .= '<p>'. t("Where <em>my_custom_list</em> is the name of a value list.") .'</p>'; |
| 260 |
|
return $output; |
| 261 |
|
} |
| 262 |
|
|
| 263 |
|
} |
| 264 |
|
|
| 265 |
|
/** |
| 266 |
|
* Build the full name the value list will be stored as in the system `variable` table. |
| 267 |
|
* Value lists are prefixed with 'valuelist_' to avoid namespace collisions. |
| 268 |
|
* |
| 269 |
|
* @param string $vl_name |
| 270 |
|
* @return string |
| 271 |
|
*/ |
| 272 |
|
function _valuelist_sys_list_name($vl_name) { |
| 273 |
|
return 'valuelist_'. $vl_name; |
| 274 |
|
} |
| 275 |
|
|
| 276 |
|
/** |
| 277 |
|
* Strip the 'valuelist_' namespace from the specified variable name. |
| 278 |
|
* |
| 279 |
|
* @param string $vl_sys_name |
| 280 |
|
* @return string |
| 281 |
|
*/ |
| 282 |
|
function _valuelist_user_list_name($vl_sys_name) { |
| 283 |
|
return preg_replace("/^valuelist_/", '', $vl_sys_name); |
| 284 |
|
} |