<?php

/**
 * Implements hook_schema().
 */
function realname_schema() {
  $schema['realname'] = array(
    'description' => 'Computed Real Names to reduce overhead.',
    'fields' => array(
      'uid' => array(
        'description' => 'User ID, links to User table.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
      'realname' => array(
        'description' => 'The generated real name of the user.',
        'type' => 'varchar',
        'length' => 255,
        'not null' => TRUE,
        'default' => '',
      ),
      'created' => array(
        'description' => 'The UNIX timestamp of when the real name was created.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ),
    ),
    'primary key' => array('uid'),
    'indexes' => array(
      'realname' => array('realname')
    ),
    'foreign keys' => array(
      'users' => array(
        'table' => 'users',
        'columns' => array('uid' => 'uid'),
      ),
    ),
  );

  return $schema;
}

/**
 * Implements hook_requirements().
 */
function realname_requirements($phase) {
  $requirements = array();

  if ($phase == 'runtime') {
    if (strpos(variable_get('user_mail_register_admin_created_body', FALSE), '[user:name-raw]') === FALSE) {
      $requirements['user_mail_register_admin_created_body'] = array(
        'title' => t('E-mail: "Welcome (new user created by administrator)" template'),
        'value' => t('Not configured'),
        'severity' => REQUIREMENT_WARNING,
        'description' => t('Replace the token <code>[user:name]</code> with <code>[user:name-raw]</code> in <a href="@people">e-mail</a> template, if using username for login.', array('@people' => url('admin/config/people/accounts', array('fragment' => 'edit-email-admin-created')))),
      );
    }
    if (strpos(variable_get('user_mail_register_no_approval_required_body', FALSE), '[user:name-raw]') === FALSE) {
      $requirements['user_mail_register_no_approval_required_body'] = array(
        'title' => t('E-mail: "Welcome (no approval required)" template'),
        'value' => t('Not configured'),
        'severity' => REQUIREMENT_WARNING,
        'description' => t('Replace the token <code>[user:name]</code> with <code>[user:name-raw]</code> in <a href="@people">e-mail</a> template, if using username for login.', array('@people' => url('admin/config/people/accounts', array('fragment' => 'edit-email-no-approval-required')))),
      );
    }
    if (strpos(variable_get('user_mail_status_activated_body', FALSE), '[user:name-raw]') === FALSE) {
      $requirements['user_mail_status_activated_body'] = array(
        'title' => t('E-mail: "Account activation" template'),
        'value' => t('Not configured'),
        'severity' => REQUIREMENT_WARNING,
        'description' => t('Replace the token <code>[user:name]</code> with <code>[user:name-raw]</code> in <a href="@people">e-mail</a> template, if using username for login.', array('@people' => url('admin/config/people/accounts', array('fragment' => 'edit-email-activated')))),
      );
    }
  }

  return $requirements;
}

/**
 * Implements hook_disable().
 */
function realname_disable() {
  drupal_set_message('You need to replace the token <code>[user:name-raw]</code> with <code>[user:name]</code> in e-mail templates.', 'warning');
}

/**
 * Implements hook_uninstall().
 */
function realname_uninstall() {
  variable_del('realname_pattern');
}

/**
 * Update the {realname} table for Drupal 7.
 */
function realname_update_7000() {
  // Add the {realname}.created field if it doesn't already exist.
  if (!db_field_exists('realname', 'created')) {
    $fields['created'] = array(
      'description' => 'The UNIX timestamp of when the real name was created.',
      'type' => 'int',
      'unsigned' => TRUE,
      'not null' => TRUE,
      'default' => 0,
    );
    db_add_field('realname', 'created', $fields['created']);
  }

  $fields['realname'] = array(
    'description' => 'The generated real name of the user.',
    'type' => 'varchar',
    'length' => '255',
    'not null' => TRUE,
    'default' => '',
  );
  db_change_field('realname', 'realname', 'realname', $fields['realname']);
}
