Installation & Uninstallation
These functions can be used run any code required when activating or deactivating the module. For example, to create custom tables, database entries, or perform license checks. They can also be used to return messages, or errors to a user. So, if you want to fail the process due to a problem, or want to notify the user of a missing requirement or license issue.
The deactivation function should undo everything that the activation function does to remove the module from the users system.
There will already be an active database connection when the module is run, so to access the WHMCS database you won’t need to reconnect to the database.
For example, the activate and deactivate functions could create and drop a table for use by the custom module as below:
Activate Function Example:
<?php
function yourmodule_activate() {
// Create custom tables and schema required by your module
try {
Method 1:
//Your Addon Activation Code perform here
$dbCommands[] = "CREATE TABLE IF NOT EXISTS mod_example_table (
client_id int(11) NOT NULL,
service_id int(11) NOT NULL,
ip varchar(25) COLLATE utf8_unicode_ci DEFAULT '0.0.0.0'
) ENGINE=InnoDB DEFAULT CHARSET=utf8 COLLATE=utf8_unicode_ci";
$dbCommands[] = "ALTER TABLE mod_example_table ADD PRIMARY KEY (id);";
foreach ($dbCommands as $cmd)
{
$created = full_query($cmd);
}
Method 2:
Capsule::schema()
->create(
'mod_example_table',
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_example_table: ' . $e->getMessage(),
];
}
}
Deactivate Function Example:
<?php
function yourmodule_deactivate() {
// Create custom tables and schema required by your module
try {
Capsule::schema()->dropIfExists('mod_example_table');
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_example_table: ' . $e->getMessage(),
];
}
}