hosting_example.service.inc

Example service implementation for the hosting front end.

Classes

Namesort descending Description
hostingService_example The base service type class , registered with hook_hosting_service_type.
hostingService_example_basic

File

example/hosting_example.service.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Example service implementation for the hosting front end.
  5. *
  6. * @addtogroup hostingserviceapi
  7. * @{
  8. */
  9. /**
  10. * The base service type class , registered with hook_hosting_service_type.
  11. */
  12. class hostingService_example extends hostingService {
  13. /**
  14. * the value stored in the service column of hosting_service table.
  15. */
  16. public $service = 'example';
  17. }
  18. /**
  19. * An implementation of the example service type, registered with hook_hosting_service.
  20. */
  21. class hostingService_example_basic extends hostingService_example {
  22. /**
  23. * the value stored in the type column of the hosting_service table.
  24. */
  25. public $type = 'basic';
  26. /**
  27. * this service needs to have a port specified for it.
  28. */
  29. public $has_port = TRUE;
  30. /**
  31. * the default value for the port input.
  32. */
  33. function default_port() {
  34. return 12345;
  35. }
  36. /**
  37. * this service needs to be restarted with a shell command.
  38. */
  39. public $has_restart_cmd = TRUE;
  40. /**
  41. * The default value for the restart command input.
  42. */
  43. function default_restart_cmd() {
  44. return "/usr/bin/true";
  45. }
  46. /**
  47. * node operations
  48. */
  49. /**
  50. * Load associated values for the service.
  51. *
  52. * In this example we will use the variable system to retrieve values.
  53. */
  54. function load() {
  55. // REMEMBER TO CALL THE PARENT!
  56. parent::load();
  57. $this->example_field = variable_get('hosting_example_field_' . $this->server->nid, 'default value');
  58. /**
  59. * Although this example does not have it's own tables, we provide some utitilty functions
  60. * for use in this method.
  61. *
  62. * If this example used it's own tables, we could use the mergeData method below to merge in the
  63. * results automatically, instead of iterating through the results ourself.
  64. */
  65. // $this->mergeData("SELECT example_field FROM {hosting_example} WHERE vid = :vid", array(':vid' => $this->server->vid));
  66. }
  67. /**
  68. * Display settings on the server node page.
  69. *
  70. * Modify the reference passed to the method to add additional implementation
  71. * specific fields to be displayed.
  72. *
  73. * @param
  74. * A reference to the associative array of the subsection of the page
  75. * reserved for this service implementation.
  76. */
  77. function view(&$render) {
  78. // REMEMBER TO CALL THE PARENT!
  79. parent::view($render);
  80. $render['example_field'] = array(
  81. '#type' => 'item',
  82. '#title' => t('Example field'),
  83. // Remember to pass the display through filter_xss!
  84. '#markup' => filter_xss($this->example_field),
  85. );
  86. }
  87. /**
  88. * Extend the server node form.
  89. *
  90. * Modify the reference passed to the method to add additional implementation
  91. * specific fields to be stored and managed.
  92. *
  93. * @param
  94. * A reference to the associative array of the subsection of the form
  95. * reserved for this service implementation.
  96. */
  97. function form(&$form) {
  98. // REMEMBER TO CALL THE PARENT!
  99. parent::form($form);
  100. $form['example_field'] = array(
  101. '#type' => 'textfield',
  102. '#title' => t('Example field'),
  103. '#description' => t('An example field for the user to fill in.'),
  104. '#size' => 40,
  105. '#default_value' => ($this->example_field) ? $this->example_field : 'default value',
  106. '#maxlength' => 64,
  107. '#weight' => 5,
  108. );
  109. }
  110. /**
  111. * Validate a form submission.
  112. */
  113. function validate(&$node, &$form) {
  114. // REMEMBER TO CALL THE PARENT!
  115. parent::validate($node, $form);
  116. if (sizeof($this->example_field) > 30) {
  117. form_set_error('example_field', t("The example string must not be longer than 30 characters"));
  118. }
  119. }
  120. /**
  121. * Insert a record into the database.
  122. *
  123. * Called by hosting_server_hook_insert().
  124. *
  125. * The values associated with this implementation have already
  126. * been set as properties of $this object, so we now need to
  127. * save them.
  128. *
  129. * For this example we will use the variables table, but you should
  130. * create your own tables with an install file and hook_schema.
  131. */
  132. function insert() {
  133. // REMEMBER TO CALL THE PARENT!
  134. parent::insert();
  135. variable_set('hosting_example_field_' . $this->server->nid, $this->example_field);
  136. }
  137. /**
  138. * Update a record in the database.
  139. *
  140. * Called by hosting_server_hook_update().
  141. *
  142. * For this example we will use the variables table, but you should
  143. * create your own tables with an install file and hook_schema.
  144. */
  145. function update() {
  146. // REMEMBER TO CALL THE PARENT!
  147. parent::update();
  148. variable_set('hosting_example_field_' . $this->server->nid, $this->example_field);
  149. }
  150. /**
  151. * Delete a record from the database, based on server node.
  152. */
  153. function delete() {
  154. // REMEMBER TO CALL THE PARENT!
  155. parent::delete();
  156. variable_del('hosting_example_field_' . $this->server->nid);
  157. }
  158. /**
  159. * Delete a specific reivision from the database.
  160. *
  161. * Not relevant in our example but shown anyway.
  162. */
  163. function delete_revision() {
  164. // REMEMBER TO CALL THE PARENT!
  165. parent::delete_revision();
  166. }
  167. /**
  168. * Pass values to the provision backend when we call provision-save.
  169. *
  170. * By selecting this type we already pass the '--example_service_type=basic' option
  171. * to the command, which will load the matching provisionService class in the backend.
  172. *
  173. * This backend class will be responsible for receiving and reacting to the options
  174. * passed here.
  175. *
  176. * @ingroup backend-frontend-IPC
  177. */
  178. public function context_options($task_type, $ref_type, &$task) {
  179. // REMEMBER TO CALL THE PARENT!
  180. parent::context_options($task_type, $ref_type, $task);
  181. $task->context_options['example_field'] = $this->example_field;
  182. }
  183. }
  184. /**
  185. * @} End of "addtogroup hostingserviceapi".
  186. */

Related topics