editcategories.inc.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131
  1. <?php
  2. /**
  3. * Insipid
  4. * Personal web-bookmark-system
  5. *
  6. * Copyright 2016-2020 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 = false;
  29. $formData = false;
  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'][] = 'Categories renamed successfully.';
  54. foreach ($catToUpdate as $k=>$v) {
  55. $catObjAlternative = new Category($DB);
  56. $do = $catObjAlternative->initbystring($v,true);
  57. if($do === 1) { # existing
  58. // the target cat should not be removed!
  59. $catDoNotDeleteFromUpdate[$catObjAlternative->getData('id')] = $catObjAlternative->getData('id');
  60. $catObjOld = new Category($DB);
  61. if(!empty($catObjOld->initbyid($k))) {
  62. $linksToUpdate = $catObjOld->getReleations();
  63. if(!empty($linksToUpdate)) {
  64. foreach($linksToUpdate as $linkId) {
  65. $catObjAlternative->setRelation($linkId);
  66. }
  67. $catObjOld->delete();
  68. }
  69. }
  70. else {
  71. $submitFeedback['message'][] = 'Categories could not be renamed.';
  72. $submitFeedback['status'] = 'error';
  73. }
  74. }
  75. elseif ($do === 3) { # not existing one. Can be renamed
  76. $catObjRename = new Category($DB);
  77. if(!empty($catObjRename->initbyid($k))) {
  78. $catObjRename->rename($v);
  79. }
  80. }
  81. else {
  82. $submitFeedback['message'][] = 'Categories could not be renamed.';
  83. $submitFeedback['status'] = 'error';
  84. }
  85. }
  86. }
  87. if(!empty($deleteCategoryData)) {
  88. $submitFeedback['message'][] = 'Categories deleted successfully.';
  89. foreach($deleteCategoryData as $k=>$v) {
  90. if($v == "delete" && !isset($catDoNotDeleteFromUpdate[$k])) {
  91. $catObj = new Category($DB);
  92. $load = $catObj->initbyid($k);
  93. if($load !== false) {
  94. $catObj->delete();
  95. }
  96. else {
  97. $submitFeedback['message'][] = 'Categories could not be deleted.';
  98. $submitFeedback['status'] = 'error';
  99. }
  100. }
  101. }
  102. }
  103. if(!empty($newCategory)) {
  104. $submitFeedback['message'][] = 'Categories added successfully.';
  105. $catArr = Summoner::prepareTagOrCategoryStr($newCategory);
  106. foreach($catArr as $c) {
  107. $catObj = new Category($DB);
  108. $do = $catObj->initbystring($c);
  109. if($do === false) {
  110. $submitFeedback['message'][] = 'Category could not be added.';
  111. $submitFeedback['status'] = 'error';
  112. }
  113. }
  114. }
  115. }
  116. # show all the categories we have
  117. $categoryCollection = $Management->categories(false, true);
  118. $subHeadline = 'All the categories <i class="ion-md-filing"></i>';