TOOL_GOOGLEBOOKS_BROWSER_AGENT,
'browserLang' => TOOL_GOOGLEBOOKS_BROWSER_ACCEPT_LANG,
'browserAccept' => TOOL_GOOGLEBOOKS_BROWSER_ACCEPT,
'debug' => true
));
$TemplateData['bookData'] = array();
$TemplateData['saveToSelection'] = '';
$TemplateData['showMatchingForm'] = false;
// prepare fields to save into selection
// create one time and then reuse it
$collectionFields = $ManangeCollectionsFields->getExistingFields(false, true);
if(!empty($collectionFields)) {
foreach ($collectionFields as $k=>$v) {
$TemplateData['saveToSelection'] .= "\n";
}
}
if(isset($_POST['submitFormSearch'])) {
$fdata = $_POST['fdata'];
if (!empty($fdata)) {
$search = trim($fdata['search']);
$search = Summoner::validate($search) ? $search : false;
if(!empty($search)) {
$booksearch = $GOOGLEBOOKS->searchForISBN($search);
if(!empty($booksearch)) {
$TemplateData['bookData'] = $booksearch;
$TemplateData['showMatchingForm'] = true;
} else {
$TemplateData['message']['content'] = $I18n->t('global.message.nothingFound');
$TemplateData['message']['status'] = "error";
}
}
else {
$TemplateData['message']['content'] = $I18n->t('global.message.invalidSearchTerm');
$TemplateData['message']['status'] = "error";
}
}
}
if(isset($_POST['submitFormSave'])) {
$fdata = $_POST['fdata'];
if (!empty($fdata)) {
// build data array based on submit
// see creation log for structure
$_data = array();
foreach($fdata['into'] as $k=>$v) {
if(!empty($v) && isset($fdata['from'][$k])) {
if(isset($collectionFields[$v])) {
$_data[$v] = $collectionFields[$v];
$_data[$v]['valueToSave'] = $fdata['from'][$k];
// special case for image
if($k == "cover") {
$fieldData = array();
$_f = $GOOGLEBOOKS->downloadCover($fdata['from'][$k]);
if($_f && is_file($_f)) {
$_e = UPLOAD_ERR_OK;
// build _FILES based on regular add form
$fieldData['name'][$_data[$v]['identifier']] = 'cover.jpg';
$fieldData['type'][$_data[$v]['identifier']] = mime_content_type($_f);
$fieldData['size'][$_data[$v]['identifier']] = filesize($_f);
$fieldData['tmp_name'][$_data[$v]['identifier']] = $_f;
$fieldData['error'][$_data[$v]['identifier']] = UPLOAD_ERR_OK;
$fieldData['rebuildUpload'][$_data[$v]['identifier']] = true;
}
$_data[$v]['uploadData'] = $fieldData;
}
}
}
}
$_r = $Tools->getDefaultCreationInfo();
if(!empty($TemplateData['editEntry'])) {
// update existing one
$do = $Manageentry->create($_data,
$_r['id'],
$_r['group'],
$_r['rights'],
$TemplateData['editEntry']['id']
);
$TemplateData['message']['content'] = $I18n->t('global.message.dataSaved');
}
else {
// create into loaded collection
$do = $Manageentry->create($_data,
$_r['id'],
$_r['group'],
$_r['rights']
);
$TemplateData['message']['content'] = $I18n->t('global.message.dataSaved')." ".$I18n->t('global.view')."";
}
if(!empty($do)) {
$TemplateData['message']['status'] = "success";
}
else {
$TemplateData['message']['content'] = $I18n->t('global.message.couldNotBeSaved');
$TemplateData['message']['status'] = "error";
}
// remove tmp file
if(isset($_f) && is_file($_f) && file_exists($_f)) {
unlink($_f);
}
}
}
/**
* Helper function. Takes the prebuild options for the target selection field and search for a matching key.
* Since the optionString is prebuild, avoiding looping over and over again, the selection needs to be done
* by search and replace.
* Checks if TOOL_GOOGLEBOOKS_FIELDS_TO is defined and a matching key=>value pair is available
*
* @param string $optionString
* @param string $googleKey
* @return string
*/
function toolMethod_GetTargetSelection(string $optionString, string $googleKey): string {
if(defined('TOOL_GOOGLEBOOKS_FIELDS_TO') & !empty($googleKey)) {
if(isset(TOOL_GOOGLEBOOKS_FIELDS_TO[$googleKey])) {
$_k = "sel_".TOOL_GOOGLEBOOKS_FIELDS_TO[$googleKey];
$optionString = str_replace($_k,'selected="selected"',$optionString);
}
}
return $optionString;
}