Title: Message
All right I am having serious problems trying to get my head around this one, I have made a CFC that traverses through the directory structure and returns a query with the whole directory structure underneath the parent it was given.
 
Then I have made a CFC that is to work with this query and visually recreate the directory structure with HTML <ul> and <li> I am almost there, but there is a problem somewhere, which I can't pinpoint. It recreates the directory structure fine as long as the structure is going up once it goes down there is a problem somewhere.
 
Code to run for testing (if anyone is interested in getting this working)
 
<cfscript>
directory = createObject("component", "/openAMS_development/frameWork/component/directory");
tree = createObject("component", "/openAMS_development/frameWork/component/tree");
 
qDirectory = directory.traverse( 'D:\Inetpub\wwwroot\internet\development\openams.com\index\' );
</cfscript>
 
<cfquery name="q" dbtype="query">
SELECT *
FROM qDirectory
WHERE [type] = 'Dir'
</cfquery>
 
<cfset myTree = tree.html(q, "nothingyet", "nothingyet", "nothingyet")>
 
<cfoutput>#myTree#</cfoutput>
 
directory.cfc
*****************************************
<cfcomponent><cfsilent>
 
 <cffunction access="public" name="create" output="false">
  <cfargument name="path" type="string" required="true">
  
  <!--- Remove the trailing slash, if any --->
  <cfif right(arguments.path, 1) IS "/">
   <cfset arguments.path = mid(arguments.path, 1, len(arguments.path) - 1)>
  </cfif>
  <!--- Get the parent path to see if it exists --->
  <cfset pathParent = listDeleteAt(arguments.path, listLen(arguments.path, "\"), "\")>
  <cfif directoryExists(pathParent)>
   <!--- Make sure the directory does not already exist --->
   <cfif NOT directoryExists(arguments.path)>
    <cfdirectory directory="#arguments.path#" action="">
   <cfelse>
    <!--- Return message specifying the directory already exists --->
   </cfif>
  <cfelse>
   <!--- Return message specifying that the parent directory does not exist --->
  </cfif>
 </cffunction>
 

 <cffunction access="public"
   name="traverse"
   output="true"
   returntype="query"
   hint="I traverse through the path I am provided with.">
  <cfargument name="path"
    type="string"
    required="true"
    default=""
    hint="Fully qualified path to the directory">
  <cfargument name="q"
    type="query"
    required="true"
    default="#queryNew("path,level,name,size,type,dateLastModified,attributes,mode")#">
  <cfargument name="i"
    type="numeric"
    required="true"
    default="1">
 
  <!--- Remove trailing slash if any --->
  <cfscript>
  if ( right(arguments.path, 1) IS "\" )
  {
   arguments.path = mid(arguments.path, 1, len(arguments.path) - 1);
  }
  if ( arguments.i EQ 1 )
  {
   queryAddRow(q, 1);
   querySetCell(q, "path", listDeleteAt(arguments.path, listLen(arguments.path, "\"), "\"));
   querySetCell(q, "level", 0);
   querySetCell(q, "name", listLast(arguments.path, "\"));
   querySetCell(q, "size", 0);
   querySetCell(q, "type", "Dir");
   // got an issue with this date for some reason, need to fix it evaluate("qList" & i & ".dateLastModified")
   querySetCell(q, "dateLastModified", "");
   querySetCell(q, "attributes", "");
   querySetCell(q, "mode", "");
  }
  </cfscript>
 
  <!--- Retrieve contents of the directory --->
  <cfdirectory directory="#arguments.path#" action="" name="qList#i#">
 
  <cfif evaluate("qList" & i & ".recordCount")>
   <cfloop query="qList#i#">
    <cfscript>
    queryAddRow(q, 1);
    querySetCell(q, "path", arguments.path);
    querySetCell(q, "level", i);
    querySetCell(q, "name", evaluate("qList" & i & ".name"));
    querySetCell(q, "size", evaluate("qList" & i & ".size"));
    querySetCell(q, "type", evaluate("qList" & i & ".type"));
    // got an issue with this date for some reason, need to fix it evaluate("qList" & i & ".dateLastModified")
    querySetCell(q, "dateLastModified", "");
    querySetCell(q, "attributes", evaluate("qList" & i & ".attributes"));
    querySetCell(q, "mode", evaluate("qList" & i & ".mode"));
    // Only traverse if it is a directory
    if ( evaluate("qList" & i & ".type") IS "dir" )
    {
     directoryName = evaluate("qList" & i & ".name");
     traverse( arguments.path & "\" & directoryName, arguments.q, i + 1 );
    }
    </cfscript>
   </cfloop>
  </cfif>
  <!--- Return the query --->
  <cfreturn q>
 
 </cffunction>
 
</cfsilent></cfcomponent>
 
*****************************************
 
tree.cfc
*****************************************
<cfcomponent><cfsilent>
 
 <cffunction access="public" name="html" output="false" returntype="string">
  <cfargument name="q" type="query" required="true">
  <cfargument name="display" type="string" required="true">
  <cfargument name="value" type="string" required="true">
  <cfargument name="link" type="string" required="true">
  <cfargument name="previousLevel" type="numeric" required="true" default="1">
 
  <cfset var string = "">
 
  <cfloop query="q">
   <cfset string = string & createList(arguments.previousLevel, q.level, val(q.level[q.currentRow + 1]), q.name, "nothing")>
   <cfset arguments.previousLevel = q.level>
  </cfloop>
 
  <cfset string = string & "</ul>">
 
  <cfreturn string>
 
 </cffunction>
 
 <cffunction access="private" name="createList" output="false" returntype="string">
  <cfargument name="previousLevel" type="numeric" required="true">
  <cfargument name="currentLevel" type="numeric" required="true">
  <cfargument name="nextLevel" type="numeric" required="true">
  <cfargument name="value" type="string" required="true">
  <cfargument name="link" type="string" required="true">
 
  <cfset var string = "">
  <cfset var close = "">
 
  <!--- If this is the top parent folder --->
  <cfif arguments.currentLevel EQ 0>
   <cfset string = string & "<ul><li>#arguments.value#">
 
  <!--- If current level is the same as the previous level --->
  <cfelseif arguments.currentLevel EQ arguments.previousLevel>
   <cfset string = string & "<li>#arguments.value#">
   
   <!--- If next level is the same or less than the current level --->
   <cfif arguments.nextLevel EQ arguments.currentLevel OR arguments.nextLevel LT arguments.currentLevel>
    <!--- Close the tag --->
    <cfset string = string & "</li>">
   <cfelse>
    <cfset close = "</li>">
   </cfif>
 
  <!--- If current level is greater than previous level (going up) --->
  <cfelseif arguments.currentLevel GT arguments.previousLevel>
   <cfset string = string & "<ul><li>#arguments.value# (cur: #arguments.currentLevel#) (prev: #arguments.previousLevel#)">
 
   <!--- If the next level is the same as the current level --->
   <cfif arguments.nextLevel EQ arguments.currentLevel>
    <!--- Close the tag --->
    <cfset string = string & "</li>">
   </cfif>
   
   <!--- If the next level is less than the current level --->
   <cfif arguments.nextLevel LT arguments.currentLevel>
    <!--- Close the tag --->
    <cfset string = string & "</li></ul>">
   </cfif>
 
  <!--- If current level is less than previous level (going down) --->
  <cfelseif arguments.currentLevel LT arguments.previousLevel>
   <!--- Close whatever items are unclosed --->
   <cfset string = string & close>
   <cfset string = string & repeatString("</li></ul>", (arguments.previousLevel - arguments.currentLevel) - 1)>
   <cfset string = string & "<li>#arguments.value# (cur: #arguments.currentLevel#) (prev: #arguments.previousLevel#) (close: #(arguments.previousLevel - arguments.currentLevel)#)">
  </cfif>
 
  <cfreturn string>
 
 </cffunction>
 
</cfsilent></cfcomponent>
*****************************************
 
 
Taco Fleur

Tell me and I will forget
Show me and I will remember
Teach me and I will learn
 
---
You are currently subscribed to cfaussie as: [EMAIL PROTECTED]
To unsubscribe send a blank email to [EMAIL PROTECTED]
MXDU2004 + Macromedia DevCon AsiaPac + Sydney, Australia
http://www.mxdu.com/ + 24-25 February, 2004

Reply via email to