<cfcomponent displayname="paginationRealData" hint="calculates and returns pagination info and record set" output="false">


	<cffunction name="pageInfo" displayname="pageInfo" access="remote" output="false" returnFormat="json" returnType="xml">
		<cfargument name="pageNumber" type="Numeric" required="true">
		<cfargument name="orderBy" type="string" required="true">
		<cfargument name="fromDate" type="string" default="">
		<cfargument name="toDate" type="string" default="">
		<cfargument name="status" type="string" default="All">
		<cfargument name="eventSubject" type="string" default="0">
		<cfargument name="patient" type="string" default="0">
		<cfargument name="employee" type="string" default="0">	
		<cfinclude template="../Application-CustomerSpecific.cfm">  
		<!---creates a query with some test data. This process would be best done in a cfc but is shown 
this way for easy demo purposes. 



Your cfc should return a record set and the two variables needed by the the tag to function, in a
structure. i.e NumberOfLinksOnPage and numberOfPages --->	
<cfset result = structNew()>


<CFIF not isDefined("arguments.pageNumber") >
<cfset arguments.pageNumber = 1>
</CFIF>

<cfquery  name="myQuery" datasource="#Application.DataSrc#">
  SELECT Event.Event_ID, Event.EventSub_ID, Event.Event_Date,
  patient.Last_Name_M0040, Employee.Employee_ID, Employee.Last_Name, EventSubjects.EventSub_ID, EventSubjects.Subject, Event.Resolved
  FROM #Request.prefix_db_agency#.Event,#Request.prefix_db_lookup#.Employee, #Request.prefix_db_agency#.patient, #Request.prefix_db_agency#.EventSubjects
  WHERE Event.Agency_ID = #session.AgencyID#
  And Event.Employee_ID = Employee.Employee_ID
  And Event.Patient_ID = patient.Patient_ID
  And Event.EventSub_ID = EventSubjects.EventSub_ID
  <cfif arguments.eventSubject neq 0>
And Event.EventSub_ID = #arguments.eventSubject#
</cfif>
<cfif arguments.patient neq 0>
And Event.Patient_ID = #arguments.patient#
</cfif>
<cfif arguments.employee neq 0>
And Event.Employee_ID = #arguments.employee#
</cfif>
<cfif arguments.status neq 'All'>
And Event.Resolved = '#arguments.status#'
</cfif>
<cfif arguments.fromDate neq ''>
AND Event_Date >= "#dateformat(ParseDatetime(arguments.fromDate),'YYYY-MM-DD')#"
</cfif>
<cfif arguments.toDate neq ''>
AND Event_Date <= "#dateformat(ParseDatetime(arguments.toDate),'YYYY-MM-DD')#"
</cfif>
ORDER BY #arguments.orderby# DESC
</cfquery>
<cfif arguments.employee neq 0>
	<cfquery  name="GetE" datasource="#Application.DataSrc#">
	SELECT Employee_ID, First_Name, Last_Name 
	FROM #Request.prefix_db_lookup#.Employee
	WHERE Agency_ID = #session.AgencyID#
	AND Status = 'active'
		AND EmployeeType != 'Physician' and Employee_ID = #arguments.employee#
	ORDER BY Last_Name
	</cfquery>
	<cfset result.employeeName = '#GetE.Last_Name#, #GetE.First_Name#'>
<cfelse>
	<cfset result.employeeName = 'All'>
</cfif>
<cfif arguments.patient neq 0>
	<cfquery  name="GetP" datasource="#Application.DataSrc#">
	SELECT Patient_ID, Last_Name_M0040, First_Name_M0040
	FROM #Request.prefix_db_agency#.patient
	WHERE Agency_ID = #session.AgencyID#
	AND Status = 'active' and Patient_ID = #arguments.patient#
	ORDER BY Last_Name_M0040
	</cfquery>
	<cfset result.patientName = '#GetP.Last_Name_M0040#, #GetP.First_Name_M0040#'>
<cfelse>
	<cfset result.patientName = 'All'>
</cfif>
<cfif arguments.eventSubject neq 0>
	<cfquery  name="GetEventSub" datasource="#Application.DataSrc#">
	SELECT EventSub_ID, Subject
	FROM #Request.prefix_db_agency#.EventSubjects
	WHERE Agency_ID = #session.AgencyID# and EventSub_ID = #arguments.eventSubject#
	ORDER BY Subject
	</cfquery>
	<cfset result.subjectName = '#GetEventSub.Subject#'>
<cfelse>
	<cfset result.subjectName = 'All'>
</cfif>
<cfif #arguments.status# eq 'No'>
	<cfset result.status = 'Unresolved'>
<cfelse>
	<cfif #arguments.status# eq 'No'>
		<cfset result.status = 'Resolved'>
	<cfelse>
		<cfset result.status = #arguments.status#>
	</cfif>
</cfif>

<cfset result.orderBy = #arguments.orderby#>
<cfset result.numberOfRecordsOnPage = 10>
<cfif #arguments.pageNumber# eq 0>
	<cfset numberOfPagesNoCeil = #myQuery.recordCount#>
<cfelse>
	<cfset numberOfPagesNoCeil = (#myQuery.recordCount# / #result.numberOfRecordsOnPage#)>
</cfif>
<cfset result.numberOfPages = #ceiling(numberOfPagesNoCeil)#>        
<cfif #arguments.pageNumber# eq 0>
	<cfset result.from = 1>
	<cfset result.to = #myQuery.recordCount#>
<cfelse>
	<!---here using url.pagenumber to work out what records to display on current page --->
	<cfset result.from = ((#arguments.pageNumber# * #result.numberOfRecordsOnPage#) - #result.numberOfRecordsOnPage#) + 1>
    <cfset result.to = (#arguments.pageNumber# * #result.numberOfRecordsOnPage#)>
	
	<!---if on last page display the actual number of records in record set as the last  to' figure'. Otherwise it gives 
	a false reading and gives the pagenumber * numberOfRecordsOnPage which is always a multiple of 10--->
	<cfif #result.to# eq (#result.numberOfPages# * 10)>
	 <cfset result.to = myQuery.recordCount>
	</cfif>
</cfif>	
		
		
<!---Query to send back --->
<!--- Create a new 2-column query, specifying the column data types --->
<cfset recordsToGoBack = QueryNew("field1, field2, field3, field4", "VarChar, VarChar, VarChar, VarChar")>
<!--- --->
<cfscript>
   MyDoc = XmlNew();
   MyDoc.xmlRoot = XmlElemNew(MyDoc,"MyRoot");
   MyDoc.MyRoot.XmlChildren[1] = XmlElemNew(MyDoc,"orderBy");
   MyDoc.MyRoot.XmlChildren[1].XmlText = "#result.orderBy#";
   MyDoc.MyRoot.XmlChildren[2] = XmlElemNew(MyDoc,"numberOfRecordsOnPage");
   MyDoc.MyRoot.XmlChildren[2].XmlText = "#result.numberOfRecordsOnPage#";
   MyDoc.MyRoot.XmlChildren[3] = XmlElemNew(MyDoc,"numberOfPages");
   MyDoc.MyRoot.XmlChildren[3].XmlText = "#result.numberOfPages#";
   MyDoc.MyRoot.XmlChildren[4] = XmlElemNew(MyDoc,"from");
   MyDoc.MyRoot.XmlChildren[4].XmlText = "#result.from#";
   MyDoc.MyRoot.XmlChildren[5] = XmlElemNew(MyDoc,"to");
   MyDoc.MyRoot.XmlChildren[5].XmlText = "#result.to#";
   MyDoc.MyRoot.XmlChildren[6] = XmlElemNew(MyDoc,"status");
   MyDoc.MyRoot.XmlChildren[6].XmlText = "#result.status#";
   MyDoc.MyRoot.XmlChildren[7] = XmlElemNew(MyDoc,"employee");
   MyDoc.MyRoot.XmlChildren[7].XmlText = "#result.employeeName#";
   MyDoc.MyRoot.XmlChildren[8] = XmlElemNew(MyDoc,"subject");
   MyDoc.MyRoot.XmlChildren[8].XmlText = "#result.subjectName#";
   MyDoc.MyRoot.XmlChildren[9] = XmlElemNew(MyDoc,"patient");
   MyDoc.MyRoot.XmlChildren[9].XmlText = "#result.patientName#";
   MyDoc.MyRoot.XmlChildren[10] = XmlElemNew(MyDoc,"fromDate");
   MyDoc.MyRoot.XmlChildren[10].XmlText = "#arguments.fromDate#";
   MyDoc.MyRoot.XmlChildren[11] = XmlElemNew(MyDoc,"toDate");
   MyDoc.MyRoot.XmlChildren[11].XmlText = "#arguments.toDate#";
   MyDoc.MyRoot.XmlChildren[12] = XmlElemNew(MyDoc,"recordsToGoBack");
</cfscript>
<cfset i = 1>
<cfloop query="myQuery" startrow="#result.from#" endrow="#result.TO#">
<cfscript>
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i] = XmlElemNew(MyDoc,"data");
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[1] = XmlElemNew(MyDoc,"field1");
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[1].XmlText = "#DateFormat(Event_Date,"mm/dd/yyyy")#";
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[2] = XmlElemNew(MyDoc,"field2");
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[2].XmlText = "#Last_Name_M0040#";
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[3] = XmlElemNew(MyDoc,"field3");
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[3].XmlText = "#Last_Name#";
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[4] = XmlElemNew(MyDoc,"field4");
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[4].XmlText = "#Subject#";
   MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[5] = XmlElemNew(MyDoc,"field5");
   if (#Resolved#  eq 'No') {
   		MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[5].XmlText = 'Unresolved';
   }
   else {
		MyDoc.MyRoot.recordsToGoBack.XmlChildren[i].XmlChildren[5].XmlText = 'Resolved';
   }
   i = i + 1;
</cfscript>
<!--- Set the values of the cells in the query --->
<cfset temp = QueryAddRow(recordsToGoBack)>
<cfset temp = QuerySetCell(recordsToGoBack, "field1", '#DateFormat(Event_Date,"mm/dd/yyyy")#')>
<cfset temp = QuerySetCell(recordsToGoBack, "field2", '#Last_Name_M0040#')>
<cfset temp = QuerySetCell(recordsToGoBack, "field3", '#Last_Name#')>
<cfset temp = QuerySetCell(recordsToGoBack, "field4", '#Subject#')>
</cfloop>

<!---These are the only 2 values that the tag needs to run. --->
<cfset result.recordsToGoBack = #recordsToGoBack#>
		<cfset test='{"NUMBEROFPAGES":2.0,"ORDERBY":"Event_Date","TO":1.0,"FROM":1.0,"NUMBEROFRECORDSONPAGE":1.0,"RECORDSTOGOBACK":{"COLUMNS":["FIELD1","FIELD2","FIELD3","FIELD4"],"DATA":[["02\/12\/2010","Allen","Cott","missed visit"]]}}'>
		<cfreturn MyDoc/>
	</cffunction>
</cfcomponent>