Projet

Général

Profil

Paste
Télécharger (14,5 ko) Statistiques
| Branche: | Révision:

root / drupal7 / sites / all / modules / panels / panels_mini / panels_mini.module @ 5a7e6170

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

    
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

    
119
  $block = array();
120

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

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

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

    
139
/**
140
 * Implements hook_block_list_alter().
141
 *
142
 * Remove the block if the required contexts are not available.
143
 */
144
function panels_mini_block_list_alter(&$blocks) {
145
  if (module_exists('page_manager')) {
146
    $current_page = page_manager_get_current_page();
147
  }
148
  foreach ($blocks as $key => $block) {
149
    if ($block->module != 'panels_mini') {
150
      // This block was added by a contrib module, leave it in the list.
151
      continue;
152
    }
153

    
154
    $panel_mini = panels_mini_load($block->delta);
155
    if (empty($panel_mini)) {
156
      // Bail out early if the specified mini panel doesn't exist.
157
      unset($blocks[$key]);
158
      continue;
159
    }
160

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

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

    
194
/**
195
 * Implements hook_contextual_links_view_alter().
196
 */
197
function panels_mini_contextual_links_view_alter(&$element, $items) {
198

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

    
202
    $admin_pages = array(
203
      t('Configure mini panel settings') => 'basic',
204
      t('Configure mini panel context') => 'context',
205
      t('Configure mini panel layout') => 'layout',
206
      t('Configure mini panel content') => 'content',
207
    );
208

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

    
219
/**
220
 * Statically store all used IDs to ensure all mini panels get a unique id.
221
 */
222
function panels_mini_get_id($name) {
223
  $id_cache = &drupal_static(__FUNCTION__, array());
224

    
225
  $id = 'mini-panel-' . $name;
226
  if (!empty($id_cache[$name])) {
227
    $id .= "-" . $id_cache[$name]++;
228
  }
229
  else {
230
    $id_cache[$name] = 1;
231
  }
232

    
233
  return $id;
234
}
235

    
236
// ---------------------------------------------------------------------------
237
// Database functions.
238

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

    
247
/**
248
 * Load a single mini panel.
249
 */
250
function panels_mini_load($name) {
251
  $cache = &drupal_static('panels_mini_load_all', array());
252

    
253
  // We use array_key_exists because failed loads will be NULL and
254
  // isset() will try to load it again.
255
  if (!array_key_exists($name, $cache)) {
256
    ctools_include('export');
257
    $result = ctools_export_load_object('panels_mini', 'names', array($name));
258
    if (isset($result[$name])) {
259
      if (empty($result[$name]->display)) {
260
        $result[$name]->display = panels_load_display($result[$name]->did);
261
        if (!empty($result[$name]->title) && empty($result[$name]->display->title)) {
262
          $result[$name]->display->title = $result[$name]->title;
263
        }
264
      }
265
      $cache[$name] = $result[$name];
266
      if (!empty($result[$name]->title) && empty($result[$name]->admin_title)) {
267
        $cache[$name]->admin_title = $result[$name]->title;
268
      }
269
    }
270
    else {
271
      $cache[$name] = NULL;
272
    }
273
  }
274

    
275
  if (isset($cache[$name])) {
276
    return $cache[$name];
277
  }
278
}
279

    
280
/**
281
 * Load all mini panels.
282
 */
283
function panels_mini_load_all($reset = FALSE) {
284
  $cache = &drupal_static('panels_mini_load_all', array());
285
  static $all_loaded = FALSE;
286

    
287
  // We check our own private static because individual minis could have
288
  // been loaded prior to load all and we need to know that.
289
  if (!$all_loaded || $reset) {
290
    $all_loaded = TRUE;
291
    if ($reset) {
292
      $cache = array();
293
    }
294

    
295
    ctools_include('export');
296
    $minis = ctools_export_load_object('panels_mini');
297
    $dids = array();
298
    foreach ($minis as $mini) {
299
      if (empty($cache[$mini->name])) {
300
        if (!empty($mini->did)) {
301
          $dids[$mini->did] = $mini->name;
302
        }
303
        else {
304
        // Translate old style titles into new titles.
305
          if (!empty($mini->title) && empty($mini->display->title)) {
306
            $mini->display->title = $mini->title;
307
          }
308
        }
309
        // Translate old style titles into new titles.
310
        if (isset($mini->title) && empty($mini->admin_title)) {
311
          $mini->admin_title = $mini->title;
312
        }
313
        $cache[$mini->name] = $mini;
314
      }
315
    }
316

    
317
    $displays = panels_load_displays(array_keys($dids));
318
    foreach ($displays as $did => $display) {
319
      if (!empty($cache[$dids[$did]]->title) && empty($display->title)) {
320
        $display->title = $cache[$dids[$did]]->title;
321
      }
322
      $cache[$dids[$did]]->display = $display;
323
    }
324
  }
325

    
326
  // Strip out NULL entries that may have been added by panels_mini_load().
327
  return array_filter($cache);
328
}
329

    
330
/**
331
 * Write a mini panel to the database.
332
 */
333
function panels_mini_save(&$mini) {
334
  if (!empty($mini->display)) {
335
    $display = panels_save_display($mini->display);
336
    $mini->did = $display->did;
337
  }
338

    
339
  $update = (isset($mini->pid) && $mini->pid != 'new') ? array('pid') : array();
340
  drupal_write_record('panels_mini', $mini, $update);
341

    
342
  return $mini;
343
}
344

    
345
/**
346
 * Remove a mini panel.
347
 */
348
function panels_mini_delete($mini) {
349
  db_delete('panels_mini')
350
    ->condition('name', $mini->name)
351
    ->execute();
352

    
353
  if (db_table_exists('block') && $mini->type != t('Overridden')) {
354
    // Also remove from block table as long as there isn't a default that may appear.
355
    db_delete('block')
356
      ->condition('delta', $mini->name)
357
      ->condition('module', 'panels_mini')
358
      ->execute();
359
  }
360
  return panels_delete_display($mini->did);
361
}
362

    
363
/**
364
 * Export a mini panel.
365
 */
366
function panels_mini_export($mini, $indent = '') {
367
  ctools_include('export');
368
  $output = ctools_export_object('panels_mini', $mini, $indent);
369
  // Export the primary display
370
  $display = !empty($mini->display) ? $mini->display : panels_load_display($mini->did);
371
  $output .= panels_export_display($display, $indent);
372
  $output .= $indent . '$mini->display = $display' . ";\n";
373
  return $output;
374
}
375

    
376
/**
377
 * Remove the block version of mini panels from being available content types.
378
 */
379
function panels_mini_ctools_block_info($module, $delta, &$info) {
380
  $info = NULL;
381
}
382

    
383
/**
384
 * Implementation of hook_ctools_plugin_directory() to let the system know
385
 * we implement task and task_handler plugins.
386
 */
387
function panels_mini_ctools_plugin_directory($module, $plugin) {
388
  if ($module == 'ctools' && ($plugin == 'content_types' || $plugin == 'export_ui')) {
389
    return 'plugins/' . $plugin;
390
  }
391
}
392

    
393
/**
394
 * Get the display cache for the panels_mini plugin.
395
 */
396
function _panels_mini_panels_cache_get($key) {
397
  ctools_include('export-ui');
398
  $plugin = ctools_get_export_ui('panels_mini');
399
  $handler = ctools_export_ui_get_handler($plugin);
400
  if (!$handler) {
401
    return;
402
  }
403

    
404
  $item = $handler->edit_cache_get($key);
405
  if (!$item) {
406
    $item = ctools_export_crud_load($handler->plugin['schema'], $key);
407
  }
408

    
409
  return array($handler, $item);
410
}
411

    
412
/**
413
 * Get display edit cache for the panels mini export UI
414
 *
415
 * The key is the second half of the key in this form:
416
 * panels_mini:TASK_NAME:HANDLER_NAME;
417
 */
418
function panels_mini_panels_cache_get($key) {
419
  ctools_include('common', 'panels');
420
  list($handler, $item) = _panels_mini_panels_cache_get($key);
421
  if (isset($item->mini_panels_display_cache)) {
422
    return $item->mini_panels_display_cache;
423
  }
424

    
425
  $cache = new stdClass();
426
  $cache->display = $item->display;
427
  $cache->display->context = ctools_context_load_contexts($item);
428
  $cache->display->cache_key = 'panels_mini:' . $key;
429
  $cache->content_types = panels_common_get_allowed_types('panels_mini', $cache->display->context);
430
  $cache->display_title = TRUE;
431

    
432
  // @TODO support locking
433
  $cache->locked = FALSE;
434

    
435
  return $cache;
436
}
437

    
438
/**
439
 * Store a display edit in progress in the page cache.
440
 */
441
function panels_mini_panels_cache_set($key, $cache) {
442
  list($handler, $item) = _panels_mini_panels_cache_get($key);
443
  $item->mini_panels_display_cache = $cache;
444
  $handler->edit_cache_set_key($item, $key);
445
}
446

    
447
/**
448
 * Save all changes made to a display using the panels mini UI cache.
449
 */
450
function panels_mini_panels_cache_clear($key, $cache) {
451
  list($handler, $item) = _panels_mini_panels_cache_get($key);
452
  $handler->edit_cache_clear($item);
453
}
454

    
455
/**
456
 * Save all changes made to a display using the panels mini UI cache.
457
 */
458
function panels_mini_panels_cache_save($key, $cache) {
459
  list($handler, $item) = _panels_mini_panels_cache_get($key);
460
  $item->display = $cache->display;
461
  panels_mini_save($item);
462

    
463
  $handler->edit_cache_clear($item);
464
}
465

    
466
/**
467
 * Break the lock on a panels mini page.
468
 */
469
function panels_mini_panels_cache_break_lock($key, $cache) {
470
}
471

    
472
/**
473
 * Implementation of hook_panels_dashboard_blocks().
474
 *
475
 * Adds mini panels information to the Panels dashboard.
476
 */
477
function panels_mini_panels_dashboard_blocks(&$vars) {
478
  $vars['links']['panels_mini'] = array(
479
    'title' => l(t('Mini panel'), 'admin/structure/mini-panels/add'),
480
    'description' => t('Mini panels are small content areas exposed as blocks, for when you need to have complex block layouts or layouts within layouts.'),
481
    'weight' => -1,
482
  );
483

    
484
  // Load all mini panels and their displays.
485
  $panel_minis = panels_mini_load_all();
486
  $count = 0;
487
  $rows = array();
488

    
489
  foreach ($panel_minis as $panel_mini) {
490
    $rows[] = array(
491
      check_plain($panel_mini->admin_title),
492
      array(
493
        'data' => l(t('Edit'), "admin/structure/mini-panels/list/$panel_mini->name/edit"),
494
        'class' => 'links',
495
      ),
496
    );
497

    
498
    // Only show 10.
499
    if (++$count >= 10) {
500
      break;
501
    }
502
  }
503

    
504
  if ($rows) {
505
    $content = theme('table', array('rows' => $rows, 'attributes' => array('class' => 'panels-manage')));
506
  }
507
  else {
508
    $content = '<p>' . t('There are no mini panels.') . '</p>';
509
  }
510

    
511
  $vars['blocks']['panels_mini'] = array(
512
    'weight' => -100,
513
    'title' => t('Manage mini panels'),
514
    'link' => l(t('Go to list'), 'admin/structure/mini-panels'),
515
    'content' => $content,
516
    'class' => 'dashboard-mini-panels',
517
    'section' => 'left',
518
  );
519

    
520
}