You are here

smiley_import.module in Smiley 6

File

smiley_import.module
View source
<?php

/**
 * Implementation of hook_help().
 */
function smiley_import_help($section) {
  switch ($section) {
    case 'admin/settings/smiley/import':
      return t("To import new Smileys, download smiley packs for !phpBB or !Adium and extract them in <em>smiley</em> folder under website's files directory.", array(
        '!phpBB' => l('phpBB', 'http://www.phpbb.com/styles/db/index.php?i=browse&mode=group:component&sub=smilies'),
        '!Adium' => l('Adium', 'http://www.adiumxtras.com/index.php?a=search&cat_id=2'),
      ));
  }
}

/**
 * Implementation of hook_menu().
 */
function smiley_import_menu() {
  $items = array();
  $items['admin/settings/smiley/import'] = array(
    'title' => t('Import'),
    'page callback' => 'smiley_import_page',
    'access arguments' => array(
      'administer smiley',
    ),
    'weight' => 2,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/settings/smiley/export'] = array(
    'title' => t('Export'),
    'page callback' => 'smiley_export_page',
    'access arguments' => array(
      'administer smiley',
    ),
    'weight' => 3,
    'type' => MENU_LOCAL_TASK,
  );
  $items['admin/settings/smiley/import/add'] = array(
    'page callback' => 'smiley_import_add',
    'access arguments' => array(
      'administer smiley',
    ),
    'type' => MENU_CALLBACK,
  );
  $items['admin/settings/smiley/import/delete'] = array(
    'page callback' => 'smiley_import_delete',
    'access arguments' => array(
      'administer smiley',
    ),
    'type' => MENU_CALLBACK,
  );
  return $items;
}

/**
 * Menu callback; Present the page for operations on Smiley packs.
 */
function smiley_import_page() {
  $header = array(
    t('Smiley Packs'),
    t('Operations'),
  );
  $rows = array();
  $packs = smiley_import_packs();
  foreach ($packs as $pack) {
    $smileyp = db_fetch_array(db_query("SELECT * FROM {smiley} WHERE package = '%s'", $pack->name));
    $option_link = $smileyp ? l(t('Uninstall'), 'admin/settings/smiley/import/delete/' . $pack->name) : l(t('Install'), 'admin/settings/smiley/import/add/' . $pack->name);
    $rows[] = array(
      '<strong>' . check_plain($pack->name) . '</strong>',
      $option_link,
    );
  }
  $output = theme('table', $header, $rows);
  if (empty($rows)) {
    drupal_set_message(t('No smiley packs found.'));
    $output = '';
  }
  return $output;
}

/**
 * Menu callback; Present a page for .pak file for a Smiley pack.
 */
function smiley_export_page() {
  if ($package = arg(4)) {
    $smileys = db_query("SELECT * FROM {smiley} WHERE package = '%s' ORDER BY weight", $package);
    if (!($packd = smiley_import_packs($package))) {
      drupal_set_message(t('Cannot find smiley package %pack.', array(
        '%pack' => $package,
      )));
      drupal_goto('admin/settings/smiley/export');
    }
    $smile_pak = "";
    $delimeter = '=+:';
    while ($smiley = db_fetch_object($smileys)) {
      $smile_pak .= str_replace(dirname($packd->filename) . '/', '', $smiley->image) . $delimeter;
      $smile_pak .= $smiley->description . $delimeter;
      $smile_pak .= $smiley->acronyms . "\n";
    }
    $output = "<div id=\"module-message\">\n";
    $output .= t('Bundle a .pak file containing the following text along with each smiley image in the same folder as defined by example:<br /><strong>%package</strong>', array(
      '%package' => file_directory_path() . '/smiley/' . $package . '/' . $package . '.pak',
    )) . "\n";
    $output .= "</div>\n";
    $output .= "<div id=\"module-code\">\n";
    $output .= "<textarea cols=\"80\" rows=\"15\">\n";
    $output .= check_plain($smile_pak);
    $output .= "</textarea>\n";
    $output .= "</div>\n";
  }
  else {
    $header = array(
      t('Smiley Packs'),
      t('Operations'),
    );
    $smileyp = db_query("SELECT DISTINCT package FROM {smiley} ORDER BY package");
    while ($pack = db_fetch_object($smileyp)) {
      if ($pack->package != 'Uncategorized') {
        $rows[] = array(
          '<strong>' . check_plain($pack->package) . '</strong>',
          l(t('Export'), 'admin/settings/smiley/export/' . check_plain($pack->package)),
        );
      }
    }
    $output = theme('table', $header, $rows);
    if (empty($rows)) {
      drupal_set_message(t('No smiley packs to export.'));
      $output = '';
    }
  }
  return $output;
}

/**
 * Custom callback; Scan for available packs.
 */
function smiley_import_packs($a = NULL) {
  $packs = file_scan_directory(drupal_get_path('module', 'smiley') . '/packs', '\\.pak$');
  $files_directory = file_directory_path() . '/smiley';
  file_check_directory($file_directory, TRUE);
  $packs_from_files = file_scan_directory($files_directory, '\\.pak$');
  $packs = array_merge($packs, $packs_from_files);
  smiley_import_packs_plist($packs);
  ksort($packs);
  $pack_names = array();
  if ($a) {
    $packd = NULL;
    foreach ($packs as $pack) {
      if ($pack->name == $a) {
        $packd = $pack;
      }
    }
    return $packd;
  }
  return $packs;
}
function smiley_import_packs_plist(&$packs) {
  $plists = file_scan_directory(drupal_get_path('module', 'smiley') . '/packs', '\\.plist$');
  $files_directory = file_directory_path() . '/smiley';
  file_check_directory($file_directory, TRUE);
  $plists_from_files = file_scan_directory($files_directory, '\\.plist$');
  $plists = array_merge($plists, $plists_from_files);
  foreach ($plists as $key => $plist) {
    $plists[$key]->name = str_ireplace(array(
      drupal_get_path('module', 'smiley') . '/packs/',
      '.AdiumEmoticonset',
      '/Emoticons.plist',
    ), '', $plist->filename);
  }
  if (!empty($packs)) {
    $packs = array_merge($packs, $plists);
  }
  else {
    $packs = $plists;
  }
}

/**
 * Custom callback; Parse the plist information for a smiley package.
 */
function smiley_import_parse_plist($plist_pack = NULL) {
  include 'plist.inc';
  $packs = array();
  $key = array();
  smiley_import_packs_plist($packs);
  foreach ($packs as $pack) {
    $path = dirname(__FILE__) . str_replace(drupal_get_path('module', 'smiley'), '', $pack->filename);
    $plistDocument = new DOMDocument();
    $plistDocument
      ->load($path);
    $keys[$pack->name] = parsePlist($plistDocument);
  }
  if (!empty($plist_pack) && !empty($keys[$plist_pack])) {
    return $keys[$plist_pack];
  }
  return $keys;
}
function smiley_import_add_plist($plist_pack, $extension = '') {
  smiley_import_delete($plist_pack, 1);
  if (!($packd = smiley_import_packs($plist_pack))) {
    drupal_set_message(t('Cannot find smiley package %pack.', array(
      '%pack' => $plist_pack,
    )));
    drupal_goto('admin/settings/smiley/import');
  }
  $parse_data = smiley_import_parse_plist($plist_pack);
  foreach ($parse_data['Emoticons'] as $file => $smiley) {
    $acronyms = implode(' ', $smiley['Equivalents']);
    $acronyms = str_replace("<", "&lt;", $acronyms);
    $acronyms = str_replace(">", "&gt;", $acronyms);
    db_query("INSERT INTO {smiley} (acronyms, image, description, standalone, promote_to_box, package) VALUES ('%s', '%s', '%s', %d, 0, '%s')", $acronyms, drupal_get_path('module', 'smiley') . '/packs/' . $plist_pack . $extension . '/' . $file, $smiley['Name'], 1, $plist_pack);
  }
}
function smiley_import_add_pak($pack) {
  $delimeter = '=+:';
  smiley_import_delete($pack, 1);
  if (!($packd = smiley_import_packs($pack))) {
    drupal_set_message(t('Cannot find smiley package %pack.', array(
      '%pack' => $pack,
    )));
    drupal_goto('admin/settings/smiley/import');
  }
  $fcontents = @file(dirname($packd->filename) . '/' . $pack . '.pak');
  for ($i = 0; $i < count($fcontents); $i++) {
    $smile_data = explode($delimeter, trim(addslashes($fcontents[$i])));
    for ($j = 2; $j < count($smile_data); $j++) {
      $smile_data[$j] = str_replace("<", "&lt;", $smile_data[$j]);
      $smile_data[$j] = str_replace(">", "&gt;", $smile_data[$j]);
      db_query("INSERT INTO {smiley} (acronyms, image, description, standalone, promote_to_box, package) VALUES ('%s', '%s', '%s', %d, 0, '%s')", str_replace("\\'", "''", $smile_data[$j]), dirname($packd->filename) . '/' . str_replace("\\'", "''", $smile_data[0]), str_replace("\\'", "''", $smile_data[1]), 1, $pack);
    }
  }
}

/**
 * Menu callback; Delete a Smiley pack.
 */
function smiley_import_delete($packname, $redirect = 0) {
  $pack = $packname ? $packname : arg(5);
  db_query("DELETE FROM {smiley} WHERE package = '%s'", $pack);
  if ($redirect == 0) {
    drupal_set_message(t('Smiley package %pack uninstalled.', array(
      '%pack' => $pack,
    )));
    drupal_goto('admin/settings/smiley/import');
  }
}

/**
 * Menu callback; Add a Smiley pack.
 */
function smiley_import_add($packname) {
  $pack = $packname ? $packname : arg(5);
  $type = smiley_import_check_type($pack);
  if ($type == 'plist') {
    smiley_import_add_plist($pack, '.AdiumEmoticonset');
  }
  else {
    if ($type == 'plist2') {
      smiley_import_add_plist($pack);
    }
    else {
      smiley_import_add_pak($pack);
    }
  }
  drupal_set_message(t('Smiley package %pack installed.', array(
    '%pack' => $pack,
  )));
  drupal_goto('admin/settings/smiley/import');
}
function smiley_import_check_type($pack) {
  if (file_exists(dirname(__FILE__) . '/packs/' . $pack . '.AdiumEmoticonset/Emoticons.plist')) {
    return 'plist';
  }
  else {
    if (file_exists(dirname(__FILE__) . '/packs/' . $pack . '/Emoticons.plist')) {
      return 'plist2';
    }
    else {
      if (file_exists(dirname(__FILE__) . '/packs/' . $pack . '/' . $pack . '.pak')) {
        return 'pak';
      }
    }
  }
}

Functions

Namesort descending Description
smiley_export_page Menu callback; Present a page for .pak file for a Smiley pack.
smiley_import_add Menu callback; Add a Smiley pack.
smiley_import_add_pak
smiley_import_add_plist
smiley_import_check_type
smiley_import_delete Menu callback; Delete a Smiley pack.
smiley_import_help Implementation of hook_help().
smiley_import_menu Implementation of hook_menu().
smiley_import_packs Custom callback; Scan for available packs.
smiley_import_packs_plist
smiley_import_page Menu callback; Present the page for operations on Smiley packs.
smiley_import_parse_plist Custom callback; Parse the plist information for a smiley package.