Projet

Général

Profil

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

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

1
<?php
2

    
3
/**
4
 * @file panels_mini.module
5
 *
6
 * This module provides mini panels which are basically panels that can be
7
 * used within blocks or other panels.
8
 */
9

    
10
/**
11
 * Implementation of hook_permission().
12
 */
13
function panels_mini_permission() {
14
  return array(
15
    'create mini panels' => array(
16
      'title' => t('Create mini panels'),
17
      'description' => t('Create new mini panels'),
18
    ),
19
    'administer mini panels' => array(
20
      'title' => t('Administer mini panels'),
21
      'description' => t('Edit and delete mini panels'),
22
    ),
23
  );
24
}
25

    
26
/**
27
 * Implementation of hook_menu().
28
 */
29
function panels_mini_menu() {
30
  // Safety: go away if CTools is not at an appropriate version.
31
  if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
32
    return array();
33
  }
34

    
35
  $items['admin/structure/mini-panels/settings'] = array(
36
    'title' => 'Settings',
37
    'page callback' => 'panels_mini_settings',
38
    'access arguments' => array('create mini panels'),
39
    'type' => MENU_LOCAL_TASK,
40
  );
41

    
42
  // Also provide settings on the main panel UI
43
  $items['admin/structure/panels/settings/panels-mini'] = array(
44
    'title' => 'Mini panels',
45
    'page callback' => 'panels_mini_settings',
46
    'access arguments' => array('create mini panels'),
47
    'type' => MENU_LOCAL_TASK,
48
  );
49

    
50
  return $items;
51
}
52

    
53
/**
54
 * Settings for mini panels.
55
 */
56
function panels_mini_settings() {
57
  ctools_include('common', 'panels');
58
  return drupal_get_form('panels_common_settings', 'panels_mini');
59
}
60

    
61
// ---------------------------------------------------------------------------
62
// Allow the rest of the system access to mini panels
63

    
64
/**
65
 * Implementation of hook_block_info().
66
 */
67
function panels_mini_block_info() {
68
  // Safety: go away if CTools is not at an appropriate version.
69
  if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
70
    return array();
71
  }
72

    
73
  $blocks = array();
74

    
75
  $minis = panels_mini_load_all();
76
  foreach ($minis as $panel_mini) {
77
    if (empty($panel_mini->disabled) && (module_exists('page_manager') || empty($panel_mini->requiredcontexts))) {
78
      $blocks[$panel_mini->name] = array(
79
        'info' => t('Mini panel: "@title"', array('@title' => $panel_mini->admin_title)),
80
        'cache' => DRUPAL_NO_CACHE,
81
      );
82
    }
83
  }
84

    
85
  return $blocks;
86
}
87

    
88
/**
89
 * Implementation of hook_block_view().
90
 *
91
 * @see panels_mini_panels_mini_content_type_render().
92
 */
93
function panels_mini_block_view($delta = 0) {
94
  // static recursion protection.
95
  static $viewing = array();
96
  if (!empty($viewing[$delta])) {
97
    return;
98
  }
99
  $viewing[$delta] = TRUE;
100

    
101
  $panel_mini = panels_mini_load($delta);
102
  if (empty($panel_mini)) {
103
    // Bail out early if the specified mini panel doesn't exist.
104
    return;
105
  }
106

    
107
  ctools_include('context');
108

    
109
  $contexts = array();
110
  if (module_exists('page_manager') && $current_page = page_manager_get_current_page()) {
111
    if (!empty($current_page['contexts'])) {
112
      $contexts = ctools_context_match_required_contexts($panel_mini->requiredcontexts, $current_page['contexts']);
113
    }
114
  }
115
  drupal_alter('panels_mini_block_contexts', $contexts, $panel_mini);
116

    
117
  $panel_mini->context = $panel_mini->display->context =  ctools_context_load_contexts($panel_mini, FALSE, $contexts);
118
  $panel_mini->display->css_id = panels_mini_get_id($panel_mini->name);
119
  $panel_mini->display->owner = $panel_mini;
120

    
121
  $block = array();
122

    
123
  $block['content'] = panels_render_display($panel_mini->display);
124
  $block['subject'] = $panel_mini->display->get_title();
125

    
126
  unset($viewing[$delta]);
127
  return $block;
128
}
129

    
130
/**
131
 * Implementation of hook_block_configure().
132
 */
133
function panels_mini_block_configure($delta = 0) {
134
  return array(
135
    'admin_shortcut' => array(
136
      '#markup' => l(t('Manage this mini-panel'), 'admin/structure/mini-panels/list/' . $delta . '/edit')
137
    ),
138
  );
139
}
140

    
141
/**
142
 * Implements hook_block_list_alter().
143
 *
144
 * Remove the block if the required contexts are not available.
145
 */
146
function panels_mini_block_list_alter(&$blocks) {
147
  if (module_exists('page_manager')) {
148
    $current_page = page_manager_get_current_page();
149
  }
150

    
151
  // Load add at once to save time.
152
  panels_mini_load_all();
153

    
154
  foreach ($blocks as $key => $block) {
155
    if ($block->module != 'panels_mini') {
156
      // This block was added by a contrib module, leave it in the list.
157
      continue;
158
    }
159

    
160
    $panel_mini = panels_mini_load($block->delta);
161
    if (empty($panel_mini)) {
162
      // Bail out early if the specified mini panel doesn't exist.
163
      unset($blocks[$key]);
164
      continue;
165
    }
166

    
167
    if (!empty($panel_mini->requiredcontexts)) {
168
      if (!$current_page || empty($current_page['contexts'])) {
169
        unset($blocks[$key]);
170
        continue;
171
      }
172
      else {
173
        $required = array();
174
        foreach ($panel_mini->requiredcontexts as $context) {
175
          $info = ctools_get_context($context['name']);
176
          $required[] = new ctools_context_required($context['identifier'], $info['context name']);
177
        }
178
        if (!ctools_context_match_requirements($current_page['contexts'], $required)) {
179
          unset($blocks[$key]);
180
          continue;
181
        }
182
      }
183
    }
184
  }
185
}
186

    
187
/**
188
 * Implements hook_get_pane_links_alter().
189
 */
190
function panels_mini_get_pane_links_alter(&$links, $pane, $content_type) {
191
  if ($pane->type == 'panels_mini') {
192
    $links['top']['edit_panels_mini'] = array(
193
      'title' => t('Edit mini panel'),
194
      'href' => url('admin/structure/mini-panels/list/' . $pane->subtype . '/edit/content', array('absolute' => TRUE)),
195
      'attributes' => array('target' => array('_blank')),
196
    );
197
  }
198
}
199

    
200
/**
201
 * Implements hook_contextual_links_view_alter().
202
 */
203
function panels_mini_contextual_links_view_alter(&$element, $items) {
204

    
205
  // Add contextual links to all mini panel blocks with bid property.
206
  if (isset($element['#element']['#block']) && isset($element['#element']['#block']->bid) && strpos((string) $element['#element']['#block']->bid, 'panels_mini') === 0) {
207

    
208
    $admin_pages = array(
209
      t('Configure mini panel settings') => 'basic',
210
      t('Configure mini panel context') => 'context',
211
      t('Configure mini panel layout') => 'layout',
212
      t('Configure mini panel content') => 'content',
213
    );
214

    
215
    foreach ($admin_pages as $title => $tail) {
216
      $element['#links']['mini-panels-' . $tail] = array(
217
        'title' => $title,
218
        'href' => 'admin/structure/mini-panels/list/' . $element['#element']['#block']->delta . '/edit/' . $tail,
219
        'query' => drupal_get_destination(),
220
      );
221
    }
222
  }
223
}
224

    
225
/**
226
 * Statically store all used IDs to ensure all mini panels get a unique id.
227
 */
228
function panels_mini_get_id($name) {
229
  $id_cache = &drupal_static(__FUNCTION__, array());
230

    
231
  $id = 'mini-panel-' . $name;
232
  if (!empty($id_cache[$name])) {
233
    $id .= "-" . $id_cache[$name]++;
234
  }
235
  else {
236
    $id_cache[$name] = 1;
237
  }
238

    
239
  return $id;
240
}
241

    
242
// ---------------------------------------------------------------------------
243
// Database functions.
244

    
245
/**
246
 * Create a new page with defaults appropriately set from schema.
247
 */
248
function panels_mini_new($set_defaults = TRUE) {
249
  ctools_include('export');
250
  return ctools_export_new_object('panels_mini', $set_defaults);
251
}
252

    
253
/**
254
 * Load a single mini panel.
255
 */
256
function panels_mini_load($name) {
257
  $cache = &drupal_static('panels_mini_load_all', array());
258

    
259
  // We use array_key_exists because failed loads will be NULL and
260
  // isset() will try to load it again.
261
  if (!array_key_exists($name, $cache)) {
262
    $cid = 'panels_mini_load:' . $name;
263
    $result = cache_get($cid, 'cache_panels');
264
    if (!empty($result->data)) {
265
      $cache[$name] = $result->data;
266
    }
267
    else {
268
      ctools_include('export');
269
      $result = ctools_export_load_object('panels_mini', 'names', array($name));
270
      if (isset($result[$name])) {
271
        if (empty($result[$name]->display)) {
272
          $result[$name]->display = panels_load_display($result[$name]->did);
273
          if (!empty($result[$name]->title) && empty($result[$name]->display->title)) {
274
            $result[$name]->display->title = $result[$name]->title;
275
          }
276
        }
277
        $cache[$name] = $result[$name];
278
        if (!empty($result[$name]->title) && empty($result[$name]->admin_title)) {
279
          $cache[$name]->admin_title = $result[$name]->title;
280
        }
281
        cache_set($cid, $cache[$name], 'cache_panels', CACHE_TEMPORARY);
282
      }
283
      else {
284
        $cache[$name] = NULL;
285
      }
286
    }
287
  }
288

    
289
  if (isset($cache[$name])) {
290
    return $cache[$name];
291
  }
292
}
293

    
294
/**
295
 * Load all mini panels.
296
 */
297
function panels_mini_load_all($reset = FALSE) {
298
  $cache = &drupal_static('panels_mini_load_all', array());
299
  static $all_loaded = FALSE;
300

    
301
  // We check our own private static because individual minis could have
302
  // been loaded prior to load all and we need to know that.
303
  if (!$all_loaded || $reset) {
304
    $all_loaded = TRUE;
305
    if ($reset) {
306
      $cache = array();
307
    }
308
    else {
309
      $panel_names = db_select('panels_mini', 'pm')
310
        ->fields('pm', array('name'))
311
        ->execute();
312
      $cids = array();
313
      foreach ($panel_names as $name) {
314
        $cids[] = 'panels_mini_load:' . $name->name;
315
      }
316
      $output = cache_get_multiple($cids, 'cache_panels');
317
      foreach ($output as $mini) {
318
        if (!empty($mini->data)) {
319
          $mini = $mini->data;
320
          $cache[$mini->name] = $mini;
321
        }
322
      }
323
    }
324

    
325
    ctools_include('export');
326
    $minis = ctools_export_load_object('panels_mini');
327
    $dids = array();
328
    foreach ($minis as $mini) {
329
      if (empty($cache[$mini->name])) {
330
        if (!empty($mini->did)) {
331
          $dids[$mini->did] = $mini->name;
332
        }
333
        else {
334
        // Translate old style titles into new titles.
335
          if (!empty($mini->title) && empty($mini->display->title)) {
336
            $mini->display->title = $mini->title;
337
          }
338
        }
339
        // Translate old style titles into new titles.
340
        if (isset($mini->title) && empty($mini->admin_title)) {
341
          $mini->admin_title = $mini->title;
342
        }
343
        $cache[$mini->name] = $mini;
344
      }
345
    }
346

    
347
    $displays = panels_load_displays(array_keys($dids));
348
    foreach ($displays as $did => $display) {
349
      if (!empty($cache[$dids[$did]]->title) && empty($display->title)) {
350
        $display->title = $cache[$dids[$did]]->title;
351
      }
352
      $cache[$dids[$did]]->display = $display;
353
    }
354
  }
355

    
356
  // Strip out NULL entries that may have been added by panels_mini_load().
357
  return array_filter($cache);
358
}
359

    
360
/**
361
 * Write a mini panel to the database.
362
 */
363
function panels_mini_save(&$mini) {
364
  if (!empty($mini->display)) {
365
    $mini->display->storage_id = $mini->name;
366
    $display = panels_save_display($mini->display);
367
    $mini->did = $display->did;
368
  }
369

    
370
  // Clear the panels_mini_load cache.
371
  cache_clear_all('panels_mini_load:', 'cache_panels', TRUE);
372

    
373
  $update = (isset($mini->pid) && $mini->pid != 'new') ? array('pid') : array();
374
  drupal_write_record('panels_mini', $mini, $update);
375

    
376
  return $mini;
377
}
378

    
379
/**
380
 * Remove a mini panel.
381
 */
382
function panels_mini_delete($mini) {
383
  db_delete('panels_mini')
384
    ->condition('name', $mini->name)
385
    ->execute();
386

    
387
  if (db_table_exists('block') && $mini->type != t('Overridden')) {
388
    // Also remove from block table as long as there isn't a default that may appear.
389
    db_delete('block')
390
      ->condition('delta', $mini->name)
391
      ->condition('module', 'panels_mini')
392
      ->execute();
393
  }
394
  return panels_delete_display($mini->did);
395
}
396

    
397
/**
398
 * Export a mini panel.
399
 */
400
function panels_mini_export($mini, $indent = '') {
401
  ctools_include('export');
402
  $output = ctools_export_object('panels_mini', $mini, $indent);
403
  // Export the primary display
404
  $display = !empty($mini->display) ? $mini->display : panels_load_display($mini->did);
405
  $output .= panels_export_display($display, $indent);
406
  $output .= $indent . '$mini->display = $display' . ";\n";
407
  return $output;
408
}
409

    
410
/**
411
 * Remove the block version of mini panels from being available content types.
412
 */
413
function panels_mini_ctools_block_info($module, $delta, &$info) {
414
  $info = NULL;
415
}
416

    
417
/**
418
 * Implementation of hook_ctools_plugin_directory() to let the system know
419
 * we implement task and task_handler plugins.
420
 */
421
function panels_mini_ctools_plugin_directory($module, $plugin) {
422
  if ($module == 'ctools' && ($plugin == 'content_types' || $plugin == 'export_ui')) {
423
    return 'plugins/' . $plugin;
424
  }
425
  if ($module == 'panels' && $plugin == 'panels_storage') {
426
    return 'plugins/' . $plugin;
427
  }
428
}
429

    
430
/**
431
 * Implements hook_default_panels_mini_alter().
432
 *
433
 * If a default Panels display has no storage type, set it.
434
 */
435
function panels_default_panels_mini_alter(&$mini_panels) {
436
  foreach ($mini_panels as &$mini_panel) {
437
    $display =& $mini_panel->display;
438
    if (empty($display->storage_type)) {
439
      $display->storage_type = 'panels_mini';
440
      $display->storage_id = $mini_panel->name;
441
    }
442
  }
443
}
444

    
445
/**
446
 * Get the display cache for the panels_mini plugin.
447
 */
448
function _panels_mini_panels_cache_get($key) {
449
  ctools_include('export-ui');
450
  $plugin = ctools_get_export_ui('panels_mini');
451
  $handler = ctools_export_ui_get_handler($plugin);
452
  if (!$handler) {
453
    return;
454
  }
455

    
456
  $item = $handler->edit_cache_get($key);
457
  if (!$item) {
458
    $item = ctools_export_crud_load($handler->plugin['schema'], $key);
459
  }
460

    
461
  return array($handler, $item);
462
}
463

    
464
/**
465
 * Get display edit cache for the panels mini export UI
466
 *
467
 * The key is the second half of the key in this form:
468
 * panels_mini:TASK_NAME:HANDLER_NAME;
469
 */
470
function panels_mini_panels_cache_get($key) {
471
  ctools_include('common', 'panels');
472
  list($handler, $item) = _panels_mini_panels_cache_get($key);
473
  if (isset($item->mini_panels_display_cache)) {
474
    return $item->mini_panels_display_cache;
475
  }
476

    
477
  $cache = new stdClass();
478
  $cache->display = $item->display;
479
  $cache->display->context = ctools_context_load_contexts($item);
480
  $cache->display->cache_key = 'panels_mini:' . $key;
481
  $cache->display->storage_type = 'panels_mini';
482
  // Temporary storage id that's replaced in panels_mini_save().
483
  $cache->display->storage_id = 'panels_mini';
484
  $cache->content_types = panels_common_get_allowed_types('panels_mini', $cache->display->context);
485
  $cache->display_title = TRUE;
486

    
487
  // @TODO support locking
488
  $cache->locked = FALSE;
489

    
490
  return $cache;
491
}
492

    
493
/**
494
 * Store a display edit in progress in the page cache.
495
 */
496
function panels_mini_panels_cache_set($key, $cache) {
497
  list($handler, $item) = _panels_mini_panels_cache_get($key);
498
  $item->mini_panels_display_cache = $cache;
499
  $handler->edit_cache_set_key($item, $key);
500
}
501

    
502
/**
503
 * Save all changes made to a display using the panels mini UI cache.
504
 */
505
function panels_mini_panels_cache_clear($key, $cache) {
506
  list($handler, $item) = _panels_mini_panels_cache_get($key);
507
  $handler->edit_cache_clear($item);
508
}
509

    
510
/**
511
 * Save all changes made to a display using the panels mini UI cache.
512
 */
513
function panels_mini_panels_cache_save($key, $cache) {
514
  list($handler, $item) = _panels_mini_panels_cache_get($key);
515
  $item->display = $cache->display;
516
  panels_mini_save($item);
517

    
518
  $handler->edit_cache_clear($item);
519
}
520

    
521
/**
522
 * Break the lock on a panels mini page.
523
 */
524
function panels_mini_panels_cache_break_lock($key, $cache) {
525
}
526

    
527
/**
528
 * Implements hook_panels_pre_render().
529
 */
530
function panels_mini_panels_pre_render($display, $renderer) {
531
  if (isset($display->owner->table) && $display->owner->table == 'panels_mini' && $renderer instanceof panels_renderer_standard) {
532
    $renderer->show_empty_layout = FALSE;
533
  }
534
}
535

    
536
/**
537
 * Implementation of hook_panels_dashboard_blocks().
538
 *
539
 * Adds mini panels information to the Panels dashboard.
540
 */
541
function panels_mini_panels_dashboard_blocks(&$vars) {
542
  $vars['links']['panels_mini'] = array(
543
    'title' => l(t('Mini panel'), 'admin/structure/mini-panels/add'),
544
    'description' => t('Mini panels are small content areas exposed as blocks, for when you need to have complex block layouts or layouts within layouts.'),
545
    'weight' => -1,
546
  );
547

    
548
  // Load all mini panels and their displays.
549
  $panel_minis = panels_mini_load_all();
550
  $count = 0;
551
  $rows = array();
552

    
553
  foreach ($panel_minis as $panel_mini) {
554
    $rows[] = array(
555
      check_plain($panel_mini->admin_title),
556
      array(
557
        'data' => l(t('Edit'), "admin/structure/mini-panels/list/$panel_mini->name/edit"),
558
        'class' => 'links',
559
      ),
560
    );
561

    
562
    // Only show 10.
563
    if (++$count >= 10) {
564
      break;
565
    }
566
  }
567

    
568
  if ($rows) {
569
    $content = theme('table', array('rows' => $rows, 'attributes' => array('class' => 'panels-manage')));
570
  }
571
  else {
572
    $content = '<p>' . t('There are no mini panels.') . '</p>';
573
  }
574

    
575
  $vars['blocks']['panels_mini'] = array(
576
    'weight' => -100,
577
    'title' => t('Manage mini panels'),
578
    'link' => l(t('Go to list'), 'admin/structure/mini-panels'),
579
    'content' => $content,
580
    'class' => 'dashboard-mini-panels',
581
    'section' => 'left',
582
  );
583

    
584
}
585

    
586
/**
587
 * Implements template_preprocess_ctools_wizard_trail().
588
 *
589
 * Customize the divider used in the CTools wizard to build the edit pages for
590
 * Mini Panels as a stop-gap measure until the UX can be completely re-done.
591
 */
592
function panels_mini_preprocess_ctools_wizard_trail(&$variables) {
593
  $variables['divider'] = ' | ';
594
  drupal_add_css(drupal_get_path('module', 'panels_mini') . '/panels_mini.css');
595
}