<?php

/**
 * @file
 * Installation code for the download_count module.
 */

use Drupal\user\RoleInterface;

/**
 * Implements hook_schema().
 */
function download_count_schema() {
  $schema['download_count'] = [
    'fields' => [
      'dcid' => [
        'description' => 'Primary Key: Unique download count id.',
        'type' => 'serial',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'fid' => [
        'description' => 'The {file_managed}.fid of the file downloaded.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'uid' => [
        'description' => 'The {user}.uid of user who downloaded the file.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
      ],
      'type' => [
        'description' => 'The name of the entity type to which the file was attached when downloaded.',
        'type' => 'varchar',
        'length' => 64,
        'not null' => TRUE,
        'default' => '',
      ],
      'id' => [
        'description' => 'The primary key of the entity to which the file was attached when downloaded.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
      'ip_address' => [
        'description' => 'The IP address of the downloading user.',
        'type' => 'varchar',
        'length' => 128,
        'not null' => TRUE,
      ],
      'referrer' => [
        'description' => 'Referrer URI.',
        'type' => 'text',
        'not null' => TRUE,
      ],
      'timestamp' => [
        'description' => 'The date-time the file was downloaded.',
        'type' => 'int',
        'unsigned' => TRUE,
        'not null' => TRUE,
        'default' => 0,
      ],
    ],
    'primary key' => ['dcid'],
    'indexes' => [
      'dc_fid' => ['fid'],
      'dc_uid' => ['uid'],
      'dc_type' => ['type'],
      'dc_id' => ['id'],
      'dc_ip' => ['ip_address'],
      'dc_timestamp' => ['timestamp'],
      'dc_fid_type_id' => ['fid', 'type', 'id'],
    ],
  ];

  return $schema;
}

/**
 * Implements hook_uninstall().
 */
function download_count_uninstall() {
  \Drupal::service('config.factory')
    ->getEditable('download_count.settings')
    ->delete();
  \Drupal::messenger()->addStatus(t('The download count module has been uninstalled.'));
}

/**
 * Implements hook_install().
 *
 * Perform actions related to the installation of download_count.
 */
function download_count_install() {
  // Grant the default permissions for this module.
  user_role_grant_permissions(
    RoleInterface::ANONYMOUS_ID,
    [
      'view download counts',
    ]
  );
  user_role_grant_permissions(
    RoleInterface::AUTHENTICATED_ID,
    [
      'view download counts',
    ]
  );
}

/**
 * Increase download count flood window from 2 to 5 seconds.
 */
function download_count_update_8001(&$sandbox) {
  $config = \Drupal::service('config.factory')
    ->getEditable('download_count.settings');
  $config->set('download_count_flood_window', 5);
  $config->save();
}
