You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 5 Next »

 

Required Methods

The following methods are required to be implemented for each module.

getName()

The getName() method simply returns the name of the method. It's always best to define any language in your module using language files (see Translating Blesta for more information).

class MyModule extends Module {
...
	public function getName() {
		return Language::_("MyModule.name", true);
	}
...
}

getVersion()

This method must return the current version number of the module. Upon installation or upgrade, Blesta records the current code version so that it can tell when an upgrade/downgrade is available. The version number should be compatible with PHP's version_compare() function.

class MyModule extends Module {
	const VERSION = "1.0.0";	
...
	public function getVersion() {
		return self::VERSION;
	}
...

}

getAuthors()

This method returns an array containing information about the authors of the module.

class MyModule extends Module {
    private static $authors = array(array('name' => "Phillips Data, Inc.", 'url' =>"http://www.blesta.com"));
...
	public function getAuthors() {
		return self::$authors;
	}
...
}

getServiceName($service)

Given a particular service, this method should return the label used to identify the service. For example, if the module were representing VOIP services, the value returned by this method might be the phone number associated with the VOIP service.

class MyModule extends Module {
...
	public function getServiceName($service) {
		$key = "phone_number";
        foreach ($service->fields as $field) {
            if ($field->key == $key)
                return $field->value;
        }
        return null;
	}
...
}

moduleRowName()

The moduleRowName() method returns the value used to represent a module row. For example, if the module were representing shared hosting services and each module row were a physical server the value returned by this method might be "Server".

class MyModule extends Module {
...
	public function moduleRowName() {
		return Language::_("MyModule.module_row", true);
	}
...
}

moduleRowNamePlural()

A plural representation of the moduleRowName() method (e.g. "Servers").

class MyModule extends Module {
...
	public function moduleRowNamePlural() {
		return Language::_("MyModule.module_row_plural", true);
	}
...
}

moduleGroupName()

The moduleGroupName() method returns the value used to represent a module group.

class MyModule extends Module {
...
	public function moduleGroupName() {
		return Language::_("MyModule.module_group", true);
	}
...
}

moduleRowMetaKey()

This method returns a string identifying the module meta key value that is used to identify module rows. This is used to identify module rows from one another.  For example, servers may be identified by their host name.

class MyModule extends Module {
...
	public function moduleRowMetaKey() {
		return "host_name";
	}
...
}

 

Optional Methods

The methods below are optional, but may be required to implement a module of any utility.

install/upgrade/uninstall()

The methods are invoked when the module is installed, upgraded, or uninstalled respectively.

class MyModule extends Module {
...
	public function install() {
		// Perform install logic
	}

    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

class MyModule extends Module {
...
	public function getLogo() {
		return "some/path/to/my/logo.png";
	}
...
}

validateService($package, array $vars=null)

The validateService() method performs any input validation against the selected package and vars, and sets any input errors. This is typically called before attempting to provision a service within the addService() or editService() methods. It returns a boolean value indicating whether the given input is valid.

class MyModule extends Module {
...
	public function validateService($package, array $vars=null) {
		// Set any input rules to validate against
		$rules = array(
			'mymodule_field' => array(
				'format' => array(
					'rule' => "isEmpty",
					'negate' => true,
					'message' => Language::_("MyModule.!error.mymodule_field.format", true)
				);
			)
		);

		$this->Input->setRules($rules);

		// Determine whether the input validates
		return $this->Input->validates($vars);
	}
...
}

 

addService($package, array $vars=null, $parent_package=null, $parent_service=null, $status="pending")

This method attempts to add a service given the package and input vars, as well as the intended status. If this service is an addon service, the parent package will be given. The parent service will also be given if the parent service has already been provisioned. This method returns an array containing an array of key=>value fields for each service field and its value, as well as whether the value should be encrypted.

class MyModule extends Module {
...
	public function addService($package, array $vars=null, $parent_package=null, $parent_service=null, $status="pending") {
		// Get the module row used for this service
		$row = $this->getModuleRow();
		
		// Attempt to validate the input and return on failure
		$this->validateService($package, $vars);
		if ($this->Input->errors())
			return;

		// Only provision the service remotely if 'use_module' is true
		if (isset($vars['use_module']) && $vars['use_module'] == "true") {
			// Log the input being sent remotely, careful to mask any sensitive information
			$masked_params = $params
			$masked_params['mymodule_field'] = "***";

			// Set the URL to where the remote request is being sent (assuming 'host_name' is a valid module row meta field)
			$remote_url = $row->meta->host_name;
			$this->log($remote_url . "|api_command", serialize($masked_params), "input", true);

			// Provision the service remotely (this is dependent on the module's API, omitted here)
			$response = $this->makeRequest($params);

			// Return on error
			if ($this->Input->errors())
				return;
		}

		// Return the service fields
		return array(
			array(
				'key' => "mymodule_field",
				'value' => (isset($vars['mymodule_field']) ? $vars['mymodule_field'] : null),
				'encrypted' => 0
			)
		);
	}

	private function makeRequest($params) {
		// Get the module row used for this service
		$row = $this->getModuleRow();

		// Perform the remote request (this is dependent on the module's API, omitted here)
		$response = $this->apiCall($params);

		// Retrieve the response from the module and evaluate its result as true/false, setting any input errors
		$success = true;
		if (isset($response->status) && !$response->status) {
			$this->Input->setErrors(array('api' => array('response' => "The request could not be performed.")));
			$success = false;
		}
		
		// Log the response
		$this->log($row->meta->host_name, $response, "output", $success);

		// Return the result
		if (!$success)
			return;
		return $response;
	}

	private function apiCall($params) {
		// Make the API call to the module (this is dependent on the module's API, omitted here)
		return (object)array('status' => false);
	}
...
}

 

editService($package, $service, array $vars=array(), $parent_package=null, $parent_service=null)

This method attempts to update an existing service given the package, the service, and any input vars. If this service is an addon service, the parent package will be given. The parent service will also be given if the parent service has already been provisioned. This method returns an array containing an array of key=>value fields for each service field and its value, as well as whether the value should be encrypted.

This method is very similar to addService().

cancelService($package, $service, $parent_package=null, $parent_service=null)

This method attempts to cancel an existing service given the package and the service. If this service is an addon service, the parent package will be given. The parent service will also be given if the parent service has already been provisioned. This method may return null, or the service fields as an array containing an array of key=>value fields for each service field and its value, as well as whether the value should be encrypted.

class MyModule extends Module {
...
	public function cancelService($package, $service, $parent_package=null, $parent_service=null) {
		// Get the module row used for this service
		if (($row = $this->getModuleRow())) {
			// Format the list of service fields as an object
			$service_fields = $this->serviceFieldsToObject($service->fields);

			// Set the URL to where the remote request is being sent (assuming 'host_name' is a valid module row meta field)
			$remote_url = $row->meta->host_name;
			$this->log($remote_url . "|api_command", serialize($service_fields), "input", true);

			// Provision the service remotely (this is dependent on the module's API, omitted here)
			$response = $this->makeRequest($service_fields);
		}
		return null;
	}

	private function makeRequest($params) {
		// Get the module row used for this service
		$row = $this->getModuleRow();

		// Perform the remote request (this is dependent on the module's API, omitted here)
		$response = $this->apiCall($params);

		// Retrieve the response from the module and evaluate its result as true/false, setting any input errors
		$success = true;
		if (isset($response->status) && !$response->status) {
			$this->Input->setErrors(array('api' => array('response' => "The request could not be performed.")));
			$success = false;
		}
		
		// Log the response
		$this->log($row->meta->host_name, $response, "output", $success);

		// Return the result
		if (!$success)
			return;
		return $response;
	}

	private function apiCall($params) {
		// Make the API call to the module (this is dependent on the module's API, omitted here)
		return (object)array('status' => false);
	}
...
}

 

suspendService($package, $service, $parent_package=null, $parent_service=null)

 

 

unsuspendService($package, $service, $parent_package=null, $parent_service=null)

 

renewService($package, $service, $parent_package=null, $parent_service=null)

 

changeServicePackage($package_from, $package_to, $service, $parent_package=null, $parent_service=null)

 

addPackage(array $vars=null)

 

editPackage($package, array $vars=null)

 

deletePackage($package)

 

manageModule($module, array &$vars)

 

manageAddRow(array &$vars)

 

manageEditRow($module_row, array &$vars)

 

addModuleRow(array &$vars)

 

editModuleRow($module_row, array &$vars)

 

deleteModuleRow($module_row)

 

getGroupOrderOptions()

 

selectModuleRow($module_group_id)

 

getPackageFields($vars=null)

 

getEmailTags()

 

getAdminAddFields($package, $vars=null)

 

getClientAddFields($package, $vars=null)

 

getAdminEditFields($package, $vars=null)

 

getAdminServiceInfo($service, $package)

 

getClientServiceInfo($service, $package)

 

getAdminTabs($package)

 

getClientTabs($package)

 

  • No labels