Source for file classes_folder.php

Documentation is available at classes_folder.php

  1. <?php
  2.  
  3. class folder extends b_resource {
  4. //base resource definition
  5. function folder( $constructID = NULL ) {
  6. parent::b_resource( $constructID, 'folders' );
  7. }
  8. function get_index() {
  9. if( $this->name ) {
  10. return $this->name;
  11. } else if( $this->path ) {
  12. $path_parts = pathinfo($this->path);
  13. return $path_parts['basename'];
  14. } else {
  15. return "new folder";
  16. }
  17. }
  18. function scan( $include_subdirs = TRUE, $resize = FALSE ) {
  19. //scans the folder and returns an subfolders and files which do not exist in the database
  20. if( file_exists( $this->path ) ) {
  21. $dir = get_directory_tree( $this->path );
  22. $subdir =& $GLOBALS['core']->get_relationship_type('parent folder-subfolders');
  23. $subfile =& $GLOBALS['core']->get_relationship_type('parent folder-images');
  24. //retrieve all files and create new DB entries for new ones
  25. foreach( $dir['files'] as $file ) {
  26. if( $class = resource_class( $file ) ) {
  27.  
  28. //set object in DB
  29. $resource = new $class( $file );
  30. if( $resource->id == '*new*' ) {
  31. $resource->verify_set( array( 'path'=>$file, 'name'=>$path_parts['basename'] ) );
  32. //relate to parent directory
  33. new relationship( NULL, $subfile->type_id, $this->id, $resource->id );
  34. }
  35. }
  36. }
  37. foreach( $dir['folders'] as $folder ) {
  38. //set object in DB
  39. $resource = new folder( $folder );
  40. if( $resource->id == '*new*' ) {
  41. $resource->verify_set( array( 'path'=>$folder ) );
  42.  
  43. //relate to parent directory
  44. new relationship( NULL, $subdir->type_id, $this->id, $resource->id );
  45. }
  46. }
  47. //validate and propogate
  48. if( $files = $this->get_files() ) {
  49. foreach( $files as $file ) {
  50. //validate
  51. if( !$file->validate() ) { $file->delete(); }
  52. else if( $resize && get_class( $file ) == 'image' ) {
  53. //create derivative files for screen and icon display
  54. if( !$file->derivative_paths['icon'] ) {
  55. $path_parts = pathinfo( $file->path );
  56. $target_path = $path_parts['dirname'].'/_icon_images/'.$path_parts['basename'];
  57. $file->resize_proportional( 100, 100, $target_path, 'icon' );
  58. }
  59. if( !$file->derivative_paths['screen'] ) {
  60. $path_parts = pathinfo( $file->path );
  61. $target_path = $path_parts['dirname'].'/_screen_images/'.$path_parts['basename'];
  62. $file->resize_proportional( 500, 400, $target_path, 'screen' );
  63. }
  64. $file->verify_set( array( 'derivative_paths' => $file->derivative_paths ) );
  65. }
  66. }
  67. }
  68. if( $folders = $this->get_subfolders() ) {
  69. foreach( $folders as $folder ) {
  70. //validate
  71. if( !$folder->validate() ) { $folder->delete(); }
  72. //propogate
  73. else if( $include_subdirs ) { $folder->scan( $include_subdirs ); }
  74. }
  75. }
  76. } else {
  77. $this->delete();
  78. }
  79. }
  80. function get_folder_contents() {
  81. return array( 'subfolders'=>$this->get_subfolders(), 'files'=>$this->get_files() );
  82. }
  83. function get_subfolders() {
  84. if( $type =& $GLOBALS['core']->get_relationship_type( 'parent folder-subfolders' ) ) {
  85. if( $relationships = $type->get_byValue( array(array('attribute'=>'primaryID','operator'=>'=','value'=>$this->id)) ) ) {
  86. foreach( $relationships as $relationship ) {
  87. $returnArray[] = get_object($relationship->secondaryID);
  88. }
  89. }
  90. return $returnArray;
  91. }
  92. }
  93. function get_files() {
  94. //NOTE: if any other file types are defined they'll need to be added here
  95. if( $type =& $GLOBALS['core']->get_relationship_type( 'parent folder-images' ) ) {
  96. if( $relationships = $type->get_byValue( array(array('attribute'=>'primaryID','operator'=>'=','value'=>$this->id)) ) ) {
  97. foreach( $relationships as $relationship ) {
  98. $returnArray[] =& get_object($relationship->secondaryID);
  99. }
  100. }
  101. return $returnArray;
  102. }
  103. }
  104. function delete( $delete_file = TRUE ) {
  105. //remove resource if local
  106. if( $delete_file && file_exists( $this->path ) ) {
  107. //delete directory and contents
  108. //NOTE: this is from the old delete directory function, but it doesn't work with the database
  109. /*if ($handle = opendir($this->path)) {
  110. while (false !== ($file = readdir($handle))) {
  111. if ($file != "." && $file != ".." ) {
  112. if( is_dir( $path."/".$file ) ) {
  113. delete_directory( $path."/".$file );
  114. } else {
  115. unlink( $path."/".$file );
  116. }
  117. }
  118. }
  119. closedir($handle);
  120. }*/
  121. //NOTE: i think this is a better approach
  122. if( $files = $this->get_files() ) { foreach( $files as $file ) {
  123. $file->delete();
  124. } }
  125.  
  126. //get any subfolders or files in the folder and delete them
  127. if( $subfolders = $this->get_subfolders() ) { foreach( $subfolders as $folder ) {
  128. $folder->delete();
  129. } }
  130. //delete this folder
  131. rmdir( $this->path );
  132. }
  133. //remove from DB
  134. parent::delete();
  135. }
  136. function get_contents( $navArray = NULL ) {
  137. if( allowed( 'view any '.$this->type->type_name ) ) {
  138. if( !$fieldArray ) { $fieldArray = $this->type->get_attribute( 'type_object_field_list' ); }
  139. if( $_GET['module'] ) { $module = 'module='.$_GET['module'].'&'; }
  140. $returnString;
  141. //open the form
  142. $returnString .='<form method="post" name="theForm" action="'.echo_url(true, false).'?'.$module.'object_type='.$this->type->get_attribute( 'type_name' ).'&object='.$this->id.'">';
  143.  
  144. //output toolbar and navigation
  145. $returnString .= $this->get_sidebar( $navArray );
  146. //open the content div
  147. $returnString .= '<div id="sub_content">';
  148. /*global $db;
  149. $db->get_results("SELECT * FROM object_permissions ORDER BY name");
  150. $db->debug();*/
  151. //output title info
  152. $returnString .= $this->get_title();
  153. //output an errors in submitted form data
  154. $returnString .= error_text();
  155. //output the folder's contents
  156. $returnString .= $this->get_folder_table();
  157. if( $this->id == '*new*' ) {
  158. $fieldArray = array( array( 'src'=>'var', 'type'=>'input', 'label'=>'folder path', 'value'=>'path' ) );
  159. }
  160. //output the object's data
  161. $returnString .= $this->get_object_table( $fieldArray );
  162. //close the form
  163. $returnString .= '</form>';
  164. //output any relationships
  165. //if( $this->id != '*new*' ) {
  166. //$returnString .= $this->get_relationships_content();
  167. //}
  168. //close the content div
  169. $returnString .= '</div>';
  170. return $returnString;
  171. }
  172. }
  173. function get_folder_table() {
  174. //this should display the folder's contents, differentiating between folders and files, and providing links to edit or delete each file
  175. if( $folders = $this->get_subfolders() ) {
  176. foreach( $folders as $folder ) {
  177. $output[] =& $folder;
  178. }
  179. }
  180. if( $files = $this->get_files() ) {
  181. foreach( $files as $file ) {
  182. $output[] = $file;
  183. }
  184. }
  185. if( $output ) {
  186. $fieldArray = array(
  187. array( 'src'=>'var', 'label'=>'type', 'value'=>'resource_type' ),
  188. array( 'src'=>'var', 'label'=>'name', 'value'=>'name' ),
  189. array( 'src'=>'var', 'label'=>'date', 'value'=>'create_date' ),
  190. array( 'src'=>'switch', 'value'=>'edit', 'permission'=>"view any single folder" ),
  191. array( 'src'=>'switch', 'value'=>'delete', 'permission'=>"modify folders" )
  192. );
  193.  
  194. return table( $output, '', $fieldArray );
  195. }
  196. }
  197. }
  198.  
  199. ?>

Documentation generated on Tue, 24 May 2005 03:57:06 -0400 by phpDocumentor 1.3.0RC3