$value) { $convertedtmsdata[$module['module_name']][$r][$key] = tms_convert_value($value, $key, $module); } } else { foreach($tmsdata as $key => $value) { $convertedtmsdata[$module['module_name']][$key] = tms_convert_value($value, $key, $module); } } } } odbc_close($conn); return $convertedtmsdata; } function tms_link_get_tms_resources(array $module) { $module_rtfs = array_filter(array_filter(array_map('intval', [$module['rs_uid_field'], $module['checksum_field']])), 'is_int_loose'); if(empty($module_rtfs)) { return []; } $sql_rtf_in = ps_param_insert(count($module_rtfs)); return ps_query( " SELECT rn.resource, max(CASE WHEN n.resource_type_field = ? THEN n.`name` ELSE NULL END) AS identifier, max(CASE WHEN n.resource_type_field = ? THEN n.`name` ELSE NULL END) AS `checksum` FROM resource_node AS rn INNER JOIN node AS n ON rn.node = n.ref AND n.resource_type_field IN ({$sql_rtf_in}) WHERE rn.resource > 0 GROUP BY rn.resource ORDER BY rn.resource", array_merge( [ 'i', $module['rs_uid_field'], 'i', $module['checksum_field'], ], ps_param_fill($module_rtfs, 'i') ) ); } function tms_link_test() { global $tms_link_dsn_name,$tms_link_user,$tms_link_password, $tms_link_checksum_field, $tms_link_table_name,$tms_link_object_id_field, $tms_link_text_columns, $tms_link_numeric_columns; $conn=odbc_connect($tms_link_dsn_name, $tms_link_user, $tms_link_password); if($conn) { $tmssql = "SELECT TOP 10 * FROM " . $tms_link_table_name . " ;"; // Execute the query to get the data from TMS $tmsresultset = odbc_exec($conn,$tmssql); $resultcount=odbc_num_rows ($tmsresultset); if($resultcount == 0) { global $lang; odbc_close($conn); return $lang["tms_link_no_tms_data"]; } $convertedtmsdata=array(); for ($r=1;$r<=$resultcount;$r++) { $tmsdata=odbc_fetch_array ($tmsresultset,$r); foreach($tmsdata as $key=>$value) { $convertedtmsdata[$key]=$value; } } odbc_close($conn); return $convertedtmsdata; } else { $error=odbc_errormsg(); exit($error); return $error; } } function tms_add_mediaxref($mediamasterid,$tms_object_id,$create=true) { global $conn,$tms_link_tms_loginid; // Check if the file already exists $tmssql = "select MediaXrefID FROM MediaXRefs where MediaMasterID='" . $mediamasterid . "' and ID='" . $tms_object_id . "' and TableID='108'"; debug("tms_link: SQL - " . $tmssql); $mediaxrefresult=odbc_exec($conn,$tmssql); if(!$mediaxrefresult) { return false; } $mediaxrefs=array(); while($row = odbc_fetch_array($mediaxrefresult)) { $mediaxrefs[] = $row["MediaXrefID"]; } if(count($mediaxrefs)>0) { return $mediaxrefs[0]; } elseif($create) { $tmssql="INSERT INTO MediaXRefs (MediaMasterID, ID, TableID, Rank, PrimaryDisplay, LoginID) values ('" . $mediamasterid . "', '" . $tms_object_id . "', 108, 1, 0, '" . $tms_link_tms_loginid. "')"; debug("tms_link: SQL - " . $tmssql); $tms_update_mediaxrefs=odbc_exec($conn,$tmssql); if(!$tms_update_mediaxrefs) { $errormessage=odbc_errormsg(); exit($errormessage); } return tms_add_mediaxref($mediamasterid, $tms_object_id, false); } else { return false; } } function tms_link_create_tms_thumbnail($resource, $alternative=-1) { global $conn,$tms_link_dsn_name,$tms_link_user,$tms_link_password, $tms_link_checksum_field, $tms_link_table_name,$tms_link_object_id_field, $tms_link_text_columns, $tms_link_numeric_columns, $tms_link_tms_loginid,$storagedir, $tms_link_media_path, $tms_link_push_image_sizes; // Set up connection, need to increase bytes returned ini_set("odbc.defaultlrl", "100K"); $conn=odbc_connect($tms_link_dsn_name, $tms_link_user, $tms_link_password); if($conn) { // Check if we already have a TMS ID $modules_mappings = tms_link_get_modules_mappings(); foreach($modules_mappings as $module) { if($module['tms_uid_field'] == "ObjectID") { $idfield = $module['rs_uid_field']; break; } } if(!isset($idfield)) { odbc_close($conn); return false; } $tms_object_id = get_data_by_field($resource, $idfield); if($tms_object_id == "") { odbc_close($conn); return false; // No TMS ID found, we can't add the image to TMS } // Get TMS Path ID of filestore path $pathid=tms_get_mediapathid($tms_link_media_path); debug("tms_link: Found PathID for " . $tms_link_media_path . " - " . $pathid); // Get details of the image to send to TMS foreach($tms_link_push_image_sizes as $tms_link_push_image_size) { $preview_path=get_resource_path($resource,true,$tms_link_push_image_size,false,'jpg',-1,1,false,'',$alternative); if(file_exists($preview_path) && filesize_unlimited($preview_path)<65536) { if(isset($storagedir) && $storagedir!="") { $tmsrelfilepath=substr($preview_path,strlen($storagedir) + 1); } else { $tmsrelfilepath=substr($preview_path,strpos($preview_path,'filestore')+10); } break; } } if(!isset($tmsrelfilepath)) { debug("tms_link: No valid image files found to be uploaded"); odbc_close($conn); return false; } // Check if mediafile already exists $existingmediafile=tms_check_thumb($pathid,$preview_path,$tmsrelfilepath); if($existingmediafile!==false) { // Update MediaRenditions with new thumbnail and return as everything else stays the same debug("tms_link: Found existing media record for Object ID #" . $tms_object_id . " - Mediamaster: " . $existingmediafile["MediaMasterID"]); tms_update_media_rendition_thumb($existingmediafile["MediaMasterID"],$existingmediafile["PrimaryFileID"], $pathid, $preview_path,$tmsrelfilepath); odbc_close($conn); return true; } // No existing record for the path defined, Add a new record // ============================ MediaMaster Table ================================ // Get a MediaMaster record ID to use, if there is not one unused then create one $mediamasterid = tms_get_mediamasterid(true,$resource); if(!$mediamasterid) { debug("tms_link: ERROR: Unable to get a MediaMasterID. "); odbc_close($conn); return false; } debug("tms_link: Using MediaMasterID: " . $mediamasterid); // ============================ MediaRenditions Table ============================ $renditionid=tms_get_renditionid($mediamasterid,$resource,true); if(!$renditionid) { debug("tms_link: ERROR - Unable to create a new RenditionID. "); odbc_close($conn); return false; } debug("Using RenditionID: " . $renditionid); // UPDATE MediaMaster with new value $tmssql="UPDATE MediaMaster Set DisplayRendID = '" . $renditionid . "', PrimaryRendID = '" . $renditionid . "' WHERE MediaMasterID = '" . $mediamasterid . "'"; odbc_exec($conn,$tmssql); $tms_set_rendition=odbc_exec($conn,$tmssql); if(!$tms_set_rendition) { $errormessage=odbc_errormsg(); debug("tms_link: SQL = " . $tmssql); debug("tms_link: Unable to update MediaMaster table with RenditionID " . $errormessage); odbc_close($conn); return false; } $mediafileid=tms_add_mediafile($renditionid,$pathid,$preview_path,$tmsrelfilepath,true); debug("tms_link: added new mediafile in MediaXRefs. Media FileID: " . $mediafileid); // Update MediaRenditions with new mediafile $updaterendition=tms_update_media_rendition($mediamasterid,$mediafileid); if(!$updaterendition) { debug("tms_link: ERROR: Unable to update media rendition"); odbc_close($conn); return false; } // Update MediaRenditions with new thumbnail tms_update_media_rendition_thumb($mediamasterid,$mediafileid, $pathid, $preview_path,$tmsrelfilepath); // ============================ MediaXRefs Table - Create Link to TMS Objects Module $mediaxrefid=tms_add_mediaxref($mediamasterid,$tms_object_id,true); if(!$mediaxrefid) { debug("tms_link: ERROR: Unable to create row in MediaXRefs"); odbc_close($conn); return false; } } else { $error=odbc_errormsg(); exit($error); return false; } } /** * tms_get_mediamasterid * Retrieve a list of unused Media Master IDs from the TMS database and return the first available. * If no IDs are found then create a new one and then retry * To avoid Media Master IDs being used by multiple resources $tms_link_mediapaths_resource_reference_column can be set. * This will store the resource ID in the MediaMaster table when creating the new ID so that it is not used by another Resource * * @param bool $create flag to create a new ID if none found * @param int $resource ResourceSpace resource ID * @return bool|string False if no ID found, otherwise the Media Master ID is returned */ function tms_get_mediamasterid(bool $create = true, ?int $resource = null) { global $conn, $errormessage, $tms_link_tms_loginid,$tms_link_mediapaths_resource_reference_column ; if (!preg_match('/^[a-zA-Z0-9_]*$/', $tms_link_tms_loginid)) { debug('tms_link: $tms_link_tms_loginid can only contain letters, numbers or underscore.'); return false; } // Get the latest inserted ID that we have not used $tmssql = "SELECT MediaMasterID FROM MediaMaster WHERE LoginID = '" . $tms_link_tms_loginid . "' AND DisplayRendID='-1' AND PrimaryRendID='-1'"; if ($tms_link_mediapaths_resource_reference_column != "" && $resource !=null) { if (!preg_match('/^[a-zA-Z0-9_]*$/', $tms_link_mediapaths_resource_reference_column)) { debug('tms_link: $tms_link_mediapaths_resource_reference_column can only contain letters, numbers or underscore.'); return false; } $tmssql .= " AND $tms_link_mediapaths_resource_reference_column = CAST(CAST(" . (int) $resource . " AS VARCHAR(20)) AS INT)"; } $mediamasterresult = odbc_exec($conn, $tmssql); if(!$mediamasterresult) { debug("tms_link: SQL = " . $tmssql); $errormessage=odbc_errormsg(); debug("tms_link: ERROR = " . $errormessage); return false; } $mediamasterids=array(); while($row = odbc_fetch_array($mediamasterresult)) { $mediamasterids[] = $row["MediaMasterID"]; } if(count($mediamasterids)>0) { debug("tms_link: FOUND " . count($mediamasterids) . " available MediaMasterIDs =" . implode(",",$mediamasterids)); return $mediamasterids[0]; } elseif($create) { $insert_columns = ["LoginID","DisplayRendID","PrimaryRendID"]; $insert_values = [$tms_link_tms_loginid,-1,-1]; if ($tms_link_mediapaths_resource_reference_column != "" && $resource !="") { $insert_columns[]=$tms_link_mediapaths_resource_reference_column; $insert_values[]=$resource; } $insert_columns = implode(",",$insert_columns); $insert_values= implode("','",$insert_values); $tmssql="INSERT INTO MediaMaster ($insert_columns) VALUES ('$insert_values')"; $tmsinsert=odbc_exec($conn,$tmssql); if(!$tmsinsert) { $errormessage=odbc_errormsg(); debug("tms_link: ERROR = " . $errormessage); return false; } return tms_get_mediamasterid(false, $resource); } else { return false; } } function tms_get_renditionid($mediamasterid,$resourceid,$create=true) { global $conn, $tms_link_tms_loginid; // Get the latest ID that we have not used $tmssql = "select RenditionID, RenditionNumber,SortNumber,MediaTypeID,ParentRendID,LoginID FROM MediaRenditions where MediaMasterID='" . $mediamasterid . "' and LoginID='" . $tms_link_tms_loginid . "'"; $renditionresult=odbc_exec($conn,$tmssql); if(!$renditionresult) { debug("tms_link: SQL = " . $tmssql); $errormessage=odbc_errormsg(); debug("tms_link: ERROR = " . $errormessage); return false; } $renditionids=array(); while($row = odbc_fetch_array($renditionresult)) { $renditionids[] = $row["RenditionID"]; } if(count($renditionids)>0) { debug("tms_link: FOUND " . count($renditionids) . " available RenditionIDs