Changeset 1507

Show
Ignore:
Timestamp:
03/22/08 15:53:00 (8 months ago)
Author:
skippy
Message:

Checking in the bulk of bjohnson's patch for #185, providing
enhancements to the SimpleFile media silo. One may now create
directories and upload files.

thanks, bjohnson!

Location:
trunk/htdocs/system
Files:
4 modified

Legend:

Unmodified
Added
Removed
  • trunk/htdocs/system/admin/media.js

    r1300 r1507  
    1717                }, 
    1818                function(result) { 
     19                    var reload = $('.pathstore.toload', container).size(); 
    1920                    habari.media.unqueueLoad(); 
    2021                    $('.pathstore', container).html(result.path); 
     
    3334                        $('.mediadir', container).html(output); 
    3435                    } 
     36                    else if(reload) { 
     37                        // only need to reload the current directory 
     38                        $('.media_dirlevel:last', container).remove(); 
     39                        $('.mediadir', container).append(output); 
     40                    } 
     41 
    3542                    output = '<table><tr>'; 
    3643                    var first = ' first'; 
     
    112119    }, 
    113120 
    114     submitPanel: function(form) { 
    115         var query = $(form).serialize(); 
     121    submitPanel: function(path, panel, form, callback) { 
     122        var query = $(form).serializeArray(); 
     123        query.push({name: 'path', value: path}); 
     124        query.push({name: 'panel', value: panel}); 
     125         
     126        $.post( 
     127            habari.url.habari + '/admin_ajax/media_panel', 
     128            query, 
     129            function(result) { 
     130                $('.media_panel').html(result.panel); 
     131                if (callback != '') { 
     132                    eval(callback); 
     133                } 
     134            }, 
     135            'json'); 
    116136    }, 
    117137 
     
    124144            habari.media.output.image(id, this.assets[id]); 
    125145        } 
     146    }, 
     147     
     148    clearSelections: function() { 
     149        var container = $('.mediasplitter:visible') 
     150        // remove all highlights 
     151        $('.mediadir .directory', container).removeClass('active'); 
     152        // remove second level directories 
     153        $('.mediadir .media_dirlevel', $('.mediasplitter:visible')).nextAll().remove() 
    126154    } 
    127155 
  • trunk/htdocs/system/admin/publish.php

    r1336 r1507  
    4141                <div class="toload pathstore" style="display:none;"><?php echo $silodir->path; ?></div> 
    4242                <div class="splitterinside"> 
    43                     <div class="media_controls"><ul><li><a href="#" onclick="habari.media.showdir('<?php echo $silodir->path; ?>');return false;">Root</a></li></ul></div> 
     43                    <div class="media_controls"><ul><li><a href="#" onclick="habari.media.showdir('<?php echo $silodir->path; ?>');habari.media.clearSelections();return false;">Root</a></li></ul></div> 
    4444                    <div style="white-space:nowrap;overflow-x:scroll;overflow-y:hidden;" class="media_browser"> 
    4545                        <div class="mediadir"></div> 
  • trunk/htdocs/system/classes/formui.php

    r1341 r1507  
    248248    /** 
    249249     * Configure all the options necessary to make this form work inside a media bar panel 
    250      */ 
    251     public function media_panel() 
     250     * @param string $path Identifies the silo  
     251     * @param string $panel The panel in the silo to submit to 
     252     * @param string $callback Javascript function to call on form submission 
     253     */ 
     254    public function media_panel($path, $panel, $callback) 
    252255    { 
    253256        $this->options['show_form_on_success'] = false; 
     
    255258        $this->options['ajax'] = true; 
    256259        $this->options['form_action'] = URL::get('admin_ajax', array('context' => 'media_panel')); 
    257         $this->options['on_submit'] = 'habari.media.submitPanel();return false;'; 
     260        $this->options['on_submit'] = "habari.media.submitPanel('$path', '$panel', this, '{$callback}');return false;"; 
    258261    } 
    259262 
     
    444447        } 
    445448 
    446         $out= '<div class="' . $class . '"><label>' . $this->caption . '<input type="text" name="' . $this->field . '" value="' . $this->value . '"></label>'; 
     449        $out= '<div class="' . $class . '"><label>' . $this->caption . '<input type="text" name="' . $this->name . '" value="' . $this->value . '"></label>'; 
    447450        if(isset($message)) { 
    448451            $out.= '<p class="error">' . $message . '</p>'; 
     
    786789} 
    787790 
     791/** 
     792 * A hidden field control based on FormControl for output via a FormUI. 
     793 */ 
     794class FormControlHidden extends FormControl 
     795{ 
     796 
     797    /** 
     798     * Produce HTML output for this hidden control. 
     799     * 
     800     * @param boolean $forvalidation True if this control should render error information based on validation. 
     801     * @return string HTML that will render this control in the form 
     802     */ 
     803    public function out($forvalidation) 
     804    { 
     805        return '<input type="hidden" name="' . $this->name . '" value="' . $this->default . '">'; 
     806    } 
     807 
     808} 
     809 
     810 
    788811?> 
  • trunk/htdocs/system/plugins/simplefilesilo/simplefilesilo.plugin.php

    r1300 r1507  
    99class SimpleFileSilo extends Plugin implements MediaSilo 
    1010{ 
    11     protected $root = null; 
    12     protected $url = null; 
    13  
    14     const SILO_NAME = 'Local Files'; 
    15  
    16     const DERIV_DIR = '.deriv'; 
     11    protected $root= null; 
     12    protected $url= null; 
     13 
     14    const SILO_NAME= 'Local Files'; 
     15 
     16    const DERIV_DIR= '.deriv'; 
    1717 
    1818    /** 
     
    3838    public function action_init() 
    3939    { 
    40         $this->root = HABARI_PATH . '/' . Site::get_path('user', true) . 'files'; //Options::get('simple_file_root'); 
    41         $this->url = Site::get_url('user', true) . 'files';  //Options::get('simple_file_url'); 
     40        $user_path= HABARI_PATH . '/' . Site::get_path('user', true); 
     41        $this->root= $user_path . 'files'; //Options::get('simple_file_root'); 
     42        $this->url= Site::get_url('user', true) . 'files';  //Options::get('simple_file_url'); 
     43        /* Check for the existence of the files directory */ 
     44        if ( ! is_dir( $this->root ) ) { 
     45            if ( is_writable( $user_path ) ) { 
     46                mkdir( $this->root, 0766 ); 
     47            } else { 
     48                Session::error( "Web server does not have permission to create 'files' directory for SimpleFile Media Silo." ); 
     49            } 
     50        } 
     51          
    4252    } 
    4353 
     
    6070    public function silo_dir( $path ) 
    6171    { 
    62         if(!isset( $this->root )) { 
     72        if( ! isset( $this->root ) ) { 
    6373            return array(); 
    6474        } 
    6575 
    66         $path = preg_replace('%\.{2,}%', '.', $path); 
    67         $results = array(); 
    68  
    69         $dir = glob($this->root . ($path == '' ? '' : '/') . $path . '/*'); 
    70  
    71  
    72         foreach($dir as $item) { 
    73             if( substr(basename($item), 0, 1) == '.' ) { 
     76        $path= preg_replace('%\.{2,}%', '.', $path); 
     77        $results= array(); 
     78 
     79        $dir= glob($this->root . ( $path == '' ? '' : '/' ) . $path . '/*'); 
     80 
     81 
     82        foreach( $dir as $item ) { 
     83            if( substr( basename( $item ), 0, 1 ) == '.' ) { 
    7484                continue; 
    7585            } 
    76             if( basename($item) == 'desktop.ini' ) { 
     86            if( basename( $item ) == 'desktop.ini' ) { 
    7787                continue; 
    7888            } 
    7989 
    80             $file = basename($item); 
     90            $file = basename( $item ); 
    8191            $props = array( 
    82                 'title' => basename($item), 
     92                'title' => basename( $item ), 
    8393            ); 
    84             if(!is_dir($item)) { 
    85                 $thumbnail_suffix = SimpleFileSilo::DERIV_DIR . '/' . $file . '.thumbnail.jpg'; 
    86                 $thumbnail_url = $this->url . '/' . $path . ($path == '' ? '' : '/') . $thumbnail_suffix; 
    87  
    88                 if(!file_exists(dirname($item) . $thumbnail_suffix)) { 
    89                     if(!$this->create_thumbnail($item)) { 
     94            if( ! is_dir( $item ) ) { 
     95                $thumbnail_suffix= SimpleFileSilo::DERIV_DIR . '/' . $file . '.thumbnail.jpg'; 
     96                $thumbnail_url= $this->url . '/' . $path . ($path == '' ? '' : '/') . $thumbnail_suffix; 
     97 
     98                if( ! file_exists( dirname( $item ) . '/' . $thumbnail_suffix ) ) { 
     99                    if( ! $this->create_thumbnail( $item ) ) { 
    90100                        // Do something if we can't create a thumbnail, like return a default image 
    91101                    } 
     
    102112 
    103113            $results[] = new MediaAsset( 
    104                 self::SILO_NAME . '/' . $path . ($path == '' ? '' : '/') . basename($item), 
    105                 is_dir($item), 
     114                self::SILO_NAME . '/' . $path . ($path == '' ? '' : '/') . basename( $item ), 
     115                is_dir( $item ), 
    106116                $props 
    107117            ); 
     
    120130    public function silo_get( $path, $qualities = null ) 
    121131    { 
    122         if(!isset( $this->root )) { 
     132        if( ! isset( $this->root ) ) { 
    123133            return false; 
    124134        } 
    125135 
    126         $path = preg_replace('%\.{2,}%', '.', $path); 
    127  
    128         $file = $this->root . '/' . $path; 
    129  
    130         if(file_exists($file)) { 
    131             $asset = new MediaAsset( self::SILO_NAME . '/' . $path ); 
    132             $asset->set(file_get_contents($file)); 
     136        $path= preg_replace('%\.{2,}%', '.', $path); 
     137 
     138        $file= $this->root . '/' . $path; 
     139 
     140        if( file_exists( $file ) ) { 
     141            $asset= new MediaAsset( self::SILO_NAME . '/' . $path ); 
     142            $asset->set( file_get_contents( $file ) ); 
    133143            return $asset; 
    134144        } 
     
    148158    { 
    149159        // Does derivative directory not exist? 
    150         $thumbdir = dirname($src_filename) . '/'.SimpleFileSilo::DERIV_DIR.''; 
    151         if(!is_dir($thumbdir)) { 
     160        $thumbdir = dirname( $src_filename ) . '/' . SimpleFileSilo::DERIV_DIR . ''; 
     161        if( ! is_dir( $thumbdir ) ) { 
    152162            // Create the derivative driectory 
    153             if(!mkdir($thumbdir, 0766)){ 
     163            if( ! mkdir( $thumbdir, 0766 ) ){ 
    154164                // Couldn't make derivative directory 
    155165                return false; 
     
    158168 
    159169    // Get information about the image 
    160     list($src_width, $src_height, $type, $attr) = getimagesize( $src_filename ); 
     170    list( $src_width, $src_height, $type, $attr )= getimagesize( $src_filename ); 
    161171 
    162172    // Load the image based on filetype 
     
    180190 
    181191    // Calculate the output size based on the original's aspect ratio 
     192    $y_displacement= 0; 
    182193    if ( $src_width / $src_height > $max_width / $max_height ) { 
    183       $thumb_w = $max_width; 
    184       $thumb_h = $src_height * $max_width / $src_width; 
     194      $thumb_w= $max_width; 
     195      $thumb_h= $src_height * $max_width / $src_width; 
     196 
     197      // thumbnail is not full height, position it down so that it will be padded on the 
     198      // top and bottom with black 
     199      $y_displacement= ($max_height - $thumb_h) / 2; 
    185200    } 
    186201    else { 
    187       $thumb_w = $src_width * $max_height / $src_height; 
    188       $thumb_h = $max_height; 
     202      $thumb_w= $src_width * $max_height / $src_height; 
     203      $thumb_h= $max_height; 
    189204    } 
    190205 
    191206    // Create the output image and copy to source to it 
    192     $dst_img = ImageCreateTrueColor( $thumb_w, $thumb_h ); 
    193     imagecopyresampled( $dst_img, $src_img, 0, 0, 0, 0, $thumb_w, $thumb_h, $src_width, $src_height ); 
     207    $dst_img= ImageCreateTrueColor( $thumb_w, $max_height ); 
     208    imagecopyresampled( $dst_img, $src_img, 0, $y_displacement, 0, 0, $thumb_w, $thumb_h, $src_width, $src_height ); 
    194209 
    195210    /* Sharpen before save? 
    196     $sharpenMatrix = array( array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1) ); 
    197     $divisor = 8; 
    198     $offset = 0; 
     211    $sharpenMatrix= array( array(-1, -1, -1), array(-1, 16, -1), array(-1, -1, -1) ); 
     212    $divisor= 8; 
     213    $offset= 0; 
    199214    imageconvolution( $dst_img, $sharpenMatrix, $divisor, $offset ); 
    200215    //*/ 
     
    230245    public function silo_put( $path, $filedata ) 
    231246    { 
    232         $path = preg_replace('%\.{2,}%', '.', $path); 
    233         $file = $this->root . '/' . $path; 
     247        $path= preg_replace('%\.{2,}%', '.', $path); 
     248        $file= $this->root . '/' . $path; 
    234249 
    235250        return $filedata->save( $file ); 
     
    271286    public function link_path( $path, $title = '' ) 
    272287    { 
    273         if($title == '') { 
    274             $title = basename($path); 
     288        if( $title == '' ) { 
     289            $title = basename( $path ); 
    275290        } 
    276291        return '<a href="#" onclick="habari.media.showdir(\''.$path.'\');return false;">' . $title . '</a>'; 
     
    304319    { 
    305320        $class = __CLASS__; 
    306         if($silo instanceof $class) { 
    307             $controls[] = $this->link_path(self::SILO_NAME . '/' . $path, 'Current Path'); 
    308             if(User::identify()->can('upload_media')) { 
    309                 $controls[] = $this->link_panel(self::SILO_NAME . '/' . $path, 'upload', 'Upload'); 
    310             } 
    311             if(User::identify()->can('create_directories')) { 
    312                 $controls[] = $this->link_panel(self::SILO_NAME . '/' . $path, 'mkdir', 'Create Directory'); 
     321        if( $silo instanceof $class ) { 
     322            $controls[]= $this->link_path( self::SILO_NAME . '/' . $path, 'Browse' ); 
     323            if( User::identify()->can( 'upload_media' ) ) { 
     324                $controls[]= $this->link_panel(self::SILO_NAME . '/' . $path, 'upload', 'Upload'); 
     325            } 
     326            if( User::identify()->can( 'create_directories' ) ) { 
     327                $controls[]= $this->link_panel(self::SILO_NAME . '/' . $path, 'mkdir', 'Create Directory'); 
    313328            } 
    314329        } 
     
    339354    { 
    340355        $class = __CLASS__; 
    341         if($silo instanceof $class) { 
    342             switch($panelname) { 
     356        if( $silo instanceof $class ) { 
     357            switch( $panelname ) { 
    343358                case 'mkdir': 
    344                     $form = new FormUI('simplefilesilomkdir'); 
    345                     $form->add('text', 'directory', 'Enter the name of the new directory to create here'); 
    346                     $form->media_panel(); 
    347                     $form->on_success(array(&$this, 'mkdir'), $panel, $silo, $path); 
    348                     return $form->get(); 
     359                    /* catch form submission */ 
     360                    if ( isset( $_POST['directory'] ) ) { 
     361                        $dir= preg_replace('%\.{2,}%', '.', $_POST['directory']); 
     362                        if ( isset( $path ) ) { 
     363                            $dir= $this->root . '/' . $path . '/' . $dir; 
     364                        } else { 
     365                            $dir= $this->root . '/' . $dir; 
     366                        } 
     367                        if ( mkdir( $dir, 0766 ) ) { 
     368                            $panel= "<div class=\"span-18\"style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\"><p>Directory Created: {$dir}</p>"; 
     369                        } 
     370                        else { 
     371                            $panel= "<div class=\"span-18\"style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\"><p>Failed to create directory: {$dir}. Check that the webserver has write permission in the parent directory</p></div>"; 
     372                        } 
     373                        return $panel; 
     374 
     375                    } 
     376                    else { 
     377                        $fullpath= self::SILO_NAME . '/' . $path; 
     378 
     379                        $form= new FormUI( 'simplefilesilomkdir' ); 
     380                        $form->add('static', 'ParentDirectory', "Parent Directory: <strong>/{$path}</strong>"); 
     381                        $form->add('text', 'directory', 'Enter the name of the new directory to create here'); 
     382                        $form->media_panel($fullpath, $panelname, 'habari.media.forceReload();'); 
     383                        return $form->get(); 
     384                        return $panel; 
     385                    } 
    349386                    break; 
    350387                case 'upload': 
    351                     if(isset($_FILES['file'])) { 
    352                         $size = Utils::human_size($_FILES['file']['size']); 
    353                         $panel .= "<div class=\"span-18\" style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\"><p>File Uploaded: {$_FILES['file']['name']} ($size)</p>"; 
    354  
    355                         $path = self::SILO_NAME . '/' . preg_replace('%\.{2,}%', '.', $path). '/' . $_FILES['file']['name']; 
    356                         $asset = new MediaAsset($path, false); 
    357                         $asset->upload($_FILES['file']); 
    358  
    359                         if($asset->put()) { 
    360                             $panel .= '<p>File added successfully.</p>'; 
     388                    if( isset( $_FILES['file'] ) ) { 
     389                        $size= Utils::human_size($_FILES['file']['size']); 
     390                        $panel.= "<div class=\"span-18\" style=\"padding-top:30px;color: #e0e0e0;margin: 0px auto;\"><p>File Uploaded: {$_FILES['file']['name']} ($size)</p>"; 
     391 
     392                        $path= self::SILO_NAME . '/' . preg_replace('%\.{2,}%', '.', $path). '/' . $_FILES['file']['name']; 
     393                        $asset= new MediaAsset($path, false); 
     394                        $asset->upload( $_FILES['file'] ); 
     395 
     396                        if( $asset->put() ) { 
     397                            $panel.= '<p>File added successfully.</p>'; 
    361398                        } 
    362399                        else { 
    363                             $panel .= '<p>File could not be added to the silo.</p>'; 
     400                            $panel.= '<p>File could not be added to the silo.</p>'; 
    364401                        } 
    365402 
    366                         $panel .= '<p><a href="#" onclick="habari.media.forceReload();habari.media.showdir(\'' . dirname($path) . '\');">Browse the current silo path.</a></p></div>'; 
     403                        $panel.= '<p><a href="#" onclick="habari.media.forceReload();habari.media.showdir(\'' . dirname($path) . '\');">Browse the current silo path.</a></p></div>'; 
    367404                    } 
    368405                    else { 
    369406 
    370                         $fullpath = self::SILO_NAME . '/' . $path; 
    371                         $form_action = URL::get('admin_ajax', array('context' => 'media_panel')); 
    372                         $panel .= <<< UPLOAD_FORM 
     407                        $fullpath= self::SILO_NAME . '/' . $path; 
     408                        $form_action= URL::get('admin_ajax', array('context' => 'media_panel')); 
     409                        $panel.= <<< UPLOAD_FORM 
    373410<form enctype="multipart/form-data" method="post" id="simple_upload" target="simple_upload_frame" action="{$form_action}" class="span-10" style="margin:0px auto;text-align: center"> 
    374411    <p style="padding-top:30px;">Upload to: <b style="font-weight:normal;color: #e0e0e0;font-size: 1.2em;">/{$path}</b></p> 
     
    401438    } 
    402439 
     440    /* this function should convert the virtual path to a real path and  
     441     * then call php's mkdir function */ 
    403442    public function mkdir($form, $panel, $silo, $path) 
    404443    { 
    405         return false; 
     444        /* check that the regular expression is required for this case */ 
     445        $path= preg_replace('%\.{2,}%', '.', $path); 
     446        $dir= $this->root . '/' . $path; 
     447        return mkdir( $dir ); 
    406448    } 
    407449