ExportTemplateController.php 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers;
  4. use PhpMyAdmin\Export\Template as ExportTemplate;
  5. use PhpMyAdmin\Export\TemplateModel;
  6. use PhpMyAdmin\Relation;
  7. use PhpMyAdmin\Response;
  8. use PhpMyAdmin\Template;
  9. use function is_array;
  10. use function is_string;
  11. final class ExportTemplateController extends AbstractController
  12. {
  13. /** @var TemplateModel */
  14. private $model;
  15. /** @var Relation */
  16. private $relation;
  17. /**
  18. * @param Response $response
  19. */
  20. public function __construct(
  21. $response,
  22. Template $template,
  23. TemplateModel $model,
  24. Relation $relation
  25. ) {
  26. parent::__construct($response, $template);
  27. $this->model = $model;
  28. $this->relation = $relation;
  29. }
  30. public function create(): void
  31. {
  32. global $cfg;
  33. $cfgRelation = $this->relation->getRelationsParam();
  34. if (! $cfgRelation['exporttemplateswork']) {
  35. return;
  36. }
  37. $template = ExportTemplate::fromArray([
  38. 'username' => $cfg['Server']['user'],
  39. 'exportType' => $_POST['exportType'] ?? '',
  40. 'name' => $_POST['templateName'] ?? '',
  41. 'data' => $_POST['templateData'] ?? '',
  42. ]);
  43. $result = $this->model->create($cfgRelation['db'], $cfgRelation['export_templates'], $template);
  44. if (is_string($result)) {
  45. $this->response->setRequestStatus(false);
  46. $this->response->addJSON('message', $result);
  47. return;
  48. }
  49. $templates = $this->model->getAll(
  50. $cfgRelation['db'],
  51. $cfgRelation['export_templates'],
  52. $template->getUsername(),
  53. $template->getExportType()
  54. );
  55. $this->response->setRequestStatus(true);
  56. $this->response->addJSON(
  57. 'data',
  58. $this->template->render('export/template_options', [
  59. 'templates' => is_array($templates) ? $templates : [],
  60. 'selected_template' => $_POST['template_id'] ?? null,
  61. ])
  62. );
  63. }
  64. public function delete(): void
  65. {
  66. global $cfg;
  67. $cfgRelation = $this->relation->getRelationsParam();
  68. if (! $cfgRelation['exporttemplateswork']) {
  69. return;
  70. }
  71. $result = $this->model->delete(
  72. $cfgRelation['db'],
  73. $cfgRelation['export_templates'],
  74. $cfg['Server']['user'],
  75. (int) $_POST['templateId']
  76. );
  77. if (is_string($result)) {
  78. $this->response->setRequestStatus(false);
  79. $this->response->addJSON('message', $result);
  80. return;
  81. }
  82. $this->response->setRequestStatus(true);
  83. }
  84. public function load(): void
  85. {
  86. global $cfg;
  87. $cfgRelation = $this->relation->getRelationsParam();
  88. if (! $cfgRelation['exporttemplateswork']) {
  89. return;
  90. }
  91. $template = $this->model->load(
  92. $cfgRelation['db'],
  93. $cfgRelation['export_templates'],
  94. $cfg['Server']['user'],
  95. (int) $_POST['templateId']
  96. );
  97. if (! $template instanceof ExportTemplate) {
  98. $this->response->setRequestStatus(false);
  99. $this->response->addJSON('message', $template);
  100. return;
  101. }
  102. $this->response->setRequestStatus(true);
  103. $this->response->addJSON('data', $template->getData());
  104. }
  105. public function update(): void
  106. {
  107. global $cfg;
  108. $cfgRelation = $this->relation->getRelationsParam();
  109. if (! $cfgRelation['exporttemplateswork']) {
  110. return;
  111. }
  112. $template = ExportTemplate::fromArray([
  113. 'id' => (int) $_POST['templateId'],
  114. 'username' => $cfg['Server']['user'],
  115. 'data' => $_POST['templateData'],
  116. ]);
  117. $result = $this->model->update($cfgRelation['db'], $cfgRelation['export_templates'], $template);
  118. if (is_string($result)) {
  119. $this->response->setRequestStatus(false);
  120. $this->response->addJSON('message', $result);
  121. return;
  122. }
  123. $this->response->setRequestStatus(true);
  124. }
  125. }