Projet

Général

Profil

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

root / drupal7 / sites / all / modules / panels / panels_node / panels_node.module @ 5a7e6170

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
    'create panel-nodes' => array(
21
      'title' => t('Create panel nodes'),
22
      'description' => t('Create new panel nodes.'),
23
    ),
24
    'edit any panel-nodes' => array(
25
      'title' => t('Edit any panel-nodes'),
26
      'description' => t('Edit all pre-existing panel nodes regardless of ownership.'),
27
    ),
28
    'edit own panel-nodes' => array(
29
      'title' => t('Edit own panel nodes'),
30
      'description' => t('Edit panel nodes owned by this user.'),
31
    ),
32
    'administer panel-nodes' => array(
33
      'title' => t('Administer panel nodes'),
34
      'description' => t('Full administrative access to panel nodes including create, update and delete all'),
35
    ),
36
    'delete any panel-nodes' => array(
37
      'title' => t('Delete any panel nodes'),
38
      'description' => t('Delete any panel node regardless of ownership'),
39
    ),
40
    'delete own panel-nodes' => array(
41
      'title' => t('Delete own panel nodes'),
42
      'description' => t('Delete any panel node owned by this user.'),
43
    ),
44
  );
45
}
46

    
47
/**
48
 * Implementation of hook_menu().
49
 */
50
function panels_node_menu() {
51
  // Safety: go away if CTools is not at an appropriate version.
52
  if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
53
    return array();
54
  }
55

    
56
  $items['admin/structure/panels/settings/panel-node'] = array(
57
    'title' => 'Panel nodes',
58
    'description' => 'Configure which content is available to add to panel node displays.',
59
    'access arguments' => array('administer panel-nodes'),
60
    'page callback' => 'panels_node_settings',
61
    'type' => MENU_LOCAL_TASK,
62
  );
63

    
64
  // Avoid some repetition on these:
65
  $base = array(
66
    'access callback' => 'panels_node_edit_node',
67
    'access arguments' => array(1),
68
    'page arguments' => array(1),
69
    'type' => MENU_LOCAL_TASK,
70
  );
71

    
72
  $items['node/%node/panel_layout'] = array(
73
    'title' => 'Panel layout',
74
    'page callback' => 'panels_node_edit_layout',
75
    'weight' => 2,
76
  ) + $base;
77

    
78
  $items['node/%node/panel_content'] = array(
79
    'title' => 'Panel content',
80
    'page callback' => 'panels_node_edit_content',
81
    'weight' => 3,
82
  ) + $base;
83

    
84
  $items['node/add/panel/choose-layout'] = array(
85
    'title' => 'Choose layout',
86
    'access arguments' => array('create panel-nodes'),
87
    'page callback' => 'panels_node_add',
88
    'type' => MENU_CALLBACK,
89
  );
90

    
91
  return $items;
92
}
93

    
94
/**
95
 * Access callback to determine if a user has edit access
96
 */
97
function panels_node_edit_node($node) {
98
  if (!isset($node->panels_node)) {
99
    return FALSE;
100
  }
101

    
102
  return node_access('update', $node);
103
}
104

    
105
/**
106
 * Override of node add page to force layout selection prior
107
 * to actually editing a node.
108
 */
109
function panels_node_add() {
110
  $output = '';
111

    
112
  ctools_include('plugins', 'panels');
113
  ctools_include('common', 'panels');
114

    
115
  $layouts = panels_common_get_allowed_layouts('panels_node');
116
  return panels_common_print_layout_links($layouts, 'node/add/panel', array('query' => $_GET));
117
}
118

    
119
// ---------------------------------------------------------------------------
120
// Node hooks
121

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

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

    
144
/**
145
 * Implementation of hook_access().
146
 */
147
function panels_node_node_access($node, $op, $account) {
148
  if ($op == 'create' && $node != 'panel') {
149
    return NODE_ACCESS_IGNORE;
150
  }
151

    
152
  if (is_object($node) && $node->type != 'panel') {
153
    return NODE_ACCESS_IGNORE;
154
  }
155

    
156
  if (user_access('administer panel-nodes', $account)) {
157
    return NODE_ACCESS_ALLOW;
158
  }
159

    
160
  if ($op == 'create' && user_access('create panel-nodes', $account)) {
161
    return NODE_ACCESS_ALLOW;
162
  }
163

    
164
  if ($op == 'update' && (user_access('edit any panel-nodes', $account) || $node->uid == $account->uid && user_access('edit own panel-nodes', $account))) {
165
    return NODE_ACCESS_ALLOW;
166
  }
167

    
168

    
169
  if ($op == 'delete' && (user_access('delete any panel-nodes') || $node->uid == $account->uid && user_access('delete own panel-nodes'))) {
170
    return NODE_ACCESS_ALLOW;
171
  }
172
}
173

    
174
/**
175
 * Implementation of hook_form().
176
 */
177
function panels_node_hook_form(&$node, &$form_state) {
178
  ctools_include('plugins', 'panels');
179

    
180
  $form['panels_node']['#tree'] = TRUE;
181
  if (empty($node->nid) && arg(0) == 'node' && arg(1) == 'add') {
182
    // Grab our selected layout from the $node, If it doesn't exist, try arg(3)
183
    // and if that doesn't work present them with a list to pick from.
184
    $panel_layout = isset($node->panel_layout) ? $node->panel_layout : arg(3);
185
    if (empty($panel_layout)) {
186
      $opts = $_GET;
187
      unset($opts['q']);
188
      return drupal_goto('node/add/panel/choose-layout', $opts);
189
    }
190

    
191
    $layout = panels_get_layout($panel_layout);
192
    if (empty($layout)) {
193
      return drupal_not_found();
194
    }
195
    $form['panels_node']['layout'] = array(
196
      '#type' => 'value',
197
      '#value' => $panel_layout,
198
    );
199
  }
200

    
201
  $type = node_type_get_type($node);
202

    
203
  $form['title'] = array(
204
    '#type' => 'textfield',
205
    '#title' => check_plain($type->title_label),
206
    '#required' => TRUE,
207
    '#default_value' => $node->title,
208
  );
209

    
210
  $css_id = '';
211
  if (!empty($node->panels_node['css_id'])) {
212
    $css_id = $node->panels_node['css_id'];
213
  }
214

    
215
  $form['panels_node']['css_id'] = array(
216
    '#type' => 'textfield',
217
    '#title' => t('CSS ID'),
218
    '#size' => 30,
219
    '#description' => t('An ID that can be used by CSS to style the panel.'),
220
    '#default_value' => $css_id,
221
  );
222

    
223
  // Support for different rendering pipelines
224
  // Mostly borrowed from panel_context.inc
225
  $pipelines = panels_get_renderer_pipelines();
226

    
227
  $options = array();
228
  foreach ($pipelines as $name => $pipeline) {
229
    $options[$name] = check_plain($pipeline->admin_title) . '<div class="description">' . check_plain($pipeline->admin_description) . '</div>';
230
  }
231

    
232
  $form['panels_node']['pipeline'] = array(
233
    '#type' => 'radios',
234
    '#options' => $options,
235
    '#title' => t('Renderer'),
236
    '#default_value' => isset($node->panels_node['pipeline']) ? $node->panels_node['pipeline'] : 'standard',
237
  );
238

    
239
  return $form;
240
}
241

    
242
/**
243
 * Implementation of hook_validate().
244
 */
245
function panels_node_hook_validate($node, $form, &$form_state) {
246
  if (!$node->nid && empty($node->panels_node['layout'])) {
247
    form_error($form['panels_node']['layout'], t('Please select a layout.'));
248
  }
249
}
250

    
251
/**
252
 * Implementation of hook_load().
253
 *
254
 * Panels does not use revisions for nodes because that would open us up
255
 * to have completely separate displays, and we'd have to copy them,
256
 * and that's going to be a LOT of data.
257
 */
258
function panels_node_hook_load($nodes) {
259
  // We shortcut this because only in some really drastic corruption circumstance will this
260
  // not work.
261
  $result = db_query("SELECT * FROM {panels_node} WHERE nid IN (:nids)", array(':nids' => array_keys($nodes)));
262
  foreach ($result as $record) {
263
    $nodes[$record->nid]->panels_node = (array) $record;
264
  }
265
}
266

    
267
/**
268
 * Implementation of hook_insert().
269
 */
270
function panels_node_hook_insert(&$node) {
271
  // Create a new display and record that.
272
  $display = panels_new_display();
273
  $display->layout = $node->panels_node['layout'];
274

    
275
  // Special handling for nodes being imported from an export.module data dump.
276
  if (!empty($node->export_display)) {
277
    // This works by overriding the $display set above
278
    eval($node->export_display);
279
    unset($node->export_display);
280
  }
281

    
282
  panels_save_display($display);
283
  $node->panels_node['did'] = $display->did;
284

    
285
  db_insert('panels_node')
286
    ->fields(array(
287
      'nid' => $node->nid,
288
      'did' => $display->did,
289
      'css_id' => $node->panels_node['css_id'],
290
      'pipeline' => $node->panels_node['pipeline'],
291
    ))
292
    ->execute();
293
}
294

    
295
/**
296
 * Implementation of hook_delete().
297
 */
298
function panels_node_hook_delete(&$node) {
299
  db_delete('panels_node')->condition('nid', $node->nid)->execute();
300
  if (!empty($node->panels_node['did'])) {
301
    panels_delete_display($node->panels_node['did']);
302
  }
303
}
304

    
305
/**
306
 * Implementation of hook_update().
307
 */
308
function panels_node_hook_update($node) {
309
  db_update('panels_node')
310
    ->condition('nid', $node->nid)
311
    ->fields(array(
312
      'css_id' => $node->panels_node['css_id'],
313
      'pipeline' => $node->panels_node['pipeline'],
314
    ))
315
    ->execute();
316
}
317

    
318
/**
319
 * Implementation of hook_view().
320
 */
321
function panels_node_hook_view($node, $view_mode) {
322
  static $rendering = array();
323

    
324
  // Prevent loops if someone foolishly puts the node inside itself:
325
  if (!empty($rendering[$node->nid])) {
326
    return $node;
327
  }
328

    
329
  $rendering[$node->nid] = TRUE;
330
  ctools_include('plugins', 'panels');
331
  if ($view_mode == 'teaser') {
332
    // Because our teasier is never the same as our content, *always* provide
333
    // the read more flag.
334
    $node->readmore = TRUE;
335
  }
336
  else {
337
    if (!empty($node->panels_node['did'])) {
338
      $display = panels_load_display($node->panels_node['did']);
339
      $display->css_id = $node->panels_node['css_id'];
340
      // TODO: Find a way to make sure this can't node_view.
341
      $display->context = panels_node_get_context($node);
342
      $display->cache_key = 'panels_node:' . $node->nid;
343
      $renderer = panels_get_renderer($node->panels_node['pipeline'], $display);
344
      $node->content['body'] = array(
345
        '#markup' => panels_render_display($display, $renderer),
346
        '#weight' => 0,
347
      );
348
    }
349
  }
350

    
351
  unset($rendering[$node->nid]);
352
  return $node;
353
}
354

    
355
// ---------------------------------------------------------------------------
356
// Administrative pages
357

    
358
/**
359
 * Settings for panel nodes.
360
 */
361
function panels_node_settings() {
362
  ctools_include('common', 'panels');
363
  return drupal_get_form('panels_common_settings', 'panels_node');
364
}
365

    
366
// ---------------------------------------------------------------------------
367
// Meat of the Panels API; almost completely passing through to panels.module
368

    
369
/**
370
 * Pass through to the panels layout editor.
371
 */
372
function panels_node_edit_layout($node) {
373
//  ctools_include('plugins', 'panels');
374
  ctools_include('context');
375
  $display = panels_load_display($node->panels_node['did']);
376
  $display->context = panels_node_get_context($node);
377
  return panels_edit_layout($display, t('Save'), "node/$node->nid/panel_layout", 'panels_node');
378
}
379

    
380
/**
381
 * Pass through to the panels content editor.
382
 */
383
function panels_node_edit_content($node) {
384
  ctools_include('context');
385
  $display = panels_load_display($node->panels_node['did']);
386
  $display->context = panels_node_get_context($node);
387
  ctools_include('common', 'panels');
388
  $content_types = panels_common_get_allowed_types('panels_node', $display->context);
389

    
390
  return panels_edit($display, "node/$node->nid/panel_content", $content_types);
391
}
392

    
393
/**
394
 * Build the context to use for a panel node.
395
 */
396
function panels_node_get_context(&$node) {
397
  ctools_include('context');
398
  $context = ctools_context_create('node', $node);
399
  $context->identifier = t('This node');
400
  $context->keyword = 'node';
401
  return array('panel-node' => $context);
402
}
403

    
404
/**
405
 * Implementation of hook_export_node_alter()
406
 *
407
 * Integrate with export.module for saving panel_nodes into code.
408
 */
409
function panels_node_export_node_alter(&$node, $original_node, $method) {
410
  if ($method == 'export') {
411
    $node_export_omitted = variable_get('node_export_omitted', array());
412
    if (variable_get('node_export_method', '') != 'save-edit' && (array_key_exists('panel', $node_export_omitted) && !$node_export_omitted['panel'])) {
413
      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."));
414
    }
415
    $display = panels_load_display($node->panels_node['did']);
416
    $export = panels_export_display($display);
417
    $node->export_display = $export;
418
  }
419
}
420

    
421
/**
422
 * Implementation of hook_panels_dashboard_blocks().
423
 *
424
 * Adds panel nodes information to the Panels dashboard.
425
 */
426
function panels_node_panels_dashboard_blocks(&$vars) {
427
  $vars['links']['panels_node'] = array(
428
    'title' => l(t('Panel node'), 'node/add/panel'),
429
    'description' => t('Panel nodes are node content and appear in your searches, but are more limited than panel pages.'),
430
    'weight' => -1,
431
  );
432
}
433

    
434
// ---------------------------------------------------------------------------
435
// Callbacks for panel caching.
436

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

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

    
462
  return $cache;
463
}
464

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

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

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