tool-googlebooks.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  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. /**
  21. * this is the special file for a tool.
  22. * Requirements and more information come from the main tool.php file
  23. */
  24. require_once 'lib/googlebookparser.class.php';
  25. if(file_exists(PATH_ABSOLUTE.'/config/config-googlebooks.php')) {
  26. require_once 'config/config-googlebooks.php';
  27. }
  28. $GOOGLEBOOKS = new GoogleBooks(array(
  29. 'browserAgent' => TOOL_GOOGLEBOOKS_BROWSER_AGENT,
  30. 'browserLang' => TOOL_GOOGLEBOOKS_BROWSER_ACCEPT_LANG,
  31. 'browserAccept' => TOOL_GOOGLEBOOKS_BROWSER_ACCEPT,
  32. 'debug' => true
  33. ));
  34. $TemplateData['bookData'] = array();
  35. $TemplateData['saveToSelection'] = '';
  36. $TemplateData['showMatchingForm'] = false;
  37. // prepare fields to save into selection
  38. // create one time and then reuse it
  39. $collectionFields = $ManangeCollectionsFields->getExistingFields(false, true);
  40. if(!empty($collectionFields)) {
  41. foreach ($collectionFields as $k=>$v) {
  42. $TemplateData['saveToSelection'] .= "<option value='".$k."' sel_".$v['identifier'].">".$I18n->t($v['displayname'])."</option>\n";
  43. }
  44. }
  45. if(isset($_POST['submitFormSearch'])) {
  46. $fdata = $_POST['fdata'];
  47. if (!empty($fdata)) {
  48. $search = trim($fdata['search']);
  49. $search = Summoner::validate($search) ? $search : false;
  50. if(!empty($search)) {
  51. $booksearch = $GOOGLEBOOKS->searchForISBN($search);
  52. if(!empty($booksearch)) {
  53. $TemplateData['bookData'] = $booksearch;
  54. $TemplateData['showMatchingForm'] = true;
  55. } else {
  56. $TemplateData['message']['content'] = $I18n->t('global.message.nothingFound');
  57. $TemplateData['message']['status'] = "error";
  58. }
  59. }
  60. else {
  61. $TemplateData['message']['content'] = $I18n->t('global.message.invalidSearchTerm');
  62. $TemplateData['message']['status'] = "error";
  63. }
  64. }
  65. }
  66. if(isset($_POST['submitFormSave'])) {
  67. $fdata = $_POST['fdata'];
  68. if (!empty($fdata)) {
  69. // build data array based on submit
  70. // see creation log for structure
  71. $_data = array();
  72. foreach($fdata['into'] as $k=>$v) {
  73. if(!empty($v) && isset($fdata['from'][$k])) {
  74. if(isset($collectionFields[$v])) {
  75. $_data[$v] = $collectionFields[$v];
  76. $_data[$v]['valueToSave'] = $fdata['from'][$k];
  77. // special case for image
  78. if($k == "cover") {
  79. $fieldData = array();
  80. $_f = $GOOGLEBOOKS->downloadCover($fdata['from'][$k]);
  81. if($_f && is_file($_f)) {
  82. $_e = UPLOAD_ERR_OK;
  83. // build _FILES based on regular add form
  84. $fieldData['name'][$_data[$v]['identifier']] = 'cover.jpg';
  85. $fieldData['type'][$_data[$v]['identifier']] = mime_content_type($_f);
  86. $fieldData['size'][$_data[$v]['identifier']] = filesize($_f);
  87. $fieldData['tmp_name'][$_data[$v]['identifier']] = $_f;
  88. $fieldData['error'][$_data[$v]['identifier']] = UPLOAD_ERR_OK;
  89. $fieldData['rebuildUpload'][$_data[$v]['identifier']] = true;
  90. }
  91. $_data[$v]['uploadData'] = $fieldData;
  92. }
  93. }
  94. }
  95. }
  96. $_r = $Tools->getDefaultCreationInfo();
  97. if(!empty($TemplateData['editEntry'])) {
  98. // update existing one
  99. $do = $Manageentry->create($_data,
  100. $_r['id'],
  101. $_r['group'],
  102. $_r['rights'],
  103. $TemplateData['editEntry']['id']
  104. );
  105. $TemplateData['message']['content'] = $I18n->t('global.message.dataSaved');
  106. }
  107. else {
  108. // create into loaded collection
  109. $do = $Manageentry->create($_data,
  110. $_r['id'],
  111. $_r['group'],
  112. $_r['rights']
  113. );
  114. $TemplateData['message']['content'] = $I18n->t('global.message.dataSaved')." <a href='index.php?p=manageentry&collection=".$collection['id']."&id=".$do."'>".$I18n->t('global.view')."</a>";
  115. }
  116. if(!empty($do)) {
  117. $TemplateData['message']['status'] = "success";
  118. }
  119. else {
  120. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotBeSaved');
  121. $TemplateData['message']['status'] = "error";
  122. }
  123. // remove tmp file
  124. if(isset($_f) && is_file($_f) && file_exists($_f)) {
  125. unlink($_f);
  126. }
  127. }
  128. }
  129. /**
  130. * Helper function. Takes the prebuild options for the target selection field and search for a matching key.
  131. * Since the optionString is prebuild, avoiding looping over and over again, the selection needs to be done
  132. * by search and replace.
  133. * Checks if TOOL_GOOGLEBOOKS_FIELDS_TO is defined and a matching key=>value pair is available
  134. *
  135. * @param string $optionString
  136. * @param string $googleKey
  137. * @return string
  138. */
  139. function toolMethod_GetTargetSelection(string $optionString, string $googleKey): string {
  140. if(defined('TOOL_GOOGLEBOOKS_FIELDS_TO') & !empty($googleKey)) {
  141. if(isset(TOOL_GOOGLEBOOKS_FIELDS_TO[$googleKey])) {
  142. $_k = "sel_".TOOL_GOOGLEBOOKS_FIELDS_TO[$googleKey];
  143. $optionString = str_replace($_k,'selected="selected"',$optionString);
  144. }
  145. }
  146. return $optionString;
  147. }