<!--- 
 Filename: RetireActor.cfm
 Author:  Nate Weiss (NMW)
 Purpose: Generates a retirement memo as an RTF document for Microsoft Word
--->

<!--- If no Actor ID passed, diplay list of links --->
<cfif not isDefined("URL.actorID")>
 
 <!--- Get a list of actors from database --->
 <cfquery name="getActors" datasource="ows">
  SELECT ActorID, NameFirst, NameLast
  FROM Actors
  ORDER BY NameLast, NameFirst
 </cfquery>
  
 <!--- Page Title, etc ---> 
 <html>
 <head><title>Actor Retirement System</title></head>
 <body>
 <h3>Which Actor Would You Like To Retire?</h3>
 
 <!--- For each Actor, include simple link to ---> 
 <!--- this page, passing the Actor ID in URL --->
 <cfoutput query="getActors">
  <cfset linkURL = "#CGI.script_name#?actorID=#ActorID#">
  <a href="#linkURL#">#NameFirst# <b>#NameLast#</b></a><br>
 </cfoutput>
 </body>
 </html> 
  
<!--- If Actor ID passed, generate Word doc ---> 
<cfelse>
 
 <!--- Make sure Actor ID in URL is a number --->
 <cfparam name="URL.actorID" type="numeric">
 
 <!--- Get this Actor's name from database --->
 <cfquery name="getActor" datasource="ows">
  SELECT NameFirst, NameLast, 
   NameFirstReal, NameLastReal,
   (SELECT Min(DateInTheaters)
    FROM Films f, FilmsActors fa
    WHERE fa.FilmID = f.FilmID
    AND fa.ActorID = a.ActorID) AS DateFirstFilm
  FROM Actors a
  WHERE ActorID = #URL.actorID#
 </cfquery>
 
 <!--- How long has Actor been with company? --->
 <cfset monthsEmployed = dateDiff("m", getActor.DateFirstFilm, now())>

 <!--- Determine severance package --->
 <cfif monthsEmployed gte 36>
  <cfset sevPackage = "Gold Watch (Digital)">
 <cfelseif monthsEmployed gte 18> 
  <cfset sevPackage = "$100 Starbucks Gift Certificate">
 <cfelse> 
  <cfset sevPackage = "ColdFusion Web Application Construction Kit">
 </cfif>
 
 <!--- Location of our RTF "template" document --->
 <cfset thisFolder  = getDirectoryFromPath(getCurrentTemplatePath())>  
 <cfset templatePath = thisFolder & "DocTemplate.rtf"> 
 
 <!--- Read RTF template into variable called "RTF" --->
 <cffile action="read" file="#templatePath#" variable="rtf">
 
 <!--- Replace "placeholders" with specific information ---> 
 <cfset todaysDate = dateFormat(Now(), "dddd, mmmm d, yyyy")>
 <cfset rtf = replace(rtf, "%CurrentDate%", todaysDate,"ALL")>
 <cfset rtf = replace(rtf, "%FreePrize%", sevPackage,"ALL")> 
 <cfset rtf = replace(rtf, "%NameFirst%", getActor.NameFirst,"ALL")> 
 <cfset rtf = replace(rtf, "%NameLast%", getActor.NameLast,"ALL")> 
 <cfset rtf = replace(rtf, "%NameFirstReal%", getActor.NameFirstReal,"ALL")> 
 <cfset rtf = replace(rtf, "%NameLastReal%", getActor.NameLastReal,"ALL")> 
 
 <!--- Suggest default filename for document --->
 <cfheader name="Content-Disposition" value="filename=RetireMemo.doc">
 <!--- Set the content-type so Word is invoked --->
 <cfcontent type="application/msword"><cfoutput>#rtf#</cfoutput>
</cfif>
