AddFieldController.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. <?php
  2. declare(strict_types=1);
  3. namespace PhpMyAdmin\Controllers\Table;
  4. use PhpMyAdmin\Config;
  5. use PhpMyAdmin\CreateAddField;
  6. use PhpMyAdmin\DatabaseInterface;
  7. use PhpMyAdmin\DbTableExists;
  8. use PhpMyAdmin\Html\Generator;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Relation;
  11. use PhpMyAdmin\Response;
  12. use PhpMyAdmin\Table\ColumnsDefinition;
  13. use PhpMyAdmin\Template;
  14. use PhpMyAdmin\Transformations;
  15. use PhpMyAdmin\Url;
  16. use PhpMyAdmin\Util;
  17. use function intval;
  18. use function is_array;
  19. use function min;
  20. use function strlen;
  21. /**
  22. * Displays add field form and handles it.
  23. */
  24. class AddFieldController extends AbstractController
  25. {
  26. /** @var Transformations */
  27. private $transformations;
  28. /** @var Config */
  29. private $config;
  30. /** @var Relation */
  31. private $relation;
  32. /** @var DatabaseInterface */
  33. private $dbi;
  34. /**
  35. * @param Response $response
  36. * @param string $db Database name.
  37. * @param string $table Table name.
  38. * @param DatabaseInterface $dbi
  39. */
  40. public function __construct(
  41. $response,
  42. Template $template,
  43. $db,
  44. $table,
  45. Transformations $transformations,
  46. Config $config,
  47. Relation $relation,
  48. $dbi
  49. ) {
  50. parent::__construct($response, $template, $db, $table);
  51. $this->transformations = $transformations;
  52. $this->config = $config;
  53. $this->relation = $relation;
  54. $this->dbi = $dbi;
  55. }
  56. public function index(): void
  57. {
  58. global $err_url, $message, $action, $active_page, $sql_query;
  59. global $num_fields, $regenerate, $result, $db, $table;
  60. $this->addScriptFiles(['table/structure.js']);
  61. // Check parameters
  62. Util::checkParameters(['db', 'table']);
  63. $cfg = $this->config->settings;
  64. /**
  65. * Defines the url to return to in case of error in a sql statement
  66. */
  67. $err_url = Url::getFromRoute('/table/sql', [
  68. 'db' => $db,
  69. 'table' => $table,
  70. ]);
  71. // check number of fields to be created
  72. if (isset($_POST['submit_num_fields'])) {
  73. if (isset($_POST['orig_after_field'])) {
  74. $_POST['after_field'] = $_POST['orig_after_field'];
  75. }
  76. if (isset($_POST['orig_field_where'])) {
  77. $_POST['field_where'] = $_POST['orig_field_where'];
  78. }
  79. $num_fields = min(
  80. intval($_POST['orig_num_fields']) + intval($_POST['added_fields']),
  81. 4096
  82. );
  83. $regenerate = true;
  84. } elseif (isset($_POST['num_fields']) && intval($_POST['num_fields']) > 0) {
  85. $num_fields = min(4096, intval($_POST['num_fields']));
  86. } else {
  87. $num_fields = 1;
  88. }
  89. if (isset($_POST['do_save_data'])) {
  90. // avoid an incorrect calling of PMA_updateColumns() via
  91. // /table/structure below
  92. unset($_POST['do_save_data']);
  93. $createAddField = new CreateAddField($this->dbi);
  94. [$result, $sql_query] = $createAddField->tryColumnCreationQuery($db, $table, $err_url);
  95. if ($result !== true) {
  96. $error_message_html = Generator::mysqlDie(
  97. '',
  98. '',
  99. false,
  100. $err_url,
  101. false
  102. );
  103. $this->response->addHTML($error_message_html ?? '');
  104. $this->response->setRequestStatus(false);
  105. return;
  106. }
  107. // Update comment table for mime types [MIME]
  108. if (isset($_POST['field_mimetype'])
  109. && is_array($_POST['field_mimetype'])
  110. && $cfg['BrowseMIME']
  111. ) {
  112. foreach ($_POST['field_mimetype'] as $fieldindex => $mimetype) {
  113. if (! isset($_POST['field_name'][$fieldindex])
  114. || strlen($_POST['field_name'][$fieldindex]) <= 0
  115. ) {
  116. continue;
  117. }
  118. $this->transformations->setMime(
  119. $db,
  120. $table,
  121. $_POST['field_name'][$fieldindex],
  122. $mimetype,
  123. $_POST['field_transformation'][$fieldindex],
  124. $_POST['field_transformation_options'][$fieldindex],
  125. $_POST['field_input_transformation'][$fieldindex],
  126. $_POST['field_input_transformation_options'][$fieldindex]
  127. );
  128. }
  129. }
  130. // Go back to the structure sub-page
  131. $message = Message::success(
  132. __('Table %1$s has been altered successfully.')
  133. );
  134. $message->addParam($table);
  135. $this->response->addJSON(
  136. 'message',
  137. Generator::getMessage($message, $sql_query, 'success')
  138. );
  139. return;
  140. }
  141. $url_params = ['db' => $db, 'table' => $table];
  142. $err_url = Util::getScriptNameForOption($cfg['DefaultTabTable'], 'table');
  143. $err_url .= Url::getCommon($url_params, '&');
  144. DbTableExists::check();
  145. $active_page = Url::getFromRoute('/table/structure');
  146. /**
  147. * Display the form
  148. */
  149. $action = Url::getFromRoute('/table/add-field');
  150. $this->addScriptFiles(['vendor/jquery/jquery.uitablefilter.js', 'indexes.js']);
  151. $templateData = ColumnsDefinition::displayForm(
  152. $this->transformations,
  153. $this->relation,
  154. $this->dbi,
  155. $action,
  156. $num_fields,
  157. $regenerate
  158. );
  159. $this->render('columns_definitions/column_definitions_form', $templateData);
  160. }
  161. }