1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
<?php 

class CustomFileField extends FileIFrameField {

  protected $startFolder = 0;

  function __construct($name, $title = null, $value = null, $form = null, $rightTitle = null, $folderName = null, $StartFolder = Null) {

    if(isset($StartFolder) && $ID = $this->getFolderID($StartFolder)) $this->startFolder = $ID;

    parent::__construct($name, $title, $value, $form, $rightTitle, $folderName);
  }

  public function getFolderID($FolderName){
    if($Folder = DataObject::get_one('Folder', "Name = '" . $FolderName . "'")){
      return $Folder->ID;
    }
  }

  public function EditFileForm() {
    $uploadFile = _t('FileIFrameField.FROMCOMPUTER', 'From your Computer');
    $selectFile = _t('FileIFrameField.FROMFILESTORE', 'From the File Store');

    if($this->AttachedFile() && $this->AttachedFile()->ID) {
      $title = sprintf(_t('FileIFrameField.REPLACE', 'Replace %s'), $this->FileTypeName());
    } else {
      $title = sprintf(_t('FileIFrameField.ATTACH', 'Attach %s'), $this->FileTypeName());
    }

    $fileSources = array();

    if(singleton($this->dataClass())->canCreate()) {
      if($this->canUploadNewFile) {
        $fileSources["new//$uploadFile"] = new FileField('Upload', '');
      }
    }

    $DropdownField = new TreeDropdownField('ExistingFile', '', 'File');
    $DropdownField->setTreeBaseID($this->startFolder);

    $fileSources["existing//$selectFile"] = $DropdownField;

    $fields = new FieldSet (
      new HeaderField('EditFileHeader', $title),
      new SelectionGroup('FileSource', $fileSources)
    );

    // locale needs to be passed through from the iframe source
    if(isset($_GET['locale'])) {
      $fields->push(new HiddenField('locale', '', $_GET['locale']));
    }

    return new Form (
      $this,
      'EditFileForm',
      $fields,
      new FieldSet(
        new FormAction('save', $title)
      )
    );
  }
}