Projet

Général

Profil

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

root / drupal7 / sites / all / modules / panels / panels_node / panels_node.module @ a2bb1a14

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 callback' => 'panels_add_panel_access_callback',
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
 * Access callback to determine if user has access to add panel nodes.
96
 */
97
function panels_add_panel_access_callback() {
98
  return user_access('create panel content') || user_access('administer panel-nodes');
99
}
100

    
101
/**
102
 * Override of node add page to force layout selection prior
103
 * to actually editing a node.
104
 */
105
function panels_node_add() {
106
  $output = '';
107

    
108
  ctools_include('plugins', 'panels');
109
  ctools_include('common', 'panels');
110

    
111
  $layouts = panels_common_get_allowed_layouts('panels_node');
112
  return panels_common_print_layout_links($layouts, 'node/add/panel', array('query' => drupal_get_query_parameters()));
113
}
114

    
115
// ---------------------------------------------------------------------------
116
// Node hooks
117

    
118
/**
119
 * Implementation of hook_node_info().
120
 */
121
function panels_node_node_info() {
122
  // Safety: go away if CTools is not at an appropriate version.
123
  if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
124
    return array();
125
  }
126

    
127
  return array(
128
    'panel' => array(
129
      'name' => t('Panel'),
130
      // We use panels_node_hook so that panels_node private
131
      // callbacks do not get confused with panels versions of
132
      // nodeapi callbacks.
133
      'base' => 'panels_node_hook',
134
      'body_label' => t('Teaser'),
135
      'description' => t("A panel layout broken up into rows and columns."),
136
    ),
137
  );
138
}
139

    
140
/**
141
 * Implementation of hook_access().
142
 */
143
function panels_node_node_access($node, $op, $account) {
144
  if ($op == 'create' && $node != 'panel') {
145
    return NODE_ACCESS_IGNORE;
146
  }
147

    
148
  if (is_object($node) && $node->type != 'panel') {
149
    return NODE_ACCESS_IGNORE;
150
  }
151

    
152
  if (user_access('administer panel-nodes', $account)) {
153
    return NODE_ACCESS_ALLOW;
154
  }
155
}
156

    
157
/**
158
 * Implementation of hook_form().
159
 */
160
function panels_node_hook_form(&$node, &$form_state) {
161
  ctools_include('plugins', 'panels');
162

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

    
172
    $layout = panels_get_layout($panel_layout);
173
    if (empty($layout)) {
174
      return MENU_NOT_FOUND;
175
    }
176
    $form['panels_node']['layout'] = array(
177
      '#type' => 'value',
178
      '#value' => $panel_layout,
179
    );
180
  }
181

    
182
  $type = node_type_get_type($node);
183

    
184
  $form['title'] = array(
185
    '#type' => 'textfield',
186
    '#title' => check_plain($type->title_label),
187
    '#required' => TRUE,
188
    '#default_value' => $node->title,
189
  );
190

    
191
  $css_id = '';
192
  if (!empty($node->panels_node['css_id'])) {
193
    $css_id = $node->panels_node['css_id'];
194
  }
195

    
196
  $form['panels_node']['css_id'] = array(
197
    '#type' => 'textfield',
198
    '#title' => t('CSS ID'),
199
    '#size' => 30,
200
    '#description' => t('An ID that can be used by CSS to style the panel.'),
201
    '#default_value' => $css_id,
202
  );
203

    
204
  // Support for different rendering pipelines
205
  // Mostly borrowed from panel_context.inc
206
  $pipelines = panels_get_renderer_pipelines();
207

    
208
  $options = array();
209
  foreach ($pipelines as $name => $pipeline) {
210
    $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
211
  }
212

    
213
  $form['panels_node']['pipeline'] = array(
214
    '#type' => 'radios',
215
    '#options' => $options,
216
    '#title' => t('Renderer'),
217
    '#default_value' => isset($node->panels_node['pipeline']) ? $node->panels_node['pipeline'] : variable_get('panels_renderer_default', 'standard'),
218
  );
219

    
220
  return $form;
221
}
222

    
223
/**
224
 * Implementation of hook_validate().
225
 */
226
function panels_node_hook_validate($node, $form, &$form_state) {
227
  if (!$node->nid && empty($node->panels_node['layout'])) {
228
    form_error($form['panels_node']['layout'], t('Please select a layout.'));
229
  }
230
}
231

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

    
248
/**
249
 * Implementation of hook_insert().
250
 */
251
function panels_node_hook_insert(&$node) {
252
  // Create a new display and record that.
253
  $display = panels_new_display();
254
  $display->layout = $node->panels_node['layout'];
255
  $display->storage_type = 'panels_node';
256
  $display->storage_id = $node->nid;
257

    
258
  // Special handling for nodes being imported from an export.module data dump.
259
  if (!empty($node->export_display)) {
260
    // This works by overriding the $display set above
261
    eval($node->export_display);
262
    unset($node->export_display);
263
  }
264

    
265
  panels_save_display($display);
266
  $node->panels_node['did'] = $display->did;
267

    
268
  db_insert('panels_node')
269
    ->fields(array(
270
      'nid' => $node->nid,
271
      'did' => $display->did,
272
      'css_id' => $node->panels_node['css_id'],
273
      'pipeline' => $node->panels_node['pipeline'],
274
    ))
275
    ->execute();
276
}
277

    
278
/**
279
 * Implementation of hook_delete().
280
 */
281
function panels_node_hook_delete(&$node) {
282
  db_delete('panels_node')->condition('nid', $node->nid)->execute();
283
  if (!empty($node->panels_node['did'])) {
284
    panels_delete_display($node->panels_node['did']);
285
  }
286
}
287

    
288
/**
289
 * Implementation of hook_update().
290
 */
291
function panels_node_hook_update($node) {
292
  db_update('panels_node')
293
    ->condition('nid', $node->nid)
294
    ->fields(array(
295
      'css_id' => $node->panels_node['css_id'],
296
      'pipeline' => $node->panels_node['pipeline'],
297
    ))
298
    ->execute();
299
}
300

    
301
/**
302
 * Implementation of hook_view().
303
 */
304
function panels_node_hook_view($node, $view_mode) {
305
  static $rendering = array();
306

    
307
  // Prevent loops if someone foolishly puts the node inside itself:
308
  if (!empty($rendering[$node->nid])) {
309
    return $node;
310
  }
311

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

    
334
  unset($rendering[$node->nid]);
335
  return $node;
336
}
337

    
338
// ---------------------------------------------------------------------------
339
// Administrative pages
340

    
341
/**
342
 * Settings for panel nodes.
343
 */
344
function panels_node_settings() {
345
  ctools_include('common', 'panels');
346
  return drupal_get_form('panels_common_settings', 'panels_node');
347
}
348

    
349
// ---------------------------------------------------------------------------
350
// Meat of the Panels API; almost completely passing through to panels.module
351

    
352
/**
353
 * Pass through to the panels layout editor.
354
 */
355
function panels_node_edit_layout($node) {
356
//  ctools_include('plugins', 'panels');
357
  ctools_include('context');
358
  $display = panels_load_display($node->panels_node['did']);
359
  $display->context = panels_node_get_context($node);
360
  return panels_edit_layout($display, t('Save'), "node/$node->nid/panel_layout", 'panels_node');
361
}
362

    
363
/**
364
 * Pass through to the panels content editor.
365
 */
366
function panels_node_edit_content($node) {
367
  ctools_include('context');
368
  $display = panels_load_display($node->panels_node['did']);
369
  $display->context = panels_node_get_context($node);
370
  ctools_include('common', 'panels');
371
  $content_types = panels_common_get_allowed_types('panels_node', $display->context);
372

    
373
  return panels_edit($display, "node/$node->nid/panel_content", $content_types);
374
}
375

    
376
/**
377
 * Build the context to use for a panel node.
378
 */
379
function panels_node_get_context(&$node) {
380
  ctools_include('context');
381
  $context = ctools_context_create('node', $node);
382
  $context->identifier = t('This node');
383
  $context->keyword = 'node';
384
  return array('panel-node' => $context);
385
}
386

    
387
/**
388
 * Implementation of hook_export_node_alter()
389
 *
390
 * Integrate with export.module for saving panel_nodes into code.
391
 */
392
function panels_node_export_node_alter(&$node, $original_node, $method) {
393
  if ($method == 'export') {
394
    $node_export_omitted = variable_get('node_export_omitted', array());
395
    if (variable_get('node_export_method', '') != 'save-edit' && (array_key_exists('panel', $node_export_omitted) && !$node_export_omitted['panel'])) {
396
      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."));
397
    }
398
    $display = panels_load_display($node->panels_node['did']);
399
    $export = panels_export_display($display);
400
    $node->export_display = $export;
401
  }
402
}
403

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

    
417
/**
418
 * Implements hook_panels_ipe_access().
419
 */
420
function panels_node_panels_ipe_access($display) {
421
  // We only care about Panels displays from panels_node.
422
  if (isset($display->context['panel-node'])) {
423
    // Only allow access to use the IPE if the user has 'update' access to
424
    // the underlying node.
425
    $node = $display->context['panel-node']->data;
426
    return node_access('update', $node); 
427
  }
428
}
429

    
430
// ---------------------------------------------------------------------------
431
// Callbacks for panel caching.
432

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

    
449
    ctools_include('common', 'panels');
450
    $cache->display = panels_load_display($node->panels_node['did']);
451
    $cache->display->css_id = $node->panels_node['css_id'];
452
    $cache->display->context = panels_node_get_context($node);
453
    $cache->display->cache_key = 'panels_node:' . $node->nid;
454
    $cache->content_types =   panels_common_get_allowed_types('panels_node', $cache->display->context);
455
    $cache->allwed_layouts = panels_common_get_allowed_layouts('panels_node');
456
  }
457

    
458
  return $cache;
459
}
460

    
461
/**
462
 * Store a display edit in progress in the panels cache.
463
 */
464
function panels_node_panels_cache_set($nid, $cache) {
465
  ctools_include('object-cache');
466
  ctools_object_cache_set('panels_node_display_cache', $nid, $cache);
467
}
468

    
469
/**
470
 * Clear all changes made to a display using the panels cache.
471
 */
472
function panels_node_panels_cache_clear($nid, $cache) {
473
  ctools_include('object-cache');
474
  ctools_object_cache_clear('panels_node_display_cache', $nid);
475
}
476

    
477
/**
478
 * React to a cache save and save the display and clear cache.
479
 */
480
function panels_node_panels_cache_save($nid, $cache) {
481
  panels_save_display($cache->display);
482
  ctools_include('object-cache');
483
  ctools_object_cache_clear('panels_node_display_cache', $nid);
484
}