hosting_server.service.inc

Define the base Hosting service class.

Classes

Namesort descending Description
hostingService

File

server/hosting_server.service.inc
View source
  1. <?php
  2. /**
  3. * @file
  4. * Define the base Hosting service class.
  5. */
  6. class hostingService {
  7. public $server;
  8. public $available = 0;
  9. protected $has_restart_cmd = FALSE;
  10. protected $has_port = FALSE;
  11. function __construct($node, $values = NULL) {
  12. self::save($node);
  13. self::setValues($values);
  14. }
  15. public function has_port() {
  16. return $this->has_port;
  17. }
  18. public function load() {
  19. $this->mergeData("SELECT port, restart_cmd, available
  20. FROM {hosting_service}
  21. WHERE vid = :vid
  22. AND type = :type",
  23. array(
  24. ':vid' => $this->server->vid,
  25. ':type' => $this->type
  26. )
  27. );
  28. }
  29. protected function mergeData($query, $args = array()) {
  30. $result = db_query($query, $args)->fetchAssoc();
  31. $this->setValues($result);
  32. }
  33. public function setValues($record = NULL) {
  34. if (is_array($record)) {
  35. foreach ($record as $key => $value) {
  36. $this->{$key} = $value;
  37. }
  38. }
  39. }
  40. public function save($node) {
  41. if (isset($node->nid)) {
  42. $this->server = new stdClass();
  43. $this->server->nid = $node->nid;
  44. $this->server->vid = $node->vid;
  45. $this->server->title = $node->title;
  46. }
  47. }
  48. public function default_restart_cmd() {
  49. return '';
  50. }
  51. public function default_port() {
  52. return 0;
  53. }
  54. public function insert() {
  55. $id = db_insert('hosting_service')
  56. ->fields(array(
  57. 'nid' => $this->server->nid,
  58. 'vid' => $this->server->vid,
  59. 'service' => $this->service,
  60. 'type' => $this->type,
  61. 'port' => $this->port,
  62. 'restart_cmd' => isset($this->restart_cmd) ? $this->restart_cmd : '',
  63. 'available' => $this->available,
  64. ))
  65. ->execute();
  66. }
  67. public function update() {
  68. $this->delete_revision();
  69. $this->insert();
  70. }
  71. public function delete() {
  72. db_delete('hosting_service')
  73. ->condition('service', $this->service)
  74. ->condition('nid', $this->server->nid)
  75. ->execute();
  76. }
  77. public function delete_revision() {
  78. db_delete('hosting_service')
  79. ->condition('service', $this->service)
  80. ->condition('vid', $this->server->vid)
  81. ->execute();
  82. }
  83. public function form(&$form) {
  84. if ($this->has_restart_cmd) {
  85. $form['restart_cmd'] = array(
  86. '#type' => 'textfield',
  87. '#title' => t('Restart command'),
  88. '#required' => !empty($this->available),
  89. '#description' => t('The command to run to restart this service.'),
  90. '#default_value' => (isset($this->restart_cmd)) ? $this->restart_cmd : $this->default_restart_cmd(),
  91. '#size' => 40,
  92. '#maxlength' => 255,
  93. '#weight' => -7,
  94. );
  95. }
  96. else {
  97. $form['restart_cmd'] = array(
  98. '#type' => 'value',
  99. '#value' => NULL,
  100. );
  101. }
  102. if ($this->has_port) {
  103. $form['port'] = array(
  104. '#type' => 'textfield',
  105. '#title' => t('Port'),
  106. '#required' => !empty($this->available),
  107. '#size' => 40,
  108. '#default_value' => isset($this->port) ? $this->port : $this->default_port(),
  109. '#description' => t("The port that this service is listening on."),
  110. '#maxlength' => 255,
  111. '#weight' => -8,
  112. );
  113. }
  114. else {
  115. $form['port'] = array(
  116. '#type' => 'value',
  117. '#value' => '0',
  118. );
  119. }
  120. }
  121. public function view(&$render) {
  122. if ($this->has_restart_cmd) {
  123. $render['restart_cmd'] = array(
  124. '#type' => 'item',
  125. '#title' => t('Restart command'),
  126. '#markup' => filter_xss($this->restart_cmd),
  127. );
  128. }
  129. if ($this->has_port) {
  130. $render['port'] = array(
  131. '#type' => 'item',
  132. '#title' => t('Port'),
  133. '#markup' => filter_xss($this->port),
  134. );
  135. }
  136. }
  137. public function validate(&$node, &$form) {
  138. if ($this->has_port) {
  139. if ((int) $this->port <= 0) {
  140. form_set_error('port', t("The port you specify must be a number."));
  141. }
  142. }
  143. }
  144. public function context_options($task_type, $ref_type, &$task) {
  145. $task->context_options[$this->service . '_service_type'] = $this->type;
  146. if ($this->has_restart_cmd) {
  147. $task->context_options[$this->service . '_restart_cmd'] = $this->restart_cmd;
  148. }
  149. if ($this->has_port) {
  150. $task->context_options[$this->service . '_port'] = $this->port;
  151. }
  152. }
  153. public function context_import($context) {
  154. $this->available = 1;
  155. if ($this->has_port) {
  156. $this->port = $context->{$this->service . '_port'};
  157. }
  158. if ($this->has_restart_cmd) {
  159. $this->restart_cmd = $context->{$this->service . '_restart_cmd'};
  160. }
  161. }
  162. }