manageentry.php 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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. $TemplateData['pageTitle'] = 'Manage entry - ';
  25. $TemplateData['editFields'] = array();
  26. $TemplateData['editData'] = array();
  27. $TemplateData['loadedCollection'] = '';
  28. $TemplateData['storagePath'] = '';
  29. $TemplateData['existingCollections'] = array();
  30. $TemplateData['possibleDuplicates'] = array();
  31. $TemplateData['_editFieldViewDefault'] = Summoner::themefile('manageentry/field-unknown.html', UI_THEME);
  32. $_collection = '';
  33. if(isset($_GET['collection']) && !empty($_GET['collection'])) {
  34. $_collection = trim($_GET['collection']);
  35. $_collection = Summoner::validate($_collection,'digit') ? $_collection : '';
  36. }
  37. $_id = '';
  38. if(isset($_GET['id']) && !empty($_GET['id'])) {
  39. $_id = trim($_GET['id']);
  40. $_id = Summoner::validate($_id,'digit') ? $_id : '';
  41. }
  42. if(!empty($_collection)) {
  43. $TemplateData['loadedCollection'] = $Trite->load($_collection, "write");
  44. if(!empty($TemplateData['loadedCollection'])) {
  45. $ManageEntry->setCollection($Trite->param('id'));
  46. $TemplateData['editFields'] = $ManageEntry->getEditFields();
  47. $TemplateData['availableTools'] = $Trite->getAvailableTools();
  48. $TemplateData['pageTitle'] = 'Add - '.$Trite->param('name');
  49. if(!empty($_id)) {
  50. $TemplateData['storagePath'] = PATH_WEB_STORAGE . '/' . $_collection . '/' . $_id;
  51. // prefill template data. Used also later to check if on edit mode
  52. $TemplateData['editData'] = $ManageEntry->getEditData($_id);
  53. // special case. Title field should be always available.
  54. if(!isset($TemplateData['editData']['title'])) {
  55. $TemplateData['message']['content'] = $I18n->t('manageentry.message.noTitle');
  56. $TemplateData['message']['status'] = "error";
  57. }
  58. else {
  59. $TemplateData['pageTitle'] = 'Edit - '.$TemplateData['editData']['title'].' - '.$Trite->param('name');
  60. $TemplateData['possibleDuplicates'] = $ManageEntry->checkForDuplicates($TemplateData['editData']);
  61. }
  62. }
  63. if(isset($_POST['submitForm'])) {
  64. $fdata = $_POST['fdata'];
  65. $fupload = array('name' => ''); // match $_FILES
  66. if(!empty($_FILES) && isset($_FILES['fdata'])) {
  67. $fupload = $_FILES['fdata'];
  68. }
  69. $_fieldsToSave = array();
  70. if (!empty($fdata)) {
  71. // default
  72. $_owner = $Doomguy->param('id');
  73. $_group = $Trite->param('group');
  74. $_rights = $Trite->param('rights');
  75. if(!empty($fdata['rights'])) {
  76. $_rightsString = Summoner::prepareRightsString($fdata['rights']);
  77. if(!empty($_rightsString)) {
  78. $_rights = $_rightsString;
  79. }
  80. }
  81. foreach ($TemplateData['editFields'] as $fieldId=>$fieldData) {
  82. if(isset($fdata[$fieldData['identifier']])) {
  83. $_value = trim($fdata[$fieldData['identifier']]);
  84. $fieldData['valueToSave'] = trim($fdata[$fieldData['identifier']]);
  85. $_fieldsToSave[$fieldData['identifier']] = $fieldData;
  86. } elseif(isset($fupload['name'][$fieldData['identifier']])) { // special case upload
  87. if(isset($fdata[$fieldData['identifier']."_delete"])) {
  88. $fieldData['deleteData'] = $fdata[$fieldData['identifier']."_delete"];
  89. }
  90. // $_FILES data is combined if multiple
  91. $fieldData['uploadData'] = $fupload;
  92. $_fieldsToSave[$fieldData['identifier']] = $fieldData;
  93. }
  94. }
  95. // special case. Title field should be always available.
  96. if(!empty($TemplateData['editData']['title'])) { // EDIT
  97. if(isset($fdata['doDelete'])) {
  98. $do = $ManageEntry->delete($_id);
  99. if ($do === true) {
  100. $TemplateData['refresh'] = 'index.php?p=collections&collection='.$_collection;
  101. } else {
  102. $TemplateData['message']['content'] = $I18n->t('manageentry.message.couldNotBeRemoved');
  103. $TemplateData['message']['status'] = "error";
  104. }
  105. } elseif (!empty($_fieldsToSave) && isset($_fieldsToSave['title'])) {
  106. $do = $ManageEntry->create($_fieldsToSave, $_owner, $_group, $_rights, $_id);
  107. if ($do !== 0) {
  108. $TemplateData['refresh'] = 'index.php?p=entry&collection='.$_collection.'&id='.$_id;
  109. } else {
  110. $TemplateData['message']['content'] = $I18n->t('manageentry.message.couldNotBeUpdated');
  111. $TemplateData['message']['status'] = "error";
  112. }
  113. }
  114. }
  115. else { // ADD
  116. // special case. Title field should be always available.
  117. if (!empty($_fieldsToSave) && !empty($_fieldsToSave['title']['valueToSave'])) {
  118. $do = $ManageEntry->create($_fieldsToSave, $_owner, $_group, $_rights);
  119. if (!empty($do)) {
  120. $TemplateData['message']['content'] = "<a href='index.php?p=entry&collection=".$_collection."&id=".$do."'>".$I18n->t('manageentry.message.viewNewEntry')."</a>
  121. | <a href='index.php?p=manageentry&collection=".$_collection."&id=".$do."'>".$I18n->t('manageentry.message.editNewEntry')."</a>";
  122. $TemplateData['message']['status'] = "success";
  123. } else {
  124. // use editData to display given data
  125. $TemplateData['editData'] = $fdata;
  126. $TemplateData['message']['content'] = $I18n->t('manageentry.message.couldNotBeAdded');
  127. $TemplateData['message']['status'] = "error";
  128. }
  129. } else {
  130. // use editData to display given data
  131. $TemplateData['editData'] = $fdata;
  132. $TemplateData['message']['content'] = $I18n->t('manageentry.message.missingTitle');
  133. $TemplateData['message']['status'] = "error";
  134. }
  135. }
  136. }
  137. }
  138. }
  139. else {
  140. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotLoadCollection');
  141. $TemplateData['message']['status'] = "error";
  142. $TemplateData['existingCollections'] = $Trite->getCollections("write");
  143. }
  144. }
  145. else {
  146. $TemplateData['pageTitle'] .= 'collection overview';
  147. $TemplateData['existingCollections'] = $Trite->getCollections("write");
  148. }