Documentation is available at classes_folder.php
- <?php
- class folder extends b_resource {
- //base resource definition
- function folder( $constructID = NULL ) {
- parent::b_resource( $constructID, 'folders' );
- }
- function get_index() {
- if( $this->name ) {
- return $this->name;
- } else if( $this->path ) {
- $path_parts = pathinfo($this->path);
- return $path_parts['basename'];
- } else {
- return "new folder";
- }
- }
- function scan( $include_subdirs = TRUE, $resize = FALSE ) {
- //scans the folder and returns an subfolders and files which do not exist in the database
- if( file_exists( $this->path ) ) {
- $dir = get_directory_tree( $this->path );
- $subdir =& $GLOBALS['core']->get_relationship_type('parent folder-subfolders');
- $subfile =& $GLOBALS['core']->get_relationship_type('parent folder-images');
- //retrieve all files and create new DB entries for new ones
- foreach( $dir['files'] as $file ) {
- if( $class = resource_class( $file ) ) {
- //set object in DB
- $resource = new $class( $file );
- if( $resource->id == '*new*' ) {
- $resource->verify_set( array( 'path'=>$file, 'name'=>$path_parts['basename'] ) );
- //relate to parent directory
- new relationship( NULL, $subfile->type_id, $this->id, $resource->id );
- }
- }
- }
- foreach( $dir['folders'] as $folder ) {
- //set object in DB
- $resource = new folder( $folder );
- if( $resource->id == '*new*' ) {
- $resource->verify_set( array( 'path'=>$folder ) );
- //relate to parent directory
- new relationship( NULL, $subdir->type_id, $this->id, $resource->id );
- }
- }
- //validate and propogate
- if( $files = $this->get_files() ) {
- foreach( $files as $file ) {
- //validate
- if( !$file->validate() ) { $file->delete(); }
- else if( $resize && get_class( $file ) == 'image' ) {
- //create derivative files for screen and icon display
- if( !$file->derivative_paths['icon'] ) {
- $path_parts = pathinfo( $file->path );
- $target_path = $path_parts['dirname'].'/_icon_images/'.$path_parts['basename'];
- $file->resize_proportional( 100, 100, $target_path, 'icon' );
- }
- if( !$file->derivative_paths['screen'] ) {
- $path_parts = pathinfo( $file->path );
- $target_path = $path_parts['dirname'].'/_screen_images/'.$path_parts['basename'];
- $file->resize_proportional( 500, 400, $target_path, 'screen' );
- }
- $file->verify_set( array( 'derivative_paths' => $file->derivative_paths ) );
- }
- }
- }
- if( $folders = $this->get_subfolders() ) {
- foreach( $folders as $folder ) {
- //validate
- if( !$folder->validate() ) { $folder->delete(); }
- //propogate
- else if( $include_subdirs ) { $folder->scan( $include_subdirs ); }
- }
- }
- } else {
- $this->delete();
- }
- }
- function get_folder_contents() {
- return array( 'subfolders'=>$this->get_subfolders(), 'files'=>$this->get_files() );
- }
- function get_subfolders() {
- if( $type =& $GLOBALS['core']->get_relationship_type( 'parent folder-subfolders' ) ) {
- if( $relationships = $type->get_byValue( array(array('attribute'=>'primaryID','operator'=>'=','value'=>$this->id)) ) ) {
- foreach( $relationships as $relationship ) {
- $returnArray[] = get_object($relationship->secondaryID);
- }
- }
- return $returnArray;
- }
- }
- function get_files() {
- //NOTE: if any other file types are defined they'll need to be added here
- if( $type =& $GLOBALS['core']->get_relationship_type( 'parent folder-images' ) ) {
- if( $relationships = $type->get_byValue( array(array('attribute'=>'primaryID','operator'=>'=','value'=>$this->id)) ) ) {
- foreach( $relationships as $relationship ) {
- $returnArray[] =& get_object($relationship->secondaryID);
- }
- }
- return $returnArray;
- }
- }
- function delete( $delete_file = TRUE ) {
- //remove resource if local
- if( $delete_file && file_exists( $this->path ) ) {
- //delete directory and contents
- //NOTE: this is from the old delete directory function, but it doesn't work with the database
- /*if ($handle = opendir($this->path)) {
- while (false !== ($file = readdir($handle))) {
- if ($file != "." && $file != ".." ) {
- if( is_dir( $path."/".$file ) ) {
- delete_directory( $path."/".$file );
- } else {
- unlink( $path."/".$file );
- }
- }
- }
- closedir($handle);
- }*/
- //NOTE: i think this is a better approach
- if( $files = $this->get_files() ) { foreach( $files as $file ) {
- $file->delete();
- } }
- //get any subfolders or files in the folder and delete them
- if( $subfolders = $this->get_subfolders() ) { foreach( $subfolders as $folder ) {
- $folder->delete();
- } }
- //delete this folder
- rmdir( $this->path );
- }
- //remove from DB
- parent::delete();
- }
- function get_contents( $navArray = NULL ) {
- if( allowed( 'view any '.$this->type->type_name ) ) {
- if( !$fieldArray ) { $fieldArray = $this->type->get_attribute( 'type_object_field_list' ); }
- if( $_GET['module'] ) { $module = 'module='.$_GET['module'].'&'; }
- $returnString;
- //open the form
- $returnString .='<form method="post" name="theForm" action="'.echo_url(true, false).'?'.$module.'object_type='.$this->type->get_attribute( 'type_name' ).'&object='.$this->id.'">';
- //output toolbar and navigation
- $returnString .= $this->get_sidebar( $navArray );
- //open the content div
- $returnString .= '<div id="sub_content">';
- /*global $db;
- $db->get_results("SELECT * FROM object_permissions ORDER BY name");
- $db->debug();*/
- //output title info
- $returnString .= $this->get_title();
- //output an errors in submitted form data
- $returnString .= error_text();
- //output the folder's contents
- $returnString .= $this->get_folder_table();
- if( $this->id == '*new*' ) {
- $fieldArray = array( array( 'src'=>'var', 'type'=>'input', 'label'=>'folder path', 'value'=>'path' ) );
- }
- //output the object's data
- $returnString .= $this->get_object_table( $fieldArray );
- //close the form
- $returnString .= '</form>';
- //output any relationships
- //if( $this->id != '*new*' ) {
- //$returnString .= $this->get_relationships_content();
- //}
- //close the content div
- $returnString .= '</div>';
- return $returnString;
- }
- }
- function get_folder_table() {
- //this should display the folder's contents, differentiating between folders and files, and providing links to edit or delete each file
- if( $folders = $this->get_subfolders() ) {
- foreach( $folders as $folder ) {
- $output[] =& $folder;
- }
- }
- if( $files = $this->get_files() ) {
- foreach( $files as $file ) {
- $output[] = $file;
- }
- }
- if( $output ) {
- $fieldArray = array(
- array( 'src'=>'var', 'label'=>'type', 'value'=>'resource_type' ),
- array( 'src'=>'var', 'label'=>'name', 'value'=>'name' ),
- array( 'src'=>'var', 'label'=>'date', 'value'=>'create_date' ),
- array( 'src'=>'switch', 'value'=>'edit', 'permission'=>"view any single folder" ),
- array( 'src'=>'switch', 'value'=>'delete', 'permission'=>"modify folders" )
- );
- return table( $output, '', $fieldArray );
- }
- }
- }
- ?>
Documentation generated on Tue, 24 May 2005 03:57:06 -0400 by phpDocumentor 1.3.0RC3