Skip to main content

This site requires you to update your browser. Your browsing experience maybe affected by not having the most up to date version.

We've moved the forum!

Please use forum.silverstripe.org for any new questions (announcement).
The forum archive will stick around, but will be read only.

You can also use our Slack channel or StackOverflow to ask for help.
Check out our community overview for more options to contribute.

Form Questions /

Moderators: martimiz, Sean, Ed, biapar, Willr, Ingo, swaiba

Double document add form


Go to End


2 Posts   2159 Views

Avatar
darjus01

Community Member, 6 Posts

2 February 2015 at 10:33pm

Hi, I am traing tu make double document add form is is checkbox enabled. but i cant find the way to take uploaded file id the is my object. and in image some logic explanation. maybe some one can tell what I am doing wrong? please.

<?php
class Document extends DataObject {
    private static $default_sort = 'SortOrder';
    private static $db = array(
        'SortOrder' => 'Int',
        'Title' => 'Varchar(255)',
        'HaveChild' => 'Boolean',
        'ParentDocumentID' => 'Int'
    );
    private static $has_one = array(
        'File' => 'File',
        'Category' => 'Category',
        'Location' => 'Location',
        'Product' => 'Product'
    );
    
    public function getCMSFields() {
        $fields = parent::getCMSFields();
        $fields = new FieldList(
            DropdownField::create('ProductID', 'Product', Product::get()->map('ID', 'Title'))->setEmptyString('(Select one)'),
            DropdownField::create('CategoryID', 'Category', Category::get()->map('ID', 'Title'))->setEmptyString('(Select one)')->displayIf("ProductID")->isNotEqualTo("")->end(),
            DropdownField::create('LocationID', 'Location', Location::get()->map('ID', 'Title'))->setEmptyString('(Select one)')->displayIf("ProductID")->isNotEqualTo("")->andIf("ProductID")->isNotEqualTo(3)->end(),
            TextField::create('Title', 'Title')->displayIf("ProductID")->isNotEqualTo("")->end(),
            UploadField::create('File', 'File')->setFolderName('Uploads/Files')->setAllowedExtensions(array('odt', 'jpg', 'jpeg', 'png', 'gif', 'doc', 'docx', 'xls', 'xlsx', 'ppt', 'pdf'))->displayIf("ProductID")->isNotEqualTo("")->end(),
                
            CheckboxField::create('HaveChild', 'I want to upload additional document')->displayIf("ProductID")->isEqualTo(5)->orIf("ProductID")->isEqualTo(6)->end(),
            DropdownField::create('CategoryID2', 'Category2', Category::get()->map('ID', 'Title'))->setEmptyString('(Select one)')->displayIf('HaveChild')->isChecked()->end(),
            DropdownField::create('LocationID2', 'Location2', Location::get()->map('ID', 'Title'))->setEmptyString('(Select one)')->displayIf('HaveChild')->isChecked()->end(),
            TextField::create('Title2', 'Title2')->displayIf('HaveChild')->isChecked()->end(),
            UploadField::create('File2', 'File2')->displayIf('HaveChild')->isChecked()->end()
        );
        
        
        return $fields;
    }
    
    private static $summary_fields = array(
        'SortOrder' => '#',
        'Location.Title' => 'Location',
        'Title' => 'Title',
        'Category.Title' => 'Category',
        'File.Name' => 'File',
        'File.Size' => 'Size',
        'LastEdited' => 'LastEdited',
        'Product.Name' => 'Product'
    );
    
    public function LocationDocumentTitle() {
        $lt = $this->Location()->Title;
        if ($lt == "" OR $lt == NULL)
        {
            return $this->Title;
        }
        else
        {
            return $this->Location()->Title . ' - ' . $this->Title;
        }
        
    }
    
    public function getFileToDownload() {
        $documentID = $this->ID;
    }
    
    
    public function onAfterWrite() {
        // 1. if selected add additional document
        if ($this->HaveChild == 1) {
            
            print_r($this);
            $parentID = $this->ID;
            
            // 2. if is anny file with productID associated
            $countOfParent = Document::get()->where("`Document`.`ParentDocumentID` = ".$parentID."")->Count();
            if ($countOfParent == 0) { // if 0 means first save then creation of sub document prossed
                $associatedDocument = new Document();
                $associatedDocument->Title = $this->Title2;
                $associatedDocument->CategoryID = $this->CategoryID2;
                $associatedDocument->LocationID = $this->LocationID2;
                
                if ($this->ProductID == 5) {
                    $associatedDocument->ProductID = 2;
                }
                elseif ($this->ProductID == 6) {
                    $associatedDocument->ProductID = 1;
                }
                $associatedDocument->ParentDocumentID = $parentID;
                
                //print_r($_POST['File2[Files][]']);
                
                //$associatedDocument->FileID = $this->File2;
                
                $associatedDocument->write();
            }
        }
        // else do nothing
        
        
        parent::onAfterWrite();
    }
}

Attached Files
Avatar
darjus01

Community Member, 6 Posts

3 February 2015 at 9:46pm

Founded myself just used POST to get hidden input, $_POST['File2']['Files'][0]