123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853 |
- <?php
- /**
- * @file
- * Handles Skinr UI functionality allowing users to apply skins to their site.
- */
- /**
- * 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 rules, import and 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',
- );
- }
- // Rules.
- $items['admin/structure/skinr/rules'] = array(
- 'title' => 'Rules',
- 'page callback' => 'skinr_rules',
- 'type' => MENU_LOCAL_TASK,
- 'access arguments' => array('administer skinr'),
- 'weight' => 1,
- 'description' => t('Configure region and page level Skinr rules.'),
- 'file' => 'skinr_ui.rules.inc',
- );
- $items['admin/structure/skinr/rules/add'] = array(
- 'title' => 'Create a new rule',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('skinr_rule_add'),
- 'type' => MENU_LOCAL_ACTION,
- 'access arguments' => array('administer skinr'),
- 'file' => 'skinr_ui.rules.inc',
- );
- $items['admin/structure/skinr/rules/%skinr_rule/edit'] = array(
- 'title' => 'Edit rule',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('skinr_rule_edit', 4),
- 'type' => MENU_CALLBACK,
- 'access arguments' => array('administer skinr'),
- 'file' => 'skinr_ui.rules.inc',
- );
- $items['admin/structure/skinr/rules/%skinr_rule/delete'] = array(
- 'title' => 'Delete rule',
- 'page callback' => 'drupal_get_form',
- 'page arguments' => array('skinr_rule_delete_confirm', 4),
- 'type' => MENU_CALLBACK,
- 'access arguments' => array('administer skinr'),
- 'file' => 'skinr_ui.rules.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',
- );
- // Configure skin settings for an element.
- $items['admin/structure/skinr/edit/%skinr_js/%/%'] = array(
- 'title' => 'Edit skin',
- 'title callback' => 'skinr_ui_edit_title',
- 'title arguments' => array(5, 6),
- 'page callback' => 'skinr_ui_edit',
- 'page arguments' => array(4, 5, 6), // js|nojs, module, element
- 'type' => MENU_CALLBACK,
- 'context' => MENU_CONTEXT_PAGE | MENU_CONTEXT_INLINE,
- 'access arguments' => array('edit skin settings'),
- );
- $items['admin/structure/skinr/edit/%skinr_js/%/%/configure'] = array(
- 'title' => 'Edit skin',
- 'title callback' => 'skinr_ui_edit_contextual_title',
- 'title arguments' => array(5, 6),
- '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',
- );
- // 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;
- }
- /**
- * Helper function to determine if ajax is used to call a function.
- */
- function skinr_js_load($js = 'nojs') {
- if ($js == 'ajax') {
- return TRUE;
- }
- return 0;
- }
- /**
- * 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',
- ),
- 'skinr_ui_admin_library_summary' => array(
- 'variables' => array('name' => NULL, 'description' => NULL),
- '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/rule':
- return '<p>' . t('Below is a list of Skinr rules. Rules can be created for <em>region</em> and <em>page</em> elements. Start by creating a new rule.') . '</p>';
- case 'admin/structure/skinr/rule/add':
- return '<p>' . t('Choose the type of rule you wish to create. Page rules apply classes to the <body> tag. Region rules apply to the region wrapper <div> tag.') . '</p>';
- 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[5], $arg[6]);
- return '<p>' . t('Manage which skins you want to apply to the hooks <strong>!hooks</strong>.', array('!hooks' => implode(', ', $theme_hooks))) . '</p>';
- case 'admin/structure/skinr/rules/%/edit':
- // @todo Make this help text more relevant.
- $theme_hooks = skinr_theme_hooks('rules', $arg[4]);
- 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));
- }
- /**
- * Menu title callback; sets the title for a skins configuration form page.
- *
- * @param $module
- * The module that we're editing settings for.
- * @param $element
- * The element we're editing settings for.
- */
- function skinr_ui_edit_contextual_title($module, $element) {
- $contextual_links = skinr_ui_get_contextual_links();
- foreach ($contextual_links as $hook => $links) {
- $counter = 1;
- foreach ($links as $link) {
- if ($link[1][0] == $module && $link[1][1] == $element) {
- if (count($links) > 1) {
- return t('Edit skin !number', array('!number' => $counter++));
- }
- break 2;
- }
- }
- }
- return t('Edit skin');
- }
- /**
- * Menu callback; prepares some variables and displays a Skinr edit form.
- *
- * @param $js
- * TRUE if called from javascript, FALSE otherwise.
- * @param $module
- * The module that we're editing settings of.
- * @param $element
- * The element of the object we're editing settings of.
- * @param $elements
- * An array of $element when more than one is returned from the preprocess
- * index handler. Used by the javascript UI to update all elements involved.
- */
- function skinr_ui_edit($js = FALSE, $module, $element, $elements = NULL) {
- if ($js) {
- // Do additional ajax related stuff.
- }
- $arguments = array(
- 'skinr' => array(
- 'module' => $module,
- 'element' => $element,
- 'elements' => $elements,
- ),
- );
- return drupal_get_form('skinr_ui_form', $arguments);
- }
- /**
- * Form builder for the skins configuration form.
- *
- * @param $arguments
- * An array of arguments as passed in by skinr_ui_edit().
- *
- * @ingroup forms
- */
- function skinr_ui_form($form, &$form_state, $arguments) {
- $form = array(
- '#attributes' => array('class' => 'skinr-form'),
- );
- $form['skinr']['module'] = array(
- '#type' => 'hidden',
- '#value' => !empty($form_state['skinr']['module']) ? $form_state['skinr']['module'] : $arguments['skinr']['module'],
- );
- $form['skinr']['element'] = array(
- '#type' => 'hidden',
- '#value' => !empty($form_state['skinr']['element']) ? $form_state['skinr']['element'] : $arguments['skinr']['element'],
- );
- if (!empty($form_state['skinr']['elements']) || !empty($arguments['skinr']['elements'])) {
- $form['skinr']['elements'] = array(
- '#type' => 'hidden',
- '#value' => !empty($form_state['skinr']['elements']) ? $form_state['skinr']['elements'] : $arguments['skinr']['elements'],
- );
- }
- $form['actions'] = array('#type' => 'actions');
- $form['actions']['submit'] = array(
- '#type' => 'submit',
- '#value' => t('Save'),
- '#weight' => 50,
- );
- return $form;
- }
- /**
- * Implements hook_form_alter().
- */
- function skinr_ui_form_alter(&$form, $form_state, $form_id) {
- // Fix for update script.
- if ($form_id == 'update_script_selection_form') {
- return;
- }
- // Ensure module and element values are set.
- if (empty($form['skinr']['module']['#value']) || empty($form['skinr']['element']['#value'])) {
- return;
- }
- // Check for access.
- if (!skinr_ui_access('edit skin settings')) {
- // Deny access.
- return;
- }
- $module = $form['skinr']['module']['#value'];
- $element = $form['skinr']['element']['#value'];
- $groups = skinr_get_group_info();
- $skin_infos = skinr_get_skin_info();
- // Apply overridden status to skins.
- foreach ($skin_infos as $skin_name => $skin_info) {
- $skin_infos[$skin_name]['status'] = skinr_skin_info_status_get($skin_infos[$skin_name]);
- }
- // Invoke hook_skinr_theme_hooks() and hook_skinr_theme_hooks_alter().
- $theme_hooks = skinr_theme_hooks($module, $element);
- $form['skinr_settings'] = array(
- '#tree' => TRUE,
- // Set weight to accommodate Rules UI.
- '#weight' => 0,
- );
- $themes = list_themes();
- ksort($themes);
- // Get current theme, but make sure it's not the admin theme when we're editing with AJAX.
- $current_theme = skinr_current_theme(TRUE);
- foreach ($themes as $theme) {
- if (!$theme->status) {
- continue;
- }
- // If this hook is a region, and the region does not exist for this
- // theme, don't bother outputting any of the settings.
- if (strpos($theme_hooks[0], 'region') === 0) {
- // Strip the region__ part off the region name.
- $region = substr($theme_hooks[0], 8);
- $regions = system_region_list($theme->name, REGIONS_VISIBLE);
- if (!isset($regions[$region])) {
- continue;
- }
- }
- if (!$form_state['submitted']) {
- $params = array(
- 'theme' => $theme->name,
- 'module' => $module,
- 'element' => $element,
- );
- if ($skins = skinr_skin_load_multiple(skinr_skin_get_sids($params))) {
- $defaults = array();
- foreach ($skins as $skin) {
- $defaults[$skin->skin] = $skin->options;
- }
- }
- else {
- $defaults = array();
- }
- }
- else {
- // Handle preview before submit.
- // @todo Is this still needed? If so, it needs to be fixed.
- $defaults = $form_state['values'];
- }
- if (!isset($form['skinr_settings'][$module . '_type'])) {
- $form['skinr_settings'][$module . '_type'] = array(
- '#type' => 'container',
- );
- if ($module == 'rules') {
- $form['skinr_settings']['skinr_settings_title'] = array(
- '#type' => 'item',
- '#title' => t('Skinr settings'),
- '#weight' => -1,
- );
- }
- }
- $form['skinr_settings'][$module . '_type'][$theme->name] = array(
- '#type' => 'fieldset',
- '#title' => $theme->info['name'] . ($theme->name == $current_theme ? ' (' . t('enabled + default') . ')' : ''),
- '#collapsible' => TRUE,
- '#collapsed' => $theme->name == $current_theme ? FALSE : TRUE,
- );
- if ($theme->name == $current_theme) {
- // Current theme goes at the top.
- $form['skinr_settings'][$module . '_type'][$theme->name]['#attributes'] = array('class' => array('skinr-ui-current-theme'));
- $form['skinr_settings'][$module . '_type'][$theme->name]['#weight'] = -10;
- // Use vertical tabs.
- $form['skinr_settings'][$module . '_type'][$theme->name]['groups'] = array(
- '#type' => 'vertical_tabs',
- );
- }
- // Create individual widgets for each skin.
- foreach ($skin_infos as $skin_name => $skin_info) {
- // Check if this skin is disabled.
- if (empty($skin_info['status'][$theme->name])) {
- continue;
- }
- // Check if this skin applies to this hook.
- if (!is_array($skin_info['theme hooks']) || (!in_array('*', $skin_info['theme hooks']) && !_skinr_is_featured($theme_hooks, $skin_info['theme hooks']))) {
- continue;
- }
- // Create widget.
- $field = array();
- if (!empty($skin_info['form callback'])) {
- // Process custom form callbacks.
- // Load include file.
- if (!empty($skin_info['source']['filename'])) {
- skinr_load_include($skin_info['source']['path'] . '/' . $skin_info['source']['filename']);
- }
- // Execute form callback.
- if (function_exists($skin_info['form callback'])) {
- $context = array(
- 'theme' => $theme->name,
- 'skin_name' => $skin_name,
- 'skin_info' => $skin_info,
- );
- $field = $skin_info['form callback']($form, $form_state, $context);
- }
- }
- else {
- switch ($skin_info['type']) {
- case 'checkboxes':
- $field = array(
- '#type' => 'checkboxes',
- '#multiple' => TRUE,
- '#title' => t($skin_info['title']),
- '#options' => skinr_ui_info_options_to_form_options($skin_info['options']),
- '#default_value' => isset($defaults[$skin_name]) ? $defaults[$skin_name] : array(),
- '#description' => t($skin_info['description']),
- '#weight' => isset($skin_info['weight']) ? $skin_info['weight'] : NULL,
- );
- break;
- case 'radios':
- $field = array(
- '#type' => 'radios',
- '#title' => t($skin_info['title']),
- '#options' => array_merge(array('' => '<none>'), skinr_ui_info_options_to_form_options($skin_info['options'])),
- '#default_value' => isset($defaults[$skin_name]) ? $defaults[$skin_name] : '',
- '#description' => t($skin_info['description']),
- '#weight' => isset($skin_info['weight']) ? $skin_info['weight'] : NULL,
- );
- break;
- case 'select':
- $field = array(
- '#type' => 'select',
- '#title' => t($skin_info['title']),
- '#options' => array_merge(array('' => '<none>'), skinr_ui_info_options_to_form_options($skin_info['options'])),
- '#default_value' => isset($defaults[$skin_name]) ? $defaults[$skin_name] : '',
- '#description' => t($skin_info['description']),
- '#weight' => isset($skin_info['weight']) ? $skin_info['weight'] : NULL,
- );
- break;
- default:
- // Raise an error.
- drupal_set_message(t("Widget %name's type is invalid.", array('%name' => $skin_name)), 'error', FALSE);
- break;
- }
- }
- if (empty($skin_info['group']) || empty($groups[$skin_info['group']])) {
- $form['skinr_settings'][$module . '_type'][$theme->name][$skin_name] = $field;
- }
- else {
- if (!empty($field) && !isset($form['skinr_settings'][$module . '_type'][$theme->name]['groups'][$skin_info['group']])) {
- $group = $groups[$skin_info['group']];
- $form['skinr_settings'][$module . '_type'][$theme->name]['groups'][$skin_info['group']] = array(
- '#type' => 'fieldset',
- '#title' => t($group['title']),
- '#description' => t($group['description']),
- '#weight' => isset($group['weight']) ? $group['weight'] : NULL,
- );
- }
- $form['skinr_settings'][$module . '_type'][$theme->name]['groups'][$skin_info['group']][$skin_name] = $field;
- }
- }
- // Check for access.
- if (skinr_ui_access('edit advanced skin settings')) {
- $skin_name = '_additional';
- $form['skinr_settings'][$module . '_type'][$theme->name]['groups']['_additional'] = array(
- '#type' => 'fieldset',
- '#title' => t('Advanced'),
- '#weight' => 50,
- );
- $form['skinr_settings'][$module . '_type'][$theme->name]['groups']['_additional']['_additional'] = array(
- '#type' => 'textfield',
- '#title' => t('CSS classes'),
- '#size' => 40,
- '#description' => t('To add CSS classes manually, enter classes separated by a single space i.e. <code>first-class second-class</code>'),
- '#default_value' => isset($defaults[$skin_name]) ? $defaults[$skin_name] : '',
- );
- }
- }
- // Only add validation handler once.
- if (!isset($form['#validate']) || !in_array('skinr_ui_form_validate', $form['#validate'])) {
- $form['#validate'][] = 'skinr_ui_form_validate';
- }
- // Only add submit handler once.
- if (!isset($form['#submit']) || !in_array('skinr_ui_form_submit', $form['#submit'])) {
- $form['#submit'][] = 'skinr_ui_form_submit';
- }
- }
- /**
- * Form validation handler for skinr_ui_form_alter().
- */
- function skinr_ui_form_validate($form, &$form_state) {
- $module = $form_state['values']['module'];
- $element = $form_state['values']['element'];
- $error = FALSE;
- if (isset($form_state['values']['skinr_settings'][$module . '_type'])) {
- foreach ($form_state['values']['skinr_settings'][$module . '_type'] as $theme_name => $theme) {
- if (isset($theme['groups']['_additional']['_additional'])) {
- // Validate additional classes field.
- if (preg_match('/[^a-zA-Z0-9\-\_\s]/', $theme['groups']['_additional']['_additional'])) {
- form_set_error('skinr_settings][' . $module . '_type][' . $theme_name . '][groups][_additional][_additional', t('Additional classes for Skinr may only contain alphanumeric characters, spaces, - and _.'));
- $error = TRUE;
- }
- }
- }
- }
- if (!$error) {
- $groups = skinr_get_group_info();
- if (isset($form_state['values']['skinr_settings'][$module . '_type'])) {
- foreach ($form_state['values']['skinr_settings'][$module . '_type'] as $theme_name => $theme) {
- // Unset active tab variables.
- foreach ($theme['groups'] as $skin_name => $options) {
- if (strpos($skin_name, '__groups__active_tab') !== FALSE) {
- unset($form_state['values']['skinr_settings'][$module . '_type'][$theme_name]['groups'][$skin_name]);
- continue;
- }
- }
- // Undo any grouping to ease processing on submit.
- foreach ($groups as $group_name => $group) {
- if (!empty($theme['groups'][$group_name]) && is_array($theme['groups'][$group_name])) {
- $group_values = $theme['groups'][$group_name];
- unset($form_state['values']['skinr_settings'][$module . '_type'][$theme_name]['groups'][$group_name]);
- $form_state['values']['skinr_settings'][$module . '_type'][$theme_name]['groups'] = array_merge($form_state['values']['skinr_settings'][$module . '_type'][$theme_name]['groups'], $group_values);
- }
- }
- }
- }
- }
- }
- /**
- * Form submission handler for skinr_ui_form_alter().
- */
- function skinr_ui_form_submit($form, &$form_state) {
- $current_theme = skinr_current_theme(TRUE);
- $module = $form_state['values']['module'];
- $element = $form_state['values']['element'];
- if (isset($form_state['values']['skinr_settings'][$module . '_type'])) {
- foreach ($form_state['values']['skinr_settings'][$module . '_type'] as $theme_name => $theme) {
- // Process widgets.
- if (!empty($theme['groups']) && is_array($theme['groups'])) {
- foreach ($theme['groups'] as $skin_name => $options) {
- if ($skin_name == '_additional' && !user_access('edit advanced skin settings')) {
- // This user doesn't have access to alter these options.
- continue;
- }
- // Ensure options is an array.
- if (!is_array($options)) {
- $options = $skin_name == '_additional' ? explode(' ', $options) : array($options);
- }
- // Sanitize options.
- $options = _skinr_array_strip_empty($options);
- // Find existing skin.
- $params = array(
- 'theme' => $theme_name,
- 'module' => $module,
- 'element' => $element,
- 'skin' => $skin_name,
- );
- $sids = skinr_skin_get_sids($params);
- unset($skin);
- if (!empty($sids)) {
- $sid = reset($sids);
- $skin = skinr_skin_load($sid);
- }
- if (empty($options)) {
- if (!empty($skin)) {
- // Delete this skin configuration.
- skinr_skin_delete($skin->sid);
- }
- continue;
- }
- if (empty($skin)) {
- // It doesn't exist, so create a new skin.
- $skin = new stdClass();
- $skin->theme = $theme_name;
- $skin->module = $module;
- $skin->element = $element;
- $skin->skin = $skin_name;
- }
- $skin->options = $options;
- $skin->status = 1;
- // Save skin.
- if (!skinr_skin_save($skin)) {
- drupal_set_message(t("Skinr settings for %skin weren't saved due to an error.", array('%skin' => $skin_name)), 'error');
- }
- }
- }
- }
- }
- }
- /**
- * 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) {
- $contextual_links['skinr-' . $module . '-' . $counter++] = array(
- 'admin/structure/skinr/edit/nojs', array($module, $element),
- );
- }
- }
- 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) {
- _skinr_ui_set_contextual_links($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;
- }
- }
- }
- /**
- * Get all contextual links as returned from Skinr's contextual links handler.
- *
- * @return
- * An array of contextual links data.
- */
- function skinr_ui_get_contextual_links() {
- return _skinr_ui_set_contextual_links();
- }
- /**
- * Store contextual links internally for future use.
- *
- * @return
- * An array of contextual links data.
- */
- function _skinr_ui_set_contextual_links($hook = NULL, $links = NULL) {
- static $contextual_links = array();
- if ($hook && $links) {
- if (!isset($contextual_links[$hook])) {
- $contextual_links[$hook] = $links;
- }
- }
- return $contextual_links;
- }
- /**
- * Helper function to determine whether one of a set of hooks exists in a list
- * of required theme hooks.
- *
- * @param $theme_hooks
- * An array of theme hooks available to this element.
- * @param $allowed_hooks
- * An array of allowed theme hooks.
- *
- * @return
- * TRUE if an overlap is found, FALSE otherwise.
- *
- * @todo Rename function to be more descriptive.
- */
- function _skinr_is_featured($theme_hooks, $allowed_hooks) {
- foreach ($theme_hooks as $theme_hook) {
- if (in_array($theme_hook, $allowed_hooks)) {
- return TRUE;
- }
- }
- return FALSE;
- }
- /**
- * Helper function to retrieve a unique id for each skinr class. Used by AJAX.
- *
- * @return
- * A unique ID number.
- *
- * @todo Evaluate the usefulness of this function. Should it go into
- * a UI front-end specific file?
- */
- function _skinr_ui_ajax_id() {
- static $skinr_id = 0;
- return ++$skinr_id;
- }
- /**
- * Helper function to convert an array of options, as specified in the .info
- * file, into an array usable by Form API.
- *
- * @param $options
- * An array containing at least the 'class' and 'label' keys.
- *
- * @return
- * A Form API compatible array of options.
- *
- * @todo Rename function to be more descriptive.
- */
- function skinr_ui_info_options_to_form_options($options) {
- $form_options = array();
- foreach ($options as $option_name => $option) {
- $form_options[$option_name] = t($option['title']);
- }
- return $form_options;
- }
|