<?php

/**
 * Implementation of hook_custom_theme()
 */
function role_theme_switcher_custom_theme()
{
  global $user;
  $custom_theme = NULL;

  foreach ($user->roles as $id => $role)
  {
    $role_theme = variable_get('role_theme_switcher_' . $id . '_theme', '');

    if ($role_theme != 'Default')
    {
      $custom_theme = $role_theme;
    }
  }

  return array($custom_theme);
}

/**
 * Implementation of hook_menu()
 */
function role_theme_switcher_menu()
{
  $items['admin/people/rolethemeswitcher'] = array(
    'type'            => MENU_LOCAL_TASK,
    'title'           => 'Role theme switcher',
    'description'     => 'Settings for role theme switcher.',
    'page callback'   => 'drupal_get_form',
    'page arguments'  => array('role_theme_switcher_admin_settings'),
    'access arguments'=> array('administer site configuration'),
  );

  return $items;
}

/**
 * Implementation of hook_settings()
 */
function role_theme_switcher_admin_settings()
{
  $roles = user_roles();

  // Get all themes
  $themes = list_themes();

  $themes_list = array('Default');

  foreach ($themes AS $theme_name => $theme)
  {
    if (drupal_theme_access($theme))
    {
    	$themes_list[] = $theme_name;
    }
  }


  foreach ($roles as $id => $role)
  {
    $form['role_theme_switcher_' . $id . '_theme'] = array(
       '#type'          => 'select',
       '#title'         => drupal_ucfirst(drupal_strtolower($role)),
       '#options'       => drupal_map_assoc($themes_list),
       '#default_value' => variable_get('role_theme_switcher_' . $id . '_theme', '')
    );
  }

    $form['hint'] = array(
       '#markup'        => t('Only enabled themes are shown. You can manage themes in') . ' ' . l('Appearance', 'admin/appearance') . ' ' . t('section'),
  );

  return system_settings_form($form);
}
