| 1 |
<?php
|
| 2 |
// $Id: pds_admin.inc,v 1.6 2009/05/28 19:43:42 coltrane Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file Administration pages and forms for managing PDS items.
|
| 6 |
*/
|
| 7 |
|
| 8 |
/**
|
| 9 |
* Private function to retrieve all PDS data.
|
| 10 |
*/
|
| 11 |
function _pds_items_load() {
|
| 12 |
$pds_items = array();
|
| 13 |
$result = db_query("SELECT * FROM {pds_items}");
|
| 14 |
while ($data = db_fetch_array($result)) {
|
| 15 |
$pds_items[] = $data;
|
| 16 |
}
|
| 17 |
return $pds_items;
|
| 18 |
}
|
| 19 |
|
| 20 |
/**
|
| 21 |
* Private function loads all names.
|
| 22 |
*/
|
| 23 |
function _pds_names_load() {
|
| 24 |
$pds_names = array();
|
| 25 |
$result = db_query("SELECT DISTINCT(pds_name) FROM {pds_items}");
|
| 26 |
while ($data = db_fetch_array($result)) {
|
| 27 |
$pds_names[] = $data;
|
| 28 |
}
|
| 29 |
return $pds_names;
|
| 30 |
}
|
| 31 |
|
| 32 |
/**
|
| 33 |
* Menu callback, lists PDS items by name collection.
|
| 34 |
*/
|
| 35 |
function pds_overview() {
|
| 36 |
$pds_names = _pds_names_load();
|
| 37 |
foreach ($pds_names as $name) {
|
| 38 |
$items[] = l($name['pds_name'], 'admin/build/pds/' . $name['pds_name']);
|
| 39 |
}
|
| 40 |
if (empty($items)) {
|
| 41 |
drupal_set_message(t('No PDS namespaces or items exist, please create one.'));
|
| 42 |
drupal_goto('admin/build/pds/add');
|
| 43 |
}
|
| 44 |
else {
|
| 45 |
$output = theme('item_list', $items, t('Names'));
|
| 46 |
}
|
| 47 |
|
| 48 |
return $output;
|
| 49 |
}
|
| 50 |
|
| 51 |
/**
|
| 52 |
* Menu callback, returns list of PDS data items.
|
| 53 |
*/
|
| 54 |
function pds_item_list($pds_items = NULL) {
|
| 55 |
$name = $pds_items[0]['pds_name'];
|
| 56 |
$links[] = l(t('Order @name keys', array('@name' => $name)), 'admin/build/pds/order/' . $name);
|
| 57 |
$links[] = l(t('Add a new item'), 'admin/build/pds/add/' . $name);
|
| 58 |
$output = theme('item_list', $links);
|
| 59 |
$header = array(
|
| 60 |
t('Type/Unit'),
|
| 61 |
array('data' => t('Key'), 'field' => 'pds_key'),
|
| 62 |
array('data' => t('Value'), 'field' => 'pds_value'),
|
| 63 |
array('data' => t('Property'), 'field' => 'pds_properties'),
|
| 64 |
array('data' => t('Operations'), 'colspan' => 2),
|
| 65 |
);
|
| 66 |
foreach ($pds_items as $item) {
|
| 67 |
$rows[] = array(
|
| 68 |
array('data' => check_plain($item['pds_type'] . ' ' . $item['pds_unit'])),
|
| 69 |
array('data' => check_plain($item['pds_key'])),
|
| 70 |
array('data' => check_plain($item['pds_value'])),
|
| 71 |
array('data' => check_plain($item['pds_properties'])),
|
| 72 |
array('data' => l('Edit', 'admin/build/pds/edit/' . $item['pds_id'])),
|
| 73 |
array('data' => l('Delete', 'admin/build/pds/delete/' . $item['pds_id'])),
|
| 74 |
);
|
| 75 |
}
|
| 76 |
if ($rows) {
|
| 77 |
$output .= theme('table', $header, $rows);
|
| 78 |
}
|
| 79 |
return $output;
|
| 80 |
}
|
| 81 |
|
| 82 |
/**
|
| 83 |
* Form for adding or editing a PDS data item.
|
| 84 |
*/
|
| 85 |
function pds_item_form(&$form_state, $pds_item = NULL) {
|
| 86 |
if (isset($pds_item[0])) {
|
| 87 |
// We've received an array of PDS items, hold on to just the name.
|
| 88 |
$pds_name = $pds_item[0]['pds_name'];
|
| 89 |
$pds_item = array();
|
| 90 |
}
|
| 91 |
else {
|
| 92 |
$pds_name = $pds_item['pds_name'];
|
| 93 |
}
|
| 94 |
$form['pds_id'] = array(
|
| 95 |
'#type' => 'hidden',
|
| 96 |
'#value' => empty($pds_item['pds_id']) ? '' : $pds_item['pds_id'],
|
| 97 |
);
|
| 98 |
$form['pds_name'] = array(
|
| 99 |
'#type' => 'textfield',
|
| 100 |
'#title' => t('Name'),
|
| 101 |
'#description' => t('Machine-readable namespace. Defines a collection of PDS items.'),
|
| 102 |
'#required' => TRUE,
|
| 103 |
'#default_value' => empty($pds_name) ? '' : $pds_name,
|
| 104 |
);
|
| 105 |
$form['item_path'] = array(
|
| 106 |
'#type' => 'fieldset',
|
| 107 |
'#description' => t('Type and unit are used to match the Drupal path.'),
|
| 108 |
);
|
| 109 |
$form['item_path']['pds_type'] = array(
|
| 110 |
'#type' => 'textfield',
|
| 111 |
'#title' => t('Type'),
|
| 112 |
'#description' => t(' Example, if you are defining a PDS item to appear on node/1, the type is "node".'),
|
| 113 |
'#required' => TRUE,
|
| 114 |
'#default_value' => empty($pds_item['pds_type']) ? '' : $pds_item['pds_type'],
|
| 115 |
);
|
| 116 |
$form['item_path']['pds_unit'] = array(
|
| 117 |
'#type' => 'textfield',
|
| 118 |
'#title' => t('Unit'),
|
| 119 |
'#description' => t("Example, the unit for a PDS item on a node's page is the node ID."),
|
| 120 |
'#required' => TRUE,
|
| 121 |
'#default_value' => empty($pds_item['pds_unit']) ? '' : $pds_item['pds_unit'],
|
| 122 |
);
|
| 123 |
$form['item_value'] = array(
|
| 124 |
'#type' => 'fieldset',
|
| 125 |
'#description' => t("Define the key-value pair to be stored on this item's path (type and unit)."),
|
| 126 |
);
|
| 127 |
$form['item_value']['pds_key'] = array(
|
| 128 |
'#type' => 'textfield',
|
| 129 |
'#title' => t('Key'),
|
| 130 |
'#description' => t('Case sensitive identifier for this item. For example, "Category" is not the same as "category".'),
|
| 131 |
'#required' => TRUE,
|
| 132 |
'#default_value' => empty($pds_item['pds_key']) ? '' : $pds_item['pds_key'],
|
| 133 |
);
|
| 134 |
$form['item_value']['pds_value'] = array(
|
| 135 |
'#type' => 'textfield',
|
| 136 |
'#title' => t('Value'),
|
| 137 |
'#description' => t('The value that corresponds to the key for this item.'),
|
| 138 |
'#required' => TRUE,
|
| 139 |
'#default_value' => empty($pds_item['pds_value']) ? '' : $pds_item['pds_value'],
|
| 140 |
);
|
| 141 |
$form['properties'] = array(
|
| 142 |
'#type' => 'fieldset',
|
| 143 |
'#title' => t('Advanced'),
|
| 144 |
'#collapsible' => TRUE,
|
| 145 |
'#collapsed' => TRUE,
|
| 146 |
);
|
| 147 |
$form['properties']['pds_properties'] = array(
|
| 148 |
'#type' => 'textfield',
|
| 149 |
'#title' => t('Property'),
|
| 150 |
'#description' => t('Items can have properties that define how they relate to other items in their namespace.'),
|
| 151 |
'#required' => FALSE,
|
| 152 |
'#default_value' => empty($pds_item['pds_properties']) ? '' : $pds_item['pds_properties'],
|
| 153 |
);
|
| 154 |
|
| 155 |
$form['submit'] = array(
|
| 156 |
'#type' => 'submit',
|
| 157 |
'#value' => t('Save'),
|
| 158 |
'#required' => TRUE,
|
| 159 |
);
|
| 160 |
$form['cancel'] = array(
|
| 161 |
'#type' => 'markup',
|
| 162 |
'#value' => l(t('Cancel'), $path),
|
| 163 |
);
|
| 164 |
return $form;
|
| 165 |
}
|
| 166 |
|
| 167 |
function pds_item_form_submit($form, &$form_state) {
|
| 168 |
// Trim the name and the key.
|
| 169 |
$form_state['values']['pds_name'] = trim($form_state['values']['pds_name']);
|
| 170 |
$form_state['values']['pds_key'] = trim($form_state['values']['pds_key']);
|
| 171 |
if (empty($form_state['values']['pds_id'])) {
|
| 172 |
// New pds item, unset the PDS element.
|
| 173 |
unset($form_state['values']['pds_id']);
|
| 174 |
pds_save($form_state['values']);
|
| 175 |
}
|
| 176 |
else {
|
| 177 |
// Update existing item.
|
| 178 |
pds_save($form_state['values']);
|
| 179 |
}
|
| 180 |
// Go to item weight order form.
|
| 181 |
$form_state['redirect'] = 'admin/build/pds/order/' . $form_state['values']['pds_name'];
|
| 182 |
return;
|
| 183 |
}
|
| 184 |
|
| 185 |
function pds_item_delete_confirm(&$form_state, $pds_item = NULL) {
|
| 186 |
if (is_null($pds_item)) {
|
| 187 |
drupal_goto('admin/build/pds');
|
| 188 |
}
|
| 189 |
|
| 190 |
$form['pds_id'] = array('#type' => 'value', '#value' => $pds_item['pds_id']);
|
| 191 |
$form['pds_name'] = array('#type' => 'value', '#value' => $pds_item['pds_name']);
|
| 192 |
$message = t('Are you sure you want to delete the @name item?', array('@name' => $pds_item['pds_item']));
|
| 193 |
|
| 194 |
return confirm_form($form, $message, 'admin/build/pds', t('This action cannot be undone.'), t('Delete'));
|
| 195 |
}
|
| 196 |
|
| 197 |
function pds_item_delete_confirm_submit($form, &$form_state) {
|
| 198 |
pds_delete($form_state['values']);
|
| 199 |
$form_state['redirect'] = 'admin/build/pds';
|
| 200 |
return;
|
| 201 |
}
|
| 202 |
|
| 203 |
/**
|
| 204 |
* Helper function organizes PDS items for setting weights.
|
| 205 |
* Duplicate keys are grouped and get the same weight.
|
| 206 |
*/
|
| 207 |
function _pds_items_distinct_keys_weights($pds_items) {
|
| 208 |
$keys = array();
|
| 209 |
foreach ($pds_items as $item) {
|
| 210 |
if (!in_array($item['pds_key'], $keys)) {
|
| 211 |
$keys[$item['pds_weight']] = $item['pds_key'];
|
| 212 |
}
|
| 213 |
}
|
| 214 |
ksort($keys);
|
| 215 |
return $keys;
|
| 216 |
}
|
| 217 |
|
| 218 |
/**
|
| 219 |
* Form for ordering pds item keys.
|
| 220 |
*/
|
| 221 |
function pds_item_order_form(&$form_state, $pds_items = array()) {
|
| 222 |
$form = array();
|
| 223 |
$items = _pds_items_distinct_keys_weights($pds_items);
|
| 224 |
if (!empty($items)) {
|
| 225 |
foreach ($items as $weight => $key) {
|
| 226 |
$form[$weight]['pds_key'] = array(
|
| 227 |
'#value' => check_plain($key),
|
| 228 |
);
|
| 229 |
$form[$weight]['pds_weight'] = array(
|
| 230 |
'#type' => 'weight',
|
| 231 |
'#default_value' => $weight,
|
| 232 |
);
|
| 233 |
}
|
| 234 |
$form['pds_items'] = array(
|
| 235 |
'#type' => 'value',
|
| 236 |
'#value' => $pds_items,
|
| 237 |
);
|
| 238 |
$form['pds_name'] = array(
|
| 239 |
'#type' => 'value',
|
| 240 |
'#value' => $pds_items[0]['pds_name'],
|
| 241 |
);
|
| 242 |
$form['#redirect'] = 'admin/build/pds/' . $pds_items[0]['pds_name'];
|
| 243 |
$form['submit'] = array('#type' => 'submit', '#value' => t('Save configuration'));
|
| 244 |
$form['#tree'] = TRUE;
|
| 245 |
}
|
| 246 |
return $form;
|
| 247 |
}
|
| 248 |
|
| 249 |
/**
|
| 250 |
* Theme the item order form to use tabledrag.
|
| 251 |
*/
|
| 252 |
function theme_pds_item_order_form($form) {
|
| 253 |
$output = '';
|
| 254 |
$header = array(
|
| 255 |
t('Item'),
|
| 256 |
t('Weight'),
|
| 257 |
);
|
| 258 |
$rows = array();
|
| 259 |
foreach (element_children($form) as $key) {
|
| 260 |
if (is_numeric($key)) {
|
| 261 |
$element = &$form[$key];
|
| 262 |
$row = array();
|
| 263 |
$row[] = drupal_render($element['pds_key']);
|
| 264 |
$element['pds_weight']['#attributes'] = array('class' => 'pds-order');
|
| 265 |
$row[] = drupal_render($element['pds_weight']);
|
| 266 |
$rows[] = array('data' => $row, 'class' => 'draggable');
|
| 267 |
}
|
| 268 |
}
|
| 269 |
|
| 270 |
$output = theme('table', $header, $rows, array('id' => 'pds-items-order'));
|
| 271 |
|
| 272 |
$output .= drupal_render($form['submit']);
|
| 273 |
$output .= drupal_render($form); // Render everything else now.
|
| 274 |
|
| 275 |
drupal_add_tabledrag('pds-items-order', 'order', 'sibling', 'pds-order');
|
| 276 |
|
| 277 |
return $output;
|
| 278 |
}
|
| 279 |
|
| 280 |
function pds_item_order_form_submit($form, &$form_state) {
|
| 281 |
$pds_items = $form_state['values']['pds_items'];
|
| 282 |
$pds_name = $form_state['values']['pds_name'];
|
| 283 |
$items = _pds_items_distinct_keys_weights($pds_items);
|
| 284 |
foreach ($items as $weight => $key) {
|
| 285 |
_pds_item_write_weights($pds_name, $key, $form_state['values'][$weight]['pds_weight']);
|
| 286 |
}
|
| 287 |
return;
|
| 288 |
}
|
| 289 |
|
| 290 |
/**
|
| 291 |
* Since there may be multiple PDS items with the same key we save the weight for each.
|
| 292 |
*/
|
| 293 |
function _pds_item_write_weights($pds_name, $pds_key, $pds_weight) {
|
| 294 |
$pds_items = _pds_items_key_load($pds_name, $pds_key);
|
| 295 |
foreach ($pds_items as $pds_item) {
|
| 296 |
$pds_item['pds_weight'] = $pds_weight;
|
| 297 |
pds_save($pds_item);
|
| 298 |
}
|
| 299 |
}
|