tool-imdbweb.php 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182
  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/imdbwebparser.class.php';
  25. if(file_exists(PATH_ABSOLUTE.'/config/config-imdbweb.php')) {
  26. require_once 'config/config-imdbweb.php';
  27. }
  28. $IMDB = new IMDB(array(
  29. 'sSearchFor' => TOOL_IMDBWEB_SEARCH,
  30. 'showFields' => TOOL_IMDBWEB_FIELDS,
  31. 'storage' => PATH_SYSTEMOUT,
  32. 'browserAgent' => TOOL_IMDBWEB_BROWSER_AGENT,
  33. 'browserLang' => TOOL_IMDBWEB_BROWSER_ACCEPT_LANG,
  34. 'browserAccept' => TOOL_IMDBWEB_BROWSER_ACCEPT,
  35. 'debug' => false
  36. ));
  37. $TemplateData['movieData'] = array();
  38. $TemplateData['saveToSelection'] = '';
  39. $TemplateData['showMatchingForm'] = false;
  40. // prepare fields to save into selection
  41. // create one time and then reuse it
  42. $collectionFields = $ManangeCollectionsFields->getExistingFields(false, true);
  43. if(!empty($collectionFields)) {
  44. foreach ($collectionFields as $k=>$v) {
  45. $TemplateData['saveToSelection'] .= "<option value='".$k."' sel_".$v['identifier'].">".$I18n->t($v['displayname'])."</option>\n";
  46. }
  47. }
  48. if(isset($_POST['submitFormSearch'])) {
  49. $fdata = $_POST['fdata'];
  50. if (!empty($fdata)) {
  51. $search = trim($fdata['search']);
  52. $search = Summoner::validate($search) ? $search : false;
  53. if(!empty($search)) {
  54. try {
  55. $IMDB->search($search);
  56. }
  57. catch (Exception $e) {
  58. if(DEBUG) Summoner::sysLog("[DEBUG] imdb search catch: ".$e->getMessage());
  59. }
  60. if ($IMDB->isReady) {
  61. $TemplateData['movieData'] = $IMDB->getAll();
  62. $TemplateData['movieImdbId'] = "tt".$IMDB->iId; // this is the IMDB id you can search for
  63. $TemplateData['showMatchingForm'] = true;
  64. } else {
  65. $TemplateData['message']['content'] = $I18n->t('global.message.nothingFound');
  66. $TemplateData['message']['status'] = "error";
  67. }
  68. }
  69. else {
  70. $TemplateData['message']['content'] = $I18n->t('global.message.invalidSearchTerm');
  71. $TemplateData['message']['status'] = "error";
  72. }
  73. }
  74. }
  75. if(isset($_POST['submitFormSave'])) {
  76. $fdata = $_POST['fdata'];
  77. if (!empty($fdata)) {
  78. $_imdbId = $fdata['imdbId'];
  79. $_imdbId = Summoner::validate($_imdbId,'nospace') ? $_imdbId : false;
  80. if(!empty($_imdbId)) {
  81. try {
  82. $IMDB->search($_imdbId); // cache used
  83. }
  84. catch (Exception $e) {
  85. if(DEBUG) Summoner::sysLog("[DEBUG] imdb search catch: ".$e->getMessage());
  86. }
  87. if ($IMDB->isReady) {
  88. $TemplateData['movieImdbId'] = $_imdbId;
  89. $_movieData = $IMDB->getAll();
  90. // build data array based on submit
  91. // see creation log for structure
  92. $_data = array();
  93. foreach($fdata['into'] as $k=>$v) {
  94. if(!empty($v)) {
  95. $_t = $IMDB->$k();
  96. // multiple selections format for field type lookup_multiple
  97. if(strstr($_t, $IMDB->sSeparator)) {
  98. $_t = str_replace($IMDB->sSeparator,",", $_t);
  99. }
  100. if(isset($collectionFields[$v])) {
  101. $_data[$v] = $collectionFields[$v];
  102. $_data[$v]['valueToSave'] = $_t;
  103. }
  104. }
  105. }
  106. $_r = $Tools->getDefaultCreationInfo();
  107. if(!empty($TemplateData['editEntry'])) {
  108. // update existing one
  109. $do = $Manageentry->create($_data,
  110. $_r['id'],
  111. $_r['group'],
  112. $_r['rights'],
  113. $TemplateData['editEntry']['id']
  114. );
  115. $TemplateData['message']['content'] = $I18n->t('global.message.dataSaved');
  116. }
  117. else {
  118. // create into loaded collection
  119. $do = $Manageentry->create($_data,
  120. $_r['id'],
  121. $_r['group'],
  122. $_r['rights']
  123. );
  124. $TemplateData['message']['content'] = $I18n->t('global.message.dataSaved')." <a href='index.php?p=manageentry&collection=".$collection['id']."&id=".$do."'>".$I18n->t('global.view')."</a>";
  125. }
  126. if(!empty($do)) {
  127. $TemplateData['message']['status'] = "success";
  128. }
  129. else {
  130. $TemplateData['message']['content'] = $I18n->t('global.message.couldNotBeSaved');
  131. $TemplateData['message']['status'] = "error";
  132. }
  133. } else {
  134. $TemplateData['message']['content'] = $I18n->t('global.message.nothingFound');
  135. $TemplateData['message']['status'] = "error";
  136. }
  137. }
  138. else {
  139. $TemplateData['message']['content'] = "IMDB search result information lost.";
  140. $TemplateData['message']['status'] = "error";
  141. }
  142. }
  143. }
  144. /**
  145. * Helper function. Takes the prebuild options for the target selection field and search for a matching key.
  146. * Since the optionString is prebuild, avoiding looping over and over again, the selection needs to be done
  147. * by search and replace.
  148. * Checks if TOOL_IMDBWEB_FIELDS_TO is defined and a matching key=>value pair is available
  149. *
  150. * @param string $optionString
  151. * @param string $imdbKey
  152. * @return string
  153. */
  154. function toolMethod_GetTargetSelection(string $optionString, string $imdbKey): string {
  155. if(defined('TOOL_IMDBWEB_FIELDS_TO') & !empty($imdbKey)) {
  156. if(isset(TOOL_IMDBWEB_FIELDS_TO[$imdbKey])) {
  157. $_k = "sel_".TOOL_IMDBWEB_FIELDS_TO[$imdbKey];
  158. $optionString = str_replace($_k,'selected="selected"',$optionString);
  159. }
  160. }
  161. return $optionString;
  162. }