editcategories.inc.php 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2023 Johannes Keßler
  7. *
  8. * Development starting from 2011: Johannes Keßler
  9. * https://www.bananas-playground.net/projekt/insipid/
  10. *
  11. * creator:
  12. * Luke Reeves <luke@neuro-tech.net>
  13. *
  14. * This program is free software: you can redistribute it and/or modify
  15. * it under the terms of the GNU General Public License as published by
  16. * the Free Software Foundation, either version 3 of the License, or
  17. * (at your option) any later version.
  18. *
  19. * This program is distributed in the hope that it will be useful,
  20. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  21. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  22. * GNU General Public License for more details.
  23. *
  24. * You should have received a copy of the GNU General Public License
  25. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  26. *
  27. */
  28. $submitFeedback = array();
  29. $formData = array();
  30. # very simple security check.
  31. # can/should be extended in the future.
  32. Summoner::simpleAuth();
  33. if(isset($_POST['category']) && !empty($_POST['category']) && isset($_POST['updateCategories'])) {
  34. $categoryData = $_POST['category'];
  35. $newCategory = $_POST['newCategory'];
  36. # first update then deletion and then add
  37. # adding a new one which matches an existing one will update it.
  38. $submitFeedback['message'] = array();
  39. $submitFeedback['status'] = 'success';
  40. $catToUpdate = array();
  41. foreach ($categoryData as $cid=>$cNewValue) {
  42. $_c = Summoner::validate($cNewValue,'nospace');
  43. if($_c === true) {
  44. $catToUpdate[$cid] = $cNewValue;
  45. }
  46. }
  47. $deleteCategoryData = array();
  48. if(isset($_POST['deleteCategory'])) {
  49. $deleteCategoryData = $_POST['deleteCategory'];
  50. }
  51. $catDoNotDeleteFromUpdate = array();
  52. if(!empty($catToUpdate)) {
  53. $submitFeedback['message'][] = $T->t('edit.category.renamed');
  54. foreach ($catToUpdate as $k=>$v) {
  55. $catObjAlternative = new Category($DB);
  56. $do = $catObjAlternative->initbystring($v,true);
  57. if($do === 1) { # existing
  58. if($k == $catObjAlternative->getData('id')) {
  59. // Rename to the same. Do nothing
  60. continue;
  61. }
  62. // the target cat should not be removed!
  63. $catDoNotDeleteFromUpdate[$catObjAlternative->getData('id')] = $catObjAlternative->getData('id');
  64. $catObjOld = new Category($DB);
  65. if(!empty($catObjOld->initbyid($k))) {
  66. $linksToUpdate = $catObjOld->getRelations();
  67. if(!empty($linksToUpdate)) {
  68. foreach($linksToUpdate as $linkId) {
  69. $catObjAlternative->setRelation($linkId);
  70. }
  71. $catObjOld->delete();
  72. }
  73. }
  74. else {
  75. $submitFeedback['message'][] = $T->t('edit.category.rename.fail');
  76. $submitFeedback['status'] = 'error';
  77. }
  78. }
  79. elseif ($do === 3) { # not existing one. Can be renamed
  80. $catObjRename = new Category($DB);
  81. if(!empty($catObjRename->initbyid($k))) {
  82. $catObjRename->rename($v);
  83. }
  84. }
  85. else {
  86. $submitFeedback['message'][] = $T->t('edit.category.rename.fail');
  87. $submitFeedback['status'] = 'error';
  88. }
  89. }
  90. }
  91. if(!empty($deleteCategoryData)) {
  92. $submitFeedback['message'][] = $T->t('edit.category.deleted');
  93. foreach($deleteCategoryData as $k=>$v) {
  94. if($v == "delete" && !isset($catDoNotDeleteFromUpdate[$k])) {
  95. $catObj = new Category($DB);
  96. $load = $catObj->initbyid($k);
  97. if(!empty($load)) {
  98. $catObj->delete();
  99. }
  100. else {
  101. $submitFeedback['message'][] = $T->t('edit.category.delete.fail');
  102. $submitFeedback['status'] = 'error';
  103. }
  104. }
  105. }
  106. }
  107. if(!empty($newCategory)) {
  108. $submitFeedback['message'][] = $T->t('edit.category.added');
  109. $catArr = Summoner::prepareTagOrCategoryStr($newCategory);
  110. foreach($catArr as $c) {
  111. $catObj = new Category($DB);
  112. $do = $catObj->initbystring($c);
  113. if($do === false) {
  114. $submitFeedback['message'][] = $T->t('edit.category.add.fail');
  115. $submitFeedback['status'] = 'error';
  116. }
  117. }
  118. }
  119. }
  120. # show all the categories we have
  121. $categoryCollection = $Management->categories(0, true);
  122. $subHeadline = $T->t('view.categories').' <i class="ion-md-filing"></i>';