[ Index ]

PHP Cross Reference of Drupal 6 (gatewave)

title

Body

[close]

/sites/all/modules/panels/panels_mini/ -> panels_mini.module (source)

   1  <?php
   2  // $Id: panels_mini.module,v 1.5.2.30 2010/10/19 21:43:25 merlinofchaos Exp $
   3  
   4  /**
   5   * @file panels_mini.module
   6   *
   7   * This module provides mini panels which are basically panels that can be
   8   * used within blocks or other panels.
   9   */
  10  
  11  /**
  12   * Implementation of hook_perm().
  13   */
  14  function panels_mini_perm() {
  15    return array('create mini panels', 'administer mini panels');
  16  }
  17  
  18  /**
  19   * Implementation of hook_menu().
  20   */
  21  function panels_mini_menu() {
  22    // Safety: go away if CTools is not at an appropriate version.
  23    if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  24      return array();
  25    }
  26    require_once drupal_get_path('module', 'panels_mini') . '/panels_mini.admin.inc';
  27    return _panels_mini_menu();
  28  }
  29  
  30  // ---------------------------------------------------------------------------
  31  // Allow the rest of the system access to mini panels
  32  
  33  /**
  34   * Implementation of hook_block().
  35   *
  36   * Expose qualifying mini panels to Drupal's block system.
  37   */
  38  function panels_mini_block($op = 'list', $delta = 0, $edit = array()) {
  39    // Safety: go away if CTools is not at an appropriate version.
  40    if (!defined('PANELS_REQUIRED_CTOOLS_API') || !module_invoke('ctools', 'api_version', PANELS_REQUIRED_CTOOLS_API)) {
  41      return array();
  42    }
  43  
  44    if ($op == 'list') {
  45      $blocks = array();
  46  
  47      $minis = panels_mini_load_all();
  48      foreach ($minis as $panel_mini) {
  49        if (empty($mini->disabled) && empty($mini->requiredcontext)) {
  50          $blocks[$panel_mini->name] = array(
  51            'info' => t('Mini panel: "@title"', array('@title' => $panel_mini->admin_title)),
  52            'cache' => BLOCK_NO_CACHE,
  53          );
  54        }
  55      }
  56  
  57      return $blocks;
  58    }
  59    elseif ($op == 'view') {
  60      // static recursion protection.
  61      static $viewing = array();
  62      if (!empty($viewing[$delta])) {
  63        return;
  64      }
  65      $viewing[$delta] = TRUE;
  66  
  67      $panel_mini = panels_mini_load($delta);
  68      if (empty($panel_mini)) {
  69        // Bail out early if the specified mini panel doesn't exist.
  70        return;
  71      }
  72  
  73      ctools_include('context');
  74      $panel_mini->context = $panel_mini->display->context = ctools_context_load_contexts($panel_mini);
  75      $panel_mini->display->css_id = panels_mini_get_id($panel_mini->name);
  76  
  77      $block = array();
  78  
  79      $block['content'] = panels_render_display($panel_mini->display);
  80      $block['subject'] = $panel_mini->display->get_title();
  81  
  82      unset($viewing[$delta]);
  83      return $block;
  84    }
  85    elseif ($op = 'configure') {
  86      return array(
  87        'admin-shortcut' => array(
  88          '#value' => l(t('Manage this mini-panel'), "admin/build/mini-panels/list/$delta/edit")
  89        ),
  90      );
  91    }
  92  }
  93  
  94  /**
  95   * Statically store all used IDs to ensure all mini panels get a unique id.
  96   */
  97  function panels_mini_get_id($name) {
  98    static $id_cache = array();
  99  
 100    $id = 'mini-panel-' . $name;
 101    if (!empty($id_cache[$name])) {
 102      $id .= "-" . $id_cache[$name]++;
 103    }
 104    else {
 105      $id_cache[$name] = 1;
 106    }
 107  
 108    return $id;
 109  }
 110  
 111  // ---------------------------------------------------------------------------
 112  // Database functions.
 113  
 114  /**
 115   * Create a new page with defaults appropriately set from schema.
 116   */
 117  function panels_mini_new($set_defaults = TRUE) {
 118    ctools_include('export');
 119    return ctools_export_new_object('panels_mini', $set_defaults);
 120  }
 121  
 122  /**
 123   * Load a single mini panel.
 124   */
 125  function panels_mini_load($name) {
 126    $cache = &ctools_static('panels_mini_load_all', array());
 127  
 128    // We use array_key_exists because failed loads will be NULL and
 129    // isset() will try to load it again.
 130    if (!array_key_exists($name, $cache)) {
 131      ctools_include('export');
 132      $result = ctools_export_load_object('panels_mini', 'names', array($name));
 133      if (isset($result[$name])) {
 134        if (empty($result[$name]->display)) {
 135          $result[$name]->display = panels_load_display($result[$name]->did);
 136          if (!empty($result[$name]->title) && empty($result[$name]->display->title)) {
 137            $result[$name]->display->title = $result[$name]->title;
 138          }
 139        }
 140        $cache[$name] = $result[$name];
 141        if (!empty($result[$name]->title) && empty($result[$name]->admin_title)) {
 142          $cache[$name]->admin_title = $result[$name]->title;
 143        }
 144      }
 145      else {
 146        $cache[$name] = NULL;
 147      }
 148    }
 149  
 150    if (isset($cache[$name])) {
 151      return $cache[$name];
 152    }
 153  }
 154  
 155  /**
 156   * Load all mini panels.
 157   */
 158  function panels_mini_load_all($reset = FALSE) {
 159    $cache = &ctools_static('panels_mini_load_all', array());
 160    static $all_loaded = FALSE;
 161  
 162    // We check our own private static because individual minis could have
 163    // been loaded prior to load all and we need to know that.
 164    if (!$all_loaded || $reset) {
 165      $all_loaded = TRUE;
 166      if ($reset) {
 167        $cache = array();
 168      }
 169  
 170      ctools_include('export');
 171      $minis = ctools_export_load_object('panels_mini');
 172      $dids = array();
 173      foreach ($minis as $mini) {
 174        if (empty($cache[$mini->name])) {
 175          if (!empty($mini->did)) {
 176            $dids[$mini->did] = $mini->name;
 177          }
 178          else {
 179          // Translate old style titles into new titles.
 180            if (!empty($mini->title) && empty($mini->display->title)) {
 181              $mini->display->title = $mini->title;
 182            }
 183          }
 184          // Translate old style titles into new titles.
 185          if (isset($mini->title) && empty($mini->admin_title)) {
 186            $mini->admin_title = $mini->title;
 187          }
 188          $cache[$mini->name] = $mini;
 189        }
 190      }
 191  
 192      $displays = panels_load_displays(array_keys($dids));
 193      foreach ($displays as $did => $display) {
 194        if (!empty($cache[$dids[$did]]->title) && empty($display->title)) {
 195          $display->title = $cache[$dids[$did]]->title;
 196        }
 197        $cache[$dids[$did]]->display = $display;
 198      }
 199    }
 200  
 201    return $cache;
 202  }
 203  
 204  /**
 205   * Write a mini panel to the database.
 206   */
 207  function panels_mini_save(&$mini) {
 208    if (!empty($mini->display)) {
 209      $display = panels_save_display($mini->display);
 210      $mini->did = $display->did;
 211    }
 212  
 213    $update = (isset($mini->pid) && $mini->pid != 'new') ? array('pid') : array();
 214    drupal_write_record('panels_mini', $mini, $update);
 215  
 216    return $mini;
 217  }
 218  
 219  /**
 220   * Remove a mini panel.
 221   */
 222  function panels_mini_delete($mini) {
 223    db_query("DELETE FROM {panels_mini} WHERE name = '%s'", $mini->name);
 224    if ($mini->type != t('Overridden')) {
 225      db_query("DELETE FROM {blocks} WHERE module = 'panels_mini' AND delta = '%s'", $mini->name);
 226    }
 227    return panels_delete_display($mini->did);
 228  }
 229  
 230  /**
 231   * Export a mini panel.
 232   */
 233  function panels_mini_export($mini, $indent = '') {
 234    ctools_include('export');
 235    $output = ctools_export_object('panels_mini', $mini, $indent);
 236    // Export the primary display
 237    $display = !empty($mini->display) ? $mini->display : panels_load_display($mini->did);
 238    $output .= panels_export_display($display, $indent);
 239    $output .= $indent . '$mini->display = $display' . ";\n";
 240    return $output;
 241  }
 242  
 243  /**
 244   * Remove the block version of mini panels from being available content types.
 245   */
 246  function panels_mini_ctools_block_info($module, $delta, &$info) {
 247    $info = NULL;
 248  }
 249  
 250  /**
 251   * Implementation of hook_ctools_plugin_directory() to let the system know
 252   * we implement task and task_handler plugins.
 253   */
 254  function panels_mini_ctools_plugin_directory($module, $plugin) {
 255    if ($module == 'ctools' && ($plugin == 'content_types' || $plugin == 'export_ui')) {
 256      return 'plugins/' . $plugin;
 257    }
 258  }
 259  
 260  /**
 261   * Get the display cache for the panels_mini plugin.
 262   */
 263  function _panels_mini_panels_cache_get($key) {
 264    ctools_include('export-ui');
 265    $plugin = ctools_get_export_ui('panels_mini');
 266    $handler = ctools_export_ui_get_handler($plugin);
 267    if (!$handler) {
 268      return;
 269    }
 270  
 271    $item = $handler->edit_cache_get($key);
 272    if (!$item) {
 273      $item = ctools_export_crud_load($handler->plugin['schema'], $key);
 274    }
 275  
 276    return array($handler, $item);
 277  }
 278  
 279  /**
 280   * Get display edit cache for the panels mini export UI
 281   *
 282   * The key is the second half of the key in this form:
 283   * panels_mini:TASK_NAME:HANDLER_NAME;
 284   */
 285  function panels_mini_panels_cache_get($key) {
 286    ctools_include('common', 'panels');
 287    list($handler, $item) = _panels_mini_panels_cache_get($key);
 288    if (isset($item->mini_panels_display_cache)) {
 289      return $item->mini_panels_display_cache;
 290    }
 291  
 292    $cache = new stdClass();
 293    $cache->display = $item->display;
 294    $cache->display->context = ctools_context_load_contexts($item);
 295    $cache->display->cache_key = 'panels_mini:' . $key;
 296    $cache->content_types = panels_common_get_allowed_types('panels_mini', $cache->display->context);
 297    $cache->display_title = TRUE;
 298  
 299    // @TODO support locking
 300    $cache->locked = FALSE;
 301  
 302    return $cache;
 303  }
 304  
 305  /**
 306   * Store a display edit in progress in the page cache.
 307   */
 308  function panels_mini_panels_cache_set($key, $cache) {
 309    list($handler, $item) = _panels_mini_panels_cache_get($key);
 310    $item->mini_panels_display_cache = $cache;
 311    $handler->edit_cache_set_key($item, $key);
 312  }
 313  
 314  /**
 315   * Save all changes made to a display using the panels mini UI cache.
 316   */
 317  function panels_mini_panels_cache_clear($key, $cache) {
 318    list($handler, $item) = _panels_mini_panels_cache_get($key);
 319    $handler->edit_cache_clear($item);
 320  }
 321  
 322  /**
 323   * Save all changes made to a display using the panels mini UI cache.
 324   */
 325  function panels_mini_panels_cache_save($key, $cache) {
 326    list($handler, $item) = _panels_mini_panels_cache_get($key);
 327    $item->display = $cache->display;
 328    panels_mini_save($item);
 329  
 330    $handler->edit_cache_clear($item);
 331  }
 332  
 333  /**
 334   * Break the lock on a panels mini page.
 335   */
 336  function panels_mini_panels_cache_break_lock($key, $cache) {
 337  }
 338  
 339  /**
 340   * Alter default mini panels to ensure they have new fields and avoid warnings.
 341   */
 342  function panels_mini_default_panels_mini_alter(&$minis) {
 343    foreach ($minis as $name => $mini) {
 344      if (!isset($mini->admin_description)) {
 345        $minis[$name]->admin_description = '';
 346      }
 347    }
 348  }
 349  
 350  /**
 351   * Implementation of hook_panels_dashboard_blocks().
 352   *
 353   * Adds mini panels information to the Panels dashboard.
 354   */
 355  function panels_mini_panels_dashboard_blocks(&$vars) {
 356    $vars['links']['panels_mini'] = array(
 357      'title' => l(t('Mini panel'), 'admin/build/mini-panels/add'),
 358      'description' => t('Mini panels are small content areas exposed as blocks, for when you need to have complex block layouts or layouts within layouts.'),
 359      'weight' => -1,
 360    );
 361  
 362    // Load all mini panels and their displays.
 363    $panel_minis = panels_mini_load_all();
 364    $count = 0;
 365    $rows = array();
 366  
 367    foreach ($panel_minis as $panel_mini) {
 368      $rows[] = array(
 369        check_plain($panel_mini->admin_title),
 370        array(
 371          'data' => l(t('Edit'), "admin/build/mini-panels/list/$panel_mini->name/edit"),
 372          'class' => 'links',
 373        ),
 374      );
 375  
 376      // Only show 10.
 377      if (++$count >= 10) {
 378        break;
 379      }
 380    }
 381  
 382    if ($rows) {
 383      $content = theme('table', array(), $rows, array('class' => 'panels-manage'));
 384    }
 385    else {
 386      $content = '<p>' . t('There are no mini panels.') . '</p>';
 387    }
 388  
 389    $vars['blocks']['panels_mini'] = array(
 390      'weight' => -100,
 391      'title' => t('Manage mini panels'),
 392      'link' => l(t('Go to list'), 'admin/build/mini-panels'),
 393      'content' => $content,
 394      'class' => 'dashboard-mini-panels',
 395      'section' => 'left',
 396    );
 397  
 398  }


Generated: Thu Mar 24 11:18:33 2011 Cross-referenced by PHPXref 0.7