| 1 |
<?php
|
| 2 |
// $Id: draggableviews_theme.inc,v 1.6 2008/09/21 20:36:49 sevi Exp $
|
| 3 |
|
| 4 |
/**
|
| 5 |
* @file
|
| 6 |
* Theme functions.
|
| 7 |
*/
|
| 8 |
|
| 9 |
function template_preprocess_draggableviews_view_draggabletable(&$vars) {
|
| 10 |
$view = $vars['view'];
|
| 11 |
|
| 12 |
// We need the raw data for this grouping, which is passed in as $vars['rows'].
|
| 13 |
// However, the template also needs to use for the rendered fields. We
|
| 14 |
// therefore swap the raw data out to a new variable and reset $vars['rows']
|
| 15 |
// so that it can get rebuilt.
|
| 16 |
$result = $vars['rows'];
|
| 17 |
$vars['rows'] = array();
|
| 18 |
|
| 19 |
$options = $view->style_plugin->options;
|
| 20 |
$handler = $view->style_plugin;
|
| 21 |
|
| 22 |
$fields = &$view->field;
|
| 23 |
$columns = $handler->sanitize_columns($options['columns'], $fields);
|
| 24 |
|
| 25 |
$active = !empty($handler->active) ? $handler->active : '';
|
| 26 |
$order = !empty($handler->order) ? $handler->order : 'asc';
|
| 27 |
|
| 28 |
$query = tablesort_get_querystring();
|
| 29 |
if ($query) {
|
| 30 |
$query = '&'. $query;
|
| 31 |
}
|
| 32 |
|
| 33 |
foreach ($columns as $field => $column) {
|
| 34 |
// render the header labels
|
| 35 |
if ($field == $column && empty($fields[$field]->options['exclude'])) {
|
| 36 |
$label = check_plain(!empty($fields[$field]) ? $fields[$field]->label() : '');
|
| 37 |
if (empty($options['info'][$field]['sortable'])) {
|
| 38 |
$vars['header'][$field] = $label;
|
| 39 |
}
|
| 40 |
else {
|
| 41 |
// @todo -- make this a setting
|
| 42 |
$initial = 'asc';
|
| 43 |
|
| 44 |
if ($active == $field && $order == 'asc') {
|
| 45 |
$initial = 'desc';
|
| 46 |
}
|
| 47 |
|
| 48 |
$image = theme('tablesort_indicator', $initial);
|
| 49 |
$title = t('sort by @s', array('@s' => $label));
|
| 50 |
$link_options = array(
|
| 51 |
'html' => TRUE,
|
| 52 |
'attributes' => array('title' => $title),
|
| 53 |
'query' => 'order='. urlencode($field) .'&sort='. $initial . $query,
|
| 54 |
);
|
| 55 |
$vars['header'][$field] = l($label . $image, $_GET['q'], $link_options);
|
| 56 |
}
|
| 57 |
}
|
| 58 |
|
| 59 |
// Create a second variable so we can easily find what fields we have and what the
|
| 60 |
// CSS classes should be.
|
| 61 |
$vars['fields'][$field] = views_css_safe($field);
|
| 62 |
if ($active == $field) {
|
| 63 |
$vars['fields'][$field] .= ' active';
|
| 64 |
}
|
| 65 |
|
| 66 |
// Render each field into its appropriate column.
|
| 67 |
foreach ($result as $num => $row) {
|
| 68 |
if (!empty($fields[$field]) && empty($fields[$field]->options['exclude'])) {
|
| 69 |
$field_output = $fields[$field]->theme($row);
|
| 70 |
|
| 71 |
// Don't bother with separators and stuff if the field does not show up.
|
| 72 |
if (!isset($field_output) && isset($vars['rows'][$num][$column])) {
|
| 73 |
continue;
|
| 74 |
}
|
| 75 |
|
| 76 |
// Place the field into the column, along with an optional separator.
|
| 77 |
if (isset($vars['rows'][$num][$column])) {
|
| 78 |
if (!empty($options['info'][$column]['separator'])) {
|
| 79 |
$vars['rows'][$num][$column] .= filter_xss_admin($options['info'][$column]['separator']);
|
| 80 |
}
|
| 81 |
}
|
| 82 |
else {
|
| 83 |
$vars['rows'][$num][$column] = '';
|
| 84 |
}
|
| 85 |
|
| 86 |
$vars['rows'][$num][$column] .= $field_output;
|
| 87 |
}
|
| 88 |
}
|
| 89 |
}
|
| 90 |
|
| 91 |
$vars['class'] = 'views-table';
|
| 92 |
if (!empty($options['sticky'])) {
|
| 93 |
drupal_add_js('misc/tableheader.js');
|
| 94 |
$vars['class'] .= " sticky-enabled";
|
| 95 |
}
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
/*********************
|
| 101 |
* TABLE_DRAG OUTPUT *
|
| 102 |
**********************/
|
| 103 |
|
| 104 |
$error = FALSE;
|
| 105 |
|
| 106 |
$info = _draggableviews_info($view);
|
| 107 |
|
| 108 |
if (!isset($info['order'])) return;
|
| 109 |
|
| 110 |
// calculate depth of all nodes.
|
| 111 |
// If one of the parents of a node is broken,
|
| 112 |
// the return will be FALSE
|
| 113 |
if (!_draggableviews_calculate_depths($info)) {
|
| 114 |
|
| 115 |
drupal_set_message(t('Draggableviews: Detected broken parents.'), 'error');
|
| 116 |
|
| 117 |
$error = TRUE;
|
| 118 |
}
|
| 119 |
|
| 120 |
// Detect order collisions
|
| 121 |
// If one order of the same depth is found
|
| 122 |
// twice the return will be FALSE.
|
| 123 |
if (!_draggableviews_detect_order_collisions($info)) {
|
| 124 |
|
| 125 |
drupal_set_message(t('Draggableviews: Detected order collisions.'), 'error');
|
| 126 |
|
| 127 |
$error = TRUE;
|
| 128 |
}
|
| 129 |
|
| 130 |
if ($error) {
|
| 131 |
// on error rebuild hierarchy and return
|
| 132 |
_draggableviews_rebuild_hierarchy($info);
|
| 133 |
|
| 134 |
return;
|
| 135 |
}
|
| 136 |
|
| 137 |
// loop through all rows the view returned
|
| 138 |
foreach ($vars['rows'] as $i => $row) {
|
| 139 |
|
| 140 |
$nid = $result[$i]->nid;
|
| 141 |
|
| 142 |
// Detect ordering errors
|
| 143 |
// The nodes order value has to equal with the
|
| 144 |
// parents order value. Otherwise the return will be FALSE.
|
| 145 |
if (_draggableviews_check_order($nid, $info) == FALSE) {
|
| 146 |
|
| 147 |
drupal_set_message(t('Draggableviews: Detected ordering errors.'), 'error');
|
| 148 |
|
| 149 |
// on error rebuild hierarchy and return
|
| 150 |
_draggableviews_rebuild_hierarchy($info);
|
| 151 |
return;
|
| 152 |
}
|
| 153 |
|
| 154 |
// build indentation (as tabledrag requires)
|
| 155 |
$indentation = theme('indentation', $info['nodes'][$nid]['depth']);
|
| 156 |
|
| 157 |
if (user_access('Allow Reordering')) {
|
| 158 |
|
| 159 |
// get node object
|
| 160 |
$node = node_load(array('nid' => $nid));
|
| 161 |
|
| 162 |
if (isset($info['types'][$node->type])) {
|
| 163 |
// set node type specification, if available (e.g. tabledrag-root)
|
| 164 |
$vars['tabledrag_type'][$i] = 'tabledrag-'. $info['types'][$node->type];
|
| 165 |
}
|
| 166 |
|
| 167 |
// Tabledrag needs all concerned fields to be input elements.
|
| 168 |
// The values of the input elements will be changed by tabledrag while
|
| 169 |
// dragging the rows. As we want to use these values we need to
|
| 170 |
// give them names.
|
| 171 |
//
|
| 172 |
// Concerned fields are
|
| 173 |
// *) the first order field
|
| 174 |
// *) the parent field (if hierarchy used)
|
| 175 |
//
|
| 176 |
// The remaining fields are not used by tabledrag. But - depending
|
| 177 |
// on the depth - one of them will simulate the first order field.
|
| 178 |
// (This behavior is based on the fact that tabledrag handles each depth as it was the only one.)
|
| 179 |
|
| 180 |
// this for loop should prevent copy->paste'ing code
|
| 181 |
for ($modus = 'hierarchy'; $modus !== FALSE ; $modus = ($modus == 'hierarchy' ? 'order' : FALSE)) {
|
| 182 |
|
| 183 |
if (isset($info[$modus])) {
|
| 184 |
if ($modus == 'hierarchy') {
|
| 185 |
$value = $info['nodes'][$nid]['parent'];
|
| 186 |
|
| 187 |
if ($value > 0 && !isset($info['nodes'][$value])) {
|
| 188 |
// if parent node doesn't exist rebuild hierarchy and return
|
| 189 |
_draggableviews_rebuild_hierarchy($info);
|
| 190 |
return;
|
| 191 |
}
|
| 192 |
|
| 193 |
// get the field that should be rendered
|
| 194 |
$field = &$info['hierarchy']['field'];
|
| 195 |
}
|
| 196 |
else {
|
| 197 |
// get depth of current field
|
| 198 |
$depth = $info['nodes'][$node->nid]['depth'];
|
| 199 |
|
| 200 |
$value = $info['nodes'][$nid]['order'][$depth];
|
| 201 |
|
| 202 |
// get the field that should be rendered
|
| 203 |
$field = &$info['order']['fields'][0];
|
| 204 |
}
|
| 205 |
|
| 206 |
// get the form element
|
| 207 |
$form_element = $field['handler']->get_form_element($value, array('field_name' => $field['field_name'] .'_'. $nid, 'class' => $field['field_name']));
|
| 208 |
|
| 209 |
// render new form element
|
| 210 |
$vars['rows'][$i][$field['field_name']] = drupal_render($form_element);
|
| 211 |
}
|
| 212 |
}
|
| 213 |
|
| 214 |
|
| 215 |
if (isset($info['hierarchy'])) {
|
| 216 |
// append the node id as a hidden field. This is needed
|
| 217 |
// because tabledrag would not know what id to assign when
|
| 218 |
// subordinating.
|
| 219 |
$hidden_nid = array(
|
| 220 |
'#type' => 'hidden',
|
| 221 |
'#name' => 'hidden_nid',
|
| 222 |
'#value' => $nid,
|
| 223 |
'#attributes' => array('class' => 'hidden_nid'),
|
| 224 |
);
|
| 225 |
// append rendered hidden node id to last rendered field
|
| 226 |
$vars['rows'][$i][$field['field_name']] .= drupal_render($hidden_nid);
|
| 227 |
}
|
| 228 |
}
|
| 229 |
|
| 230 |
if (isset($info['hierarchy'])) {
|
| 231 |
// put indentation in front of first field
|
| 232 |
$first_field = each($vars['rows'][$i]);
|
| 233 |
$vars['rows'][$i][$first_field['key']] = $indentation . $vars['rows'][$i][$first_field['key']];
|
| 234 |
}
|
| 235 |
|
| 236 |
// Let extension modules alter the output
|
| 237 |
foreach (module_implements('draggableviews_row_alter') as $module) {
|
| 238 |
$function = $module . '_draggableviews_row_alter';
|
| 239 |
$function($vars['rows'][$i], $nid, $info, $view);
|
| 240 |
}
|
| 241 |
}
|
| 242 |
|
| 243 |
// output data
|
| 244 |
$vars['tabledrag_table_id'] = 'draggableview_'. $view->name;
|
| 245 |
|
| 246 |
if (user_access('Allow Reordering')) {
|
| 247 |
|
| 248 |
// prepare tabledrag settings for output
|
| 249 |
$vars['tabledrag'] = array();
|
| 250 |
if (isset($info['order'])) {
|
| 251 |
$vars['tabledrag'][] = array(
|
| 252 |
'action' => 'order',
|
| 253 |
'relationship' => 'sibling',
|
| 254 |
'source' => $info['order']['fields'][0]['field_name'],
|
| 255 |
'group' => $info['order']['fields'][0]['field_name'],
|
| 256 |
'subgroup' => $info['order']['fields'][0]['field_name'],
|
| 257 |
'hidden' => !$info['order']['visible'],
|
| 258 |
'limit' => 0,
|
| 259 |
);
|
| 260 |
}
|
| 261 |
if (isset($info['hierarchy'])) {
|
| 262 |
$vars['tabledrag'][] = array(
|
| 263 |
'action' => 'match',
|
| 264 |
'relationship' => 'parent',
|
| 265 |
'source' => 'hidden_nid',
|
| 266 |
'group' => $info['hierarchy']['field']['field_name'],
|
| 267 |
'subgroup' => $info['hierarchy']['field']['field_name'],
|
| 268 |
'hidden' => !$info['hierarchy']['visible'],
|
| 269 |
'limit' => count($info['order']['fields']) - 1,
|
| 270 |
);
|
| 271 |
// let javascript know about fields
|
| 272 |
drupal_add_js(array('draggableviews' => array('parent' => $info['hierarchy']['field']['field_name'])), 'setting');
|
| 273 |
}
|
| 274 |
}
|
| 275 |
}
|
| 276 |
|
| 277 |
function template_preprocess_draggableviews_view_draggabletable_form($vars) {
|
| 278 |
global $user;
|
| 279 |
|
| 280 |
// get view object
|
| 281 |
$view_obj = $vars['form']['#parameters'][2];
|
| 282 |
// get structured info array
|
| 283 |
$info = _draggableviews_info($view_obj->view, FALSE);
|
| 284 |
|
| 285 |
if (user_access('Allow Reordering') && isset($info['hierarchy'])) {
|
| 286 |
|
| 287 |
// fetch expand information from database
|
| 288 |
$result = db_query("SELECT parent_nid, collapsed FROM {draggableviews_collapsed} WHERE uid = %d", $user->uid);
|
| 289 |
$states = array();
|
| 290 |
while ($state = db_fetch_object($result)) {
|
| 291 |
$states[$state->parent_nid] = $state->collapsed;
|
| 292 |
}
|
| 293 |
|
| 294 |
// check if "expand" links should be shown
|
| 295 |
if ($info['expand_links']['show']) {
|
| 296 |
|
| 297 |
drupal_add_js(drupal_get_path('module', 'draggableviews') .'/draggableviews.js');
|
| 298 |
|
| 299 |
if (empty($states)) {
|
| 300 |
// let js know whether child nodes should be expanded or not
|
| 301 |
drupal_add_js(array(
|
| 302 |
'draggableviews' => array(
|
| 303 |
'expand_default' => $options['tabledrag_expand']['collapsed']
|
| 304 |
? 1 : 0,
|
| 305 |
),
|
| 306 |
),
|
| 307 |
'setting');
|
| 308 |
}
|
| 309 |
else {
|
| 310 |
drupal_add_js(array(
|
| 311 |
'draggableviews' => array(
|
| 312 |
'states' => $states,
|
| 313 |
),
|
| 314 |
),
|
| 315 |
'setting');
|
| 316 |
}
|
| 317 |
|
| 318 |
drupal_add_css(drupal_get_path('module', 'draggableviews') .'/styles.css');
|
| 319 |
}
|
| 320 |
}
|
| 321 |
|
| 322 |
|
| 323 |
//theme view
|
| 324 |
$sets = $view_obj->render_grouping($view_obj->view->result, $options['grouping']);
|
| 325 |
|
| 326 |
$output = '';
|
| 327 |
foreach ($sets as $title => $records) {
|
| 328 |
$output .= theme($view_obj->theme_functions(), $view_obj->view, $options, $records, $title);
|
| 329 |
}
|
| 330 |
|
| 331 |
$vars['view'] = $output;
|
| 332 |
|
| 333 |
|
| 334 |
// render submit form
|
| 335 |
$vars['submit_form'] = user_access('Allow Reordering') ? drupal_render($vars['form']) : '';
|
| 336 |
}
|