Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.


Table of Contents
Table of Contents
maxLevel4
minLevel2
outlinetrue
classtoc

Required Methods

Info

As of version 3.1 of Blesta, all of these required methods can be defined in a module configuration file instead.

...

Code Block
languagephp
class MyModule extends Module {
...
	public function install() {
		// Perform install logic, such as installing cron tasks
		Loader::loadModels($this, ['CronTasks']);

		// Retrieve the cron task if it already exists for another company
		$task = $this->CronTasks->getByKey('task_one', 'my_module', 'module');

		if (!$task) {
			// Create the automation task
			$task = [
				'key' => 'task_one', // a string used to identify this cron task (see MyPluginPlugin::cron())
				'task_type' => 'module', // this cron task is for a module, so it must be set to 'module'
				'dir' => 'my_module', // the directory of this module
				'name' => 'My Module Task', // the name of this cron task
				'description' => 'This cron tasks does stuff', // the description of this task
				'type' => 'time' // "time" = once per day at a defined time, "interval" = every few minutes or hours
			];
			$task_id = $this->CronTasks->add($task);
		} else {
			$task_id = $task->id;
		}

		// Create the cron task run for this company
		if ($task_id) {
			$task_run = array(
				'time' => '14:25', // the daily 24-hour time that this task should run (optional, required if interval is not given)
				// 'interval' => '15', // the interval, in minutes, that this cron task should run (optional, required if time is not given)
				'enabled' => 1, // 1 if this cron task is enabled, 0 otherwise (optional, default 1)
			);
			$this->CronTasks->addTaskRun($task_id, $task_run);
		}
	}

    public function upgrade($current_version) {
		// Perform upgrade logic       
    }

	public function uninstall($module_id, $last_instance) {
		// Perform uninstall logic, such as deleting cron tasks
		Loader::loadModels($this, ['CronTasks']);

		// Retrieve the cron task run for this company
		$cron_task_run = $this->CronTasks->getTaskRunByKey('task_one', 'my_module', false, 'module');

		if ($last_instance) {
			// Delete all trace of this module, such as cron tasks
            // Remove the cron task altogether
            $cron_task = $this->CronTasks->getByKey('task_one', 'my_module', 'module');
            if ($cron_task) {
                $this->CronTasks->deleteTask($cron_task->id, 'module', 'my_module');
            }
		}

        // Remove individual task run
        if ($cron_task_run) {
            $this->CronTasks->deleteTaskRun($cron_task_run->task_run_id);
        }
	}
...
}

cron($key)

The cron() method is called whenever a cron task (identified by $key and) registered for the module is ready to be run. This is similar to plugin cron tasks.

Info
titleCreate your cron tasks first

You must create your cron tasks during an install() or upgrade() in order for them to exist in Blesta and be run automatically. See install/upgrade/uninstall above for an example.


Code Block
languagephp
class MyModule extends Module {
...
	public function cron($key) {
		switch ($key) {
			case "task_one":
				// Perform any action the module should do based on the cron task
				break;
	}

    public function upgrade($current_version) {
		// Perform upgrade logic       
    }

	public function uninstall($module_id, $last_instance) {
		// Perform uninstall logic

		if ($last_instance) {
			// Delete all trace of this module
		}
	}
...
}

getLogo()

The getLogo() method returns the relative path (within the module's directory) to the logo used to represent the module. The default value is views/default/images/logo.png. This translates to /install_dir/components/modules/my_module/views/default/images/logo.png

...