bulkedit.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. <?php
  2. /**
  3. * Bibliotheca
  4. *
  5. * Copyright 2018-2023 Johannes Keßler
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see http://www.gnu.org/licenses/gpl-3.0.
  19. */
  20. require_once 'lib/trite.class.php';
  21. $Trite = new Trite($DB,$Doomguy);
  22. require_once 'lib/manageentry.class.php';
  23. $ManageEntry = new Manageentry($DB,$Doomguy);
  24. $_collection = '';
  25. if(isset($_GET['collection']) && !empty($_GET['collection'])) {
  26. $_collection = trim($_GET['collection']);
  27. $_collection = Summoner::validate($_collection,'digit') ? $_collection : '';
  28. }
  29. $TemplateData['loadedCollection'] = array();
  30. $TemplateData['pageTitle'] = 'Bulkedit';
  31. $TemplateData['itemsToWorkWith'] = array();
  32. if(!empty($_collection)) {
  33. $TemplateData['loadedCollection'] = $Trite->load($_collection, "write");
  34. if(!empty($TemplateData['loadedCollection'])) {
  35. $ManageEntry->setCollection($Trite->param('id'));
  36. if(isset($_POST['bulkedit']) && !empty($_POST['bulkedit'])) {
  37. foreach($_POST['bulkedit'] as $e) {
  38. $TemplateData['itemsToWorkWith'][] = $ManageEntry->getEditData($e);
  39. }
  40. // needs this editData array since manageentry functionality is used
  41. $TemplateData['editData'] = array();
  42. $TemplateData['editFields'] = $ManageEntry->getEditFields();
  43. // @see manageentry for similar process
  44. if(isset($_POST['submitForm'])) {
  45. $fdata = $_POST['fdata'];
  46. $fupload = array('name' => ''); // match $_FILES
  47. if(!empty($_FILES) && isset($_FILES['fdata'])) {
  48. $fupload = $_FILES['fdata'];
  49. }
  50. $_fieldsToSave = array();
  51. // default
  52. $_owner = $Doomguy->param('id');
  53. $_group = $Trite->param('group');
  54. $_rights = $Trite->param('rights');
  55. if (!empty($fdata)) {
  56. foreach ($TemplateData['editFields'] as $fieldId=>$fieldData) {
  57. if(isset($fdata['additionalEditOption'][$fieldData['identifier']])) {
  58. $fieldData['bulkeditMethod'] = $fdata['additionalEditOption'][$fieldData['identifier']];
  59. if(isset($fdata[$fieldData['identifier']])) {
  60. $_value = trim($fdata[$fieldData['identifier']]);
  61. $fieldData['valueToSave'] = trim($fdata[$fieldData['identifier']]);
  62. $_fieldsToSave[$fieldData['identifier']] = $fieldData;
  63. } elseif(isset($fupload['name'][$fieldData['identifier']])) {
  64. // special case upload
  65. // $_FILES data is combined
  66. $fieldData['uploadData'] = $fupload;
  67. $_fieldsToSave[$fieldData['identifier']] = $fieldData;
  68. }
  69. }
  70. }
  71. }
  72. // now update the entries with the gathered data to save
  73. if(!empty($_fieldsToSave)) {
  74. $_messages = array();
  75. foreach ($TemplateData['itemsToWorkWith'] as $entry) {
  76. foreach ($_fieldsToSave as $ident=>$data) {
  77. switch ($data['bulkeditMethod']) {
  78. case 'add':
  79. if(is_array($entry[$ident])) { // lookup multiple
  80. $data['valueToSave'] = implode(",", $entry[$ident]).",".$data['valueToSave'];
  81. }
  82. else {
  83. $data['valueToSave'] = $entry[$ident].' '.$data['valueToSave'] ;
  84. }
  85. break;
  86. case 'replace':
  87. // leave it as it is
  88. break;
  89. case 'empty':
  90. $data['valueToSave'] = '';
  91. break;
  92. }
  93. $_fieldsToSave[$ident] = $data;
  94. }
  95. $do = $ManageEntry->create($_fieldsToSave, $_owner, $_group, $_rights, $entry['id']);
  96. if ($do !== 0) {
  97. $_messages[] = $I18n->t('bulkedit.message.entryUpdated').' '.$entry['id'];
  98. } else {
  99. $_messages[] = $I18n->t('bulkedit.message.canNotUpdate').' '.$entry['id'];
  100. }
  101. unset($data);
  102. }
  103. $TemplateData['message']['content'] = implode("<br />",$_messages);
  104. $TemplateData['message']['status'] = "info";
  105. }
  106. }
  107. }
  108. else {
  109. $TemplateData['message']['content'] = $I18n->t('bulkedit.message.missingItems');
  110. $TemplateData['message']['status'] = "error";
  111. }
  112. }
  113. else {
  114. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotLoadCollection');
  115. $TemplateData['message']['status'] = "error";
  116. }
  117. }