From: <ndi...@us...> - 2002-09-08 08:38:53
|
Update of /cvsroot/modus/org/bacfug/modus In directory usw-pr-cvs1:/tmp/cvs-serv30505/bacfug/modus Modified Files: basecontentobject.cfc Log Message: moved the sorting stuff into the baseContentObject because it did not belong in the persister. Also, I refactored it, so there is separate method just to sort any array of contentObjects, which will be much more useful once we have filtering working. At some point we may even want a component that is a contentObjectManipulator, or something like that. Index: basecontentobject.cfc =================================================================== RCS file: /cvsroot/modus/org/bacfug/modus/basecontentobject.cfc,v retrieving revision 1.6 retrieving revision 1.7 diff -C2 -d -r1.6 -r1.7 *** basecontentobject.cfc 8 Sep 2002 03:45:53 -0000 1.6 --- basecontentobject.cfc 8 Sep 2002 08:38:49 -0000 1.7 *************** *** 69,77 **** <cfreturn instance.persister.getAll(getType())> </cffunction> ! <!--- get all of this type sorted ---> ! <cffunction name="getAllSorted" access="public" returnType="array" hint="get all of this type, sorted by some fields" output="no"> <cfargument name="sortFields" required="yes" type="string"> ! <cfreturn instance.persister.getAllSorted(sortFields,getType())> </cffunction> <!--- --- 69,136 ---- <cfreturn instance.persister.getAll(getType())> </cffunction> ! ! <!--- a function for getting all instances, sorted ---> ! ! <cffunction name="getAllSorted" access="public" output="no" hint="A method to sort all instances and sort them by one or more fields"> <cfargument name="sortFields" required="yes" type="string"> ! <cfreturn sortContentObjects(getAll(),arguments.sortFields)> </cffunction> + + <!--- the sort method ---> + <!--- + + I'm really not at all satisfied with this, but it does work, for now. + + ---> + <cffunction name="sortContentObjects" access="public" output="no" returnType="array" hint="takes an array of contentObjects and sorts them based on the field(s) passed in"> + <cfargument name="contentObjects" type="array" required="yes"> + <cfargument name="sortFields" type="string" required="yes"> + <cfset var tempQuery = ""> + <cfset var sortQuery = ""> + <cfset var sortedArray = arrayNew(1)> + <cfset var objectCount = arrayLen(arguments.contentObjects)> + <cfset var sortFieldsArray = listToArray(arguments.sortFields)> + <cfset var ii = 0> + <cfset var ff = 0> + <cfset var thisObject = ""> + <cfset var thisFieldName = ""> + <cfscript> + //clean the sortFieldsArray, so we can have the sort order in there too + for(ii = 1; ii LTE arrayLen(sortFieldsArray); ii = ii + 1){ + sortFieldsArray[ii] = getToken(sortFieldsArray[ii],1); + } + //prime the tempQuery + tempQuery = queryNew("index," & arrayToList(sortFieldsArray)); + //resize the query to however many objects there are + queryAddRow(tempQuery,objectCount); + //loop through all objects, populating the appropriate columns of the query + for(ii = 1; ii LTE objectCount; ii = ii + 1){ + thisObject = arguments.contentObjects[ii]; + querySetCell(tempQuery,"index",ii,ii); + //loop through whatever fields we are sorting on, adding the values of those fields to the query + for(ff = 1; ff LTE arrayLen(sortFieldsArray); ff = ff + 1){ + thisFieldName = getToken(sortFieldsArray[ff],1); + querySetCell(tempQuery,thisFieldName,lcase(thisObject.getField(thisFieldName).getValue()),ii); + } + } + </cfscript> + <!--- ok, now let's get the sortedQuery ---> + <cfquery name="sortQuery" dbtype="query"> + SELECT * + FROM tempQuery + ORDER BY #arguments.sortFields# + </cfquery> + <!--- and now, we'll populate the sortedArray ---> + <cfscript> + //resize the array, so it doesn't have to keep allocating more memory + arrayResize(sortedArray,objectCount); + for(ii = 1; ii LTE objectCount; ii = ii + 1){ + sortedArray[ii] = arguments.contentObjects[sortQuery.index[ii]]; + } + </cfscript> + <!--- return the sorted Array ---> + <cfreturn sortedArray> + </cffunction> + <!--- |