1717
1818use function __ ;
1919use function count ;
20+ use function is_array ;
2021
2122final class PrimaryController extends AbstractController
2223{
@@ -43,26 +44,23 @@ public function __invoke(ServerRequest $request): void
4344 $ GLOBALS ['urlParams ' ] = $ GLOBALS ['urlParams ' ] ?? null ;
4445 $ GLOBALS ['errorUrl ' ] = $ GLOBALS ['errorUrl ' ] ?? null ;
4546
46- $ selected = $ request -> getParsedBodyParam ( ' selected ' , []);
47- $ selected_fld = $ request ->getParsedBodyParam ('selected_fld ' , [] );
47+ /** @var string[]|null $ selected */
48+ $ selected = $ request ->getParsedBodyParam ('selected_fld ' , $ request -> getParsedBodyParam ( ' selected ' ) );
4849
49- if (empty ($ selected ) && empty ( $ selected_fld ) ) {
50+ if (! is_array ($ selected ) || $ selected === [] ) {
5051 $ this ->response ->setRequestStatus (false );
5152 $ this ->response ->addJSON ('message ' , __ ('No column selected. ' ));
5253
5354 return ;
5455 }
5556
56- $ primary = $ this ->getKeyForTablePrimary ();
57- if (empty ($ primary ) && ! empty ($ selected_fld )) {
58- // no primary key, so we can safely create new
59- $ mult_btn = __ ('Yes ' );
60- $ selected = $ selected_fld ;
61- }
57+ $ this ->dbi ->selectDb ($ GLOBALS ['db ' ]);
58+ $ hasPrimary = $ this ->hasPrimaryKey ();
6259
63- $ mult_btn = $ _POST ['mult_btn ' ] ?? $ mult_btn ?? '' ;
60+ /** @var string|null $deletionConfirmed */
61+ $ deletionConfirmed = $ request ->getParsedBodyParam ('mult_btn ' , null );
6462
65- if (! empty ( $ selected_fld ) && ! empty ( $ primary ) ) {
63+ if ($ hasPrimary && $ deletionConfirmed === null ) {
6664 $ this ->checkParameters (['db ' , 'table ' ]);
6765
6866 $ GLOBALS ['urlParams ' ] = ['db ' => $ GLOBALS ['db ' ], 'table ' => $ GLOBALS ['table ' ]];
@@ -74,15 +72,15 @@ public function __invoke(ServerRequest $request): void
7472 $ this ->render ('table/structure/primary ' , [
7573 'db ' => $ GLOBALS ['db ' ],
7674 'table ' => $ GLOBALS ['table ' ],
77- 'selected ' => $ selected_fld ,
75+ 'selected ' => $ selected ,
7876 ]);
7977
8078 return ;
8179 }
8280
83- if ($ mult_btn === __ ('Yes ' )) {
81+ if ($ deletionConfirmed === __ ('Yes ' ) || ! $ hasPrimary ) {
8482 $ GLOBALS ['sql_query ' ] = 'ALTER TABLE ' . Util::backquote ($ GLOBALS ['table ' ]);
85- if (! empty ( $ primary ) ) {
83+ if ($ hasPrimary ) {
8684 $ GLOBALS ['sql_query ' ] .= ' DROP PRIMARY KEY, ' ;
8785 }
8886
@@ -110,27 +108,16 @@ public function __invoke(ServerRequest $request): void
110108 ($ this ->structureController )($ request );
111109 }
112110
113- /**
114- * Gets table primary key
115- *
116- * @return string
117- */
118- private function getKeyForTablePrimary ()
111+ private function hasPrimaryKey (): bool
119112 {
120- $ this ->dbi ->selectDb ($ GLOBALS ['db ' ]);
121- $ result = $ this ->dbi ->query (
122- 'SHOW KEYS FROM ' . Util::backquote ($ GLOBALS ['table ' ]) . '; '
123- );
124- $ primary = '' ;
113+ $ result = $ this ->dbi ->query ('SHOW KEYS FROM ' . Util::backquote ($ GLOBALS ['table ' ]));
114+
125115 foreach ($ result as $ row ) {
126- // Backups the list of primary keys
127- if ($ row ['Key_name ' ] !== 'PRIMARY ' ) {
128- continue ;
116+ if ($ row ['Key_name ' ] === 'PRIMARY ' ) {
117+ return true ;
129118 }
130-
131- $ primary .= $ row ['Column_name ' ] . ', ' ;
132119 }
133120
134- return $ primary ;
121+ return false ;
135122 }
136123}
0 commit comments