<?php
// $Id: bueditor.install,v 1.15.2.3 2009/03/22 14:29:51 ufku Exp $

/**
 * Implementation of hook_install()
 */
function bueditor_install() {
  drupal_install_schema('bueditor');
  bueditor_insert_latest();
}

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


/**
 * Implementation of hook_schema().
 */
function bueditor_schema() {
  $schema['bueditor_editors'] = array(
    'description' => t('Stores editors and their settings.'),
    'fields' => array(
      'eid' => array(
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'description' => t('Primary Key: Unique editor ID.'),
      ),
      'name' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => 'Noname',
        'description' => t('The editor name.'),
      ),
      'pages' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => t('Drupal paths on which the editor is visible.'),
      ),
      'excludes' => array(
        'type' => 'text',
        'not null' => TRUE,
        'description' => t('Textarea ids for which the editor is not visible.'),
      ),
      'iconpath' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '%BUEDITOR/icons',
        'description' => t('The directory path where the editor icons reside.'),
      ),
      'librarypath' => array(
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '%BUEDITOR/library',
        'description' => t('The directory path where the editor library files reside.'),
      ),
    ),
    'primary key' => array('eid'),
  );

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

  return $schema;
}

/**
 * Insert the latest version of default editor.
 */
function bueditor_insert_latest($name = 'default') {

  //editor
  db_query("INSERT INTO {bueditor_editors} (name, pages, excludes, iconpath, librarypath) VALUES ('%s', '%s', '%s', '%s', '%s')", array($name, "node/*\ncomment/*", 'edit-log', '%BUEDITOR/icons', '%BUEDITOR/library'));
  $eid = db_last_insert_id('bueditor_editors', 'eid');

  //buttons
  $buttons = array();
  $buttons[] = array($eid, 'Insert/edit image', "php:\n\$imce_url = function_exists('imce_access') && imce_access() ? url('imce') : '';\n\nreturn \"js:\nvar B = eDefBrowseButton('\$imce_url', 'attr_src', 'Browse', 'image');\nvar form = [\n {name: 'src', title: 'Image URL', suffix: B},\n {name: 'width', title: 'Width x Height', suffix: ' x ', getnext: true, attributes: {size: 3}},\n {name: 'height', attributes: {size: 3}},\n {name: 'alt', title: 'Alternative text'}\n];\neDefTagDialog('img', form, 'Insert/edit image', 'OK');\n\";", 'image.png', 'M', -10);
  $buttons[] = array($eid, 'Insert/edit link', "php:\n\$imce_url = function_exists('imce_access') && imce_access() ? url('imce') : '';\n\nreturn \"js:\nvar B = eDefBrowseButton('\$imce_url', 'attr_href', 'Browse', 'link');\nvar form = [\n {name: 'href', title: 'Link URL', suffix: B},\n {name: 'html', title: 'Link text'},\n {name: 'title', title: 'Link title'}\n];\neDefTagDialog('a', form, 'Insert/edit link', 'OK');\n\";", 'link.png', 'L', -10);
  $buttons[] = array($eid, 'tpl:', '', 'separator.png', '', -8);
  $buttons[] = array($eid, 'Bold', "<strong>%TEXT%</strong>", 'bold.png', 'B', -6);
  $buttons[] = array($eid, 'Italic', "js: eDefTagger('em');/*toggle tag*/", 'italic.png', 'I', -6);
  $buttons[] = array($eid, 'Headers', "js: eDefTagChooser([\n ['h1', 'Header1'],\n ['h2', 'Header2'],\n ['h3', 'Header3'],\n ['h4', 'Header4']\n], true, 'li', 'ul', 'slideDown');/*choose among tags*/", 'headers.png', 'H', -5);
  $buttons[] = array($eid, 'tpl:', '', 'separator.png', '', -4);
  $buttons[] = array($eid, 'Ordered list. Converts selected lines to a numbered list.', "js: eDefTagLines('<ol>\\n', ' <li>', '</li>', '\\n</ol>');", 'ol.png', 'O', -2);
  $buttons[] = array($eid, 'Unordered list. Converts selected lines to a bulleted list.', "js: eDefTagLines('<ul>\\n', ' <li>', '</li>', '\\n</ul>');", 'ul.png', 'U', -2);
  $buttons[] = array($eid, 'tpl:', '', 'separator.png', '', 0);
  $buttons[] = array($eid, 'Teaser break', "<!--break-->", 'teaserbr.png', 'T', 2);
  $buttons[] = array($eid, 'Preview', "js: eDefPreview();", 'preview.png', 'P', 6);
  $buttons[] = array($eid, 'Help', "js: eDefHelp('slideDown');", 'help.png', 'F', 10);

  foreach ($buttons as $button) {
    db_query("INSERT INTO {bueditor_buttons} (eid, title, content, icon, accesskey, weight) VALUES (%d, '%s', '%s', '%s', '%s', %d)", $button);
  }

}

/**
 * Update button contents
 */
function bueditor_buttons_update() {
  $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 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.
 * Insert the latest version of default editor.
 * 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);
  bueditor_buttons_update();
  bueditor_insert_latest('default 6.x');
  variable_del('bueditor_cron_last');
  return $ret;
}