Skip to content

Commit

Permalink
Installation : Fix SQL queries
Browse files Browse the repository at this point in the history
---------
Co-authored-by: clement-sanz <clement@oxygenz.fr>
  • Loading branch information
MacWarrior committed Feb 29, 2024
1 parent c4c3e52 commit 7d41f21
Show file tree
Hide file tree
Showing 14 changed files with 142 additions and 62 deletions.
26 changes: 17 additions & 9 deletions upload/admin_area/category.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,21 @@

$type = $_GET['type'] ?? 'video';
assign('type', $type);
assign('display_type', $type .'s');
assign('display_type', $type . 's');
/* Generating breadcrumb */
global $breadcrumb;
$breadcrumb[0] = ['title' => lang($type.'s'), 'url' => ''];
$breadcrumb[1] = ['title' => lang('manage_categories'), 'url' => DirPath::getUrl('admin_area') . 'category.php?type=' . $type];
$breadcrumb[0] = [
'title' => lang($type . 's'),
'url' => ''
];
$breadcrumb[1] = [
'title' => lang('manage_categories'),
'url' => DirPath::getUrl('admin_area') . 'category.php?type=' . $type
];

$version = Update::getInstance()->getDBVersion();
if( !($version['version'] > '5.5.0' || ($version['version'] == '5.5.0' && $version['revision'] >= 323)) ) {
e('Your database is not up-to-date. Please update your database via this link : <a href="admin_tool.php?id_tool=5">'.lang('update').'</a>', 'e', false);
if (!($version['version'] > '5.5.0' || ($version['version'] == '5.5.0' && $version['revision'] >= 323))) {
e('Your database is not up-to-date. Please update your database via this link : <a href="admin_tool.php?id_tool=5">' . lang('update') . '</a>', 'e', false);
} else {
//Making Category as Default
if (isset($_GET['make_default'])) {
Expand All @@ -32,7 +38,7 @@
'category_type' => Category::getInstance()->getIdsCategoriesType($type),
'condition' => 'category_name like \'%' . mysql_clean($_POST['category_name']) . '%\'',
'first_only' => true
])) && ($_POST['cur_name'] != $_POST['category_name']) ) {
])) && ($_POST['cur_name'] != $_POST['category_name'])) {
e(lang('add_cat_erro'));
} elseif (!empty($_POST['category_id'])) {
$id_category = $_POST['category_id'];
Expand All @@ -42,7 +48,9 @@
} else {
$params = $_POST;
$params['id_category_type'] = Category::getInstance()->getIdsCategoriesType($type);
$id_category = Category::getInstance()->insert($params);
$next_order_place = Category::getInstance()->getNextOrderForParent($type, $_POST['parent_id']);
$params['category_order'] = $next_order_place;
Category::getInstance()->insert($params);
}

if (!empty($_FILES['category_thumb']['tmp_name'])) {
Expand All @@ -63,7 +71,7 @@
} else {
$breadcrumb[2] = [
'title' => 'Editing : ' . display_clean($cat_details['category_name']),
'url' => DirPath::getUrl('admin_area') . 'category.php?type=' . $type.'&category=' . display_clean($id_category)
'url' => DirPath::getUrl('admin_area') . 'category.php?type=' . $type . '&category=' . display_clean($id_category)
];
}
}
Expand All @@ -77,7 +85,7 @@
if (isset($_POST['update_order'])) {
foreach ($_POST['category_order'] as $key => $item) {
Category::getInstance()->update([
'category_id' => $key,
'category_id' => $key,
'category_order' => $item
]);
}
Expand Down
2 changes: 1 addition & 1 deletion upload/admin_area/styles/cb_2014/layout/category.html
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ <h2>{lang($display_type)|capitalize} Categories
<input type="file" name="category_thumb" id="cat_thumb"/>
</div>
<div class="col-md-6">
<input type="submit" class="btn btn-primary pull-right button" name="update_category" id="button" value="Update Category"/>
<input type="submit" class="btn btn-primary pull-right button" name="update_category" id="button" value="{if empty($cat_details.category_id)}{lang code='add_new_category'}{else}{lang code='update_category'}{/if}"/>
</div>
</div>
</form>
Expand Down
39 changes: 19 additions & 20 deletions upload/cb_install/sql/5.5.0/00331.sql
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,12 @@ CREATE TABLE IF NOT EXISTS `{tbl_prefix}categories`
`category_desc` TEXT NULL DEFAULT NULL,
`date_added` DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP,
`category_thumb` MEDIUMTEXT NULL,
`is_default` ENUM ('yes','no') NOT NULL DEFAULT 'no'
`is_default` ENUM ('yes','no') NOT NULL DEFAULT 'no',
`old_category_id` INT(255) NULL
) ENGINE = InnoDB
DEFAULT CHARSET = utf8mb4
COLLATE utf8mb4_unicode_520_ci;
ALTER TABLE `{tbl_prefix}categories`
ADD CONSTRAINT `categorie_parent` FOREIGN KEY IF NOT EXISTS (`parent_id`) REFERENCES `{tbl_prefix}categories` (`category_id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE `{tbl_prefix}categories` ADD FULLTEXT KEY `categorie` (`category_name`);

CREATE TABLE IF NOT EXISTS `{tbl_prefix}categories_type`
Expand Down Expand Up @@ -111,8 +111,8 @@ SET @type_category = (
FROM `{tbl_prefix}categories_type`
WHERE name LIKE 'collection'
);
INSERT IGNORE INTO `{tbl_prefix}categories` (id_category_type, parent_id, category_name, category_order, category_desc, date_added, category_thumb, is_default) (
SELECT @type_category, CASE WHEN parent_id != 0 THEN (CASE WHEN parent_id = category_id THEN NULL ELSE parent_id + @id_categ END) ELSE NULL END, category_name, category_order, category_desc, date_added, category_thumb, isdefault
INSERT IGNORE INTO `{tbl_prefix}categories` (id_category_type, parent_id, category_name, category_order, category_desc, date_added, category_thumb, is_default, old_category_id) (
SELECT @type_category, CASE WHEN parent_id = '0' THEN NULL ELSE parent_id END, category_name, category_order, category_desc, date_added, category_thumb, isdefault, category_id
FROM `{tbl_prefix}collection_categories`
WHERE 1
);
Expand Down Expand Up @@ -143,14 +143,9 @@ SET @type_category = (
FROM `{tbl_prefix}categories_type`
WHERE name LIKE 'video'
);
SET @id_categ = (
SELECT max(category_id)
FROM `{tbl_prefix}categories`
WHERE 1
);

INSERT IGNORE INTO `{tbl_prefix}categories` (id_category_type, parent_id, category_name, category_order, category_desc, date_added, category_thumb, is_default) (
SELECT @type_category, CASE WHEN parent_id != 0 THEN parent_id + @id_categ ELSE NULL END, category_name, category_order, category_desc, date_added, category_thumb, isdefault
INSERT IGNORE INTO `{tbl_prefix}categories` (id_category_type, parent_id, category_name, category_order, category_desc, date_added, category_thumb, is_default, old_category_id) (
SELECT @type_category, CASE WHEN parent_id = '0' THEN NULL ELSE parent_id END , category_name, category_order, category_desc, date_added, category_thumb, isdefault, category_id
FROM `{tbl_prefix}video_categories`
WHERE 1
);
Expand All @@ -161,7 +156,7 @@ INSERT IGNORE INTO `{tbl_prefix}videos_categories` (`id_category`, `id_video`)
UNION ALL
SELECT n + 1
FROM NumberSequence
WHERE n < (SELECT COUNT(*) FROM `{tbl_prefix}collection_categories`)
WHERE n < (SELECT COUNT(*) FROM `{tbl_prefix}video_categories`)
)
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(V.category, '#', seq.n+1), '#', -1) + @id_categ AS extracted_number
Expand All @@ -188,12 +183,6 @@ SET @type_category = (
WHERE name LIKE 'user'
);

SET @id_categ = (
SELECT max(category_id)
FROM `{tbl_prefix}categories`
WHERE 1
);

INSERT IGNORE INTO `{tbl_prefix}categories` (id_category_type, parent_id, category_name, category_order, category_desc, date_added, category_thumb, is_default) (
SELECT @type_category, NULL, category_name, category_order, category_desc, date_added, category_thumb, isdefault
FROM `{tbl_prefix}user_categories`
Expand All @@ -206,7 +195,7 @@ INSERT IGNORE INTO `{tbl_prefix}users_categories` (`id_category`, `id_user`)
UNION ALL
SELECT n + 1
FROM NumberSequence
WHERE n < (SELECT COUNT(*) FROM `{tbl_prefix}collection_categories`)
WHERE n < (SELECT COUNT(*) FROM `{tbl_prefix}user_categories`)
)
SELECT
SUBSTRING_INDEX(SUBSTRING_INDEX(U.category, '#', seq.n+1), '#', -1) + @id_categ AS extracted_number
Expand All @@ -220,6 +209,16 @@ INSERT IGNORE INTO `{tbl_prefix}users_categories` (`id_category`, `id_user`)
AND SUBSTRING_INDEX(SUBSTRING_INDEX(U.category, '#', seq.n+1), '#', -1) != ''
;

UPDATE `{tbl_prefix}categories` C
INNER JOIN `{tbl_prefix}categories` CP ON CP.old_category_id = C.parent_id AND CP.id_category_type = C.id_category_type
SET C.parent_id = CP.category_id
WHERE C.parent_id != 0 AND C.parent_id IS NOT NULL;

ALTER TABLE `{tbl_prefix}categories`
ADD CONSTRAINT `categorie_parent` FOREIGN KEY IF NOT EXISTS (`parent_id`) REFERENCES `{tbl_prefix}categories` (`category_id`) ON DELETE NO ACTION ON UPDATE NO ACTION;

ALTER TABLE `{tbl_prefix}categories` DROP COLUMN IF EXISTS `old_category_id`;

ALTER TABLE `{tbl_prefix}collections` DROP COLUMN IF EXISTS `category`;
ALTER TABLE `{tbl_prefix}playlists` DROP COLUMN IF EXISTS `category`;
ALTER TABLE `{tbl_prefix}users` DROP COLUMN IF EXISTS `category`;
Expand Down
27 changes: 18 additions & 9 deletions upload/cb_install/sql/5.5.0/00367.sql
Original file line number Diff line number Diff line change
@@ -1,18 +1,21 @@
ALTER TABLE `{tbl_prefix}tools_status` RENAME `{tbl_prefix}tools_histo_status`;
ALTER TABLE `{tbl_prefix}tools_histo_status` RENAME COLUMN `id_tools_status` TO `id_tools_histo_status`;
ALTER TABLE IF EXISTS `{tbl_prefix}tools_status` RENAME `{tbl_prefix}tools_histo_status`;
ALTER TABLE `{tbl_prefix}tools_histo_status` RENAME COLUMN IF EXISTS `id_tools_status` TO `id_tools_histo_status`;

SET @constraint_name = (SELECT CONSTRAINT_NAME
FROM information_schema.key_column_usage
WHERE CONSTRAINT_SCHEMA = '{dbname}'
AND TABLE_NAME = '{tbl_prefix}tools'
AND REFERENCED_TABLE_NAME IS NOT NULL);

SET @sql = 'ALTER TABLE `{tbl_prefix}tools` DROP FOREIGN KEY @constraint_name;';
SET @sql = 'ALTER TABLE `{tbl_prefix}tools` DROP FOREIGN KEY IF EXISTS @constraint_name;';
SET @sql = REPLACE(@sql, '@constraint_name', @constraint_name);

PREPARE alterTable FROM @sql;
EXECUTE alterTable;
ALTER TABLE `{tbl_prefix}tools` DROP COLUMN id_tools_status, DROP COLUMN elements_total, DROP COLUMN elements_done;

ALTER TABLE `{tbl_prefix}tools`
DROP COLUMN IF EXISTS id_tools_status,
DROP COLUMN IF EXISTS elements_total,
DROP COLUMN IF EXISTS elements_done;

CREATE TABLE IF NOT EXISTS `{tbl_prefix}tools_histo`
(
Expand All @@ -29,8 +32,8 @@ CREATE TABLE IF NOT EXISTS `{tbl_prefix}tools_histo`
COLLATE utf8mb4_unicode_520_ci;

ALTER TABLE `{tbl_prefix}tools_histo`
ADD CONSTRAINT `id_tools_histo` FOREIGN KEY (`id_tool`) REFERENCES `{tbl_prefix}tools` (`id_tool`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `id_tools_histo_status` FOREIGN KEY (`id_tools_histo_status`) REFERENCES `{tbl_prefix}tools_histo_status` (`id_tools_histo_status`) ON DELETE NO ACTION ON UPDATE NO ACTION;
ADD CONSTRAINT `id_tools_histo` FOREIGN KEY IF NOT EXISTS (`id_tool`) REFERENCES `{tbl_prefix}tools` (`id_tool`) ON DELETE NO ACTION ON UPDATE NO ACTION,
ADD CONSTRAINT `id_tools_histo_status` FOREIGN KEY IF NOT EXISTS (`id_tools_histo_status`) REFERENCES `{tbl_prefix}tools_histo_status` (`id_tools_histo_status`) ON DELETE NO ACTION ON UPDATE NO ACTION;

CREATE TABLE IF NOT EXISTS `{tbl_prefix}tools_histo_log`
(
Expand All @@ -44,7 +47,7 @@ CREATE TABLE IF NOT EXISTS `{tbl_prefix}tools_histo_log`
COLLATE utf8mb4_unicode_520_ci;

ALTER TABLE `{tbl_prefix}tools_histo_log`
ADD CONSTRAINT `id_tools_histo_log` FOREIGN KEY (`id_histo`) REFERENCES `{tbl_prefix}tools_histo` (`id_histo`) ON DELETE NO ACTION ON UPDATE NO ACTION;
ADD CONSTRAINT `id_tools_histo_log` FOREIGN KEY IF NOT EXISTS (`id_histo`) REFERENCES `{tbl_prefix}tools_histo` (`id_histo`) ON DELETE NO ACTION ON UPDATE NO ACTION;

UPDATE `{tbl_prefix}tools` SET function_name = REPLACE(function_name, 'AdminTool::', '');

Expand All @@ -69,7 +72,13 @@ VALUES (@id_language_key, 'Afficher les derniers journaux', @language_id_fra);

ALTER TABLE `{tbl_prefix}tools` ADD COLUMN IF NOT EXISTS `code` VARCHAR(32) NOT NULL;
UPDATE `{tbl_prefix}tools` SET `code` = REPLACE( language_key_label,'_label', '');
ALTER TABLE `{tbl_prefix}tools` ADD UNIQUE(`code`);
DELETE FROM `{tbl_prefix}tools`
WHERE `id_tool` NOT IN(
SELECT MIN(`id_tool`)
FROM `{tbl_prefix}tools`
GROUP BY `code`
);
ALTER TABLE `{tbl_prefix}tools` ADD UNIQUE IF NOT EXISTS(`code`);

SET @language_key = 'tool_started' COLLATE utf8mb4_unicode_520_ci;
INSERT IGNORE INTO `{tbl_prefix}languages_keys` (`language_key`) VALUES (@language_key);
Expand Down
18 changes: 18 additions & 0 deletions upload/cb_install/sql/5.5.0/00376.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
SET @language_id_eng = (SELECT `language_id` FROM `{tbl_prefix}languages` WHERE language_code = 'en');
SET @language_id_fra = (SELECT `language_id` FROM `{tbl_prefix}languages` WHERE language_code = 'fr');

SET @language_key = 'update_category' COLLATE utf8mb4_unicode_520_ci;
INSERT IGNORE INTO `{tbl_prefix}languages_keys` (`language_key`) VALUES (@language_key);
SET @id_language_key = (SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` COLLATE utf8mb4_unicode_520_ci = @language_key);
INSERT IGNORE INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES (@id_language_key, 'Update category', @language_id_eng);
INSERT IGNORE INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES (@id_language_key, 'Mettre à jour la catégorie', @language_id_fra);

SET @language_key = 'add_new_category' COLLATE utf8mb4_unicode_520_ci;
INSERT IGNORE INTO `{tbl_prefix}languages_keys` (`language_key`) VALUES (@language_key);
SET @id_language_key = (SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` COLLATE utf8mb4_unicode_520_ci = @language_key);
INSERT IGNORE INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES (@id_language_key, 'Add new category', @language_id_eng);
INSERT IGNORE INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES (@id_language_key, 'Ajouter une nouvelle catégorie', @language_id_fra);
4 changes: 4 additions & 0 deletions upload/cb_install/sql/language_ENG.sql
Original file line number Diff line number Diff line change
Expand Up @@ -2229,3 +2229,7 @@ INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translatio
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'select_as_default_x'), 'Select as default %s', @language_id);
INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'enable_x_field'), 'Enable %s field', @language_id);
INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'update_category'), 'Update category', @language_id);
INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'add_new_category'), 'Add new category', @language_id);
4 changes: 4 additions & 0 deletions upload/cb_install/sql/language_FRA.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1391,3 +1391,7 @@ INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translatio
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'select_as_default_x'), 'Sélectionner comme %s par défaut', @language_id);
INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'enable_x_field'), 'Activer le champ %s', @language_id);
INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'update_category'), 'Mettre à jour la catégorie', @language_id);
INSERT INTO `{tbl_prefix}languages_translations` (`id_language_key`, `translation`, `language_id`)
VALUES ((SELECT id_language_key FROM `{tbl_prefix}languages_keys` WHERE `language_key` = 'add_new_category'), 'Ajouter une nouvelle catégorie', @language_id);
4 changes: 3 additions & 1 deletion upload/cb_install/sql/languages.sql
Original file line number Diff line number Diff line change
Expand Up @@ -1119,4 +1119,6 @@ VALUES ('admin_setting'),
('default_x'),
('option_require_x_enabled'),
('select_as_default_x'),
('enable_x_field');
('enable_x_field'),
('update_category'),
('add_new_category');
2 changes: 1 addition & 1 deletion upload/changelog/550.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"version":"5.5.0",
"revision":"375",
"revision":"376",
"status":"dev",
"detail":[
{
Expand Down
Loading

0 comments on commit 7d41f21

Please sign in to comment.