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 @ 64156087

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

    
72
  $blocks = array();
73

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

    
84
  return $blocks;
85
}
86

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

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

    
106
  ctools_include('context');
107

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

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

    
120
  $block = array();
121

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

    
238
  return $id;
239
}
240

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

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

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

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

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

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

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

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

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

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

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

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

    
374
  return $mini;
375
}
376

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

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

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

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

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

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

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

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

    
459
  return array($handler, $item);
460
}
461

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

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

    
485
  // @TODO support locking
486
  $cache->locked = FALSE;
487

    
488
  return $cache;
489
}
490

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

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

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

    
516
  $handler->edit_cache_clear($item);
517
}
518

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

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

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

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

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

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

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

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

    
582
}
583

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