<!--- 
 Filename: FilmDataCFC2.cfc
 Author: Nate Weiss (NMW)
 Purpose: Creates the FilmDataCFC2 ColdFusion Component, which provides
 search and data retrieval services for films in the ows database.
--->

<!--- The <CFCOMPONENT> block defines the CFC --->
<!--- The filename of this file determines the CFC's name --->
<cfcomponent 
 hint="Provides a simple interface for searching for and getting detailed 
 information about films in the Orange Whip Studios database."
 output="false">

  <!--- ListFilms() method --->
  <cffunction name="listFilms" returnType="query"
 hint="Returns a query object containing film information." output="false">
 
    <!--- Optional SearchString argument ---> 
    <cfargument name="searchString" type="string" required="No"
    hint="Optional search criteria; if not given, all films are returned.">
 
    <!--- Optional SearchString argument ---> 
    <cfargument name="actorID" type="numeric" required="No"
    hint="Allows searching for films by actor.">
 
    <!--- var scope variables --->
    <cfset var getFilms = "">
    
    <!--- Run the query ---> 
    <cfquery name="getFilms" datasource="ows">
     SELECT FilmID, MovieTitle FROM Films
     WHERE 0=0
     <!--- If a search string has been specified --->
     <cfif isDefined("ARGUMENTS.searchString")>
     AND (MovieTitle LIKE '%#ARGUMENTS.searchString#%'
     OR Summary LIKE '%#ARGUMENTS.searchString#%')
     </cfif>
 
     <!--- If an actor's name has been specified --->
     <cfif isDefined("ARGUMENTS.actorID")>
     AND FilmID IN
     (SELECT FilmID FROM FilmsActors
     WHERE ActorID = #ARGUMENTS.actorID#)
     </cfif>
     ORDER BY MovieTitle
    </cfquery>
 
    <!--- Return the query results ---> 
    <cfreturn getFilms>
  </cffunction>
 
 
  <!--- GetFilmData() method --->
  <cffunction name="getFilmData" returnType="struct" output="false"
  hint="Returns structured information about the specified film.">

    <!--- FilmID argument --->
    <cfargument name="filmID" type="numeric" required="Yes"
    hint="The film that you want information about.">

    <!--- This is what this method originally returns ---> 
    <!--- The var keyword makes it local to the method --->
    <cfset var filmData = structNew()>
    <cfset var getFilm = "">
    <cfset var getActors = "">
    
    <cfset filmData.filmID = ARGUMENTS.filmID>
 
    <!--- Select data about the film from the database --->
    <cfquery name="getFilm" datasource="ows">
    SELECT MovieTitle, Summary FROM Films
    WHERE FilmID = #ARGUMENTS.filmID#
    </cfquery>
 
    <!--- Populate the FilmData structure with film info --->
    <cfset filmData.movieTitle = getFilm.MovieTitle>
    <cfset filmData.summary = getFilm.Summary>
 
    <!--- Run second query to get actor information --->
    <cfquery name="getActors" datasource="ows">
    SELECT ActorID, NameFirst, NameLast 
    FROM Actors
    WHERE ActorID IN
    (SELECT ActorID FROM FilmsActors
    WHERE FilmID = #ARGUMENTS.filmID#)
    </cfquery>
 
    <!--- Make the GetActors query results part of the returned structure --->
    <cfset filmData.actorsQuery = getActors>

    <!--- Return the final structure ---> 
    <cfreturn FilmData>
 </cffunction>
 
  <!--- ProduceFilmListHTML() method --->
  <cffunction name="produceFilmListHTML" access="remote" output="true"
  returntype="void" hint="Produces a simple HTML display">
 
    <!--- This variable is local to only to this function --->
    <cfset var filmsQuery = "">
    <cfset var actorData = "">
    
    <!--- Call the ListFilms() method to get data about all films--->
    <!--- Pass along any arguments that were passed to this method --->
    <cfinvoke method="listFilms" returnVariable="filmsQuery"
    argumentCollection="#ARGUMENTS#">
 
    <!--- Begin displaying output --->
    <cfoutput>
    <!--- If an ActorID was provided as an argument, then we are --->
    <!--- only displaying films that star the specified actor --->
    <cfif isDefined("ARGUMENTS.actorID")>
      <cfset actorData = getFilmData(filmsQuery.FilmID).actorsQuery>
      <h3>Films Starring #actorData.NameFirst# #actorData.NameLast#</h3>
      <p><a href="FilmDataCFC2.cfc?method=produceFilmListHTML">
      [List Of All Films]</a></p>
    <!--- Otherwise, all Orange Whip films are being displayed ---> 
    <cfelse>
      <h3>Orange Whip Studios Films</h3>
    </cfif>

    <!--- For each film, display the title as a link --->
    <!--- to this component's ProduceFilmHTML() method --->
    <cfloop query="filmsQuery">
    <a href="FilmDataCFC2.cfc?method=produceFilmHTML&FilmID=#FilmID#">
    #MovieTitle#</a><br>
    </cfloop>
    </cfoutput> 
    
  </cffunction>
 
  <!--- ProduceFilmHTML() method --->
  <cffunction name="produceFilmHTML" access="remote" output="true" 
  returntype="void" hint="Produces a simple HTML display">

    <!--- FilmID argument --->
    <cfargument name="filmID" type="numeric" required="Yes"
    hint="The ID number of the film to display information about">
  
    <!--- Call the GetFilmData() method to get basic film information ---> 
    <cfset var filmData = getFilmData(ARGUMENTS.filmID)>
  
    <!--- Produce the simple film display ---> 
    <cfoutput>
    <h3>#filmData.MovieTitle#</h3>
    <p>#filmData.Summary#</p>
    
    <!--- Include a list of actors --->
    <p><b>Starring:</b>
    <ul style="margin-top:2px">
    <cfloop query="FilmData.ActorsQuery">
    <li>
    <a href="FilmDataCFC2.cfc?method=produceFilmListHTML&ActorID=#ActorID#">
    #NameFirst# #NameLast#</a>
    </li>
    </cfloop>
    </ul>
    </cfoutput>

  </cffunction>

</cfcomponent>
