Commit ca45d300 authored by azovsky's avatar azovsky

INIT #2774645 Drupal 8.x version

parents
This diff is collapsed.
INTRODUCTION
------------
Registration codes module allows you to create special codes that allow users
to register in your site with. You can make the registration code required,
which makes your site practically invite-only.
* For a full description of the module, visit the project page:
https://www.drupal.org/project/regcode
* To submit bug reports and feature suggestions, or to track changes:
https://www.drupal.org/project/issues/regcode
INSTALLATION
------------
https://www.drupal.org/docs/8/extending-drupal-8/installing-drupal-8-modules
CONFIGURATION
-------------
See page /admin/config/people/regcode for the options.
regcode_optional: false
regcode_generate_format: alpha
regcode_generate_case: ''
regcode_field_title: 'Registration Code'
regcode_field_description: 'Please enter your registration code.'
This diff is collapsed.
regcode.settings:
type: mapping
label: Settings
mapping:
regcode_optional:
type: boolean
regcode_generate_format:
type: string
regcode_generate_case:
type: string
<?php
/**
* @file
* Example hook functions for hooks provided by the core regcode module.
*/
/**
* Called when a registration code is used.
*/
function hook_regcode_used($code, $account) {
if (is_object($code)) {
drupal_set_message(t('Thanks %name, the code %code was used.', array(
'%name' => $account->name,
'%code' => $code['code'])));
}
}
/**
* Change the regcode data that is being loaded.
*
* @param array $code
* An array of reg codes, indexed by the registration code ID.
* Each element of the array is a regcode stdClass object. You can change the
* properties of individual reg codes.
*
* For reasons of backward compatibility and historical reasons, this hook is
* called from two places.
*
* Entity API module loads the data directly from the database into stdClass
* objects array, and invokes hook_regcode_load().
*
* In older versions of this module, there was hook already to alter
* individual regcode values. Since the arguments of the hook implementations
* are not consistent, reg code module now calls the hook_regcode_load() hook
* as an array. Note that adding or removing regcodes will work only if the
* caller expects multiple objects.
*
* If you need to replace and regcode that is being loaded, the array key
* should not be changed.
*
* Note that tags are not loaded when the hook is invoked by the entity API
* module.
*/
function hook_regcode_load(array $code) {
foreach ($code as $rid => $regcode) {
/*
* Do not change $rid or $regcode->rid.
*/
// Feel free to change other properties.
$regcode->group = 'foo';
}
}
/**
* Called to gather field information.
*/
//function hook_regcode_fields() {
// return array(
// 'group' => array(
// 'description' => t('The group this code should be assigned to.'),
// 'title' => t('Group'),
// ),
// );
//}
/**
* Called when a new registration code is created.
*/
function hook_regcode_presave($code) {
if ($code) {
drupal_set_message(
t('The code "@code" was created.', ['@code' => $code])
);
}
}
name: 'Registration codes'
description: 'Provides functionality for using/enabling/requiring registration codes upon user registration.'
package: Registration
core: 8.x
dependencies:
views: 'drupal:views'
type: module
<?php
/**
* @file
* Install, uninstall and scheme functions for the regcode module.
*/
/**
* Implements hook_schema().
*/
function regcode_schema() {
// Definition for the regcode table.
$schema['regcode'] = [
'description' => 'Hold registration codes',
'fields' => [
'rid' => [
'description' => 'RID',
'type' => 'serial',
'unsigned' => TRUE,
'not null' => TRUE,
],
'uid' => [
'description' => 'User ID',
'type' => 'int',
'unsigned' => TRUE,
'not null' => FALSE,
],
'created' => [
'description' => 'Code creation time',
'type' => 'int',
'not null' => FALSE,
],
'lastused' => [
'description' => 'Code last used time',
'type' => 'int',
'not null' => FALSE,
],
'begins' => [
'description' => 'Code activation date',
'type' => 'int',
'not null' => FALSE,
],
'expires' => [
'description' => 'Code expiry date',
'type' => 'int',
'not null' => FALSE,
],
'code' => [
'description' => 'The registration code',
'type' => 'varchar',
'length' => '255',
'not null' => TRUE,
'default' => '',
],
'is_active' => [
'description' => 'Whether the code is active',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
],
'maxuses' => [
'description' => 'Maximum times a code can be used',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 1,
],
'uses' => [
'description' => 'Number of times the code has been used',
'type' => 'int',
'unsigned' => TRUE,
'not null' => TRUE,
'default' => 0,
],
],
'primary key' => ['rid'],
'unique keys' => [
'code' => ['code'],
],
];
return $schema;
}
regcode.admin_list:
route_name: regcode.admin_list
title: 'Registration codes'
description: 'Manage registration codes.'
parent: user.admin_index
regcode.admin_list:
route_name: regcode.admin_list
title: List
weight: 10
base_route: regcode.admin_list
regcode.admin_create:
route_name: regcode.admin_create
title: Create
weight: 20
base_route: regcode.admin_list
regcode.admin_manage:
route_name: regcode.admin_manage
title: Manage
weight: 30
base_route: regcode.admin_list
regcode.admin_settings:
route_name: regcode.admin_settings
title: Settings
weight: 40
base_route: regcode.admin_list
This diff is collapsed.
'administer registration codes':
title: 'administer registration codes'
description: 'Configure the module and generate and delete registration codes'
regcode.admin_list:
path: /admin/config/people/regcode
defaults:
_title: 'Registration codes'
_controller: '\Drupal\regcode\Controller\DefaultController::regcode_admin_list'
requirements:
_permission: 'administer registration codes'
regcode.admin_create:
path: /admin/config/people/regcode/create
defaults:
_title: Create
_form: \Drupal\regcode\Form\RegcodeAdminCreate
requirements:
_permission: 'administer registration codes'
regcode.admin_manage:
path: /admin/config/people/regcode/manage
defaults:
_title: Manage
_form: \Drupal\regcode\Form\RegcodeAdminManage
requirements:
_permission: 'administer registration codes'
regcode.admin_settings:
path: /admin/config/people/regcode/settings
defaults:
_title: Settings
_form: \Drupal\regcode\Form\RegcodeAdminSettings
requirements:
_permission: 'administer registration codes'
<?php
/**
* @file
* Views integration for regcode module.
*/
/**
* Implements hook_views_handlers().
*/
/*function regcode_views_handlers() {
return array(
'info' => array(
'path' => drupal_get_path('module', 'regcode') . '/includes',
),
'handlers' => array(
'views_handler_field_term_regcode_tid' => array(
'parent' => 'views_handler_field_prerender_list',
),
),
);
}*/
/**
* Implements hook_views_data().
*/
function regcode_views_data() {
$data = array();
$data['regcode']['table']['group'] = t('Regcode');
$data['regcode']['table']['base'] = array(
'field' => 'rid',
'title' => t('Registration code'),
'help' => t('Registration codes created by the regcode module.'),
'weight' => -10,
);
$data['regcode']['table']['join'] = array(
'users' => array(
'left_field' => 'uid',
'field' => 'uid',
),
);
$data['regcode']['rid'] = [
'title' => t('ID'),
'help' => t('The unique index for the regcode'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
];
$data['regcode']['uid'] = array(
'title' => t('User'),
'help' => t('The last user to use this registration code.'),
'relationship' => array(
'base' => 'users_field_data',
'base field' => 'uid',
'id' => 'standard',
'label' => t('Last user'),
),
);
$data['regcode']['code'] = [
'title' => t('Code'),
'help' => t('The registration code.'),
'field' => [
'id' => 'standard',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'string',
],
'argument' => [
'id' => 'string',
],
];
$data['regcode']['created'] = [
'title' => t('Created'),
'help' => t('Date the registration code was created.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
$data['regcode']['lastused'] = [
'title' => t('Last used'),
'help' => t('Date the registration code was last used.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
$data['regcode']['begins'] = [
'title' => t('Begins'),
'help' => t('Date the registration code becomes available for use.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
$data['regcode']['expires'] = [
'title' => t('Expires'),
'help' => t('Date the registration code expires.'),
'field' => [
'id' => 'date',
],
'sort' => [
'id' => 'date',
],
'filter' => [
'id' => 'date',
],
];
$data['regcode']['is_active'] = [
'title' => t('Active'),
'help' => t('Whether the registration code is active.'),
'field' => [
'id' => 'boolean',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'boolean',
'label' => t('Active'),
'type' => 'yes-no',
'use_equal' => TRUE,
],
];
$data['regcode']['maxuses'] = [
'title' => t('Maximum uses'),
'help' => t('The maximum times this code can be used.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
];
$data['regcode']['uses'] = [
'title' => t('Uses'),
'help' => t('The number of times this code has been used.'),
'field' => [
'id' => 'numeric',
],
'sort' => [
'id' => 'standard',
],
'filter' => [
'id' => 'numeric',
],
'argument' => [
'id' => 'numeric',
],
];
return $data;
}
/**
* Implements hook_views_default_views().
*/
function regcode_views_default_views_alter(&$views) {
if (isset($views['regcode']) && \Drupal::moduleHandler()->moduleExists('views_bulk_operations')) {
$view = $views['regcode'];
$handler = $view->display['default'];
$newfields = array();
// Field: Bulk operations: Regcode
$newfields['views_bulk_operations']['id'] = 'views_bulk_operations';
$newfields['views_bulk_operations']['table'] = 'regcode';
$newfields['views_bulk_operations']['field'] = 'views_bulk_operations';
$newfields['views_bulk_operations']['label'] = '';
$newfields['views_bulk_operations']['element_label_colon'] = FALSE;
$newfields['views_bulk_operations']['vbo_settings']['display_type'] = '0';
$newfields['views_bulk_operations']['vbo_settings']['enable_select_all_pages'] = 1;
$newfields['views_bulk_operations']['vbo_settings']['force_single'] = 0;
$newfields['views_bulk_operations']['vbo_settings']['entity_load_capacity'] = '10';
$newfields['views_bulk_operations']['vbo_operations'] = array(
'action::regcode_activate_action' => array(
'selected' => 1,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::regcode_deactivate_action' => array(
'selected' => 1,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::regcode_delete_action' => array(
'selected' => 1,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::regcode_mailer_mail_action' => array(
'selected' => 1,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
'action::regcode_tag_action' => array(
'selected' => 1,
'postpone_processing' => 0,
'skip_confirmation' => 0,
'override_label' => 0,
'label' => '',
),
);
$handler->display_options['fields'] = $newfields + $handler->display_options['fields'];
}
}
<?php /**
* @file
* Contains \Drupal\regcode\Controller\DefaultController.
*/
namespace Drupal\regcode\Controller;
use Drupal\Core\Controller\ControllerBase;
/**
* Default controller for the regcode module.
*/
class DefaultController extends ControllerBase {
public function regcode_admin_list() {
return [
'#markup' => t('This page should be replaced by Views. If you are seeing this page, please check your Views configuration.'),
];
}
}
<?php
/**
* @file
* Contains \Drupal\regcode\Form\RegcodeAdminCreate.
*/
namespace Drupal\regcode\Form;
use Drupal\Core\Form\FormBase;
use Drupal\Core\Form\FormStateInterface;
use Drupal\Core\Render\Element;
class RegcodeAdminCreate extends FormBase {
/**
* {@inheritdoc}
*/
public function getFormId() {
return 'regcode_admin_create';
}
public function buildForm(array $form, \Drupal\Core\Form\FormStateInterface $form_state) {
$form = [];
// Basics.
$form['regcode_create'] = [
'#type' => 'fieldset',
'#title' => t('General settings'),
];
$form['regcode_create']['regcode_create_code'] = [
'#type' => 'textfield',
'#title' => t("Registration code"),
'#description' => t('Leave blank to have code generated. Used as prefix when <em>Number of codes</em> is greater than 1.'),
];
$form['regcode_create']['regcode_create_maxuses'] = [
'#type' => 'textfield',
'#title' => t("Maximum uses"),
'#default_value' => 1,
'#size' => 10,
'#required' => TRUE,
'#description' => t('How many times this code can be used to register (enter 0 for unlimited).'),
];
$form['regcode_create']['regcode_create_length'] = [
'#type' => 'textfield',
'#title' => t("Code size"),
'#size' => 10,
'#default_value' => 12,
];
$form['regcode_create']['regcode_create_format'] = [
'#type' => 'select',
'#default_value' => \Drupal::config('regcode.settings')->get('regcode_generate_format'),
'#title' => t('Format of the generated codes'),
'#options' => [
'alpha' => t('Letters'),
'numeric' => t('Numbers'),
'alphanum' => t('Numbers & Letters'),
'hexadec' => t('Hexadecimal'),
],
];
$form['regcode_create']['regcode_create_case'] = [
'#type' => 'checkbox',
'#title' => t('Uppercase generated codes'),
'#default_value' => \Drupal::config('regcode.settings')->get('regcode_generate_case'),
];
$form['regcode_create']['regcode_create_begins'] = [
'#type' => 'date',
'#title' => t("Active from"),
'#description' => t('When this code should activate (leave blank to activate immediately). Accepts any date format that strtotime can handle.'),
'#default_value' => [
'day' => 0,
'month' => 0,
'year' => 0,
],
'#element_validate' => [
'_regcode_date_validate'
],
];
$form['regcode_create']['regcode_create_expires'] = [
'#type' => 'date',
'#title' => t("Expires on"),
'#description' => t('When this code should expire (leave blank for no expiry). Accepts any date format that strtotime can handle.'),
'#default_value' => [
'day' => 0,
'month' => 0,
'year' => 0,
],
'#element_validate' => [
'_regcode_date_validate'
],
];
// Bulk.
$form['regcode_create_bulk'] = [
'#type' => 'fieldset',
'#title' => t('Bulk settings'),
'#description' => t('Multiple codes can be created at once, use these settings to configure the code generation.'),
];
$form['regcode_create_bulk']['regcode_create_number'] = [
'#type' => 'textfield',
'#title' => t("Number of codes to generate"),
'#size' => 10,
'#default_value' => 1,
];
$form['regcode_create_bulk_submit'] = [
'#type' => 'submit',
'#value' => t("Create codes"),
];
return $form;
}
public function validateForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
if (!is_numeric($form_state->getValue(['regcode_create_maxuses'])) || $form_state->getValue(['regcode_create_maxuses']) < 0) {
$form_state->setErrorByName('regcode_create_maxuses', t('Invalid maxuses, specify a positive integer or enter "0" for unlimited'));
}
}
public function submitForm(array &$form, \Drupal\Core\Form\FormStateInterface $form_state) {
$code = new \stdClass();
// Convert dates into timestamps.
foreach (['begins', 'expires'] as $field) {
$value = $form_state->getValue(['regcode_create_' . $field]);
if (!empty($value)) {
$code->$field = NULL;
$date = strtotime($value);
$day = date('d', $date);
$month = date('m', $date);
$year = date('Y', $date);
if (isset($year) && $year != 0) {
$code->$field = mktime(0, 0, 0, $month, $day, $year);
}
}
}
// Grab form values.
$code->is_active = 1;
$code->maxuses = $form_state->getValue(['regcode_create_maxuses']);
// Start creating codes.
for ($i = 0; $i < (int) $form_state->getValue(['regcode_create_number']); $i++) {
$code->code = $form_state->getValue(['regcode_create_code']);
// Generate a code.
if (empty($code->code) || $form_state->getValue(['regcode_create_number']) > 1) {
$gen = regcode_generate($form_state->getValue(['regcode_create_length']), $form_state->getValue(['regcode_create_format']), $form_state->getValue(['regcode_create_case']));
$code->code .= $gen;
}
// Save code.
if (regcode_save($code, REGCODE_MODE_SKIP)) {
drupal_set_message(t('Created registration code (%code)', [
'%code' => $code->code
]));
}
else {
drupal_set_message(t('Unable to create code (%code) as code already exists', [
'%code' => $code->code
]), 'warning');
}
}
}
}
<?php
/**
* @file
* Contains \Drupal\regcode\Form\RegcodeAdminManage.
*/