<!--- 
 Filename: ActorPhotoAction.cfm
 Edited By: Nate Weiss (NMW)
 Purpose:  Creates an interface for uploading actor photos
--->

<!--- Insist that an ActorID parameter is passed in the URL --->
<cfparam name="URL.actorID" type="numeric">
<!--- Also, make sure the expected form fields are present --->
<cfparam name="FORM.nameFirst" type="string">
<cfparam name="FORM.nameLast" type="string">
<cfparam name="FORM.photoFile" type="string">

<!--- Location of the directory where actor photos are stored --->
<!--- The folder name is ActorPhotos, within this page's folder --->
<cfset actorPhotoDir = expandPath("ActorPhotos")>

<!--- Function for deleting the existing photos of the actor --->
<cffunction name="deleteExistingPhotos" returnType="void" output="false">
 <cfargument name="actorID" type="numeric" required="Yes">
 
 <cfset var existingPhotos = "">
 
 <!--- Get a listing of any current image files for this actor --->
 <cfdirectory action="list" directory="#actorPhotoDir#"
  name="existingPhotos" filter="Actor#ARGUMENTS.ActorID#Photo.*">
  
 <!--- For each existing file, if any... --->   
 <cfloop query="existingPhotos">
  <!--- Delete the existing file --->
  <cffile action="delete" file="#actorPhotoDir#\#existingPhotos.Name#">
 </cfloop> 
</cffunction>

<!--- Query the database for information about the specified actor --->
<cfquery name="actorQuery" datasource="ows">
 SELECT ActorID, NameLast, NameFirst
 FROM Actors 
 WHERE ActorID = #Val(URL.actorID)#
</cfquery>

<!--- If the actor's first or last names have been edited... --->
<cfif (actorQuery.NameFirst neq FORM.nameFirst) 
  OR (actorQuery.NameLast neq FORM.nameLast)>
  <!--- Update the actor's record in the database --->
  <cfquery datasource="ows">
   UPDATE Actors 
   SET
    NameFirst = '#FORM.nameFirst#',
    NameLast = '#FORM.nameLast#'
   WHERE ActorID = #URL.actorID#
  </cfquery>
</cfif>

<!--- If the user has specified a file to upload --->
<cfif FORM.photoFile NEQ "">
 <!--- Make sure only one person is working with this actor's photos --->
 <cflock name="ActorPhotoUploads#URL.actorID#" type="exclusive" timeout="10">
  
  <!--- Accept the file upload --->
  <cffile destination="#getTempDirectory()#" action="upload"
   nameConflict="MakeUnique" filefield="PhotoFile" accept="image/*">

  <!--- Delete the existing photographs --->  
  <cfset deleteExistingPhotos(URL.actorID)>
  
  <!--- Create a new name for the uploaded file, based on the actor's ID --->
  <cfset newFile = "Actor#URL.actorID#Photo.#CFFILE.ServerFileExt#">
  
  <!--- Go ahead and rename the uploaded file --->
  <cffile action="RENAME" 
   source="#cffile.ServerDirectory#/#cffile.ServerFile#"
   destination="#actorPhotoDir#/#newFile#">
 
 </cflock> 
 
<cfelseif isDefined("FORM.deletePhoto")>
 <!--- Make sure only one person is working with this actor's photos --->
 <cflock name="ActorPhotoUploads#URL.ActorID#" type="exclusive" timeout="10">
  
  <!--- Delete the existing photographs --->  
  <cfset deleteExistingPhotos(URL.actorID)>
 </cflock> 
</cfif>
 
<!--- Now that the edit is complete, send the user back to the form page --->
<cflocation url="ActorPhotoForm.cfm?ActorID=#URL.ActorID#">
