<?php

/**
 * @file
 * Installs, updates, and uninstalls BUEditor.
 */

/**
 * Implementation of hook_install()
 */
function bueditor_install() {
  drupal_install_schema('bueditor');
  drupal_set_message(t('!module has been installed.', array('!module' => l('BUEditor', 'admin/settings/bueditor'))));
  //workaround for #200931
  $GLOBALS['bueditor_just_installed'] = TRUE;
}

/**
 * Implementation of hook_enable()
 */
function bueditor_enable() {
  //workaround for #200931
  if (isset($GLOBALS['bueditor_just_installed'])) {
    drupal_get_schema(NULL, TRUE);
    module_load_include('inc', 'bueditor', 'admin/bueditor.admin');
    bueditor_import_all();
  }
}

/**
 * Implementation of hook_uninstall().
 */
function bueditor_uninstall() {
  drupal_uninstall_schema('bueditor');
  variable_del('bueditor_user1');
  variable_del('bueditor_user1_alt');
  variable_del('bueditor_roles');
  variable_del('bueditor_sprites_dir');
}


/**
 * Implementation of hook_schema().
 */
function bueditor_schema() {
  $schema['bueditor_editors'] = array(
    'description' => 'Stores editors and their settings.',
    'fields' => array(
      'eid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique editor ID.',
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'Noname',
        'description' => 'The editor name.',
      ),
      'pages' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Drupal paths on which the editor is visible.',
      ),
      'excludes' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Textarea ids for which the editor is not visible.',
      ),
      'iconpath' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '%BUEDITOR/icons',
        'description' => 'The directory path where the editor icons reside.',
      ),
      'librarypath' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'Files to be included with the editor.',
      ),
      'spriteon' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The state of CSS sprite support.',
      ),
      'spritename' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The CSS sprite name under bueditor-sprites directory.',
      ),
    ),
    'primary key' => array('eid'),
  );

  $schema['bueditor_buttons'] = array(
    'description' => 'Stores buttons of {bueditor_editors}.',
    'fields' => array(
      'bid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => 'Primary Key: Unique button ID.',
      ),
      'eid' => array(
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The {bueditor_editors}.eid to which the button belongs.',
      ),
      'title' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'Notitle',
        'description' => 'The button title.',
      ),
      'content' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => 'The button content.',
      ),
      'icon' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The button icon or caption.',
      ),
      'accesskey' => array(
        'type' => 'varchar',
        'length' => 1,
        'not null' => TRUE,
        'default' => '',
        'description' => 'The button acceskey.',
      ),
      'weight' => array(
        'type' => 'int',
        'size' => 'tiny',
        'not null' => TRUE,
        'default' => 0,
        'description' => 'The button weight that determines the button location in the editor layout.',
      ),
    ),
    'primary key' => array('bid'),
    'indexes' => array(
      'eid' => array('eid')
    ),
  );

  return $schema;
}

/**
 * Update from 4|5.x to 6.x
 * Add new fields: {bueditor_editors}.iconpath, {bueditor_editors}.librarypath.
 * Change {bueditor_buttons}.accesskey field type from char to varchar.
 * Update {bueditor_buttons}.content field values.
 * Delete needless cron variable.
 */
function bueditor_update_6000() {
  $ret = array();
  $iconpath = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '%BUEDITOR/icons',
  );
  $librarypath = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '%BUEDITOR/library',
  );
  $accesskey = array(
    'type' => 'varchar',
    'length' => 1,
    'not null' => TRUE,
    'default' => '',
  );
  db_add_field($ret, 'bueditor_editors', 'iconpath', $iconpath);
  db_add_field($ret, 'bueditor_editors', 'librarypath', $librarypath);
  db_change_field($ret, 'bueditor_buttons', 'accesskey', 'accesskey', $accesskey);
  variable_del('bueditor_cron_last');
  //update button contents
  $result = db_query("SELECT bid, content FROM {bueditor_buttons} WHERE content LIKE '%%%s%%' OR content LIKE '%%%s%%' OR content LIKE '%%%s%%'", 'editor.', 'imce', 'eDefSelP');
  $tr = array('editor.' => 'BUE.', "user_access('access imce')" => 'imce_access()', 'imce/browse' => 'imce', 'eDefSelProcessLines' => 'eDefTagLines');
  while ($button = db_fetch_object($result)) {
    $button->content = strtr($button->content, $tr);
    db_query("UPDATE {bueditor_buttons} SET content = '%s' WHERE bid = %d", $button->content, $button->bid);
  }
  // Update bueditor_roles variable
  $roles = variable_get('bueditor_roles', array());
  foreach ($roles as $rid => $role) {
    $roles[$rid]['alt'] = 0;
  }
  variable_set('bueditor_roles', $roles);
  return $ret;
}

/**
 * 6.x-1.x to 6.x-2.x
 * Change {bueditor_editors}.librarypath field type from varchar to text.
 * It now stores file paths rather than a directory path.
 */
function bueditor_update_6200() {
  require_once(drupal_get_path('module', 'bueditor') . '/bueditor.inc');
  $ret = array();
  $librarypath = array(
    'type' => 'text',
    'not null' => TRUE
  );
  db_change_field($ret, 'bueditor_editors', 'librarypath', 'librarypath', $librarypath);
  //scan library directories for js files and insert them into librarypath field.
  $files = array('%BUEDITOR/library' => '%BUEDITOR/library/bue.min.default.js');
  foreach (bueditor_editors('all') as $eid => $editor) {
    $dir = $editor->librarypath;
    if (!isset($files[$dir])) {
      $names = file_scan_directory(bueditor_path_tr($dir), '\.js$', array('.', '..', 'CVS'), 0, 0, 'basename');
      $files[$dir] = empty($names) ? '' : $dir .'/'. implode("\n$dir/", array_keys($names));
    }
    db_query("UPDATE {bueditor_editors} SET librarypath = '%s' WHERE eid = %d", $files[$dir], $eid);
  }
  return $ret;
}

/**
 * Add sprite support
 */
function bueditor_update_6201() {
  $ret = array();
  $spriteon = array(
    'type' => 'int',
    'size' => 'tiny',
    'not null' => TRUE,
    'default' => 0,
  );
  $spritename = array(
    'type' => 'varchar',
    'length' => 255,
    'not null' => TRUE,
    'default' => '',
  );
  db_add_field($ret, 'bueditor_editors', 'spriteon', $spriteon);
  db_add_field($ret, 'bueditor_editors', 'spritename', $spritename);
  return $ret;
}

/**
 * Rename bue.html.js to bue.markup.js to prevent a false positive virus alert by some AV.
 */
function bueditor_update_6202() {
  db_query("UPDATE {bueditor_editors} SET librarypath = REPLACE(librarypath, 'bue.html.js', 'bue.markup.js')");
  return array(array('success' => TRUE, 'query' => 'bue.html.js was renamed to bue.markup.js'));
}