<?php
/**
 * @file
 * Install, update and uninstall functions for the Comment abuse module.
 */

/**
 * Implements hook_schema().
 */
function comment_abuse_schema() {

  $schema['comment_abuse'] = array(
    'description' => 'This table contains complaints reported by users.',
    'fields' => array(
      'aid' => array(
        'description' => 'Abuse ID.',
        'type' => 'serial',
        'not null' => TRUE,
      ),
      'nid' => array(
        'description' => 'Node ID.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'cid' => array(
        'description' => 'Abused comment ID.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'reason' => array(
        'description' => 'Complaint reason.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'message' => array(
        'description' => 'Complaint message.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'timestamp' => array(
        'description' => 'A Unix timestamp indicating when the challenge was generated.',
        'type' => 'int',
        'not null' => TRUE,
        'default' => 0,
      ),
      'uid' => array(
        'description' => 'User ID.',
        'type' => 'int',
        'not null' => FALSE,
        'default' => 0,
      ),
      'ip' => array(
        'description' => 'User IP.',
        'type' => 'int',
        'size' => 'big',
        'not null' => FALSE,
        'default' => 0,
      ),
    ),
    'primary key' => array('aid'),
    'indexes' => array(
      'cid' => array('cid'),
      'uid' => array('uid'),
    ),
  );

  return $schema;
}

/**
 * Implements hook_install().
 */
function comment_abuse_install() {
  drupal_set_message(st('Comment abuse module installed successfully.'));

  // Allow users to complaint on comments.
  $role = user_role_load_by_name('authenticated user');
  if ($role) {
    user_role_change_permissions($role->rid, array('send complaints on comments' => 1));
  }

  variable_set('comment_abuse_complaint_text', st('Complain about a message'));
  variable_set('comment_abuse_complaint_popup_title', st('Complain about a message...'));
  variable_set('comment_abuse_complaint_success_message', st('Your complaint has been received.'));
  variable_set('comment_abuse_complaint_fail_message', st('Your complaint has been not received.'));
}

/**
 * Implements hook_uninstall().
 */
function comment_abuse_uninstall() {
  // Delete all variables.
  db_delete('variable')->condition('name', 'comment_abuse%', 'LIKE')->execute();
}

/**
 * This update allows you to attach to the complaint on
 * comment reason of complaint and message.
 */
function comment_abuse_update_7200() {

  db_add_field(
    'comment_abuse',
    'reason',
    array(
      'description' => 'Complaint reason.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );

  db_add_field(
    'comment_abuse',
    'message',
    array(
      'description' => 'Complaint message.',
      'type' => 'varchar',
      'length' => 255,
      'not null' => TRUE,
      'default' => '',
    )
  );
}

/**
 * Implements hook_update_N().
 */
function comment_abuse_update_7201() {
  variable_set('comment_abuse_complaint_text', st('Complain about a message'));
  variable_set('comment_abuse_complaint_popup_title', st('Complain about a message...'));
  variable_set('comment_abuse_complaint_success_message', st('Your complaint has been received.'));
  variable_set('comment_abuse_complaint_fail_message', st('Your complaint has been not received.'));
}
