tool-musicbrainz.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. * https://musicbrainz.org/doc/Development
  25. * http://musicbrainz.org/ws/2/release/?query=artist:broilers%20AND%20release:muerte%20AND%20format:CD&fmt=json
  26. * http://musicbrainz.org/ws/2/release/5eb27466-fe5e-4683-aa9c-1ca396741c7c?&fmt=json
  27. */
  28. require_once 'lib/musicbrainz.class.php';
  29. if(file_exists(PATH_ABSOLUTE.'/config/config-musicbrainz.php')) {
  30. require_once 'config/config-musicbrainz.php';
  31. }
  32. $Brainz = new Musicbrainz(array(
  33. 'resultLimit' => TOOL_BRAINZ_RESULT_LIMIT,
  34. 'browserAgent' => TOOL_BRAINZ_BROWSER_AGENT,
  35. 'browserLang' => TOOL_BRAINZ_BROWSER_ACCEPT_LANG,
  36. 'browserAccept' => TOOL_BRAINZ_BROWSER_ACCEPT,
  37. 'debug' => false
  38. ));
  39. $TemplateData['releases'] = array();
  40. $TemplateData['release'] = array();
  41. $TemplateData['saveToSelection'] = '';
  42. // prepare fields to save into selection
  43. // create one time and then reuse it
  44. $collectionFields = $ManangeCollectionsFields->getExistingFields(false, true);
  45. if(!empty($collectionFields)) {
  46. foreach ($collectionFields as $k=>$v) {
  47. $TemplateData['saveToSelection'] .= "<option value='".$k."' sel_".$v['identifier'].">".$I18n->t($v['displayname'])."</option>\n";
  48. }
  49. }
  50. if(isset($_POST['submitFormSearch'])) {
  51. $fdata = $_POST['fdata'];
  52. if (!empty($fdata)) {
  53. $artist = trim($fdata['artist']);
  54. $artist = Summoner::validate($artist) ? $artist : false;
  55. $album = trim($fdata['album']);
  56. $album = Summoner::validate($album) ? $album : false;
  57. if(!empty($artist) && !empty($album)) {
  58. $releaseSearch = $Brainz->searchForRelease($artist, $album);
  59. if(!empty($releaseSearch)) {
  60. $TemplateData['releases'] = $releaseSearch;
  61. } else {
  62. $TemplateData['message']['content'] = $I18n->t('global.message.nothingFound');
  63. $TemplateData['message']['status'] = "error";
  64. }
  65. }
  66. else {
  67. $TemplateData['message']['content'] = $I18n->t('global.message.invalidSearchTerm');
  68. $TemplateData['message']['status'] = "error";
  69. }
  70. }
  71. }
  72. if(isset($_POST['submitFormReleaseSelect'])) {
  73. if (isset($_POST['fdata'])) {
  74. $fdata = $_POST['fdata'];
  75. if (!empty($fdata)) {
  76. $releaseId = $fdata['rselect'];
  77. if(!empty($releaseId)) {
  78. $releaseInfo = $Brainz->getReleaseInfo($releaseId);
  79. if(!empty($releaseInfo)) {
  80. $TemplateData['release'] = $releaseInfo;
  81. } else {
  82. $TemplateData['message']['content'] = $I18n->t('global.message.nothingFound');
  83. $TemplateData['message']['status'] = "error";
  84. }
  85. }
  86. }
  87. }
  88. else {
  89. $TemplateData['message']['content'] = "Invalid selection";
  90. $TemplateData['message']['status'] = "error";
  91. }
  92. }
  93. if(isset($_POST['submitFormSave'])) {
  94. $fdata = $_POST['fdata'];
  95. if (!empty($fdata)) {
  96. // build data array based on submit
  97. // see creation log for structure
  98. $_data = array();
  99. foreach($fdata['into'] as $k=>$v) {
  100. if(!empty($v) && isset($fdata['from'][$k])) {
  101. if(isset($collectionFields[$v])) {
  102. $_data[$v] = $collectionFields[$v];
  103. $_data[$v]['valueToSave'] = $fdata['from'][$k];
  104. // special case for image
  105. if($k == "image") {
  106. $fieldData = array();
  107. $_f = $Brainz->downloadCover($fdata['from'][$k]);
  108. if($_f && is_file($_f)) {
  109. $_e = UPLOAD_ERR_OK;
  110. // build _FILES based on regular add form
  111. $fieldData['name'][$_data[$v]['identifier']] = 'cover.jpg';
  112. $fieldData['type'][$_data[$v]['identifier']] = mime_content_type($_f);
  113. $fieldData['size'][$_data[$v]['identifier']] = filesize($_f);
  114. $fieldData['tmp_name'][$_data[$v]['identifier']] = $_f;
  115. $fieldData['error'][$_data[$v]['identifier']] = UPLOAD_ERR_OK;
  116. $fieldData['rebuildUpload'][$_data[$v]['identifier']] = true;
  117. }
  118. $_data[$v]['uploadData'] = $fieldData;
  119. }
  120. }
  121. }
  122. }
  123. $_r = $Tools->getDefaultCreationInfo();
  124. if(!empty($TemplateData['editEntry'])) {
  125. // update existing one
  126. $do = $Manageentry->create($_data,
  127. $_r['id'],
  128. $_r['group'],
  129. $_r['rights'],
  130. $TemplateData['editEntry']['id']
  131. );
  132. $TemplateData['message']['content'] = $I18n->t('global.message.dataSaved');
  133. }
  134. else {
  135. // create into loaded collection
  136. $do = $Manageentry->create($_data,
  137. $_r['id'],
  138. $_r['group'],
  139. $_r['rights']
  140. );
  141. $TemplateData['message']['content'] = $I18n->t('global.message.dataSaved')." <a href='index.php?p=manageentry&collection=".$collection['id']."&id=".$do."'>".$I18n->t('global.view')."</a>";
  142. }
  143. if(!empty($do)) {
  144. $TemplateData['message']['status'] = "success";
  145. }
  146. else {
  147. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotBeSaved');
  148. $TemplateData['message']['status'] = "error";
  149. }
  150. // remove tmp file
  151. if(isset($_f) && is_file($_f) && file_exists($_f)) {
  152. unlink($_f);
  153. }
  154. }
  155. }
  156. /**
  157. * Helper function. Takes the prebuild options for the target selection field and search for a matching key.
  158. * Since the optionString is prebuild, avoiding looping over and over again, the selection needs to be done
  159. * by search and replace.
  160. * Checks if TOOL_BRAINZ_FIELDS_TO is defined and a matching key=>value pair is available
  161. *
  162. * @param string $optionString
  163. * @param string $key
  164. * @return string
  165. */
  166. function toolMethod_GetTargetSelection(string $optionString, string $key): string {
  167. if(defined('TOOL_BRAINZ_FIELDS_TO') & !empty($key)) {
  168. if(isset(TOOL_BRAINZ_FIELDS_TO[$key])) {
  169. $_k = "sel_".TOOL_BRAINZ_FIELDS_TO[$key];
  170. $optionString = str_replace($_k,'selected="selected"',$optionString);
  171. }
  172. }
  173. return $optionString;
  174. }