| [ Index ] |
PHP Cross Reference of Drupal 6 (yi-drupal) |
[Summary view] [Print] [Text view]
1 <?php 2 /** 3 * @file 4 * Hooks and API functions for Data Node module. 5 */ 6 7 /** 8 * Implementation of hook_views_api(). 9 */ 10 function data_node_views_api() { 11 return array( 12 'api' => '2.0', 13 'path' => drupal_get_path('module', 'data_node') .'/views', 14 ); 15 } 16 17 /** 18 * Implementation of hook_theme(). 19 */ 20 function data_node_theme() { 21 return array( 22 'data_node_label' => array( 23 'arguments' => array('table' => NULL, 'id' => NULL, 'nid' => NULL, 'title' => NULL), 24 'file' => 'data_node.theme.inc', 25 ), 26 'data_node_active_form' => array( 27 'arguments' => array('form' => array()), 28 'file' => 'data_node.theme.inc', 29 ), 30 ); 31 } 32 33 /** 34 * Implementation of hook_block(). 35 */ 36 function data_node_block($op = 'list', $delta = 0) { 37 switch ($op) { 38 case 'list': 39 $blocks = array(); 40 $tables = data_get_all_tables(); 41 foreach ($tables as $table) { 42 $meta = $table->get('meta'); 43 if (!empty($meta['data_node']['content_type'])) { 44 $blocks[$table->get('name')]['info'] = t('Data node: Active node form for @table', array('@table' => $table->get('title'))); 45 } 46 } 47 return $blocks; 48 case 'view': 49 if (user_access('manage data relations') && $table = data_get_table($delta)) { 50 // Grab the node type name 51 $meta = $table->get('meta'); 52 $names = node_get_types('names'); 53 $type_name = check_plain($names[$meta['data_node']['content_type']]); 54 55 return array( 56 'subject' => t('Active !type', array('!type' => $type_name)), 57 'content' => drupal_get_form('data_node_active_form', $table), 58 ); 59 } 60 } 61 } 62 63 /** 64 * Implementation of hook_menu(). 65 */ 66 function data_node_menu() { 67 $items = array(); 68 $items['data-node/add/%data_ui_table/%/%/%'] = array( 69 'page callback' => 'data_node_add_page', 70 'page arguments' => array(2, 3, 4, 5), 71 'access callback' => 'user_access', 72 'access arguments' => array('manage data relations'), 73 'type' => MENU_CALLBACK, 74 ); 75 $items['data-node/remove/%data_ui_table/%/%/%'] = array( 76 'page callback' => 'data_node_remove_page', 77 'page arguments' => array(2, 3, 4, 5), 78 'access callback' => 'user_access', 79 'access arguments' => array('manage data relations'), 80 'type' => MENU_CALLBACK, 81 ); 82 $items['data-node/active/%data_ui_table/%'] = array( 83 'page callback' => 'data_node_active_page', 84 'page arguments' => array(2, 3), 85 'access callback' => 'user_access', 86 'access arguments' => array('manage data relations'), 87 'type' => MENU_CALLBACK, 88 ); 89 $items['admin/build/data/edit/%data_ui_table/node'] = array( 90 'title' => 'Relate to nodes', 91 'description' => 'Administer data tables.', 92 'page callback' => 'drupal_get_form', 93 'page arguments' => array('data_node_settings_form', 4), 94 'file' => 'data_node.admin.inc', 95 'access arguments' => array('administer data tables'), 96 'type' => MENU_LOCAL_TASK, 97 ); 98 return $items; 99 } 100 101 /** 102 * Implementation of hook_perm(). 103 */ 104 function data_node_perm() { 105 return array('edit data node relations'); 106 } 107 108 /** 109 * Form callback for setting the active node. 110 */ 111 function data_node_active_form(&$form_state, $table) { 112 $form = array( 113 '#attributes' => array('class' => 'data-node-active-form'), 114 '#table' => $table, 115 '#theme' => 'data_node_active_form', 116 117 // These three are here to keep JS from doing reconstructive URL surgery. 118 'ajax_url' => array( 119 '#type' => 'hidden', 120 '#value' => url('data-node/active/' . $table->get('name')), 121 '#attributes' => array('class' => 'data-node-ajax-url'), 122 ), 123 'add_url' => array( 124 '#type' => 'hidden', 125 '#value' => url('data-node/add/' . $table->get('name')), 126 '#attributes' => array('class' => 'data-node-add-url'), 127 ), 128 'remove_url' => array( 129 '#type' => 'hidden', 130 '#value' => url('data-node/remove/' . $table->get('name')), 131 '#attributes' => array('class' => 'data-node-remove-url'), 132 ), 133 ); 134 135 $nodes = array(0 => '--'. t('Select') .'--'); 136 $nodes += data_node_get_nodes($table); 137 138 // Grab the node type name and provide a creation option 139 $meta = $table->get('meta'); 140 $names = node_get_types('names'); 141 $type_name = check_plain($names[$meta['data_node']['content_type']]); 142 if (node_access('create', $meta['data_node']['content_type'])) { 143 $nodes['new'] = '< '. t('New !type', array('!type' => $type_name)) .' >'; 144 } 145 146 $form['nid'] = array( 147 '#type' => 'select', 148 '#title' => t('Active !type', array('!type' => $type_name)), 149 '#options' => $nodes, 150 '#default_value' => data_node_get_active($table->get('name')), 151 ); 152 153 if (node_access('create', $meta['data_node']['content_type'])) { 154 $form['new'] = array('#tree' => FALSE); 155 $form['new']['type'] = array( 156 '#type' => 'value', 157 '#value' => $meta['data_node']['content_type'], 158 ); 159 $form['new']['title'] = array( 160 '#type' => 'textfield', 161 '#size' => 20, 162 ); 163 $form['new']['create'] = array( 164 '#type' => 'submit', 165 '#value' => t('Create'), 166 '#submit' => array('data_node_active_form_create_submit'), 167 ); 168 } 169 170 $form['submit'] = array( 171 '#type' => 'submit', 172 '#value' => t('Set'), 173 '#submit' => array('data_node_active_form_submit'), 174 ); 175 return $form; 176 } 177 178 /** 179 * Submit handler for form. 180 */ 181 function data_node_active_form_submit($form, &$form_state) { 182 data_node_set_active($form['#table']->get('name'), $form_state['values']['nid']); 183 184 // Redirect ourselves because '#redirect' does not support queries. 185 $query = array(); 186 foreach ($_GET as $k => $v) { 187 if ($k != 'q') { 188 $query[] = "$k=$v"; 189 } 190 } 191 drupal_goto($_GET['q'], implode('&', $query)); 192 } 193 194 /** 195 * Create submit handler for form. 196 */ 197 function data_node_active_form_create_submit($form, &$form_state) { 198 $title = trim($form_state['values']['title']); 199 if (!empty($title)) { 200 global $user; 201 $node = new stdClass(); 202 $node->uid = $user->uid; 203 $node->title = $title; 204 $node->type = $form_state['values']['type']; 205 $node->body = ''; 206 foreach (variable_get('node_options_'. $node->type, array('status')) as $key) { 207 $node->{$key} = 1; 208 } 209 node_save($node); 210 211 data_node_set_active($form['#table']->get('name'), $node->nid); 212 } 213 214 // Redirect ourselves because '#redirect' does not support queries. 215 $query = array(); 216 foreach ($_GET as $k => $v) { 217 if ($k != 'q') { 218 $query[] = "$k=$v"; 219 } 220 } 221 drupal_goto($_GET['q'], implode('&', $query)); 222 } 223 224 /** 225 * Page callback for adding. 226 * 227 * @todo: add tokenizing drupal_valid_token/drupal_get_token to prevent XSS 228 */ 229 function data_node_add_page($table, $id, $nid, $token) { 230 if (drupal_valid_token($token, _data_node_hash($table, $id, $nid))) { 231 data_node_remove($table, $id, $nid); 232 data_node_add($table, $id, $nid); 233 } 234 // Handle AJAX requests 235 if (isset($_GET['ajax'])) { 236 $response = array( 237 'status' => TRUE, 238 'table' => $table->get('name'), 239 'id' => $id, 240 'nid' => $nid, 241 'labels' => data_node_render_labels($table, $id), 242 243 'add_link' => data_node_render_remove_link($table, $id, $nid), 244 'remove_link' => data_node_render_add_link($table, $id, $nid), 245 ); 246 drupal_json($response); 247 exit; 248 } 249 drupal_goto($_GET['q']); 250 } 251 252 /** 253 * Page callback for setting the active node. 254 */ 255 function data_node_active_page($table, $nid) { 256 data_node_set_active($table->get('name'), $nid); 257 // Handle AJAX requests 258 if (isset($_GET['ajax'])) { 259 // Generate new add/remove links to replace stale ones. 260 $stale = isset($_GET['stale']) ? explode('-', $_GET['stale']) : array(); 261 $refresh = array(); 262 foreach ($stale as $id) { 263 $node_list = data_node_get_nids($table, $id); 264 $refresh[$id] = in_array($nid, $node_list) ? data_node_render_remove_link($table, $id, $nid) : data_node_render_add_link($table, $id, $nid); 265 } 266 $response = array( 267 'status' => TRUE, 268 'table' => $table->get('name'), 269 'nid' => $nid, 270 'refresh' => $refresh, 271 ); 272 drupal_json($response); 273 exit; 274 } 275 drupal_goto($_GET['q']); 276 } 277 278 /** 279 * Page callback for removing. 280 * 281 * @todo: add tokenizing drupal_valid_token/drupal_get_token to prevent XSS 282 */ 283 function data_node_remove_page($table, $id, $nid, $token) { 284 if (drupal_valid_token($token, _data_node_hash($table, $id, $nid))) { 285 data_node_remove($table, $id, $nid); 286 } 287 // Handle AJAX requests 288 if (isset($_GET['ajax'])) { 289 $response = array( 290 'status' => TRUE, 291 'table' => $table->get('name'), 292 'id' => $id, 293 'nid' => $nid, 294 'labels' => data_node_render_labels($table, $id), 295 296 'add_link' => data_node_render_remove_link($table, $id, $nid), 297 'remove_link' => data_node_render_add_link($table, $id, $nid), 298 ); 299 drupal_json($response); 300 exit; 301 } 302 drupal_goto($_GET['q']); 303 } 304 305 /** 306 * Add a relationship between a data table and a node. 307 */ 308 function data_node_add($table, $id, $nid) { 309 $save = array( 310 'data_table_name' => $table->get('name'), 311 'id' => $id, 312 'nid' => $nid, 313 ); 314 return drupal_write_record('data_table_node', $save); 315 } 316 317 /** 318 * Get the nids for a given record. 319 */ 320 function data_node_get_nids($table, $id) { 321 $list = array(); 322 $result = db_query("SELECT nid FROM {data_table_node} WHERE data_table_name = '%s' AND id = %d", $table->get('name'), $id); 323 while ($row = db_fetch_object($result)) { 324 $list[] = $row->nid; 325 } 326 return $list; 327 } 328 329 /** 330 * Remove a relationship between a data table and a node. 331 */ 332 function data_node_remove($table, $id, $nid) { 333 db_query("DELETE FROM {data_table_node} WHERE data_table_name = '%s' AND id = %d AND nid = %d", $table->get('name'), $id, $nid); 334 } 335 336 /** 337 * Get all available nodes for a specific table. 338 */ 339 function data_node_get_nodes($table) { 340 $nodes = array(); 341 $meta = $table->get('meta'); 342 if ($meta['data_node']['content_type']) { 343 $result = db_query("SELECT nid, title FROM {node} WHERE type = '%s' ORDER BY title ASC", $meta['data_node']['content_type']); 344 while ($node = db_fetch_object($result)) { 345 $nodes[$node->nid] = $node->title; 346 } 347 } 348 return $nodes; 349 } 350 351 /** 352 * Set a specific node as the active node. This is used for the collection workflow. 353 */ 354 function data_node_set_active($table_name, $nid) { 355 $_SESSION['data_node_active'][$table_name] = $nid; 356 } 357 358 /** 359 * Get a the current active node. This is used for the collection workflow. 360 */ 361 function data_node_get_active($table_name) { 362 return $_SESSION['data_node_active'][$table_name]; 363 } 364 365 /** 366 * Create a simple hash of a table name, an id and a nid. 367 */ 368 function _data_node_hash($table, $id, $nid) { 369 return $table->get('name') . $id . $nid; 370 } 371 372 /** 373 * Static cache node titles to avoid unnecessary node loading. 374 */ 375 function _data_node_get_title($nid) { 376 static $nodes = array(); 377 if (!isset($nodes[$nid])) { 378 $nodes[$nid] = check_plain(db_result(db_query("SELECT title FROM {node} WHERE nid = %d", $nid))); 379 } 380 return isset($nodes[$nid]) ? $nodes[$nid] : NULL; 381 } 382 383 /** 384 * Generate the path for a remove link for an item/node pair. 385 */ 386 function data_node_remove_path($table, $id, $nid) { 387 $token = drupal_get_token(_data_node_hash($table, $id, $nid)); 388 $table_name = $table->get('name'); 389 return "data-node/remove/{$table_name}/$id/$nid/$token"; 390 } 391 392 /** 393 * Generate the path for an add link for an item/node pair. 394 */ 395 function data_node_add_path($table, $id, $nid) { 396 $token = drupal_get_token(_data_node_hash($table, $id, $nid)); 397 $table_name = $table->get('name'); 398 return "data-node/add/{$table_name}/$id/$nid/$token"; 399 } 400 401 /** 402 * Render node-data labels for a given item. 403 */ 404 function data_node_render_labels($table, $id) { 405 $nids = data_node_get_nids($table, $id); 406 foreach ($nids as $nid) { 407 $title = _data_node_get_title($nid);; 408 $output .= theme('data_node_label', $table, $id, $nid, $title); 409 } 410 $table_name = $table->get('name'); 411 $class = "data_node_labels-{$table_name}-{$id}"; 412 return "<div class='{$class} clear-block'>{$output}</div>"; 413 } 414 415 /** 416 * Render a placeholder when there are no active nodes that can be replaced via AJAX. 417 */ 418 function data_node_render_placeholder_link($table, $id) { 419 drupal_add_css(drupal_get_path('module', 'data_node') . '/data_node.css'); 420 drupal_add_js(drupal_get_path('module', 'data_node') . '/data_node.js'); 421 $table_name = $table->get('name'); 422 return "<span class='data-node-placeholder data_node_link-{$table_name}-{$id}-0'></span>"; 423 } 424 425 /** 426 * Render an add link for a given item.. 427 */ 428 function data_node_render_add_link($table, $id, $nid) { 429 drupal_add_css(drupal_get_path('module', 'data_node') . '/data_node.css'); 430 drupal_add_js(drupal_get_path('module', 'data_node') . '/data_node.js'); 431 432 $title = _data_node_get_title($nid); 433 $table_name = $table->get('name'); 434 $class = "data_node_link-{$table_name}-{$id}-{$nid}"; 435 return l(t('Add to !title', array('!title' => $title)), data_node_add_path($table, $id, $nid), array('attributes' => array('class' => "data-node-add $class"), 'query' => drupal_get_destination())); 436 } 437 438 /** 439 * Render a remove link for a given item. 440 */ 441 function data_node_render_remove_link($table, $id, $nid) { 442 drupal_add_css(drupal_get_path('module', 'data_node') . '/data_node.css'); 443 drupal_add_js(drupal_get_path('module', 'data_node') . '/data_node.js'); 444 445 $title = _data_node_get_title($nid); 446 $table_name = $table->get('name'); 447 $class = "data_node_link-{$table_name}-{$id}-{$nid}"; 448 return l(t('Remove from !title', array('!title' => $title)), data_node_remove_path($table, $id, $nid), array('attributes' => array('class' => "data-node-remove $class"), 'query' => drupal_get_destination())); 449 }
title
Description
Body
title
Description
Body
title
Description
Body
title
Body
| Generated: Mon Jul 9 18:01:44 2012 | Cross-referenced by PHPXref 0.7 |