View source
<?php
function smiley_help($path, $arg) {
switch ($path) {
case 'admin/settings/smiley':
case 'admin/settings/smiley/list':
return t('You can edit the Smileys that are recognized in your content.');
}
}
function smiley_perm() {
return array(
'administer smiley',
'use smiley select box',
);
}
function smiley_filter_tips($delta, $format, $long = false) {
if ($long) {
return t('If you include a textual smiley in your post (see chart below), it will be replaced by a graphical smiley.') . '<br />' . smiley_table();
}
else {
$GLOBALS['display_smiley_block'] = true;
return t('<a href="!smiley_help">Textual smiley</a> will be replaced with graphical ones.', array(
'!smiley_help' => url('filter/tips/' . $format, array(
'fragment' => 'filter-smiley-' . $delta,
)),
));
}
}
function _smiley_list($refresh = 0, $query_suffix = '') {
static $list;
if (!$list || $refresh) {
$list = array();
$result = db_query('SELECT * FROM {smiley} ' . $query_suffix);
while ($object = db_fetch_object($result)) {
$list[] = $object;
}
}
return $list;
}
function smiley_block($op = 'list', $delta = 0) {
if ($op == 'list') {
$blocks[0]['info'] = t('Smileys on submission pages');
return $blocks;
}
else {
if ($op == 'view') {
switch ($delta) {
case 0:
$block['subject'] = t('Smileys');
$block['content'] = $GLOBALS['display_smiley_block'] ? smiley_table() : '';
return $block;
}
}
}
}
function smiley_form_alter(&$form, $form_state, $form_id) {
if ($form_id != 'comment_form' && !isset($form['#node'])) {
return;
}
if ($form_id == 'comment_form') {
$node_type = db_result(db_query('SELECT type FROM {node} WHERE nid=%d', $form['nid']['#value']));
}
else {
if (!empty($form['type']['#value'])) {
$node_type = $form['type']['#value'];
}
}
if (in_array($node_type, variable_get('smiley_node_types_content', array()), TRUE)) {
if (user_access('use smiley select box') && (isset($form['type']) && $form['type']['#value'] . '_node_form' == $form_id && variable_get('smiley_enable_for_nodes', 0) && isset($form['body_field']) || 'comment_form' == $form_id && variable_get('smiley_enable_for_comments', 0))) {
$output = '';
if (array_key_exists('body_field', $form)) {
$key = 'body_field';
}
elseif (array_key_exists('comment_filter', $form)) {
$key = 'comment_filter';
}
else {
$key = 'smiley_wrapper';
}
$collapsed = variable_get('smiley_select_box_expanded', TRUE) ? FALSE : TRUE;
$form[$key]['smiley'] = array(
'#type' => 'fieldset',
'#title' => t('Smileys'),
'#collapsible' => TRUE,
'#collapsed' => $collapsed,
'#weight' => 0,
'#prefix' => '<div class="smiley-box">',
'#suffix' => '</div>',
);
$form[$key]['smiley']['smiley_box'] = array(
'#type' => 'markup',
'#value' => theme('smiley_select_table', TRUE),
);
_smiley_add_files();
}
}
return $form;
}
function _smiley_add_files() {
static $files_added = FALSE;
if (!$files_added) {
$path = drupal_get_path('module', 'smiley');
$dialog = variable_get('smiley_enable_dialog', TRUE);
$draggable = variable_get('smiley_dialog_draggable', FALSE);
$resizable = variable_get('smiley_dialog_resizable', FALSE);
$width = variable_get('smiley_dialog_width', 400);
$height = variable_get('smiley_dialog_height', 0);
$jquery_ui = module_exists('jquery_ui');
drupal_add_css($path . '/scripts/smiley.css');
drupal_add_js(array(
'smiley' => array(
'enable_dialog' => $dialog,
'draggable' => $draggable,
'resizable' => $resizable,
'dialog_width' => $width,
'dialog_height' => $height,
),
), 'setting');
drupal_add_js($path . '/scripts/smiley.js');
if ($dialog) {
$jquery_ui ? jquery_ui_add('ui.dialog') : drupal_add_js($path . '/scripts/ui.core.dialog.js');
}
if ($draggable) {
$jquery_ui ? jquery_ui_add('ui.draggable') : drupal_add_js($path . '/scripts/ui.draggable.js');
}
if ($resizable) {
$jquery_ui ? jquery_ui_add('ui.resizable') : drupal_add_js($path . '/scripts/ui.resizable.js');
}
}
}
function _smiley_select_table($promoted = false) {
$content = '';
$show_titles = variable_get('smiley_dialog_titles', TRUE) && !$promoted;
if ($promoted == true) {
$list = _smiley_list(1, " WHERE promote_to_box='1' ORDER BY weight");
_smiley_list(1);
}
else {
$list = _smiley_list(0, " WHERE promote_to_box='0' ORDER by weight");
}
$first_package = NULL;
if (!empty($show_titles)) {
$first_package = $list[0]->package;
$content .= '<fieldset><legend class="smileyPackageTitle">' . check_plain($first_package) . '</legend>';
}
foreach ($list as $smiley) {
$acronyms = explode(' ', $smiley->acronyms);
if ($smiley->package != $first_package && !empty($show_titles)) {
$content .= '</fieldset><fieldset><legend class="smileyPackageTitle">' . check_plain($smiley->package) . '</legend>';
$first_package = $smiley->package;
}
$content .= '<span>';
$content .= theme_image($smiley->image, $acronyms[0], $smiley->description . ' — ' . $smiley->acronyms, array(
'class' => 'smiley-class',
));
$content .= '</span>';
}
$content .= !empty($show_titles) ? '</fieldset>' : '';
return $content . (!$promoted ? '<br /><br />' : '');
}
function smiley_theme() {
return array(
'smiley_select_table' => array(
'arguments' => array(
'promoted' => FALSE,
),
),
);
}
function theme_smiley_select_table($promoted = false) {
return '<div class="smiley">' . _smiley_select_table($promoted) . '</div>';
}
function smiley_table() {
$form = array();
$output = '';
$header = array(
t('Smiley'),
t('Acronyms'),
);
$rows = array();
$list = _smiley_list(1, " ORDER BY weight");
foreach ($list as $smiley) {
$acronyms = explode(' ', $smiley->acronyms);
$rows[] = array(
'<img src="' . check_url($GLOBALS['base_url'] . '/' . check_plain($smiley->image)) . '" alt="' . check_plain($acronyms[0]) . '" title="' . check_plain($smiley->description) . '" class="smiley-class" />',
check_plain($smiley->acronyms),
);
}
$form['smiley'] = array(
'#type' => 'fieldset',
'#title' => t('Smileys'),
'#collapsible' => TRUE,
);
$form['smiley']['smiley_box'] = array(
'#type' => 'markup',
'#value' => theme('table', $header, $rows),
);
$output .= drupal_render($form);
return $output;
}
function smiley_list() {
exit(_smiley_select_table());
}
function smiley_filter($op, $delta = 0, $format = -1, $text = "") {
switch ($op) {
case 'list':
return array(
0 => t('Smileys filter'),
);
case 'description':
return t('Replaces smiley inside posts with images.');
case 'settings':
$form['smiley_filter'] = array(
'#type' => 'fieldset',
'#title' => t('Smileys filter'),
'#value' => t('You can define a global list of smiley on the !page.', array(
'!page' => l(t('smiley settings page'), 'admin/settings/smiley'),
)),
);
return $form;
case "process":
return smiley_filter_process($text);
default:
return $text;
}
}
function smiley_filter_process($text) {
$text = ' ' . $text . ' ';
$list = _smiley_list(0, ' ORDER BY weight');
$chunks = preg_split('@(</?(?:code|pre)[^>]*>)@i', $text, -1, PREG_SPLIT_DELIM_CAPTURE);
$ignore = FALSE;
$ignoretag = '';
$output = '';
foreach ($chunks as $i => $chunk) {
if ($i % 2) {
$open = $chunk[1] != '/';
list($tag) = split('[ >]', substr($chunk, 2 - $open), 2);
if (!$ignore) {
if ($open) {
$ignore = TRUE;
$ignoretag = $tag;
}
}
else {
if (!$open && $ignoretag == $tag) {
$ignore = FALSE;
$ignoretag = '';
}
}
}
else {
if (!$ignore) {
foreach ($list as $smiley) {
$acronyms = explode(" ", $smiley->acronyms);
$alt = str_replace('\\', '\\\\', check_plain($smiley->description));
foreach ($acronyms as $a) {
if ($smiley->standalone) {
$chunk = eregi_replace("([ ,\\.\\?!:\\(\\)\r\n\\<\\>])" . preg_quote($a) . "([ ,\\.\\?!:\\(\\)\r\n\\<\\>])", "\\1<img src=\"" . check_url($GLOBALS['base_url'] . '/' . $smiley->image) . "\" title=\"" . check_plain($alt) . "\" alt=\"" . check_plain($alt) . "\" class=\"smiley-content\"/>\\2", $chunk);
}
else {
$chunk = eregi_replace(preg_quote($a), '<img src="' . check_url($GLOBALS['base_url'] . '/' . $smiley->image) . '" title="' . check_plain($alt) . '" alt="' . check_plain($alt) . '" />', $chunk);
}
}
}
}
}
$output .= $chunk;
}
return $output;
}
function smiley_menu() {
$items = array();
$items['admin/settings/smiley'] = array(
'title' => t('Smileys'),
'description' => t('Manage Smileys and toggle select box behavior for nodes and comments.'),
'page callback' => 'smiley_admin_list',
'access arguments' => array(
'administer smiley',
),
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/list'] = array(
'title' => t('List'),
'page callback' => 'smiley_admin_list',
'type' => MENU_DEFAULT_LOCAL_TASK,
'weight' => -10,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/add'] = array(
'title' => t('Add'),
'page callback' => 'smiley_admin_add',
'access arguments' => array(
'administer smiley',
),
'type' => MENU_LOCAL_TASK,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/configure'] = array(
'title' => t('Settings'),
'description' => t('Smileys configuration.'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'smiley_admin_settings',
),
'access arguments' => array(
'administer smiley',
),
'weight' => 8,
'type' => MENU_LOCAL_TASK,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/edit/%'] = array(
'title' => t('Edit Smiley'),
'page callback' => 'smiley_admin_add',
'page arguments' => array(
4,
),
'access arguments' => array(
'administer smiley',
),
'type' => MENU_CALLBACK,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/delete/%'] = array(
'title' => t('Delete Smiley'),
'page callback' => 'drupal_get_form',
'page arguments' => array(
'smiley_admin_delete_confirm',
4,
),
'access arguments' => array(
'administer smiley',
),
'type' => MENU_CALLBACK,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/category'] = array(
'page callback' => 'smiley_category_handle',
'access arguments' => array(
'administer smiley',
),
'type' => MENU_CALLBACK,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/promote'] = array(
'page callback' => 'smiley_promote_handle',
'access arguments' => array(
'administer smiley',
),
'type' => MENU_CALLBACK,
'file' => 'smiley.admin.inc',
);
$items['admin/settings/smiley/weight'] = array(
'page callback' => 'smiley_weight_handle',
'access arguments' => array(
'administer smiley',
),
'type' => MENU_CALLBACK,
'file' => 'smiley.admin.inc',
);
$items['smiley/fetch'] = array(
'page callback' => 'smiley_list',
'access arguments' => array(
'use smiley select box',
),
'type' => MENU_CALLBACK,
);
return $items;
}