CentralColumnsController.php 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. <?php
  2. /**
  3. * Central Columns view/edit
  4. */
  5. declare(strict_types=1);
  6. namespace PhpMyAdmin\Controllers\Database;
  7. use PhpMyAdmin\Core;
  8. use PhpMyAdmin\Database\CentralColumns;
  9. use PhpMyAdmin\Message;
  10. use PhpMyAdmin\Response;
  11. use PhpMyAdmin\Template;
  12. use function is_bool;
  13. use function parse_str;
  14. use function sprintf;
  15. class CentralColumnsController extends AbstractController
  16. {
  17. /** @var CentralColumns */
  18. private $centralColumns;
  19. /**
  20. * @param Response $response
  21. * @param string $db Database name
  22. * @param CentralColumns $centralColumns
  23. */
  24. public function __construct($response, Template $template, $db, $centralColumns)
  25. {
  26. parent::__construct($response, $template, $db);
  27. $this->centralColumns = $centralColumns;
  28. }
  29. public function index(): void
  30. {
  31. global $cfg, $db, $message, $pos, $num_cols;
  32. if (isset($_POST['edit_save'])) {
  33. echo $this->editSave([
  34. 'col_name' => $_POST['col_name'] ?? null,
  35. 'orig_col_name' => $_POST['orig_col_name'] ?? null,
  36. 'col_default' => $_POST['col_default'] ?? null,
  37. 'col_default_sel' => $_POST['col_default_sel'] ?? null,
  38. 'col_extra' => $_POST['col_extra'] ?? null,
  39. 'col_isNull' => $_POST['col_isNull'] ?? null,
  40. 'col_length' => $_POST['col_length'] ?? null,
  41. 'col_attribute' => $_POST['col_attribute'] ?? null,
  42. 'col_type' => $_POST['col_type'] ?? null,
  43. 'collation' => $_POST['collation'] ?? null,
  44. ]);
  45. return;
  46. }
  47. if (isset($_POST['add_new_column'])) {
  48. $tmp_msg = $this->addNewColumn([
  49. 'col_name' => $_POST['col_name'] ?? null,
  50. 'col_default' => $_POST['col_default'] ?? null,
  51. 'col_default_sel' => $_POST['col_default_sel'] ?? null,
  52. 'col_extra' => $_POST['col_extra'] ?? null,
  53. 'col_isNull' => $_POST['col_isNull'] ?? null,
  54. 'col_length' => $_POST['col_length'] ?? null,
  55. 'col_attribute' => $_POST['col_attribute'] ?? null,
  56. 'col_type' => $_POST['col_type'] ?? null,
  57. 'collation' => $_POST['collation'] ?? null,
  58. ]);
  59. }
  60. if (isset($_POST['getColumnList'])) {
  61. $this->response->addJSON('message', $this->getColumnList([
  62. 'cur_table' => $_POST['cur_table'] ?? null,
  63. ]));
  64. return;
  65. }
  66. if (isset($_POST['add_column'])) {
  67. $tmp_msg = $this->addColumn([
  68. 'table-select' => $_POST['table-select'] ?? null,
  69. 'column-select' => $_POST['column-select'] ?? null,
  70. ]);
  71. }
  72. $this->addScriptFiles([
  73. 'vendor/jquery/jquery.uitablefilter.js',
  74. 'vendor/jquery/jquery.tablesorter.js',
  75. 'database/central_columns.js',
  76. ]);
  77. if (isset($_POST['edit_central_columns_page'])) {
  78. $this->editPage([
  79. 'selected_fld' => $_POST['selected_fld'] ?? null,
  80. 'db' => $_POST['db'] ?? null,
  81. ]);
  82. return;
  83. }
  84. if (isset($_POST['multi_edit_central_column_save'])) {
  85. $message = $this->updateMultipleColumn([
  86. 'db' => $_POST['db'] ?? null,
  87. 'orig_col_name' => $_POST['orig_col_name'] ?? null,
  88. 'field_name' => $_POST['field_name'] ?? null,
  89. 'field_default_type' => $_POST['field_default_type'] ?? null,
  90. 'field_default_value' => $_POST['field_default_value'] ?? null,
  91. 'field_length' => $_POST['field_length'] ?? null,
  92. 'field_attribute' => $_POST['field_attribute'] ?? null,
  93. 'field_type' => $_POST['field_type'] ?? null,
  94. 'field_collation' => $_POST['field_collation'] ?? null,
  95. 'field_null' => $_POST['field_null'] ?? null,
  96. 'col_extra' => $_POST['col_extra'] ?? null,
  97. ]);
  98. if (! is_bool($message)) {
  99. $this->response->setRequestStatus(false);
  100. $this->response->addJSON('message', $message);
  101. }
  102. }
  103. if (isset($_POST['delete_save'])) {
  104. $tmp_msg = $this->deleteSave([
  105. 'db' => $_POST['db'] ?? null,
  106. 'col_name' => $_POST['col_name'] ?? null,
  107. ]);
  108. }
  109. $this->main([
  110. 'pos' => $_POST['pos'] ?? null,
  111. 'total_rows' => $_POST['total_rows'] ?? null,
  112. ]);
  113. $pos = 0;
  114. if (Core::isValid($_POST['pos'], 'integer')) {
  115. $pos = (int) $_POST['pos'];
  116. }
  117. $num_cols = $this->centralColumns->getColumnsCount(
  118. $db,
  119. $pos,
  120. (int) $cfg['MaxRows']
  121. );
  122. $message = Message::success(
  123. sprintf(__('Showing rows %1$s - %2$s.'), $pos + 1, $pos + $num_cols)
  124. );
  125. if (! isset($tmp_msg) || $tmp_msg === true) {
  126. return;
  127. }
  128. $message = $tmp_msg;
  129. }
  130. /**
  131. * @param array $params Request parameters
  132. */
  133. public function main(array $params): void
  134. {
  135. global $text_dir, $PMA_Theme;
  136. if (! empty($params['total_rows'])
  137. && Core::isValid($params['total_rows'], 'integer')
  138. ) {
  139. $totalRows = (int) $params['total_rows'];
  140. } else {
  141. $totalRows = $this->centralColumns->getCount($this->db);
  142. }
  143. $pos = 0;
  144. if (Core::isValid($params['pos'], 'integer')) {
  145. $pos = (int) $params['pos'];
  146. }
  147. $variables = $this->centralColumns->getTemplateVariablesForMain(
  148. $this->db,
  149. $totalRows,
  150. $pos,
  151. $PMA_Theme->getImgPath(),
  152. $text_dir
  153. );
  154. $this->render('database/central_columns/main', $variables);
  155. }
  156. /**
  157. * @param array $params Request parameters
  158. *
  159. * @return array JSON
  160. */
  161. public function getColumnList(array $params): array
  162. {
  163. return $this->centralColumns->getListRaw(
  164. $this->db,
  165. $params['cur_table'] ?? ''
  166. );
  167. }
  168. public function populateColumns(): void
  169. {
  170. $columns = $this->centralColumns->getColumnsNotInCentralList($this->db, $_POST['selectedTable']);
  171. $this->render('database/central_columns/populate_columns', ['columns' => $columns]);
  172. }
  173. /**
  174. * @param array $params Request parameters
  175. *
  176. * @return true|Message
  177. */
  178. public function editSave(array $params)
  179. {
  180. $columnDefault = $params['col_default'];
  181. if ($columnDefault === 'NONE' && $params['col_default_sel'] !== 'USER_DEFINED') {
  182. $columnDefault = '';
  183. }
  184. return $this->centralColumns->updateOneColumn(
  185. $this->db,
  186. $params['orig_col_name'],
  187. $params['col_name'],
  188. $params['col_type'],
  189. $params['col_attribute'],
  190. $params['col_length'],
  191. isset($params['col_isNull']) ? 1 : 0,
  192. $params['collation'],
  193. $params['col_extra'] ?? '',
  194. $columnDefault
  195. );
  196. }
  197. /**
  198. * @param array $params Request parameters
  199. *
  200. * @return true|Message
  201. */
  202. public function addNewColumn(array $params)
  203. {
  204. $columnDefault = $params['col_default'];
  205. if ($columnDefault === 'NONE' && $params['col_default_sel'] !== 'USER_DEFINED') {
  206. $columnDefault = '';
  207. }
  208. return $this->centralColumns->updateOneColumn(
  209. $this->db,
  210. '',
  211. $params['col_name'],
  212. $params['col_type'],
  213. $params['col_attribute'],
  214. $params['col_length'],
  215. isset($params['col_isNull']) ? 1 : 0,
  216. $params['collation'],
  217. $params['col_extra'] ?? '',
  218. $columnDefault
  219. );
  220. }
  221. /**
  222. * @param array $params Request parameters
  223. *
  224. * @return true|Message
  225. */
  226. public function addColumn(array $params)
  227. {
  228. return $this->centralColumns->syncUniqueColumns(
  229. [$params['column-select']],
  230. false,
  231. $params['table-select']
  232. );
  233. }
  234. /**
  235. * @param array $params Request parameters
  236. */
  237. public function editPage(array $params): void
  238. {
  239. $rows = $this->centralColumns->getHtmlForEditingPage(
  240. $params['selected_fld'],
  241. $params['db']
  242. );
  243. $this->render('database/central_columns/edit', ['rows' => $rows]);
  244. }
  245. /**
  246. * @param array $params Request parameters
  247. *
  248. * @return true|Message
  249. */
  250. public function updateMultipleColumn(array $params)
  251. {
  252. return $this->centralColumns->updateMultipleColumn($params);
  253. }
  254. /**
  255. * @param array $params Request parameters
  256. *
  257. * @return true|Message
  258. */
  259. public function deleteSave(array $params)
  260. {
  261. $name = [];
  262. parse_str($params['col_name'], $name);
  263. return $this->centralColumns->deleteColumnsFromList(
  264. $params['db'],
  265. $name['selected_fld'],
  266. false
  267. );
  268. }
  269. }