Version: current

      1 <?php
      2
      3 class MultiUploadField extends FileField {
      4
      5     function __construct($form_name, $name, $title = null, $configuration = array()) {
      6         parent::__construct($name, $title);
      7         if(isset($value)) $this->value = $value;
      8
      9         MultiUploadConfig::$form_name = $form_name;
     10
     11         if(!empty($configuration)) {
     12             MultiUploadConfig::Configure($configuration);
     13         }
     14         $url = MultiUploadConfig::get_var('upload_url');
     15         if(empty($url))
     16             MultiUploadConfig::set_var('upload_url',Director::absoluteURL(Controller::curr()->Link('handleupload')));
     17         MultiUploadConfig::addStaticPostParam('PHPSESSID',session_id());
     18         MultiUploadConfig::addStaticPostParam(ini_get('session.name'), session_id());
     19     }
     20
     21     function FieldHolder() {
     22         $Title = $this->XML_val('Title');
     23         $Message = $this->XML_val('Message');
     24         $MessageType = $this->XML_val('MessageType');
     25         $RightTitle = $this->XML_val('RightTitle');
     26         $Type = $this->XML_val('Type');
     27         $extraClass = $this->XML_val('extraClass');
     28         $Name = $this->XML_val('Name');
     29         $Field = $this->XML_val('Field');
     30
     31         $titleBlock = (!empty($Title)) ? "<label class=\"left\" for=\"{$this->id()}\">$Title</label>" : "";
     32         $messageBlock = (!empty($Message)) ? "<span class=\"message $MessageType\">$Message</span>" : "";
     33         $rightTitleBlock = (!empty($RightTitle)) ? "<label class=\"right\" for=\"{$this->id()}\">$RightTitle</label>" : "";
     34
     35         Requirements::css('mutliupload_field/css/multiupload.css');
     36
     37         //Requirements::javascript(JQUERY_PATH); !!!!
     38         Requirements::javascript('mutliupload_field/javascript/ajax.js');
     39
     40         $uploadLink = MultiUploadConfig::get_var('upload_url');
     41         $reload_after_upload = MultiUploadConfig::get_var('reload_after_upload');
     42         $browseButtonText = MultiUploadConfig::get_var('browse_button_text');
     43         return <<<HTML
     44 <div id="$Name" class="field $Type $extraClass js_multiFilesUploader" data-postlink="{$uploadLink}" {$this->AllowedFileTypes()} {$this->MaxFileSize()} data-autosubmit="{$reload_after_upload}">$titleBlock
     45
     46     <div class="js_multiFilesUploader-input">$Field</div>
     47
     48
     49     <div class="multiuploadInfo">
     50         <span class="multiuploadInfo-count js_multiFilesUploader-count">Файлов не выбрано</span><br/>
     51         Общий размер: <span class="multiuploadInfo-size js_multiFilesUploader-size">0 КБ</span>
     52     </div>
     53
     54     <div class="multiupload-actions">
     55         <a class="addFiles">
     56             <span><img src="mutliupload_field/img/add.png" alt="">$browseButtonText</span>
     57             <input class="addFiles-innerInput js_multiFilesUploader-multipleInput" type="file" name="upload_file" id="file-field" multiple="true" />
     58         </a>
     59         <a class="js_multiFilesUploader-uploadFiles is_disabled" style="float: right;">
     60             <span>Отправить</span>
     61         </a>
     62     </div>
     63
     64     $rightTitleBlock$messageBlock
     65 </div>
     66 HTML;
     67     }
     68
     69     function Value() {
     70         return !empty($_POST['uploaded_files']) ? $_POST['uploaded_files'] : (isset($_FILES[$this->Name]) ? $_FILES[$this->Name] : "");
     71     }
     72
     73     function dataValue() {
     74         return $this->Value();
     75     }
     76
     77     function PostParams() {
     78         $html = '';
     79         $data = array();
     80         if (count(explode(',', MultiUploadConfig::$post_params))) {
     81             foreach(explode(',', MultiUploadConfig::$post_params) as $entry) {
     82                 $t = explode(':', str_replace("'", '', $entry));
     83                 $data[] = array('title' => trim($t[0]), 'value' => trim($t[1]));
     84                 //$html .= '<span class="uploader_post_params" id="'.$t[0].'">'.$t[1].'</span>';
     85             }
     86         }
     87         return '<span class="js_multiFilesUploader-postParams">'.json_encode($data).'</span>';
     88     }
     89
     90     function AllowedFileTypes() {
     91         if (count(MultiUploadConfig::$file_types)) {
     92             return "data-allowedFormats=\"[&#34;".implode("&#34;, &#34;", MultiUploadConfig::$file_types)."&#34;]\"";
     93         }
     94         return '';
     95     }
     96
     97     function MaxFileSize() {
     98         if (MultiUploadConfig::$file_size_limit) {
     99             return 'data-maxFileSize="'.MultiUploadConfig::$file_size_limit.'"';
    100         }
    101         return '';
    102     }
    103
    104     function Field() {
    105         //$uploadLink = MultiUploadConfig::get_var('upload_url');
    106         return <<<HTML
    107     <span style="display: none;">
    108         {$this->PostParams()}
    109     </span>
    110     <ul class="fileList js_multiFilesUploader-fileList"></ul>
    111     <!--<div><input class="input-file js_file_check" type="file" name="upload_file" id="file-field" multiple="true" /></div>-->
    112 HTML;
    113 }
    114 }
    115
    116 ?>

[Raise a SilverStripe Framework issue/bug](https://github.com/silverstripe/silverstripe-framework/issues/new)
- [Raise a SilverStripe CMS issue/bug](https://github.com/silverstripe/silverstripe-cms/issues/new)
- Please use the Silverstripe Forums to ask development related questions. -