Project

General

Profile

Paste
Download (17 KB) Statistics
| Branch: | Revision:

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

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
  $current_page = FALSE;
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
 * Create a new page with defaults appropriately set from schema.
246
 */
247
function panels_mini_new($set_defaults = TRUE) {
248
  ctools_include('export');
249
  return ctools_export_new_object('panels_mini', $set_defaults);
250
}
251

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

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

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

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

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

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

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

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

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

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

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

    
375
  return $mini;
376
}
377

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

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

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

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

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

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

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

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

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

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

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

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

    
489
  return $cache;
490
}
491

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

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

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

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

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

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

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

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

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

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

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

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

    
583
}
584

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