function savetoelastic($ref){ global $baseurl, $pagename; if($pagename != "upload2"){ //error_log($ref); $results=array(); $results[] = get_resource_data($ref,false); $resourcetype=get_resource_type_name($results[0]['resource_type']); if($resourcetype != "Audio"){ $thethumb = $baseurl.str_replace("/var/www/include/..","",get_resource_path($ref,true,"thm",false,"jpg")); if(!file_exists($thethumb)){ $thethumb = $results[0]["thumbnail"] = $baseurl."/gfx/no_preview/extension/".$results[0]['file_extension'].".png"; } $results[0]["thumbnail"]=$thethumb; }else{ $results[0]["thumbnail"] = $baseurl."/gfx/no_preview/extension/".$results[0]['file_extension'].".png"; } if($resourcetype == "Audio"){ $results[0]["preview"]=$baseurl.str_replace("/var/www/include/..","",get_resource_path($ref, true, "", false, "mp3")); }else if($resourcetype == "Video"){ $results[0]["preview"]=$baseurl.str_replace("/var/www/include/..","",get_resource_path($ref, true, "pre", false, "mp4")); } $results = mia_results($results); $results=json_encode($results[0]); $query = push_RStoElastic($resourcetype,$ref,$results); if($query == false){ //failed to connect to elastic search createcron($attempts=1, $ref); }else{ // var_dump($query);exit(); } return; } }
function updateResourcesFromContacts($resourcestoupdate,$rs_fields,$resref, $oldvals,$oldvalmatch){ $resourceupdates = array(); $resources=array(); $rf=array(); $rv=array(); $toindex=array();//empty array vars //filter for only resources where this name matches the resource_field_type $filteredray = array(); for ($ru=0; $ru<count($resourcestoupdate); $ru++){ if($resourcestoupdate[$ru]['resource_type_field']==$resref){ $filteredray[] = $resourcestoupdate[$ru]; }; }; //loop through the filtered resources for($r=0; $r<count($filteredray); $r++){ //for all of the resources and foreach mapped resource field foreach($rs_fields as $k => $v){ if($v !=""){ // Check to see if resource data exists for that field and resource $exists = sql_query("SELECT * FROM resource_data WHERE resource ='".$filteredray[$r]['resource']."' AND resource_type_field='".$k."'"); //if it doesn't exist and the value has changed if(empty($exists) && !in_array($v,$oldvals[0])){ //push the data that needs to be INSERTED $resourceupdates[]="(".$filteredray[$r]['resource'].",".$k.",'".$v."')"; $oldkey = matcholdkey($k,$oldvalmatch); $toindex[]=$k.",'".$v."',".$filteredray[$r]['resource'].",'".$oldvals[0][$oldkey]."'"; // else if the value does exist and the value has changed }else if(!in_array($v,$oldvals[0])){ //push the data that needs to be UPDATED $resources[]=$filteredray[$r]['resource']; $rf[]=$k; $rv[]=$v; $oldkey=matcholdkey($k,$oldvalmatch); $toindex[]=$k.",'".$v."',".$filteredray[$r]['resource'].",'".$oldvals[0][$oldkey]."'"; }; }; }; };//end filtered loop $updateq = ""; //if there is data to INSERT build the statement if(!empty($resourceupdates)){ sql_query("INSERT INTO resource_data (resource, resource_type_field, value) VALUES ". join(",",$resourceupdates)); } //if there is data to UPDATE build the statement if(!empty($resources)){ $updateq = "UPDATE resource_data SET value = CASE resource_type_field"; foreach($rs_fields as $k => $v){ if($v !=""){ $updateq .= " WHEN $k THEN '$v' \n"; } }; $updateq .= "ELSE value END "; $updateq .= "WHERE resource IN (" . join(",",$resources).")"; sql_query($updateq);//update } //add remove and index keywords for($ti = 0; $ti<count($toindex);$ti++){ $toind = explode(",", $toindex[$ti]); remove_keyword_mappings($toind[2],$toind[3],$toind[0]); add_keyword_mappings($toind[2],$toind[1],$toind[0]); } $results=array(); for($fr=0; $fr<count($filteredray); $fr++){ $ref=$filteredray[$fr]['resource']; $results[]=get_resource_data($ref); } //elastic search $results=mia_results($results); $resource_types=get_resource_types(); $results=mia_elastic_encode($resource_types,$results,false); for($e=0; $e<count($results); $e++){ $resourcetype=get_resource_type_name($results[$e]['resource_type']); $ref=$results[$e]['ref']; push_RStoElastic($resourcetype,$ref,json_encode($results[$e])); } };//end function
} }else{ $query = sql_query("INSERT INTO resource_data VALUES ($ref,$key,'$val')"); if($query === false){ $qerr[]="INSERT INTO resource_data VALUES ($ref,$key,'$val')"; } } } } if(empty($qerr)){ $results=array(); $results[] = get_resource_data($ref,false); $thumb_path = array("thumbnail"=>$baseurl.str_replace("/var/www/include/..","",get_resource_path($ref,true,"thm",false,"jpg"))); $newresults[] = array_merge($thumb_path,$results[0]); $results = $newresults; $results = mia_results($results); $resourcetype=get_resource_type_name($results[0]['resource_type']); $results=json_encode($results[0]); $query = push_RStoElastic($resourcetype,$ref,$results); if($query == false){ //failed to connect to elastic search createcron($attempts=1,$ref); echo("Resolved TMS but failed to connect to Elastic Search. A Cron Job for Elastic Search has been instantiated and will try to resolve the issue" . date('d-m-Y h:i:s')."n"); }else{ echo("Art Object data was successfully resolved from TMS and added to Elastic Search ".date('d-m-Y h:i:s')."\n"); } }else{ echo("Database Error(s) - Failed to Resolve".date('d-m-Y h:i:s')."\n".json_encode($qerr)."\n"); //handle this somehow } }