Projet

Général

Profil

Paste
Télécharger (14 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panels / panels_node / panels_node.module @ 136a805a

1
<?php
2

    
3

    
4
/**
5
 * @file panels_node.module
6
 *
7
 * This module provides the "panel" node type.
8
 * Panel nodes are useful to add additional content to the content area
9
 * on a per-node base.
10
 */
11

    
12
// ---------------------------------------------------------------------------
13
// General Drupal hooks
14

    
15
/**
16
 * Implementation of hook_permission().
17
 */
18
function panels_node_permission() {
19
  return array(
20
    'administer panel-nodes' => array(
21
      'title' => t('Administer panel nodes'),
22
      'description' => t('Full administrative access to panel nodes including create, update and delete all'),
23
    ),
24
  );
25
}
26

    
27
/**
28
 * Implementation of hook_ctools_plugin_directory().
29
 */
30
function panels_node_ctools_plugin_directory($module, $plugin) {
31
  if ($module == 'panels' && $plugin == 'panels_storage') {
32
    return 'plugins/' . $plugin;
33
  }
34
}
35

    
36
/**
37
 * Implementation of hook_menu().
38
 */
39
function panels_node_menu() {
40
  // Safety: go away if CTools is not at an appropriate version.
41
  if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
42
    return array();
43
  }
44

    
45
  $items['admin/structure/panels/settings/panel-node'] = array(
46
    'title' => 'Panel nodes',
47
    'description' => 'Configure which content is available to add to panel node displays.',
48
    'access arguments' => array('administer panel-nodes'),
49
    'page callback' => 'panels_node_settings',
50
    'type' => MENU_LOCAL_TASK,
51
  );
52

    
53
  // Avoid some repetition on these:
54
  $base = array(
55
    'access callback' => 'panels_node_edit_node',
56
    'access arguments' => array(1),
57
    'page arguments' => array(1),
58
    'type' => MENU_LOCAL_TASK,
59
  );
60

    
61
  $items['node/%node/panel_layout'] = array(
62
    'title' => 'Panel layout',
63
    'page callback' => 'panels_node_edit_layout',
64
    'weight' => 2,
65
  ) + $base;
66

    
67
  $items['node/%node/panel_content'] = array(
68
    'title' => 'Panel content',
69
    'page callback' => 'panels_node_edit_content',
70
    'weight' => 3,
71
  ) + $base;
72

    
73
  $items['node/add/panel/choose-layout'] = array(
74
    'title' => 'Choose layout',
75
    'access arguments' => array('create panel-nodes'),
76
    'page callback' => 'panels_node_add',
77
    'type' => MENU_CALLBACK,
78
  );
79

    
80
  return $items;
81
}
82

    
83
/**
84
 * Access callback to determine if a user has edit access
85
 */
86
function panels_node_edit_node($node) {
87
  if (!isset($node->panels_node)) {
88
    return FALSE;
89
  }
90

    
91
  return node_access('update', $node);
92
}
93

    
94
/**
95
 * Override of node add page to force layout selection prior
96
 * to actually editing a node.
97
 */
98
function panels_node_add() {
99
  $output = '';
100

    
101
  ctools_include('plugins', 'panels');
102
  ctools_include('common', 'panels');
103

    
104
  $layouts = panels_common_get_allowed_layouts('panels_node');
105
  return panels_common_print_layout_links($layouts, 'node/add/panel', array('query' => drupal_get_query_parameters()));
106
}
107

    
108
// ---------------------------------------------------------------------------
109
// Node hooks
110

    
111
/**
112
 * Implementation of hook_node_info().
113
 */
114
function panels_node_node_info() {
115
  // Safety: go away if CTools is not at an appropriate version.
116
  if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
117
    return array();
118
  }
119

    
120
  return array(
121
    'panel' => array(
122
      'name' => t('Panel'),
123
      // We use panels_node_hook so that panels_node private
124
      // callbacks do not get confused with panels versions of
125
      // nodeapi callbacks.
126
      'base' => 'panels_node_hook',
127
      'body_label' => t('Teaser'),
128
      'description' => t("A panel layout broken up into rows and columns."),
129
    ),
130
  );
131
}
132

    
133
/**
134
 * Implementation of hook_access().
135
 */
136
function panels_node_node_access($node, $op, $account) {
137
  if ($op == 'create' && $node != 'panel') {
138
    return NODE_ACCESS_IGNORE;
139
  }
140

    
141
  if (is_object($node) && $node->type != 'panel') {
142
    return NODE_ACCESS_IGNORE;
143
  }
144

    
145
  if (user_access('administer panel-nodes', $account)) {
146
    return NODE_ACCESS_ALLOW;
147
  }
148
}
149

    
150
/**
151
 * Implementation of hook_form().
152
 */
153
function panels_node_hook_form(&$node, &$form_state) {
154
  ctools_include('plugins', 'panels');
155

    
156
  $form['panels_node']['#tree'] = TRUE;
157
  if (empty($node->nid) && arg(0) == 'node' && arg(1) == 'add') {
158
    // Grab our selected layout from the $node, If it doesn't exist, try arg(3)
159
    // and if that doesn't work present them with a list to pick from.
160
    $panel_layout = isset($node->panel_layout) ? $node->panel_layout : arg(3);
161
    if (empty($panel_layout)) {
162
      drupal_goto('node/add/panel/choose-layout', array('query' => drupal_get_query_parameters()));
163
    }
164

    
165
    $layout = panels_get_layout($panel_layout);
166
    if (empty($layout)) {
167
      return MENU_NOT_FOUND;
168
    }
169
    $form['panels_node']['layout'] = array(
170
      '#type' => 'value',
171
      '#value' => $panel_layout,
172
    );
173
  }
174

    
175
  $type = node_type_get_type($node);
176

    
177
  $form['title'] = array(
178
    '#type' => 'textfield',
179
    '#title' => check_plain($type->title_label),
180
    '#required' => TRUE,
181
    '#default_value' => $node->title,
182
  );
183

    
184
  $css_id = '';
185
  if (!empty($node->panels_node['css_id'])) {
186
    $css_id = $node->panels_node['css_id'];
187
  }
188

    
189
  $form['panels_node']['css_id'] = array(
190
    '#type' => 'textfield',
191
    '#title' => t('CSS ID'),
192
    '#size' => 30,
193
    '#description' => t('An ID that can be used by CSS to style the panel.'),
194
    '#default_value' => $css_id,
195
  );
196

    
197
  // Support for different rendering pipelines
198
  // Mostly borrowed from panel_context.inc
199
  $pipelines = panels_get_renderer_pipelines();
200

    
201
  $options = array();
202
  foreach ($pipelines as $name => $pipeline) {
203
    $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
204
  }
205

    
206
  $form['panels_node']['pipeline'] = array(
207
    '#type' => 'radios',
208
    '#options' => $options,
209
    '#title' => t('Renderer'),
210
    '#default_value' => isset($node->panels_node['pipeline']) ? $node->panels_node['pipeline'] : variable_get('panels_renderer_default', 'standard'),
211
  );
212

    
213
  return $form;
214
}
215

    
216
/**
217
 * Implementation of hook_validate().
218
 */
219
function panels_node_hook_validate($node, $form, &$form_state) {
220
  if (!$node->nid && empty($node->panels_node['layout'])) {
221
    form_error($form['panels_node']['layout'], t('Please select a layout.'));
222
  }
223
}
224

    
225
/**
226
 * Implementation of hook_load().
227
 *
228
 * Panels does not use revisions for nodes because that would open us up
229
 * to have completely separate displays, and we'd have to copy them,
230
 * and that's going to be a LOT of data.
231
 */
232
function panels_node_hook_load($nodes) {
233
  // We shortcut this because only in some really drastic corruption circumstance will this
234
  // not work.
235
  $result = db_query("SELECT * FROM {panels_node} WHERE nid IN (:nids)", array(':nids' => array_keys($nodes)));
236
  foreach ($result as $record) {
237
    $nodes[$record->nid]->panels_node = (array) $record;
238
  }
239
}
240

    
241
/**
242
 * Implementation of hook_insert().
243
 */
244
function panels_node_hook_insert(&$node) {
245
  // Create a new display and record that.
246
  $display = panels_new_display();
247
  $display->layout = $node->panels_node['layout'];
248
  $display->storage_type = 'panels_node';
249
  $display->storage_id = $node->nid;
250

    
251
  // Special handling for nodes being imported from an export.module data dump.
252
  if (!empty($node->export_display)) {
253
    // This works by overriding the $display set above
254
    eval($node->export_display);
255
    unset($node->export_display);
256
  }
257

    
258
  panels_save_display($display);
259
  $node->panels_node['did'] = $display->did;
260

    
261
  db_insert('panels_node')
262
    ->fields(array(
263
      'nid' => $node->nid,
264
      'did' => $display->did,
265
      'css_id' => $node->panels_node['css_id'],
266
      'pipeline' => $node->panels_node['pipeline'],
267
    ))
268
    ->execute();
269
}
270

    
271
/**
272
 * Implementation of hook_delete().
273
 */
274
function panels_node_hook_delete(&$node) {
275
  db_delete('panels_node')->condition('nid', $node->nid)->execute();
276
  if (!empty($node->panels_node['did'])) {
277
    panels_delete_display($node->panels_node['did']);
278
  }
279
}
280

    
281
/**
282
 * Implementation of hook_update().
283
 */
284
function panels_node_hook_update($node) {
285
  db_update('panels_node')
286
    ->condition('nid', $node->nid)
287
    ->fields(array(
288
      'css_id' => $node->panels_node['css_id'],
289
      'pipeline' => $node->panels_node['pipeline'],
290
    ))
291
    ->execute();
292
}
293

    
294
/**
295
 * Implementation of hook_view().
296
 */
297
function panels_node_hook_view($node, $view_mode) {
298
  static $rendering = array();
299

    
300
  // Prevent loops if someone foolishly puts the node inside itself:
301
  if (!empty($rendering[$node->nid])) {
302
    return $node;
303
  }
304

    
305
  $rendering[$node->nid] = TRUE;
306
  ctools_include('plugins', 'panels');
307
  if ($view_mode == 'teaser') {
308
    // Because our teasier is never the same as our content, *always* provide
309
    // the read more flag.
310
    $node->readmore = TRUE;
311
  }
312
  else {
313
    if (!empty($node->panels_node['did'])) {
314
      $display = panels_load_display($node->panels_node['did']);
315
      $display->css_id = $node->panels_node['css_id'];
316
      // TODO: Find a way to make sure this can't node_view.
317
      $display->context = panels_node_get_context($node);
318
      $display->cache_key = 'panels_node:' . $node->nid;
319
      $renderer = panels_get_renderer($node->panels_node['pipeline'], $display);
320
      $node->content['body'] = array(
321
        '#markup' => panels_render_display($display, $renderer),
322
        '#weight' => 0,
323
      );
324
    }
325
  }
326

    
327
  unset($rendering[$node->nid]);
328
  return $node;
329
}
330

    
331
// ---------------------------------------------------------------------------
332
// Administrative pages
333

    
334
/**
335
 * Settings for panel nodes.
336
 */
337
function panels_node_settings() {
338
  ctools_include('common', 'panels');
339
  return drupal_get_form('panels_common_settings', 'panels_node');
340
}
341

    
342
// ---------------------------------------------------------------------------
343
// Meat of the Panels API; almost completely passing through to panels.module
344

    
345
/**
346
 * Pass through to the panels layout editor.
347
 */
348
function panels_node_edit_layout($node) {
349
//  ctools_include('plugins', 'panels');
350
  ctools_include('context');
351
  $display = panels_load_display($node->panels_node['did']);
352
  $display->context = panels_node_get_context($node);
353
  return panels_edit_layout($display, t('Save'), "node/$node->nid/panel_layout", 'panels_node');
354
}
355

    
356
/**
357
 * Pass through to the panels content editor.
358
 */
359
function panels_node_edit_content($node) {
360
  ctools_include('context');
361
  $display = panels_load_display($node->panels_node['did']);
362
  $display->context = panels_node_get_context($node);
363
  ctools_include('common', 'panels');
364
  $content_types = panels_common_get_allowed_types('panels_node', $display->context);
365

    
366
  return panels_edit($display, "node/$node->nid/panel_content", $content_types);
367
}
368

    
369
/**
370
 * Build the context to use for a panel node.
371
 */
372
function panels_node_get_context(&$node) {
373
  ctools_include('context');
374
  $context = ctools_context_create('node', $node);
375
  $context->identifier = t('This node');
376
  $context->keyword = 'node';
377
  return array('panel-node' => $context);
378
}
379

    
380
/**
381
 * Implementation of hook_export_node_alter()
382
 *
383
 * Integrate with export.module for saving panel_nodes into code.
384
 */
385
function panels_node_export_node_alter(&$node, $original_node, $method) {
386
  if ($method == 'export') {
387
    $node_export_omitted = variable_get('node_export_omitted', array());
388
    if (variable_get('node_export_method', '') != 'save-edit' && (array_key_exists('panel', $node_export_omitted) && !$node_export_omitted['panel'])) {
389
      drupal_set_message(t("NOTE: in order to import panel_nodes you must first set the export.module settings to \"Save as a new node then edit\", otherwise it won't work."));
390
    }
391
    $display = panels_load_display($node->panels_node['did']);
392
    $export = panels_export_display($display);
393
    $node->export_display = $export;
394
  }
395
}
396

    
397
/**
398
 * Implementation of hook_panels_dashboard_blocks().
399
 *
400
 * Adds panel nodes information to the Panels dashboard.
401
 */
402
function panels_node_panels_dashboard_blocks(&$vars) {
403
  $vars['links']['panels_node'] = array(
404
    'title' => l(t('Panel node'), 'node/add/panel'),
405
    'description' => t('Panel nodes are node content and appear in your searches, but are more limited than panel pages.'),
406
    'weight' => -1,
407
  );
408
}
409

    
410
/**
411
 * Implements hook_panels_ipe_access().
412
 */
413
function panels_node_panels_ipe_access($display) {
414
  // We only care about Panels displays from panels_node.
415
  if (isset($display->context['panel-node'])) {
416
    // Only allow access to use the IPE if the user has 'update' access to
417
    // the underlying node.
418
    $node = $display->context['panel-node']->data;
419
    return node_access('update', $node); 
420
  }
421
}
422

    
423
// ---------------------------------------------------------------------------
424
// Callbacks for panel caching.
425

    
426
/**
427
 * Get display edit cache for a panel node being edited.
428
 *
429
 * The key is the second half of the key in this form:
430
 * panels_node:NID;
431
 */
432
function panels_node_panels_cache_get($nid) {
433
  ctools_include('object-cache');
434
  $cache = ctools_object_cache_get('panels_node_display_cache', $nid);
435
  if (empty($cache)) {
436
    $cache = new stdClass();
437
    $node = node_load($nid);
438
    if (empty($node)) {
439
      return;
440
    }
441

    
442
    ctools_include('common', 'panels');
443
    $cache->display = panels_load_display($node->panels_node['did']);
444
    $cache->display->css_id = $node->panels_node['css_id'];
445
    $cache->display->context = panels_node_get_context($node);
446
    $cache->display->cache_key = 'panels_node:' . $node->nid;
447
    $cache->content_types =   panels_common_get_allowed_types('panels_node', $cache->display->context);
448
    $cache->allwed_layouts = panels_common_get_allowed_layouts('panels_node');
449
  }
450

    
451
  return $cache;
452
}
453

    
454
/**
455
 * Store a display edit in progress in the panels cache.
456
 */
457
function panels_node_panels_cache_set($nid, $cache) {
458
  ctools_include('object-cache');
459
  ctools_object_cache_set('panels_node_display_cache', $nid, $cache);
460
}
461

    
462
/**
463
 * Clear all changes made to a display using the panels cache.
464
 */
465
function panels_node_panels_cache_clear($nid, $cache) {
466
  ctools_include('object-cache');
467
  ctools_object_cache_clear('panels_node_display_cache', $nid);
468
}
469

    
470
/**
471
 * React to a cache save and save the display and clear cache.
472
 */
473
function panels_node_panels_cache_save($nid, $cache) {
474
  panels_save_display($cache->display);
475
  ctools_include('object-cache');
476
  ctools_object_cache_clear('panels_node_display_cache', $nid);
477
}