EditRessourceDataProvider.php 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Providers;
  3. use ThurData\Servers\KerioEmail\App\Enums\Kerio;
  4. use ThurData\Servers\KerioEmail\App\Helpers\KerioManager;
  5. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\Ressource;
  7. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Models\ClassOfService;
  8. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository;
  9. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Repository\ClassOfServices;
  10. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Response;
  11. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Services\Update\UpdateRessource;
  12. use ThurData\Servers\KerioEmail\App\Services\ConfigurableOptions\Strategy\Types\ClassOfServicesOptions;
  13. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  14. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  15. use ThurData\Servers\KerioEmail\Core\UI\ResponseTemplates\HtmlDataJsonResponse;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\Forms\DataProviders\BaseDataProvider;
  17. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  18. /**
  19. *
  20. * Created by PhpStorm.
  21. * User: ThurData
  22. * Date: 18.09.19
  23. * Time: 09:35
  24. * Class EditRessourceDataProvider
  25. */
  26. class EditRessourceDataProvider extends BaseDataProvider
  27. {
  28. /**
  29. *
  30. */
  31. public function read()
  32. {
  33. $fields = array(
  34. "id",
  35. "name",
  36. "description",
  37. "type",
  38. "isEnabled",
  39. "manager"
  40. );
  41. $cond = array(
  42. "fieldName" => "id",
  43. "comparator" => "Eq",
  44. "value" => $this->actionElementId
  45. );
  46. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  47. try {
  48. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  49. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  50. } catch (KerioApiException $error) {
  51. logModuleCall(
  52. 'kerioEmail',
  53. __FUNCTION__,
  54. $error,
  55. 'Debug Error',
  56. $error->getMessage()
  57. );
  58. return ['error' => $error->getMessage()];
  59. }
  60. try {
  61. $ressource = $api->getResources($fields,$domainID,[ $cond ]);
  62. } catch (KerioApiException $error) {
  63. logModuleCall(
  64. 'kerioEmail',
  65. __FUNCTION__,
  66. $error,
  67. 'Debug Error',
  68. $error->getMessage()
  69. );
  70. return ['error' => $error->getMessage()];
  71. }
  72. $attr = array(
  73. "id",
  74. "loginName",
  75. "isEnabled");
  76. try {
  77. $users = $api->getUsers($attr,$domainID);
  78. } catch (KerioApiException $error) {
  79. logModuleCall(
  80. 'kerioEmail',
  81. __FUNCTION__,
  82. $error,
  83. 'Debug Error',
  84. $error->getMessage()
  85. );
  86. return ['error' => $error->getMessage()];
  87. }
  88. $api->logout();
  89. $this->availableValues['manager'][$ressource[0]['manager']['id']] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  90. foreach($users as $user) {
  91. if($user['isEnabled']){
  92. $this->availableValues['manager'][ $user['id']] = $user['loginName'] . '@' . $this->getWhmcsParamByKey('domain');
  93. }
  94. }
  95. $lang = di('lang');
  96. $this->data['id'] = $ressource[0]['id'];
  97. $this->data['name'] = $ressource[0]['name'];
  98. $this->data['domain'] = $this->getWhmcsParamByKey('domain');
  99. $this->data['status'] = $ressource[0]['isEnabled'] == true ? Kerio::ACC_STATUS_ACTIVE : Kerio::ACC_STATUS_CLOSED;
  100. $this->data['type'] = $ressource[0]['type'] === 'Room' ? Kerio::RES_TYPE_LOCATION : Kerio::RES_TYPE_EQUIPMENT;
  101. $this->data['description'] = $ressource[0]['description'];
  102. $this->data['manager'] = $ressource[0]['manager']['name'] . '@' . $ressource[0]['manager']['domainName'];
  103. $this->availableValues['status'] = [
  104. Kerio::ACC_STATUS_ACTIVE => $lang->absoluteT('kerio','account','status','active'),
  105. Kerio::ACC_STATUS_CLOSED => $lang->absoluteT('kerio','account','status','closed'),
  106. ];
  107. $this->availableValues['type'] = [
  108. Kerio::RES_TYPE_LOCATION => $lang->absoluteT('kerio','ressource','type','location'),
  109. Kerio::RES_TYPE_EQUIPMENT => $lang->absoluteT('kerio','ressource','type','equipment')
  110. ];
  111. }
  112. /**
  113. * @return HtmlDataJsonResponse
  114. */
  115. public function update()
  116. {
  117. $fieldToProtection = [
  118. 'name',
  119. 'status',
  120. 'type',
  121. 'description',
  122. 'managert'
  123. ];
  124. foreach ($this->formData as $field => &$value)
  125. {
  126. $value = in_array($field, $fieldToProtection) ? htmlentities($value) : $value;
  127. }
  128. $attr = array(
  129. 'description' => $this->formData['description'],
  130. 'type' => $this->formData['type'],
  131. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  132. 'manager' => array(
  133. 'id' => $this->formData['manager'],
  134. 'type' => 'UserPrincipal'
  135. )
  136. );
  137. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  138. try {
  139. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  140. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  141. } catch (KerioApiException $error) {
  142. logModuleCall(
  143. 'kerioEmail',
  144. __FUNCTION__,
  145. $error,
  146. 'Debug Error',
  147. $error->getMessage()
  148. );
  149. return ['error' => $error->getMessage()];
  150. }
  151. try {
  152. $result = $api->updateResouce($attr,$this->formData['id']);
  153. } catch (KerioApiException $error) {
  154. logModuleCall(
  155. 'kerioEmail',
  156. __FUNCTION__,
  157. $error,
  158. 'Debug Error',
  159. $error->getMessage()
  160. );
  161. return ['error' => $error->getMessage()];
  162. }
  163. $api->logout();
  164. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceHasBeenUpdated')->setStatusSuccess();
  165. }
  166. /**
  167. * @return HtmlDataJsonResponse
  168. */
  169. public function updateStatus()
  170. {
  171. $attr = array(
  172. 'isEnabled' => $this->formData['status'] === 'active' ? true : false,
  173. );
  174. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  175. try {
  176. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  177. } catch (KerioApiException $error) {
  178. logModuleCall(
  179. 'kerioEmail',
  180. __FUNCTION__,
  181. $error,
  182. 'Debug Error',
  183. $error->getMessage()
  184. );
  185. return ['error' => $error->getMessage()];
  186. }
  187. try {
  188. $result = $api->updateResouce($attr,$this->formData['id']);
  189. } catch (KerioApiException $error) {
  190. logModuleCall(
  191. 'kerioEmail',
  192. __FUNCTION__,
  193. $error,
  194. 'Debug Error',
  195. $error->getMessage()
  196. );
  197. return ['error' => $error->getMessage()];
  198. }
  199. $api->logout();
  200. return (new HtmlDataJsonResponse())->setMessageAndTranslate('ressourceStatusHasBeenUpdated')->setStatusSuccess();
  201. }
  202. /**
  203. * @return HtmlDataJsonResponse
  204. */
  205. public function changePassword()
  206. {
  207. }
  208. }