Accounts.php 6.5 KB

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