Ressources.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. namespace ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Pages;
  3. use ThurData\Servers\KerioEmail\App\UI\Admin\Custom\Fields\EnabledField;
  4. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\AddRessourceButton;
  5. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\ChangeStatusButton;
  6. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\DeleteRessourceButton;
  7. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\EditRessourceButton;
  8. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\MassChangeStatusButton;
  9. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\MassDeleteRessourceButton;
  10. use ThurData\Servers\KerioEmail\App\UI\Client\Ressource\Buttons\SpanDropdownButton;
  11. use function ThurData\Servers\KerioEmail\Core\Helper\di;
  12. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\Column;
  13. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\DataProvider;
  14. use ThurData\Servers\KerioEmail\Core\UI\Interfaces\ClientArea;
  15. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataProviders\Providers\ArrayDataProvider;
  16. use ThurData\Servers\KerioEmail\Core\UI\Widget\DataTable\DataTable;
  17. use ThurData\Servers\KerioEmail\Api\KerioWhmcs;
  18. /**
  19. *
  20. * Created by PhpStorm.
  21. * User: ThurData
  22. * Date: 10.09.19
  23. * Time: 10:51
  24. * Class Ressources
  25. */
  26. class Ressources extends DataTable implements ClientArea
  27. {
  28. /**
  29. * labels for statuses
  30. */
  31. const STATUS_LABEL = [
  32. 'active' => 'success',
  33. 'closed' => 'default',
  34. 'default' => 'default'
  35. ];
  36. protected $id = 'ressources';
  37. protected $name = 'ressources';
  38. protected $title = null;
  39. /**
  40. * load columns
  41. */
  42. protected function loadHtml()
  43. {
  44. $this
  45. ->addColumn((new Column('ressource'))
  46. ->setOrderable(DataProvider::SORT_ASC)
  47. ->setSearchable(true, Column::TYPE_STRING))
  48. ->addColumn((new Column('description'))
  49. ->setOrderable()
  50. ->setSearchable(true, Column::TYPE_STRING))
  51. ->addColumn((new Column('type'))
  52. ->setOrderable()
  53. ->setSearchable(true, Column::TYPE_STRING))
  54. ->addColumn((new Column('manager'))
  55. ->setOrderable()
  56. ->setSearchable(true, Column::TYPE_STRING))
  57. ->addColumn((new Column('status'))
  58. ->setOrderable()
  59. ->setSearchable(true));
  60. }
  61. /**
  62. * @param $key
  63. * @param $row
  64. * @return mixed
  65. */
  66. public function replaceFieldStatus($key, $row)
  67. {
  68. $status = self::STATUS_LABEL[$row[$key]] ? self::STATUS_LABEL[$row[$key]] : self::STATUS_LABEL['default'];
  69. $label = di('lang')->absoluteT('kerio','account','status',$row[$key]);
  70. $field = new EnabledField();
  71. $field->setRawType($status);
  72. $field->setRawTitle($label);
  73. return $field->getHtml();
  74. }
  75. /**
  76. * @param $key
  77. * @param $row
  78. * @return mixed
  79. */
  80. public function replaceFieldLast_login($key, $row)
  81. {
  82. return $row[$key] ? $row[$key] : '-';
  83. }
  84. /**
  85. * @param $key
  86. * @param $row
  87. * @return mixed
  88. */
  89. public function replaceFieldType($key, $row)
  90. {
  91. return $row[$key] == 'Room' ? di('lang')->absoluteT('kerio','ressource','type','location') : di('lang')->absoluteT('kerio','ressource','type','equipment');
  92. }
  93. /**
  94. * load buttons
  95. */
  96. public function initContent()
  97. {
  98. $this->addMassActionButton(new MassChangeStatusButton());
  99. $this->addMassActionButton(new MassDeleteRessourceButton());
  100. $this->addButton(new AddRessourceButton());
  101. $this->addActionButton(new EditRessourceButton());
  102. $this->addActionButton(new DeleteRessourceButton());
  103. $actions = new SpanDropdownButton('actions');
  104. $actions->addButton(new ChangeStatusButton());
  105. $this->addActionButton($actions);
  106. }
  107. /**
  108. * load data
  109. */
  110. public function loadData()
  111. {
  112. $api = new KerioWhmcs('whmcsKerioEmail', 'Thurdata', '1.0');
  113. try {
  114. $api->login($this->getWhmcsParamByKey('serverhostname'), $this->getWhmcsParamByKey('serverusername'), $this->getWhmcsParamByKey('serverpassword'));
  115. $domainID = $api->getDomainId($this->getWhmcsParamByKey('domain'));
  116. } catch (KerioApiException $error) {
  117. logModuleCall(
  118. 'kerioEmail',
  119. __FUNCTION__,
  120. $error,
  121. 'Debug Error',
  122. $error->getMessage()
  123. );
  124. return ['error' => $error->getMessage()];
  125. }
  126. $fields = array(
  127. "id",
  128. "name",
  129. "description",
  130. "type",
  131. "isEnabled",
  132. "manager");
  133. try {
  134. $ressources = $api->getResources($fields, $domainID);
  135. } catch (KerioApiException $error) {
  136. logModuleCall(
  137. 'kerioEmail',
  138. __FUNCTION__,
  139. $error,
  140. 'Debug Error',
  141. $error->getMessage()
  142. );
  143. return ['error' => $error->getMessage()];
  144. }
  145. $api->logout();
  146. /**
  147. * format model to array
  148. */
  149. $data = [];
  150. foreach ($ressources as $ressource)
  151. {
  152. $ressourceArray = [
  153. 'id' => $ressource['id'],
  154. 'ressource' => $ressource['name'],
  155. 'description' => $ressource['description'],
  156. 'type' => $ressource['type'],
  157. 'manager' => $ressource['manager']['name'] . '@' . $ressource['manager']['domainName'],
  158. 'status' => $ressource['isEnabled'] ? 'active' : 'default'
  159. ];
  160. $data[] = $ressourceArray;
  161. }
  162. $dataProv = new ArrayDataProvider();
  163. $dataProv->setDefaultSorting('ressource', 'ASC')->setData($data);
  164. $this->setDataProvider($dataProv);
  165. }
  166. }