Projet

Général

Profil

Paste
Télécharger (12,7 ko) Statistiques
| Branche: | Révision:

root / htmltest / sites / all / modules / panels / panels_node / panels_node.module @ c12e7e6a

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
      $renderer = panels_get_renderer($node->panels_node['pipeline'], $display);
343
      $node->content['body'] = array(
344
        '#markup' => panels_render_display($display, $renderer),
345
        '#weight' => 0,
346
      );
347
    }
348
  }
349

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

    
354
// ---------------------------------------------------------------------------
355
// Administrative pages
356

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

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

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

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

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

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

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

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