Projet

Général

Profil

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

root / drupal7 / sites / all / modules / panels / panels_mini / panels_mini.module @ 136a805a

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
      if (empty($cids)) {
317
        return array_filter($cache);
318
      }
319
      $output = cache_get_multiple($cids, 'cache_panels');
320
      foreach ($output as $mini) {
321
        if (!empty($mini->data)) {
322
          $mini = $mini->data;
323
          $cache[$mini->name] = $mini;
324
        }
325
      }
326
    }
327

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

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

    
359
  // Strip out NULL entries that may have been added by panels_mini_load().
360
  return array_filter($cache);
361
}
362

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

    
373
  // Clear the panels_mini_load cache.
374
  cache_clear_all('panels_mini_load:', 'cache_panels', TRUE);
375

    
376
  $update = (isset($mini->pid) && $mini->pid != 'new') ? array('pid') : array();
377
  drupal_write_record('panels_mini', $mini, $update);
378

    
379
  return $mini;
380
}
381

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

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

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

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

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

    
433
/**
434
 * Get the display cache for the panels_mini plugin.
435
 */
436
function _panels_mini_panels_cache_get($key) {
437
  ctools_include('export-ui');
438
  $plugin = ctools_get_export_ui('panels_mini');
439
  $handler = ctools_export_ui_get_handler($plugin);
440
  if (!$handler) {
441
    return;
442
  }
443

    
444
  $item = $handler->edit_cache_get($key);
445
  if (!$item) {
446
    $item = ctools_export_crud_load($handler->plugin['schema'], $key);
447
  }
448

    
449
  return array($handler, $item);
450
}
451

    
452
/**
453
 * Get display edit cache for the panels mini export UI
454
 *
455
 * The key is the second half of the key in this form:
456
 * panels_mini:TASK_NAME:HANDLER_NAME;
457
 */
458
function panels_mini_panels_cache_get($key) {
459
  ctools_include('common', 'panels');
460
  list($handler, $item) = _panels_mini_panels_cache_get($key);
461
  if (isset($item->mini_panels_display_cache)) {
462
    return $item->mini_panels_display_cache;
463
  }
464

    
465
  $cache = new stdClass();
466
  $cache->display = $item->display;
467
  $cache->display->context = ctools_context_load_contexts($item);
468
  $cache->display->cache_key = 'panels_mini:' . $key;
469
  $cache->display->storage_type = 'panels_mini';
470
  // Temporary storage id that's replaced in panels_mini_save().
471
  $cache->display->storage_id = 'panels_mini';
472
  $cache->content_types = panels_common_get_allowed_types('panels_mini', $cache->display->context);
473
  $cache->display_title = TRUE;
474

    
475
  // @TODO support locking
476
  $cache->locked = FALSE;
477

    
478
  return $cache;
479
}
480

    
481
/**
482
 * Store a display edit in progress in the page cache.
483
 */
484
function panels_mini_panels_cache_set($key, $cache) {
485
  list($handler, $item) = _panels_mini_panels_cache_get($key);
486
  $item->mini_panels_display_cache = $cache;
487
  $handler->edit_cache_set_key($item, $key);
488
}
489

    
490
/**
491
 * Save all changes made to a display using the panels mini UI cache.
492
 */
493
function panels_mini_panels_cache_clear($key, $cache) {
494
  list($handler, $item) = _panels_mini_panels_cache_get($key);
495
  $handler->edit_cache_clear($item);
496
}
497

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

    
506
  $handler->edit_cache_clear($item);
507
}
508

    
509
/**
510
 * Break the lock on a panels mini page.
511
 */
512
function panels_mini_panels_cache_break_lock($key, $cache) {
513
}
514

    
515
/**
516
 * Implements hook_panels_pre_render().
517
 */
518
function panels_mini_panels_pre_render($display, $renderer) {
519
  if (isset($display->owner->table) && $display->owner->table == 'panels_mini' && $renderer instanceof panels_renderer_standard) {
520
    $renderer->show_empty_layout = FALSE;
521
  }
522
}
523

    
524
/**
525
 * Implementation of hook_panels_dashboard_blocks().
526
 *
527
 * Adds mini panels information to the Panels dashboard.
528
 */
529
function panels_mini_panels_dashboard_blocks(&$vars) {
530
  $vars['links']['panels_mini'] = array(
531
    'title' => l(t('Mini panel'), 'admin/structure/mini-panels/add'),
532
    'description' => t('Mini panels are small content areas exposed as blocks, for when you need to have complex block layouts or layouts within layouts.'),
533
    'weight' => -1,
534
  );
535

    
536
  // Load all mini panels and their displays.
537
  $panel_minis = panels_mini_load_all();
538
  $count = 0;
539
  $rows = array();
540

    
541
  foreach ($panel_minis as $panel_mini) {
542
    $rows[] = array(
543
      check_plain($panel_mini->admin_title),
544
      array(
545
        'data' => l(t('Edit'), "admin/structure/mini-panels/list/$panel_mini->name/edit"),
546
        'class' => 'links',
547
      ),
548
    );
549

    
550
    // Only show 10.
551
    if (++$count >= 10) {
552
      break;
553
    }
554
  }
555

    
556
  if ($rows) {
557
    $content = theme('table', array('rows' => $rows, 'attributes' => array('class' => 'panels-manage')));
558
  }
559
  else {
560
    $content = '<p>' . t('There are no mini panels.') . '</p>';
561
  }
562

    
563
  $vars['blocks']['panels_mini'] = array(
564
    'weight' => -100,
565
    'title' => t('Manage mini panels'),
566
    'link' => l(t('Go to list'), 'admin/structure/mini-panels'),
567
    'content' => $content,
568
    'class' => 'dashboard-mini-panels',
569
    'section' => 'left',
570
  );
571

    
572
}
573

    
574
/**
575
 * Implements template_preprocess_ctools_wizard_trail().
576
 *
577
 * Customize the divider used in the CTools wizard to build the edit pages for
578
 * Mini Panels as a stop-gap measure until the UX can be completely re-done.
579
 */
580
function panels_mini_preprocess_ctools_wizard_trail(&$variables) {
581
  $variables['divider'] = ' | ';
582
  drupal_add_css(drupal_get_path('module', 'panels_mini') . '/panels_mini.css');
583
}