345 lines
11 KiB
PHP
345 lines
11 KiB
PHP
<?php
|
|
/**
|
|
* WHMCS SDK Sample Addon Module
|
|
*
|
|
* An addon module allows you to add additional functionality to WHMCS. It
|
|
* can provide both client and admin facing user interfaces, as well as
|
|
* utilise hook functionality within WHMCS.
|
|
*
|
|
* This sample file demonstrates how an addon module for WHMCS should be
|
|
* structured and exercises all supported functionality.
|
|
*
|
|
* Addon Modules are stored in the /modules/addons/ directory. The module
|
|
* name you choose must be unique, and should be all lowercase, containing
|
|
* only letters & numbers, always starting with a letter.
|
|
*
|
|
* Within the module itself, all functions must be prefixed with the module
|
|
* filename, followed by an underscore, and then the function name. For this
|
|
* example file, the filename is "addonmodule" and therefore all functions
|
|
* begin "addonmodule_".
|
|
*
|
|
* For more information, please refer to the online documentation.
|
|
*
|
|
* @see https://developers.whmcs.com/addon-modules/
|
|
*
|
|
* @copyright Copyright (c) WHMCS Limited 2017
|
|
* @license http://www.whmcs.com/license/ WHMCS Eula
|
|
*/
|
|
|
|
/**
|
|
* Require any libraries needed for the module to function.
|
|
* require_once __DIR__ . '/path/to/library/loader.php';
|
|
*
|
|
* Also, perform any initialization required by the service's library.
|
|
*/
|
|
|
|
use WHMCS\Database\Capsule;
|
|
use WHMCS\Module\Addon\AddonModule\Admin\AdminDispatcher;
|
|
use WHMCS\Module\Addon\AddonModule\Client\ClientDispatcher;
|
|
|
|
if (!defined("WHMCS")) {
|
|
die("This file cannot be accessed directly");
|
|
}
|
|
|
|
/**
|
|
* Define addon module configuration parameters.
|
|
*
|
|
* Includes a number of required system fields including name, description,
|
|
* author, language and version.
|
|
*
|
|
* Also allows you to define any configuration parameters that should be
|
|
* presented to the user when activating and configuring the module. These
|
|
* values are then made available in all module function calls.
|
|
*
|
|
* Examples of each and their possible configuration parameters are provided in
|
|
* the fields parameter below.
|
|
*
|
|
* @return array
|
|
*/
|
|
function addonmodule_config()
|
|
{
|
|
return [
|
|
// Display name for your module
|
|
'name' => 'Addon Module Sample',
|
|
// Description displayed within the admin interface
|
|
'description' => 'This module provides an example WHMCS Addon Module'
|
|
. ' which can be used as a basis for building a custom addon module.',
|
|
// Module author name
|
|
'author' => 'Your name goes here',
|
|
// Default language
|
|
'language' => 'english',
|
|
// Version number
|
|
'version' => '1.0',
|
|
'fields' => [
|
|
// a text field type allows for single line text input
|
|
'Text Field Name' => [
|
|
'FriendlyName' => 'Text Field Name',
|
|
'Type' => 'text',
|
|
'Size' => '25',
|
|
'Default' => 'Default value',
|
|
'Description' => 'Description goes here',
|
|
],
|
|
// a password field type allows for masked text input
|
|
'Password Field Name' => [
|
|
'FriendlyName' => 'Password Field Name',
|
|
'Type' => 'password',
|
|
'Size' => '25',
|
|
'Default' => '',
|
|
'Description' => 'Enter secret value here',
|
|
],
|
|
// the yesno field type displays a single checkbox option
|
|
'Checkbox Field Name' => [
|
|
'FriendlyName' => 'Checkbox Field Name',
|
|
'Type' => 'yesno',
|
|
'Description' => 'Tick to enable',
|
|
],
|
|
// the dropdown field type renders a select menu of options
|
|
'Dropdown Field Name' => [
|
|
'FriendlyName' => 'Dropdown Field Name',
|
|
'Type' => 'dropdown',
|
|
'Options' => [
|
|
'option1' => 'Display Value 1',
|
|
'option2' => 'Second Option',
|
|
'option3' => 'Another Option',
|
|
],
|
|
'Default' => 'option2',
|
|
'Description' => 'Choose one',
|
|
],
|
|
// the radio field type displays a series of radio button options
|
|
'Radio Field Name' => [
|
|
'FriendlyName' => 'Radio Field Name',
|
|
'Type' => 'radio',
|
|
'Options' => 'First Option,Second Option,Third Option',
|
|
'Default' => 'Third Option',
|
|
'Description' => 'Choose your option!',
|
|
],
|
|
// the textarea field type allows for multi-line text input
|
|
'Textarea Field Name' => [
|
|
'FriendlyName' => 'Textarea Field Name',
|
|
'Type' => 'textarea',
|
|
'Rows' => '3',
|
|
'Cols' => '60',
|
|
'Default' => 'A default value goes here...',
|
|
'Description' => 'Freeform multi-line text input field',
|
|
],
|
|
]
|
|
];
|
|
}
|
|
|
|
/**
|
|
* Activate.
|
|
*
|
|
* Called upon activation of the module for the first time.
|
|
* Use this function to perform any database and schema modifications
|
|
* required by your module.
|
|
*
|
|
* This function is optional.
|
|
*
|
|
* @see https://developers.whmcs.com/advanced/db-interaction/
|
|
*
|
|
* @return array Optional success/failure message
|
|
*/
|
|
function addonmodule_activate()
|
|
{
|
|
// Create custom tables and schema required by your module
|
|
try {
|
|
Capsule::schema()
|
|
->create(
|
|
'mod_addonexample',
|
|
function ($table) {
|
|
/** @var \Illuminate\Database\Schema\Blueprint $table */
|
|
$table->increments('id');
|
|
$table->text('demo');
|
|
}
|
|
);
|
|
|
|
return [
|
|
// Supported values here include: success, error or info
|
|
'status' => 'success',
|
|
'description' => 'This is a demo module only. '
|
|
. 'In a real module you might report a success or instruct a '
|
|
. 'user how to get started with it here.',
|
|
];
|
|
} catch (\Exception $e) {
|
|
return [
|
|
// Supported values here include: success, error or info
|
|
'status' => "error",
|
|
'description' => 'Unable to create mod_addonexample: ' . $e->getMessage(),
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Deactivate.
|
|
*
|
|
* Called upon deactivation of the module.
|
|
* Use this function to undo any database and schema modifications
|
|
* performed by your module.
|
|
*
|
|
* This function is optional.
|
|
*
|
|
* @see https://developers.whmcs.com/advanced/db-interaction/
|
|
*
|
|
* @return array Optional success/failure message
|
|
*/
|
|
function addonmodule_deactivate()
|
|
{
|
|
// Undo any database and schema modifications made by your module here
|
|
try {
|
|
Capsule::schema()
|
|
->dropIfExists('mod_addonexample');
|
|
|
|
return [
|
|
// Supported values here include: success, error or info
|
|
'status' => 'success',
|
|
'description' => 'This is a demo module only. '
|
|
. 'In a real module you might report a success here.',
|
|
];
|
|
} catch (\Exception $e) {
|
|
return [
|
|
// Supported values here include: success, error or info
|
|
"status" => "error",
|
|
"description" => "Unable to drop mod_addonexample: {$e->getMessage()}",
|
|
];
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Upgrade.
|
|
*
|
|
* Called the first time the module is accessed following an update.
|
|
* Use this function to perform any required database and schema modifications.
|
|
*
|
|
* This function is optional.
|
|
*
|
|
* @see https://laravel.com/docs/5.2/migrations
|
|
*
|
|
* @return void
|
|
*/
|
|
function addonmodule_upgrade($vars)
|
|
{
|
|
$currentlyInstalledVersion = $vars['version'];
|
|
|
|
/// Perform SQL schema changes required by the upgrade to version 1.1 of your module
|
|
if ($currentlyInstalledVersion < 1.1) {
|
|
$schema = Capsule::schema();
|
|
// Alter the table and add a new text column called "demo2"
|
|
$schema->table('mod_addonexample', function($table) {
|
|
$table->text('demo2');
|
|
});
|
|
}
|
|
|
|
/// Perform SQL schema changes required by the upgrade to version 1.2 of your module
|
|
if ($currentlyInstalledVersion < 1.2) {
|
|
$schema = Capsule::schema();
|
|
// Alter the table and add a new text column called "demo3"
|
|
$schema->table('mod_addonexample', function($table) {
|
|
$table->text('demo3');
|
|
});
|
|
}
|
|
}
|
|
|
|
/**
|
|
* Admin Area Output.
|
|
*
|
|
* Called when the addon module is accessed via the admin area.
|
|
* Should return HTML output for display to the admin user.
|
|
*
|
|
* This function is optional.
|
|
*
|
|
* @see AddonModule\Admin\Controller::index()
|
|
*
|
|
* @return string
|
|
*/
|
|
function addonmodule_output($vars)
|
|
{
|
|
// Get common module parameters
|
|
$modulelink = $vars['modulelink']; // eg. addonmodules.php?module=addonmodule
|
|
$version = $vars['version']; // eg. 1.0
|
|
$_lang = $vars['_lang']; // an array of the currently loaded language variables
|
|
|
|
// Get module configuration parameters
|
|
$configTextField = $vars['Text Field Name'];
|
|
$configPasswordField = $vars['Password Field Name'];
|
|
$configCheckboxField = $vars['Checkbox Field Name'];
|
|
$configDropdownField = $vars['Dropdown Field Name'];
|
|
$configRadioField = $vars['Radio Field Name'];
|
|
$configTextareaField = $vars['Textarea Field Name'];
|
|
|
|
// Dispatch and handle request here. What follows is a demonstration of one
|
|
// possible way of handling this using a very basic dispatcher implementation.
|
|
|
|
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
|
|
|
|
$dispatcher = new AdminDispatcher();
|
|
$response = $dispatcher->dispatch($action, $vars);
|
|
echo $response;
|
|
}
|
|
|
|
/**
|
|
* Admin Area Sidebar Output.
|
|
*
|
|
* Used to render output in the admin area sidebar.
|
|
* This function is optional.
|
|
*
|
|
* @param array $vars
|
|
*
|
|
* @return string
|
|
*/
|
|
function addonmodule_sidebar($vars)
|
|
{
|
|
// Get common module parameters
|
|
$modulelink = $vars['modulelink'];
|
|
$version = $vars['version'];
|
|
$_lang = $vars['_lang'];
|
|
|
|
// Get module configuration parameters
|
|
$configTextField = $vars['Text Field Name'];
|
|
$configPasswordField = $vars['Password Field Name'];
|
|
$configCheckboxField = $vars['Checkbox Field Name'];
|
|
$configDropdownField = $vars['Dropdown Field Name'];
|
|
$configRadioField = $vars['Radio Field Name'];
|
|
$configTextareaField = $vars['Textarea Field Name'];
|
|
|
|
$sidebar = '<p>Sidebar output HTML goes here</p>';
|
|
return $sidebar;
|
|
}
|
|
|
|
/**
|
|
* Client Area Output.
|
|
*
|
|
* Called when the addon module is accessed via the client area.
|
|
* Should return an array of output parameters.
|
|
*
|
|
* This function is optional.
|
|
*
|
|
* @see AddonModule\Client\Controller::index()
|
|
*
|
|
* @return array
|
|
*/
|
|
function addonmodule_clientarea($vars)
|
|
{
|
|
// Get common module parameters
|
|
$modulelink = $vars['modulelink']; // eg. index.php?m=addonmodule
|
|
$version = $vars['version']; // eg. 1.0
|
|
$_lang = $vars['_lang']; // an array of the currently loaded language variables
|
|
|
|
// Get module configuration parameters
|
|
$configTextField = $vars['Text Field Name'];
|
|
$configPasswordField = $vars['Password Field Name'];
|
|
$configCheckboxField = $vars['Checkbox Field Name'];
|
|
$configDropdownField = $vars['Dropdown Field Name'];
|
|
$configRadioField = $vars['Radio Field Name'];
|
|
$configTextareaField = $vars['Textarea Field Name'];
|
|
|
|
/**
|
|
* Dispatch and handle request here. What follows is a demonstration of one
|
|
* possible way of handling this using a very basic dispatcher implementation.
|
|
*/
|
|
|
|
$action = isset($_REQUEST['action']) ? $_REQUEST['action'] : '';
|
|
|
|
$dispatcher = new ClientDispatcher();
|
|
return $dispatcher->dispatch($action, $vars);
|
|
}
|