Accounts.php 7.0 KB

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