You are here

skinr_ui.module in Skinr 7.2

Same filename and directory in other branches
  1. 6.2 skinr_ui.module

Handles Skinr UI functionality allowing users to apply skins to their site.

File

skinr_ui.module
View source
<?php

/**
 * @file
 * Handles Skinr UI functionality allowing users to apply skins to their site.
 */

/**
 * Implements hook_hook_info().
 */
function skinr_ui_hook_info() {
  $hooks = array(
    'skinr_ui_element_options',
    'skinr_ui_element_title',
  );
  $hooks = array_fill_keys($hooks, array(
    'group' => 'skinr',
  ));
  return $hooks;
}

/**
 * Implements hook_permission().
 */
function skinr_ui_permission() {
  return array(
    'administer skinr' => array(
      'title' => t('Administer Skinr'),
    ),
    'edit skin settings' => array(
      'title' => t('Edit skin settings.'),
    ),
    'edit advanced skin settings' => array(
      'title' => t('Edit advanced skin settings'),
      'description' => t('Edit advanced skin settings, such as custom CSS classes.'),
    ),
  );
}

/**
 * Determine whether the user has a given privilege.
 *
 * @param $string
 *   The permission, such as "administer nodes", being checked for.
 * @param $account
 *   (optional) The account to check, if not given use currently logged in user.
 *
 * @return
 *   Boolean TRUE if the current user has the requested permission.
 *
 * @see user_access()
 */
function skinr_ui_access($string, $account = NULL) {
  return user_access($string, $account) || user_access('administer skinr', $account);
}

/**
 * Implements hook_menu().
 */
function skinr_ui_menu() {
  $items['admin/structure/skinr'] = array(
    'title' => 'Skinr',
    'description' => 'Manage your skin configurations and import/export skin configurations.',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'skinr_ui_list',
    ),
    'access arguments' => array(
      'administer skinr',
    ),
    'file' => 'skinr_ui.admin.inc',
  );
  $items['admin/structure/skinr/list'] = array(
    'title' => 'List',
    'description' => t('Manage skinr configurations.'),
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

  // Themes.
  $default_theme = variable_get('theme_default', 'bartik');
  $items['admin/structure/skinr/library'] = array(
    'title' => 'Library',
    'description' => 'Manage what skins are available when configuring the way your site looks.',
    'page callback' => 'skinr_ui_admin_library',
    'page arguments' => array(
      $default_theme,
    ),
    'access arguments' => array(
      'administer skinr',
    ),
    'file' => 'skinr_ui.admin.inc',
    'type' => MENU_LOCAL_TASK,
  );
  foreach (list_themes() as $key => $theme) {
    $items['admin/structure/skinr/library/list/' . $key] = array(
      'title' => check_plain($theme->info['name']),
      'page arguments' => array(
        $key,
      ),
      'type' => $key == $default_theme ? MENU_DEFAULT_LOCAL_TASK : MENU_LOCAL_TASK,
      'weight' => $key == $default_theme ? -10 : 0,
      'access callback' => '_skinr_ui_themes_access',
      'access arguments' => array(
        $theme,
      ),
      'file' => 'skinr_ui.admin.inc',
    );
  }

  // Import & Export.
  $items['admin/structure/skinr/import'] = array(
    'title' => 'Import',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'skinr_ui_import_form',
    ),
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer skinr',
    ),
    'weight' => 2,
    'description' => t('Import skin configurations.'),
    'file' => 'skinr_ui.admin.inc',
  );
  $items['admin/structure/skinr/export'] = array(
    'title' => 'Export',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'skinr_ui_export_form',
    ),
    'type' => MENU_LOCAL_TASK,
    'access arguments' => array(
      'administer skinr',
    ),
    'weight' => 3,
    'description' => t('Export skin configurations.'),
    'file' => 'skinr_ui.admin.inc',
  );

  // Add skin settings for an element.
  $items['admin/structure/skinr/add'] = array(
    'title' => 'Add skin',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'skinr_ui_add',
    ),
    'type' => MENU_LOCAL_ACTION,
    'access arguments' => array(
      'edit skin settings',
    ),
    'file' => 'skinr_ui.admin.inc',
  );

  // Configure skin settings for an element.
  $items['admin/structure/skinr/edit/%/%'] = array(
    'title' => 'Edit skin',
    'title callback' => 'skinr_ui_edit_title',
    'title arguments' => array(
      4,
      5,
    ),
    'page callback' => 'skinr_ui_edit',
    'page arguments' => array(
      4,
      5,
    ),
    // module, element
    'type' => MENU_CALLBACK,
    'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
    'access arguments' => array(
      'edit skin settings',
    ),
    'file' => 'skinr_ui.edit.inc',
  );
  $items['admin/structure/skinr/edit/%/%/configure'] = array(
    'title' => 'Edit skin',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'context' => MENU_CONTEXT_INLINE,
  );

  // Enable a skin configuration.
  $items['admin/structure/skinr/skin/%skinr_skin/enable'] = array(
    'title' => 'Enable skin',
    'page callback' => 'skinr_ui_skin_status_set',
    'page arguments' => array(
      4,
      TRUE,
    ),
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer skinr',
    ),
    'file' => 'skinr_ui.admin.inc',
  );

  // Disable a skin configuration.
  $items['admin/structure/skinr/skin/%skinr_skin/disable'] = array(
    'title' => 'Disable skin',
    'page callback' => 'skinr_ui_skin_status_set',
    'page arguments' => array(
      4,
      FALSE,
    ),
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer skinr',
    ),
    'file' => 'skinr_ui.admin.inc',
  );

  // Revert a skin configuration.
  $items['admin/structure/skinr/skin/%skinr_skin/revert'] = array(
    'title' => 'Revert skin',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'skinr_ui_revert_confirm',
      4,
    ),
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer skinr',
    ),
    'file' => 'skinr_ui.admin.inc',
  );

  // Delete a skin configuration.
  $items['admin/structure/skinr/skin/%skinr_skin/delete'] = array(
    'title' => 'Delete skin',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'skinr_ui_delete_confirm',
      4,
    ),
    'type' => MENU_CALLBACK,
    'access arguments' => array(
      'administer skinr',
    ),
    'file' => 'skinr_ui.admin.inc',
  );
  return $items;
}

/**
 * Menu item access callback - only admin or enabled themes can be accessed.
 */
function _skinr_ui_themes_access($theme) {
  return user_access('administer skinr') && drupal_theme_access($theme);
}

/**
 * Implements hook_theme().
 */
function skinr_ui_theme() {
  return array(
    'skinr_ui_admin_library_fieldset' => array(
      'render element' => 'form',
      'file' => 'skinr_ui.admin.inc',
    ),
  );
}

/**
 * Implements hook_help().
 */
function skinr_ui_help($path, $arg) {
  if (module_exists('advanced_help')) {
    $advanced_help = '<p>' . t('Visit the <a href="@skinr-help">help page</a> for full documentation.', array(
      '@skinr-help' => url('admin/advanced_help/skinr'),
    )) . '</p>';
  }
  else {
    $advanced_help = '<p>' . t('Please download and enable the <a href="http://drupal.org/project/advanced_help">Advanced Help</a> module for full Skinr documentation.') . '</p>';
  }
  switch ($path) {
    case 'admin/structure/skinr':
      return '<p>' . t('Below is a list of all skin configurations in use on this site.') . '</p>' . $advanced_help;
    case 'admin/structure/skinr/import':
      return '<p>' . t('To import skin configurations, paste exported code and click the "Import" button.') . '</p>';
    case 'admin/structure/skinr/export':
      return '<p>' . t('To export skin configurations, ensure the correct theme is selected and click the "Export" button.') . '</p>';
    case 'admin/structure/skinr/edit/%/%':

      // @todo Make this help text more relevant.
      $theme_hooks = skinr_theme_hooks($arg[4], $arg[5]);
      return '<p>' . t('Manage which skins you want to apply to the hooks <strong>!hooks</strong>.', array(
        '!hooks' => implode(', ', $theme_hooks),
      )) . '</p>';
  }
}

/**
 * Menu title callback; sets the title for a skins configuration form page.
 *
 * @param $module
 *   The module that we're editing settings of.
 * @param $element
 *   The element we're editing settings of.
 */
function skinr_ui_edit_title($module, $element) {
  return t('Skin settings for !module type !element', array(
    '!module' => $module,
    '!element' => $element,
  ));
}

/**
 * Fetch all theme_hooks that are compatible with active skins.
 *
 * @return
 *   An array of all theme hooks listed in active skins for current theme.
 */
function skinr_ui_get_skinable_hooks($theme = NULL) {
  $skinable_hooks =& drupal_static(__FUNCTION__);
  if (!isset($skinable_hooks)) {
    if ($cached = cache_get('skinr_skinable_hooks')) {
      $skinable_hooks = $cached->data;
    }
  }
  if (!isset($theme)) {
    $theme = skinr_current_theme();
  }
  if (!isset($skinable_hooks[$theme])) {
    $skinable_hooks[$theme] = array();
    $skin_infos = skinr_get_skin_info();
    foreach ($skin_infos as $skin_name => $skin_info) {
      $skin_infos[$skin_name]['status'] = skinr_skin_info_status_get($skin_infos[$skin_name]);
      if (!empty($skin_infos[$skin_name]['status'][$theme])) {
        foreach ($skin_infos[$skin_name]['theme hooks'] as $active_hook) {
          if (!isset($skinable_hooks[$active_hook])) {
            $skinable_hooks[$theme][$active_hook] = $active_hook;
          }
        }
      }
    }

    // Allow modules to alter config info via hook_skinr_skinnable_hooks_alter().
    drupal_alter('skinr_skinable_hooks', $skinable_hooks);
    cache_set('skinr_skinable_hooks', $skinable_hooks);
  }
  return $skinable_hooks[$theme];
}

/**
 * Fetch all theme_hooks that are compatible with active skins.
 */
function skinr_ui_element_is_skinable($module, $element) {
  $theme_hooks = skinr_theme_hooks($module, $element);
  $skinable_hooks = skinr_ui_get_skinable_hooks();
  if (isset($skinable_hooks['*'])) {

    // Skins exist that apply to any hook.
    return TRUE;
  }
  $theme_hooks = skinr_theme_hooks($module, $element);
  list($element_base) = explode('__', $element, 2);
  foreach ($theme_hooks as $theme_hook) {
    if (isset($skinable_hooks[$theme_hook])) {
      return TRUE;
    }
  }
  return FALSE;
}

/**
 * Implements hook_preprocess().
 */
function skinr_ui_preprocess(&$variables, $hook) {
  $original_hook = $hook;
  $theme_registry = theme_get_registry();
  if (isset($theme_registry[$hook]['original hook'])) {
    $original_hook = $theme_registry[$hook]['original hook'];
  }
  $contextual_links = array();
  $counter = 0;
  $array_elements = skinr_invoke_all('skinr_elements', $variables, $original_hook, 'contextual_links');
  foreach ($array_elements as $module => $elements) {
    foreach ($elements as $element) {
      if (!skinr_ui_element_is_skinable($module, $element)) {

        // If element is not skinable, don't add contextual link.
        continue;
      }
      $contextual_links['skinr-' . $module . '-' . $counter++] = array(
        'admin/structure/skinr/edit',
        array(
          $module,
          $element,
        ),
      );
      if ($element == 'html') {
        drupal_add_css("body.contextual-links-region { position:static; }", array(
          'type' => 'inline',
        ));
      }
    }
  }
  if (!empty($contextual_links)) {
    skinr_ui_contextual_links($variables, $original_hook, $contextual_links);
  }
  return;
}

/**
 * Set contextual menu items for skinr.
 *
 * @param $variables
 *   The $variables parameter from a preprocess function.
 * @param $hook
 *   The $hook parameter from a preprocess function.
 * @param $contextual_links
 *   An array of contextual links data as returned from Skinr's contextual
 *   links handler.
 */
function skinr_ui_contextual_links(&$variables, $hook, $contextual_links) {
  $hooks = theme_get_registry();

  // Determine the primary theme function argument.
  if (!empty($hooks[$hook]['variables'])) {
    $keys = array_keys($hooks[$hook]['variables']);
    $key = $keys[0];
  }
  elseif (!empty($hooks[$hook]['render element'])) {
    $key = $hooks[$hook]['render element'];
  }
  if (!empty($key) && isset($variables[$key])) {
    $element =& $variables[$key];
  }
  if (isset($element) && is_array($element)) {
    foreach ($contextual_links as $key => $contextual_link) {
      $element['#contextual_links'][$key] = $contextual_link;
    }
  }
}

Functions

Namesort descending Description
skinr_ui_access Determine whether the user has a given privilege.
skinr_ui_contextual_links Set contextual menu items for skinr.
skinr_ui_edit_title Menu title callback; sets the title for a skins configuration form page.
skinr_ui_element_is_skinable Fetch all theme_hooks that are compatible with active skins.
skinr_ui_get_skinable_hooks Fetch all theme_hooks that are compatible with active skins.
skinr_ui_help Implements hook_help().
skinr_ui_hook_info Implements hook_hook_info().
skinr_ui_menu Implements hook_menu().
skinr_ui_permission Implements hook_permission().
skinr_ui_preprocess Implements hook_preprocess().
skinr_ui_theme Implements hook_theme().
_skinr_ui_themes_access Menu item access callback - only admin or enabled themes can be accessed.