<?php

function unl_node_prepare($node) {
  $node->menu['expanded'] = 1;
}

/**
 * Implementation of hook_wywiwyg_plugin() found in wysiwyg.api.php
 */
function unl_wysiwyg_plugin($editor) {
  switch ($editor) {
    case 'tinymce':
      return array(
        'unl' => array(
          // A URL to the plugin's homepage.
          'url' => 'http://gforge.unl.edu/gf/project/wdn_tinymce/',
           // The full path to the native editor plugin.
          'path' => $editor['library path'] . '/plugins/unl',
          // A list of editor extensions provided by this native plugin.
          // Extensions are not displayed as buttons and touch the editor's
          // internals, so you should know what you are doing.
          'extensions' => array(
            'unl' => 'UNL Extension'
          ),
          // Boolean whether this plugin is a native plugin, i.e. shipped with
          // the editor. Definition must be ommitted for plugins provided by
          // other modules.
          'internal' => TRUE,
          // Boolean whether the editor needs to load this plugin. When TRUE,
          // the editor will automatically load the plugin based on the 'path'
          // variable provided. If FALSE, the plugin either does not need to
          // be loaded or is already loaded by something else on the page.
          // Most plugins should define TRUE here.
          'load' => TRUE,
          // A list of buttons provided by this native plugin. The key has to
          // match the corresponding JavaScript implementation. The value is
          // is displayed on the editor configuration form only.
          'buttons' => array(
            'unlZenBox'   => 'UNL Zen Box',
            'unlZenTable' => 'UNL Zen Table',
            'unlLayout'   => 'UNL Layout',
            'unlTooltip'  => 'UNL Tooltip',
          ),
          // A list of global, native editor configuration settings to
          // override. To be used rarely and only when required.
          'options' => array(
            'skin' => 'unl',
            'table_styles' => 'ZenTable Bright (yellow)=zentable bright;ZenTable Cool (blue)=zentable cool;ZenTable Energetic (orange)=zentable energetic;ZenTable Soothing (green)=zentable soothing;ZenTable Primary (red)=zentable primary;ZenTable Neutral (gray)=zentable neutral;', 'doctype' => '<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">',
          ),
        )
        // Wysiwyg wrapper plugin AJAX callback.
        // 'callback' => url('myplugin/browse'),
        // 'icon' => drupal_get_path('module', 'mymodule') . '/myplugin/myplugin.png',
        // Might need to be set later on; after retrieving customized editor
        // layout.
        //'theme_advanced_buttons1' => array(t('Button title (optional)') => 'myplugin'),
      
      );
  }
}


function unl_permission() {
  return array(
    'unl migration' => array(
      'title'       => t('Migration'),
      'description' => t('Migrate UNL Template based sites to drupal'),
    ), 

    'unl site creation' => array(
      'title'       => t('Site Creation'),
      'description' => t('Create new drupal sites using the UNL profile'),
      'restrict access' => TRUE,
    ),
    
    'unl grant all permissions' => array(
      'title'       => t('Grant All Permissions'),
      'description' => t('If this is not checked, a user can only grant permissions that they themselves have. Requires the "Administer permissions" permission.'),
      'restrict access' => TRUE,
    ),
    
    'unl administer administrator permissions' => array(
      'title'       => t('Administer Administrator\'s Permissions'),
      'description' => t('If this is not checked, a user can not change the permissions of the administrator role. Requires the "Administer permissions" permission.'),
      'restrict access' => TRUE,
    ),
  );
}

/**
 * Adds UNL Migration Tool to the Content menu for admin
 */
function unl_menu() {
  $access = array();
  $items['admin/content/unl/migration'] = array(
    'title'            => 'UNL Migration Tool',
    'description'      => 'Migrate a static UNL template page into drupal',
    'access arguments' => array('unl migration'),
    'page callback'    => 'drupal_get_form',
    'page arguments'   => array('unl_migration'),
    'type'             => MENU_LOCAL_TASK,
    'file'             => 'unl_migration.php',
  );
  
  if (conf_path() == 'sites/default') {
    $items['admin/sites/unl'] = array(
      'title'            => 'UNL Site Creation Tool',
      'description'      => 'Create and manage UNL Drupal sites and aliases.',
      'access arguments' => array('unl site creation'),
      'page callback'    => 'unl_sites_page',
      'type'             => MENU_LOCAL_TASK,
      'file'             => 'unl_site_creation.php',
    );
    
    $items['admin/sites/unl/sites'] = array(
      'title'       => 'Sites',
      'description' => 'Create and manage UNL Drupal sites.',
      'type'        => MENU_DEFAULT_LOCAL_TASK,
      'weight'      => -8,
    );
    
    $items['admin/sites/unl/aliases'] = array(
      'title'            => 'Aliases',
      'description'      => 'Manage aliases of UNL Drupal sites.',
      'access arguments' => array('unl site creation'),
      'page callback'    => 'unl_aliases_page',
      'type'             => MENU_LOCAL_TASK,
      'file'             => 'unl_site_creation.php',
    );
  }
  
  return $items;
}

function unl_form_alter(&$form, $form_state, $form_id) {
  // Make new menu items expanded by default.
  if ($form_id == 'menu_edit_item' && $form['mlid']['#value'] == 0) {
    $form['expanded']['#default_value'] = TRUE;
  }
  if ($form_id == 'system_site_information_settings') {
    $form['site_information']['https'] = array(
      '#type' => 'checkbox',
      '#title' => 'SSL Enabled',
      '#default_value' => variable_get('https', FALSE),
    );
    $form['#submit'][] = 'unl_system_settings_form_submit';
  }
  
  if ($form_id == 'user_admin_permissions' && !in_array(variable_get('user_admin_role', -1), array_keys($GLOBALS['user']->roles))) {
    
    if (!user_access('unl grant all permissions')) {
      // Remove permissions this user doesn't have from the headings list.
      foreach ($form['permission'] as $permission => $sub_form) {
        if (is_int($permission)) {
          continue;
        }
        if (!user_access($permission)) {
          unset($form['permission'][$permission]);
        }
      }
    
      // Remove any empty permission section headings.
      $permission_sections = array_keys($form['permission']);
      foreach ($permission_sections as $index => $permission_section) {
        if (!is_int($permission_section)) {
          continue;
        }
        if (!isset($permission_sections[$index + 1]) || is_int($permission_sections[$index + 1])) {
          unset($form['permission'][$permission_section]);
        }
      }
    
      // Remove the permissions this user doesn't have from the checkboxes list.
      foreach ($form['checkboxes'] as $role_id => $sub_form) {
        foreach ($sub_form['#options'] as $permission => $value) {
          if (!user_access($permission)) {
            unset($form['checkboxes'][$role_id]['#options'][$permission]);
          }
        }
      }
    }
    
    if (!user_access('unl administer administrator permissions')) {
      $role_id = variable_get('user_admin_role', -1);
      unset($form['role_names'][$role_id]);
      unset($form['role_names']['#value'][$role_id]);
      unset($form['checkboxes'][$role_id]);
    }
    
    if (!in_array(variable_get('user_admin_role'), array_keys($GLOBALS['user']->roles))) {
      $administrator_permissions = array(
        'unl administer administrator permissions',
        'unl site creation',
        'administer modules',
        'administer themes',
        'administer software updates',
        'administer imce',
        'administer filters',
      );
    
      foreach ($form['permission'] as $permission => $sub_form) {
        if (in_array($permission, $administrator_permissions)) {
          unset($form['permission'][$permission]);
        }
      }
      foreach ($form['checkboxes'] as $role_id => $sub_form) {
        foreach ($sub_form['#options'] as $permission => $value) {
          if (in_array($permission, $administrator_permissions)) {
            unset($form['checkboxes'][$role_id]['#options'][$permission]);
          }
        }
      }
    }
  }
}

function unl_system_settings_form_submit($form, &$form_state) {
  variable_set('https', (bool) $form_state['values']['https']);
}

function unl_theme() {
  return array(
    'unl_site_list_table' => array(
      'render element' => 'form'
    ),
    'unl_alias_list_table' => array(
      'render element' => 'form'
    ),
  );
}

function unl_url_outbound_alter(&$path, &$options, $original_path) {
  $path_parts = parse_url($path);
  if (isset($path_parts['scheme']) || $path == 'user/cas') {
    return;
  }
  
  $user = $GLOBALS['user'];
  $user_roles = array_keys($user->roles);
  $generic_user = TRUE;
  foreach ($user_roles as $user_role) {
    if (in_array($user_role, array(DRUPAL_ANONYMOUS_RID, DRUPAL_AUTHENTICATED_RID))) {
      continue;
    }
    $generic_user = FALSE;
  }
  
  $options['https'] = (bool) (variable_get('https', 0) && !$generic_user);
}