EditSettingDataProvider.php 7.2 KB

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