You are here

restrict_abusive_words.module in Restrict Abusive Words 8

Same filename and directory in other branches
  1. 7.2 restrict_abusive_words.module
  2. 7 restrict_abusive_words.module

File

restrict_abusive_words.module
View source
<?php

use Drupal\Core\Database\Database;

/**
 * Check the word or phrase is exist in the abusive word list.
 */
function _restrict_abusive_words_search_words($words, $string) {
  if (!empty($string) && isset($words)) {
    foreach ($words as $word) {
      if (preg_match("/\\b{$word}\\b/i", $string)) {
        return $word;
      }
    }
  }
  return FALSE;
}

/**
 * Check the word or phrase is exists in the abusive word list.
 */
function _restrict_abusive_words_exists_words($words, $string) {
  if (!empty($string) && isset($words)) {
    foreach ($words as $word) {
      if ($string == $word) {
        return $word;
      }
    }
  }
  return FALSE;
}

/**
 * Implement _restrict_abusive_words_get_words_list().
 */
function _restrict_abusive_words_get_words_list($wid) {
  $output = array();
  $connection = Database::getConnection();
  $query = $connection
    ->select('restrict_abusive_words', 'raw');
  if (!empty($wid) && is_numeric($wid)) {
    $query
      ->condition('id', $wid);
  }
  $query
    ->fields('raw');

  // Execute the statement
  $data = $query
    ->execute();

  // Get all the results
  $results = $data
    ->fetchAll(\PDO::FETCH_OBJ);
  if (count($results) > 0) {
    foreach ($results as $result) {
      $output[$result->id] = $result->words;
    }
    return $output;
  }
  return FALSE;
}

/**
 * Error message against the field name(machine name).
 */
function _restrict_abusive_words_validation_message($field, $word) {
  $message = t("%word is not allowed word to use as it is abusive words. Please correct the word or contact to site administrator.", array(
    "%word" => $word,
  ));
  form_set_error($field, $message);
}

/**
 * Warning message against the Content.
 */
function _restrict_abusive_words_submit_message($entity, $title, $word) {
  $message = "{$title}( {$entity} ) is deactived, please check the word:{$word}, it is a abusive word.";
  drupal_set_message($message, 'warning');
}

/**
 * Get list of abusive word in autocomplete form.
 */
function _restrict_abusive_words_abusive_word_autocomplete($string) {
  if (!empty($string)) {
    $matches = array();
    $connection = Database::getConnection();
    $sth = $connection
      ->select('restrict_abusive_words', 'raw')
      ->fields('raw')
      ->condition('words', db_like($string) . '%', 'LIKE')
      ->orderBy('words', 'ASC')
      ->range(0, 10);

    // Execute the statement
    $data = $sth
      ->execute();

    // Get all the results
    $result = $data
      ->fetchAll(\PDO::FETCH_OBJ);

    // Save the query to matches.
    foreach ($result as $row) {
      $matches[$row->words] = check_plain($row->words);
    }

    // Return the result to the form in json.
    drupal_json_output($matches);
  }
}

Functions

Namesort descending Description
_restrict_abusive_words_abusive_word_autocomplete Get list of abusive word in autocomplete form.
_restrict_abusive_words_exists_words Check the word or phrase is exists in the abusive word list.
_restrict_abusive_words_get_words_list Implement _restrict_abusive_words_get_words_list().
_restrict_abusive_words_search_words Check the word or phrase is exist in the abusive word list.
_restrict_abusive_words_submit_message Warning message against the Content.
_restrict_abusive_words_validation_message Error message against the field name(machine name).