AbstractHookIntegrationController.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220
  1. <?php
  2. namespace ModulesGarden\ProxmoxAddon\Core\Hook;
  3. /**
  4. * a base class for every hook integration controller
  5. */
  6. abstract class AbstractHookIntegrationController
  7. {
  8. /** @var string
  9. * allowed integration type
  10. */
  11. const TYPE_APPEND = 'append';
  12. /** @var string
  13. * allowed integration type
  14. */
  15. const TYPE_PREPEND = 'prepend';
  16. /** @var string
  17. * allowed integration type
  18. */
  19. const TYPE_REPLACE = 'replace';
  20. /** @var string
  21. * allowed integration type
  22. */
  23. const TYPE_CUSTOM = 'custom';
  24. /** @var string
  25. * allowed integration type
  26. */
  27. const TYPE_AFTER = 'after';
  28. /** @var string
  29. * allowed integration type
  30. */
  31. const TYPE_BEFORE = 'before';
  32. /** @var null|string
  33. * determines the file name to be integrated which
  34. * if null, will not integrate
  35. */
  36. protected $fileName = null;
  37. /** @var null|array
  38. * determines the $_REQUEST params for which the integration should be done
  39. * if null, this condition will be skipped
  40. */
  41. protected $requestParams = null;
  42. /** @var null|callable
  43. * a callback for the admin area controller
  44. */
  45. protected $controllerCallback = null;
  46. /** @var null|string
  47. * a jQuery selector determines a DOM object to which the Vue App should be added
  48. * e.g '#exampleDiv', '.btn-container:first'
  49. */
  50. protected $jqSelector = null;
  51. /** @var string
  52. * states what type of integration should be used
  53. */
  54. protected $integrationType = self::TYPE_APPEND;
  55. /** @var null|string
  56. * a js function name, just for custom integration type
  57. */
  58. protected $jsFunctionName = null;
  59. /**
  60. * @param null|string $fileName
  61. */
  62. public function setFileName($fileName)
  63. {
  64. if ((is_string($fileName) && $fileName !== '') || $fileName === null)
  65. {
  66. $this->fileName = $fileName;
  67. }
  68. return $this;
  69. }
  70. /**
  71. * @param null|array $requestParams
  72. */
  73. public function setRequestParams($requestParams = null)
  74. {
  75. if (is_array($requestParams) || $requestParams === null)
  76. {
  77. $this->requestParams = $requestParams;
  78. }
  79. return $this;
  80. }
  81. /**
  82. * @param callable|null $controllerCallback
  83. */
  84. public function setControllerCallback($controllerCallback)
  85. {
  86. $this->controllerCallback = $controllerCallback;
  87. }
  88. /**
  89. * @param null|string $jqSelector
  90. */
  91. public function setJqSelector($jqSelector)
  92. {
  93. if ((is_string($jqSelector) && $jqSelector !== '') || $jqSelector === null)
  94. {
  95. $this->jqSelector = $jqSelector;
  96. }
  97. }
  98. /**
  99. * @param string $type
  100. * @return self::class
  101. */
  102. public function setIntegrationType($type = null)
  103. {
  104. if (in_array($type, $this->getAvailableIntegrationTypes()))
  105. {
  106. $this->integrationType = $type;
  107. }
  108. return $this;
  109. }
  110. /**
  111. * @param null|string $jsFunctionName
  112. */
  113. public function setJsFunctionName($jsFunctionName)
  114. {
  115. if ($jsFunctionName !== '' && is_string($jsFunctionName))
  116. {
  117. $this->jsFunctionName = $jsFunctionName;
  118. }
  119. return $this;
  120. }
  121. /**
  122. * @return null|string
  123. */
  124. public function getJsFunctionName()
  125. {
  126. return $this->jsFunctionName;
  127. }
  128. /**
  129. * @return null|string
  130. */
  131. public function getFileName()
  132. {
  133. return $this->fileName;
  134. }
  135. /**
  136. * @return null|array
  137. */
  138. public function getRequestParams()
  139. {
  140. return $this->requestParams;
  141. }
  142. /**
  143. * @return callable|null
  144. */
  145. public function getControllerCallback()
  146. {
  147. return $this->controllerCallback;
  148. }
  149. /**
  150. * @return null|string
  151. */
  152. public function getJqSelector()
  153. {
  154. return $this->jqSelector;
  155. }
  156. /**
  157. * @return string
  158. */
  159. public function getIntegrationType()
  160. {
  161. return $this->integrationType;
  162. }
  163. /**
  164. * @return array
  165. * returns a list of integration types possible to use
  166. */
  167. public function getAvailableIntegrationTypes()
  168. {
  169. return [
  170. self::TYPE_APPEND,
  171. self::TYPE_PREPEND,
  172. self::TYPE_REPLACE,
  173. self::TYPE_CUSTOM,
  174. self::TYPE_AFTER,
  175. self::TYPE_BEFORE
  176. ];
  177. }
  178. /**
  179. * a wrapper to set up integration process quickly
  180. */
  181. public function addIntegration($fileName = null, $requestParams = null, $controllerCallback = null, $jqSelector = null, $integrationType = null, $jsFunctionName = null)
  182. {
  183. $this->setFileName($fileName);
  184. $this->setRequestParams($requestParams);
  185. $this->setControllerCallback($controllerCallback);
  186. $this->setJqSelector($jqSelector);
  187. $this->setIntegrationType($integrationType);
  188. $this->setJsFunctionName($jsFunctionName);
  189. }
  190. }