You are here

ckeditor_htmlbuttons.module in CKEditor HTML Buttons (for WYSIWYG and CKEditor) 7

Basic module file for CKEditor HTML Buttons.

File

ckeditor_htmlbuttons.module
View source
<?php

/**
 * @file
 * Basic module file for CKEditor HTML Buttons.
 */

/**
 * Implements hook_init().
 *
 * @todo: Should be only activated with CKEditor.
 */
function ckeditor_htmlbuttons_init() {
  module_load_include('inc', 'ckeditor_htmlbuttons', 'ckeditor_htmlbuttons.ckeditor');
  _ckeditor_htmlbuttons_ckeditor_add_buttons_js();
  module_load_include('inc', 'ckeditor_htmlbuttons', 'ckeditor_htmlbuttons.wysiwyg');
}

/**
 * Implements hook_menu().
 */
function ckeditor_htmlbuttons_menu() {
  $items = array();

  // Buttons overview settings page.
  $items['admin/config/content/ckeditor-htmlbuttons'] = array(
    'title' => 'CKEditor HTML Buttons',
    'description' => 'Create and modify CKEditor HTML Buttons',
    'page callback' => 'ckeditor_htmlbuttons_overview',
    'access arguments' => array(
      'administer ckeditor html buttons',
    ),
    'file' => 'ckeditor_htmlbuttons.admin.inc',
  );

  // List buttons.
  $items['admin/config/content/ckeditor-htmlbuttons/list'] = array(
    'title' => 'List Buttons',
    'type' => MENU_DEFAULT_LOCAL_TASK,
    'weight' => -10,
  );

  // Add button.
  $items['admin/config/content/ckeditor-htmlbuttons/add'] = array(
    'title' => 'Add Button',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ckeditor_htmlbuttons_button_form',
    ),
    'access arguments' => array(
      'administer ckeditor html buttons',
    ),
    'type' => MENU_LOCAL_TASK,
    'file' => 'ckeditor_htmlbuttons.admin.inc',
  );

  // Edit button.
  $items['admin/config/content/ckeditor-htmlbuttons/edit/%ckeditor_htmlbuttons_button'] = array(
    'title' => 'Edit Button',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ckeditor_htmlbuttons_button_form',
      5,
    ),
    'access arguments' => array(
      'administer ckeditor html buttons',
    ),
    'type' => MENU_CALLBACK,
    'parent' => 'admin/config/content/ckeditor-htmlbuttons',
    'file' => 'ckeditor_htmlbuttons.admin.inc',
  );

  // Delete button.
  $items['admin/config/content/ckeditor-htmlbuttons/delete'] = array(
    'title' => 'Delete Button',
    'page callback' => 'drupal_get_form',
    'page arguments' => array(
      'ckeditor_htmlbuttons_delete_confirm',
    ),
    'access arguments' => array(
      'administer ckeditor html buttons',
    ),
    'file' => 'ckeditor_htmlbuttons.admin.inc',
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Implements hook_permission().
 */
function ckeditor_htmlbuttons_permission() {
  $perms = array();
  $perms['administer ckeditor html buttons'] = array(
    'title' => t('Administer CKEditor HTML Buttons'),
    'description' => t('Create, edit and delete CKEditor HTML Buttons.'),
  );
  return $perms;
}

/**
 * CKEditor button Wildcard menu loader.
 *
 * Prepare item for the create/modify admin form.
 *
 * @param string $name
 *   Button machine name.
 *
 * @return array|null
 *   Button data.
 */
function ckeditor_htmlbuttons_button_load($name) {
  $button = ckeditor_htmlbutton_load_button($name);
  return $button;
}

/**
 * CKEditor button database load function.
 */
function ckeditor_htmlbutton_load_button($name) {
  $button_results = db_query('SELECT * FROM {ckeditor_htmlbuttons} WHERE name = :name', array(
    ':name' => $name,
  ));
  $button = $button_results
    ->fetchObject();
  $t_item = NULL;
  if ($button != NULL) {
    $t_item = array(
      'title' => $button->title,
      'fid' => $button->fid,
      'html' => $button->html,
      'name' => $name,
    );
  }
  return $t_item;
}

/**
 * CKEditor load all buttons from the database.
 */
function ckeditor_htmlbutton_load_all() {
  global $base_url;
  $module_path = drupal_get_path('module', 'ckeditor_htmlbuttons');
  $button_results = db_query('SELECT * FROM {ckeditor_htmlbuttons}');
  $buttons = array();
  foreach ($button_results as $button) {

    // Get the icon.
    $icon = file_load($button->fid);
    if ($icon) {
      $icon_url = file_create_url($icon->uri);
      $icon_url = parse_url($icon_url, PHP_URL_PATH);
    }
    else {
      $icon_url = '';
    }
    $button_new = array(
      'title' => $button->title,
      // Because the original plugin prepends plugin path to icon and our
      // icons are in the files folder, we need to cd back to root.
      'icon' => str_repeat('../', count(explode('/', $module_path))) . '../..' . $icon_url,
      'icon_clean' => $icon_url,
      'html' => $button->html,
      'name' => $button->name,
    );
    $buttons[] = $button_new;
  }
  return $buttons;
}

/**
 * CKEditor HTML Buttons database save function.
 */
function ckeditor_htmlbutton_save_button($button) {
  $new_button = array(
    'name' => $button['name'],
    'title' => $button['title'],
    'fid' => $button['fid'],
    // \r\n fixes wrapping feature of HTML Buttons plugin.
    // There's a question posted about this here:
    // http://ckeditor.com/addon/htmlbuttons?page=1
    'html' => trim($button['html']) . "\r\n",
  );
  $existing = ckeditor_htmlbuttons_name_exists($button['name']);

  // Save to database.
  return drupal_write_record('ckeditor_htmlbuttons', $new_button, $existing ? 'name' : array());
}

/**
 * CKEditor HTML Buttons database delete function.
 *
 * @param string $name
 *   Button name.
 */
function ckeditor_htmlbuttons_delete_button($name) {
  db_query('DELETE FROM {ckeditor_htmlbuttons} WHERE name = :name', array(
    ':name' => $name,
  ));
}

/**
 * Check if button name exists.
 *
 * @param string $name
 *   Button name.
 *
 * @return bool
 *   Return true or false.
 */
function ckeditor_htmlbuttons_name_exists($name) {
  return (bool) db_query('SELECT 1 FROM {ckeditor_htmlbuttons} WHERE name = :name', array(
    ':name' => $name,
  ))
    ->fetchField();
}

Functions

Namesort descending Description
ckeditor_htmlbuttons_button_load CKEditor button Wildcard menu loader.
ckeditor_htmlbuttons_delete_button CKEditor HTML Buttons database delete function.
ckeditor_htmlbuttons_init Implements hook_init().
ckeditor_htmlbuttons_menu Implements hook_menu().
ckeditor_htmlbuttons_name_exists Check if button name exists.
ckeditor_htmlbuttons_permission Implements hook_permission().
ckeditor_htmlbutton_load_all CKEditor load all buttons from the database.
ckeditor_htmlbutton_load_button CKEditor button database load function.
ckeditor_htmlbutton_save_button CKEditor HTML Buttons database save function.