Accounts.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Pages;
  3. use ThurData\Servers\KerioEmail\App\Libs\Kerio\Components\Api\Soap\Helpers\AccountHelper;
  4. use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Fields\EnabledField;
  5. use ThurData\Servers\KerioEmail\App\Libs\Product\ProductManager;
  6. use ThurData\Servers\KerioEmail\Core\Models\Whmcs\Hosting;
  7. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\AddAccountButton;
  8. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\ChangePasswordButton;
  9. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\ChangeStatusButton;
  10. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\DeleteAccountButton;
  11. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\EditAccountButton;
  12. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\MassChangeStatusButton;
  13. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\MassDeleteAccountButton;
  14. use ThurData\Servers\KerioEmail\App\UI\Client\EmailAccount\Buttons\SpanDropdownButton;
  15. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\Column;
  17. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\DataProvider;
  18. use ThurData\Servers\KerioEmail\Core\UI\Interfaces\ClientArea;
  19. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
  20. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataTable;
  21. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  22. /**
  23. *
  24. * Created by PhpStorm.
  25. * User: ThurData
  26. * Date: 10.09.19
  27. * Time: 10:51
  28. * Class Accounts
  29. */
  30. class Accounts extends DataTable implements ClientArea
  31. {
  32. /**
  33. * labels for statuses
  34. */
  35. const STATUS_LABEL = [
  36. 'active' => 'success',
  37. 'closed' => 'default',
  38. 'default' => 'default'
  39. ];
  40. protected $id = 'accounts';
  41. protected $name = 'accounts';
  42. protected $title = null;
  43. /**
  44. * load columns
  45. */
  46. protected function loadHtml()
  47. {
  48. $this
  49. ->addColumn((new Column('mailbox'))
  50. ->setOrderable(DataProvider::SORT_ASC)
  51. ->setSearchable(true, Column::TYPE_STRING))
  52. ->addColumn((new Column('last_login'))
  53. ->setOrderable()
  54. ->setSearchable(true))
  55. ->addColumn((new Column('size'))
  56. ->setOrderable()
  57. ->setSearchable(true, Column::TYPE_INT))
  58. ->addColumn((new Column('quota'))
  59. ->setOrderable()
  60. ->setSearchable(true, Column::TYPE_INT))
  61. ->addColumn((new Column('status'))
  62. ->setOrderable()
  63. ->setSearchable(true));
  64. }
  65. /**
  66. * @param $key
  67. * @param $row
  68. * @return mixed
  69. */
  70. public function replaceFieldStatus($key, $row)
  71. {
  72. $status = self::STATUS_LABEL[$row[$key]] ? self::STATUS_LABEL[$row[$key]] : self::STATUS_LABEL['default'];
  73. $label = di('lang')->absoluteT('kerio','account','status',$row[$key]);
  74. $field = new EnabledField();
  75. $field->setRawType($status);
  76. $field->setRawTitle($label);
  77. return $field->getHtml();
  78. }
  79. /**
  80. * @param $key
  81. * @param $row
  82. * @return mixed
  83. */
  84. public function replaceFieldLast_login($key, $row)
  85. {
  86. return $row[$key] ? $row[$key] : '-';
  87. }
  88. /**
  89. * load buttons
  90. */
  91. public function initContent()
  92. {
  93. $hid = $this->getRequestValue('id');
  94. $hosting = Hosting::where('id', $hid)->first();
  95. logModuleCall(
  96. 'kerioEmail',
  97. __FUNCTION__,
  98. $hosting,
  99. 'Debug Error',
  100. $this->getRequestValue('id')
  101. );
  102. $this->addMassActionButton(new MassChangeStatusButton());
  103. $this->addMassActionButton(new MassDeleteAccountButton());
  104. $this->addButton(new AddAccountButton());
  105. $this->addActionButton(new EditAccountButton());
  106. $this->addActionButton(new DeleteAccountButton());
  107. $actions = new SpanDropdownButton('actions');
  108. $actions->addButton(new ChangeStatusButton());
  109. $actions->addButton(new ChangePasswordButton());
  110. $this->addActionButton($actions);
  111. }
  112. /**
  113. * load data
  114. */
  115. public function loadData()
  116. {
  117. $domain = $this->getWhmcsParamByKey('domain');
  118. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  119. try {
  120. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  121. $domainID = $api->getDomainId($domain);
  122. } catch (KerioApiException $error) {
  123. logModuleCall(
  124. 'kerioEmail',
  125. __FUNCTION__,
  126. $error,
  127. 'Debug Error',
  128. $error->getMessage()
  129. );
  130. return ['error' => $error->getMessage()];
  131. }
  132. $fields = array(
  133. "id",
  134. "loginName",
  135. "isEnabled",
  136. "diskSizeLimit",
  137. "consumedSize",
  138. "lastLoginInfo");
  139. try {
  140. $accounts = $api->getUsers($fields,$domainID);
  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. $api->logout();
  152. $data = [];
  153. foreach ($accounts as $account)
  154. {
  155. $accountArray = [
  156. 'id' => $account['id'],
  157. 'mailbox' => $account['loginName'] . '@' . $domain,
  158. 'last_login' => AccountHelper::getFormattedData($account['lastLoginInfo']['dateTime'], 'd/m/Y H:i'),
  159. 'size' => AccountHelper::getQuotaAsMb($account['consumedSize']['value'],$account['consumedSize']['unit']),
  160. 'quota' => $account['diskSizeLimit']['isActive'] ? AccountHelper::getQuotaAsMb($account['diskSizeLimit']['limit']['value'],$account['diskSizeLimit']['limit']['units']) : '∞',
  161. 'status' => $account['isEnabled'] ? 'active' : 'default',
  162. ];
  163. $data[] = $accountArray;
  164. }
  165. $dataProv = new ArrayDataProvider();
  166. $dataProv->setDefaultSorting('mailbox', 'ASC')->setData($data);
  167. $this->setDataProvider($dataProv);
  168. }
  169. }