<?php

/**
 * @file
 * Makes IMCE available as plugin for client-side editors integrated via
 * Wysiwyg API.
 */

/**
 * Implements hook_wysiwyg_plugin().
 */
function imce_wysiwyg_plugin($editor, $version) {
  static $integrated = array();

  if (!module_invoke('imce', 'access')) {
    return;
  }
  // Load our invocation scripts.
  if (empty($integrated)) {
    $imcepath = drupal_get_path('module', 'imce');
    $path = drupal_get_path('module', 'imce_wysiwyg');
    drupal_add_js($imcepath . '/js/imce.js');
    drupal_add_js($imcepath . '/js/imce_set_app.js');
    drupal_add_js($path . '/js/imce_wysiwyg.js');
  }

  switch ($editor) {
    case 'tinymce':
      if (!isset($integrated[$editor])) {
        $integrated[$editor] = TRUE;
        // @todo If some other editor also needs the URL to be passed via
        //   Drupal.settings.imce, then we need another sub-key '$editor'.
        $settings = array(
          'imce' => array('url' => url('imce', array('query' => array('app' => $editor . '|url@')))),
        );
        drupal_add_js($settings, 'setting');
      }
      return array(
        'imce' => array(
          'extensions' => array('imce' => t('IMCE')),
          'url' => 'http://drupal.org/project/imce',
          'options' => array(
            'file_browser_callback' => 'imceImageBrowser',
            'inline_styles' => TRUE,
          ),
          'load' => FALSE,
        ),
      );

   case 'ckeditor':
      $integrated[$editor] = TRUE;
      return array(
        'imce' => array(
          'extensions' => array('imce' => t('IMCE')),
          'url' => 'http://drupal.org/project/imce',
          'options' => array(
            'filebrowserBrowseUrl' => url('imce', array('query' => array('app' => $editor . '|sendto@imceCkeditSendTo|params@'))),
          ),
          'load' => FALSE,
        ),
      );

    case 'fckeditor':
      $integrated[$editor] = TRUE;
      return array(
        'imce' => array(
          'extensions' => array('imce' => t('IMCE')),
          'url' => 'http://drupal.org/project/imce',
          'options' => array(
            'LinkBrowser' => TRUE,
            'LinkBrowserURL' => url('imce', array('query' => array('app' => $editor . '|url@txtUrl'))),
            'ImageBrowser' => TRUE,
            'ImageBrowserURL' => url('imce', array('query' => array('app' => $editor . '|url@txtUrl|width@txtWidth|height@txtHeight'))),
            'FlashBrowser' => TRUE,
            'FlashBrowserURL' => url('imce', array('query' => array('app' => $editor . '|url@txtUrl'))),
          ),
          'load' => FALSE,
        ),
      );
  }
}

