<?php
// $Id: tinymce_aci_conf.module,v 1.2 2008/03/07 23:39:53 manello Exp $

/**
 * Implementation of hook_menu().
 */
function tinymce_aci_conf_menu($may_cache) {
  $items = array();
  if ($may_cache) {
    $items[] = array(
      'path' => 'admin/settings/tinymce_aci_conf',
      'title' => t('TinyMCE: create Bryght-style profiles'),
      'callback' => 'drupal_get_form',
      'callback arguments' => array('tinymce_aci_conf_form', NULL),
      'description' => t('Automatically creates 3 TinyMCE profiles: comments, basic, advanced.'),
      'access' => user_access('administer tinymce'),
      'type' => MENU_NORMAL_ITEM,
    );
  }
  return $items;
}

/**
 * Creates the TinyMCE automatic configuration page
 */
function tinymce_aci_conf_form() {
  $form = array();
  
  $form['description'] = array(
    '#value' => t('This will automatically create 3 TinyMCE profiles: comments, basic, and 
    advanced. 
    <br/><br/>
    This script will not assign any roles to any of the profiles. This must be done manually 
    via the ') . l('TinyMCE settings page', 'admin/settings/tinymce') . t('. You must also 
    give the proper roles the "access TinyMCE" permission via the ') . 
    l('Access control page', 'admin/user/access') .   
    t('.<br/><br/>
    Warning - Any existing TinyMCE profiles named "Comments", "Basic", and "Advanced" will be 
    overwritten by this module. Use with care.
    <br/><br/>
    This also creates a "code" style for the font-style select box. In order to utilize 
    this style, you must have this (or similar) CSS somewhere in your stylesheet:<br/><br/>
    <pre>
.code {
  background-color:#CCCCCC;
  border:1px solid #DDDDDD;
  display:block;
  font-family:\'lucida console\',courier,monospace;
  overflow:auto;
  padding:2em;
  white-space:pre;
  width: 100%;
}
    </pre>
    <br/>
    The majority of the settings for these profile were developed by Boris Mann of Raincity Studios.<br/><br/>'),
  );
  
  $form['submit'] = array(
    '#type' => 'submit',
    '#value' => t('Create/Restore TinyMCE profiles'),
  );
  return $form;
}

/**
 * Do the configuration...
 */
function tinymce_aci_conf_form_submit($form_id, $form_values) {
  // Remove default line break filter for the FULL HTML format
  // NOTE: voodoo magic -- have to know that Full HTML = 3, and that line break delta = 2
  //       Should probably define constants for this in crud.inc
  install_remove_filter(3, 'filter', 2);
  
  // expanded Filtered HTML tags	
  variable_set('allowed_html_1',
  '<a> <em> <strong> <cite> <code> <ul> <ol> <li> <dl> <dt> <dd> <img> <table> <tr> <td> <th> <div> <span> <p> <br> <blockquote> <hr>');
  
  // TinyMCE
  $comment_settings = array(
    'name' => 'Comments',
    'old_name' => 'Comments',
    'default' => 'true',
    'user_choose' => 'false',
    'show_toggle' => 'false',
    'theme' => 'advanced',
    'language' => 'en',
    'safari_message' => 'false',
    'access' => '1',
    'access_pages' => "node/*\ncomment/*",
    'buttons' => array(
      'autosave' => '1',
      'default-bold' => '1',
      'default-italic' => '1',
      'default-underline' => '1',
      'default-strikethrough' => '1',
      'default-link' => '1',
      'default-unlink' => '1',
      'emotions-emotions' => '1',
      'font-styleselect' => '1',
    ),
    'toolbar_loc' => 'top',
    'toolbar_align' => 'center',
    'path_loc' => 'bottom',
    'resizing' => 'true',
    'block_formats' => 'p,address,pre,h1,h2,h3,h4,h5,h6',
    'verify_html' => 'true',
    'preformatted' => 'false',
    'convert_fonts_to_styles' => 'true',
    'css_setting' => 'theme',
    'css_path' => '',
    'css_classes' => 'Code=code',
  );
  
  // We'll install the Comments profile and associate it with the anonymous and authenticated users
  install_tinymce_create_profile( 'Comments', $comment_settings );
  
  // Let's start off with the same base  
  $basic_settings = $comment_settings;  
  
  // now we just override the changes  
  $basic_settings['name'] = 'Basic';
  $basic_settings['old_name'] = 'Basic';
  $basic_settings['user_choose'] = 'true';
  
  // we'll start with the same button set and merge in new ones
  $basic_settings['buttons'] = array_merge(
    $comment_settings['buttons'],
    array(     
      'default-justifyleft' => '1',
      'default-justifycenter' => '1',
      'default-justifyright' => '1',
      'default-bullist' => '1',
      'default-numlist' => '1',
      'default-indent' => '1',
      'default-outdent' => '1',
      'default-hr' => '1',
      'default-image' => '1',
      'spellchecker-spellchecker' => '1',
      'drupalbreak-drupalbreak' => '1',
      'drupalimage-drupalimage' => '1'
    )
  );
  $basic_settings['toolbar_align'] = 'left';
      
  // Now we create the Basic profile and assign it to the Contributor role    
  install_tinymce_create_profile( 'Basic', $basic_settings );
  
  // Let's do the same with Advanced, start it off with Basic settings
  $advanced_settings = $basic_settings;
  
  // now we just override the changes -- main difference is a Tables button and showing the toggle to disable rich text 
  $advanced_settings['name'] = 'Advanced';
  $advanced_settings['old_name'] = 'Advanced';
  $advanced_settings['show_toggle'] = 'true';
  
  // only addition is ability to work with tables
  $advanced_settings['buttons']['table-tablecontrols'] = '1';
      
  // Now we create the Advanced profile and assign it to the Site Admin role    
  install_tinymce_create_profile( 'Advanced', $advanced_settings );


  drupal_set_message(t('The TinyMCE profiles have been created/restored.'));
}

/**
 * Remove the specified filter from the specified format 
 * @param	$format_id	The ID of the format to remove the filter from
 * @param	$module		The module this filter belongs to
 * @param	$delta		The delta of this filter
 *
 * NOTE: the module name + the delta is what uniquely identifies a filter
 */
function install_remove_filter($format_id, $module, $delta) {	
  db_query("DELETE FROM {filters} WHERE format = %d AND module = '%s' AND delta = %d", $format_id, $module, $delta);
}

/**
 * Create a new TinyMCE profile and set the settings
 * @param	$name		A text string identifying the profile
 * @param	$settings	An associative array containing key value pairs
 */
function install_tinymce_create_profile($name, $settings) {
  db_query("DELETE FROM {tinymce_settings} WHERE name='%s'", $name);
  db_query("INSERT INTO {tinymce_settings} (name, settings) VALUES ('%s', '%s')", $name, serialize($settings));
}