Versions Compared

Key

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

...

Code Block
languagephp
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."Language::_("MyModule.!error.api.response", true))));
			$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);
	}
...
}

...

Code Block
languagephp
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."Language::_("MyModule.!error.api.response", true))));
			$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);
	}
...
}

...

Code Block
languagephp
class MyModule extends Module {
...
	public function deletePackage($package) {
		// Attempt to delete a package remotely (this is dependent on the module's API, omitted here)
		$response = $this->apiCall($package);
		
		if (isset($response->status) && !$response->status)
			$this->Input->setErrors(array('api' => array('response' => "The request could not be performed."Language::_("MyModule.!error.api.response", true))));
	}

	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);
	}
...
}

...

The manageModule() method returns HTML content for the manage module page for the given module. Any post data submitted will be passed as by reference in $vars.

Code Block
languagephp
class MyModule extends Module {
...
	public function manageModule($module, array &$vars) {
		// Load the view into this object, so helpers can be automatically added to the view
        $this->view = new View("manage", "default");
        $this->view->base_uri = $this->base_uri;
        $this->view->setDefaultView("components" . DS . "modules" . DS . "my_module" . DS);
        
        // Load the helpers required for this view
        Loader::loadHelpers($this, array("Form", "Html", "Widget"));
        $this->view->set("module", $module);
        
        return $this->view->fetch();
	}
...
}

...

The manageAddRow() method returns HTML content for the add module row page. Any post data submitted will be passed as by reference in $vars.

Code Block
languagephp
class MyModule extends Module {
...
	public function manageAddRow($module, array &$vars) {
		// Load the view into this object, so helpers can be automatically added to the view
        $this->view = new View("manageadd_row", "default");
        $this->view->base_uri = $this->base_uri;
        $this->view->setDefaultView("components" . DS . "modules" . DS . "my_module" . DS);
        
        // Load the helpers required for this view
        Loader::loadHelpers($this, array("Form", "Html", "Widget"));
        $this->view->set("vars", (object)$vars);
        
        return $this->view->fetch();
	}
...
}

...

manageEditRow($module_row, array &$vars)

 

addModuleRow(array &$vars)

 

...

The manageEditRow() method returns HTML content for the edit module row page given the module row to update. Any post data submitted will be passed by reference in $vars.

Code Block
languagephp
class MyModule extends Module {
...
	public function manageEditRow($module_row, array

...

 &$vars) {
		// Load the view into this object, so helpers can be automatically added to the view
        $this->view = new View("edit_row", "default");
        $this->view->base_uri = $this->base_uri;
        $this->view->setDefaultView("components" . DS . "modules" . DS . "my_module" . DS);
        
		// Set initial module row meta fields for vars
		if (empty($vars))
			$vars = $module_row->meta;

		// Load the helpers required for this view
        Loader::loadHelpers($this, array("Form", "Html", "Widget"));
        $this->view->set("vars", (object)$vars);
        
        return $this->view->fetch();
	}
...
}

 

addModuleRow(array &$vars)

This method attempts to add a module row given the input vars, and sets any Input errors on failure. This method returns meta fields as an array containing an array of key=>value fields for each meta field and its value, as well as whether the value should be encrypted.

Code Block
languagephp
class MyModule extends Module {
...
	public function addModuleRow(array &$vars) {
		// Set a white list of fields to add to the module row
		$fields = array("field1", "password");
		$encrypted_fields = array("password");

		// Set any rules for the module row fields
		$rules = array(
			'field1' => array(
				'empty' => array(
					'rule' => "isEmpty",
					'negate' => true,
					'message' => Language::_("MyModule.!error.field1.empty", true)
				)
			),
			'password' => array(
				'empty' => array(
					'rule' => "isEmpty",
					'negate' => true,
					'message' => Language::_("MyModule.!error.password.empty", true)
				)
			)
		);

		$this->Input->setRules($rules);
		
		// Determine whether the input validates
		if ($this->Input->validates($vars)) {
			// Add each field
			$meta = array();
			foreach ($vars as $key => $value) {
				if (in_array($key, $fields)) {
					$meta[] = array(
						'key' => $key,
						'value' => $value,
						'encrypted' => in_array($key, $encrypted_fields) ? 1 : 0
					);
				}
			}

			return $meta;
		}
	}
...
}

 

editModuleRow($module_row, array &$vars)

This method attempts to update a module row given the input vars and the module row, and sets any Input errors on failure. This method returns meta fields as an array containing an array of key=>value fields for each meta field and its value, as well as whether the value should be encrypted.

This method is very similar to addModuleRow().

deleteModuleRow($module_row)

This method attempts to delete a module row from a remote server. It may set Input errors on failure.

Code Block
languagephp
class MyModule extends Module {
...
	public function deleteModuleRow($module_row) {
		// Attempt to delete a module row remotely (this is dependent on the module's API, omitted here)
		$response = $this->apiCall($module_row);
		
		if (isset($response->status) && !$response->status)
			$this->Input->setErrors(array('api' => array('response' => Language::_("MyModule.!error.api.response", true))));
	}

	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);
	}
...
}

 

getGroupOrderOptions()

This method returns an array of service delegation order methods. For example, if multiple module rows exist for a module, you may want to provide an option to assign new services to the module row with the least number of services already assigned to it.

Code Block
languagephp
class MyModule extends Module {
...
	public function getGroupOrderOptions() {
		return array('first' => Language::_("MyModule.order_options.first", true);
	}
...
}

 

selectModuleRow($module_group_id)

This method determines which module row should be used to provision a service based on the order method set for the given group. The module row ID of the chosen module row is returned.

Code Block
languagephp
class MyModule extends Module {
...
	public function selectModuleRow($module_group_id) {
		// Load the ModuleManager
		if (!isset($this->ModuleManager))
			Loader::loadModels($this, array("ModuleManager"));
		
		// Select the group associated with the given module group ID
		if (($group = $this->ModuleManager->getGroup($module_group_id))) {
			// Choose the module row to use
			switch ($group->add_order) {
				default:
				case "first":
					// Return the first row encountered
					foreach ($group->rows as $row)
						return $row->id;
					break;
			}
		}
		return 0;
	}
...
}

 

getPackageFields($vars=null)

This method returns a ModuleFields object containing all fields used when adding or editing a package, including any javascript that can be executed when the page is rendered with those fields. Any post data submitted will be passed in $vars.

Code Block
languagephp
class MyModule extends Module {
...
	public function getPackageFields($vars=null) {
		// Load any helpers required to build the fields
		Loader::loadHelpers($this, array("Html"));
		
		// Set any module fields
		$fields = new ModuleFields();
		$fields->setHtml("
			<script type=\"text/javascript\">
				$(document).ready(function() {
					// Re-fetch module options
					$('#mymodule_group').change(function() {
						fetchModuleOptions();
					});
				});
			</script>
		");

		// Fetch all packages available for the given server or server group
		$module_row = null;
		if (isset($vars->module_group) && $vars->module_group == "") {
			// Set a module row if one is given
			if (isset($vars->module_row) && $vars->module_row > 0)
				$module_row = $this->getModuleRow($vars->module_row);
			else {
				// Set the first module row of any that exist
				$rows = $this->getModuleRows();
				if (isset($rows[0]))
					$module_row = $rows[0];
				unset($rows);
			}
		}
		else {
			// Set the first module row from the list of servers in the selected group
			$rows = $this->getModuleRows($vars->module_group);
			if (isset($rows[0]))
				$module_row = $rows[0];
			unset($rows);
		}

		// Build any HTML fields
		$select_options = array('one' => "One", 'two', => "Two");
		$field = $fields->label(Language::_("MyModule.package_fields.field1", true), "mymodule_field");
		$field->attach($fields->fieldSelect("meta[field]", $select_options, $this->Html->ifSet($vars->meta['field']), array('id' => "mymodule_field")));
		$fields->setField($field);

		return $fields;
	}
...
}

 

deleteModuleRow($module_row)

 

getGroupOrderOptions()

 

selectModuleRow($module_group_id)

 

...

 

getEmailTags()

 

getAdminAddFields($package, $vars=null)

...

 

getClientTabs($package)

 

Language::_("MyModule.!error.api.response", true)