Jump to:

5541 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » Advise requested -- SS 3.1 -- JSON error on UploadField

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

Page: 1
Go to End
Author Topic: 315 Views
  • MJA
    Avatar
    Community Member
    15 Posts

    Advise requested -- SS 3.1 -- JSON error on UploadField Link to this post

    I have a DataObject that stores an uploaded document and an UploadField in the back-end CMS to upload the document. The system needs to handle a variety of file types (word documents, images, plain text files, pdf files, etc).

    The upload field works fine for image files (.jpg, .png, etc) but on other file types it throws a JSON error: SyntaxError: JSON.parse: unexpected character

    A quick search of this forum suggested that this might be a permissions problem, but this seems unlikey as:
    a) I have full write access to the assets folder and all sub-folders
    b) This only happend on some file types, not all

    The file is actually uploaded into the assets folder (confirmed with FTP) and the File table in the SS database is correctly updated (confirmed by accessing the DB with phpMyAdmin) but the id (File.ID) is not being returned to my data onbject. The result is a document record with no attached document.

    I suspect I am missing something here but I have no idea what.

    Any advice you can offer would be greatly appreciated.

    Mike Armstrong
    (Silvertoad Ltd)

    <?php
    if (!class_exists('stQuoteDocuments')) {
       class stQuoteDocuments extends DataObject {
       
          public static $singular_name   = 'Document';
          public static $plural_name      = 'Documents';
          
          public static $has_one = array (
             'Quote'      => 'stQuote',
             'DocImage'   => 'Image'
          );
          
          public static $db = array (
             'Name'      => 'Varchar(255)'   // name, title or description of document
          );
          
          public static $summary_fields = array (
             'Name'      => 'Name, title or description of document',
             'fnDocLink'   => 'Uploaded document'
          );
          
          public function getCMSFields() {
             $fields = parent::getCMSFields();
             
             $fields->removeByName('QuoteID');
             
             $fields->addFieldToTab (
                'Root.Main',
                new TextField ('Name', 'Name, title or description of document', $this->Name)
             );
             
             $fields->addFieldToTab (
                'Root.Main',
                $uploadField = new UploadField ('DocImage', 'Upload Document')
             );
             $uploadField->setAllowedMaxFileNumber(1)
                      ->setAllowedFileCategories('image', 'doc')
                      ->setCanAttachExisting(false)
                      ->setCanPreviewFolder(false)
                      ->setFolderName($this->uploadFolder());
             
             if ($this->DocImageID > 0) {
                $fields->addFieldToTab (
                   'Root.Main',
                   new LiteralField ('DocName', $this->makeDocumentLink('DocName', 'Document', $this->fnDocLink(true)))
                );
             }
             
             return $fields;
          }
          
          public function onAfterDelete() {
             // also remove the record on silverstripe's file table
             if ($doc = DataObject::get_one ('File', "ID = '" . $this->DocImageID . "'") )
                $doc->delete();
          }
          
          public function makeDocumentLink($fldName, $fldLabel, $fldContent) {
             $fld = '<div id="' . $fldName . '" class="field text">';
             $fld .= '<label class="left" for="Form_ItemEditForm_' . $fldName . '">' . $fldLabel . '</label>';
             $fld .= '<div class="middleColumn">';
             $fld .= '<span id="' . $fldName . '">' . $fldContent . '</span>';
             $fld .= '</div></div>';
             return $fld;
          }
          
          public function fnDocLink($fullLink = false) {
             $link = '';
             
             if ($this->DocImageID > 0)
                if ($doc = DataObject::get_one ('File', "ID = '" . $this->DocImageID . "'") )
                   if ($fullLink)
                      $link = '<a href="' . $doc->URL . '?nocache=' . rand() . '" target="_blank">' . $doc->Title . '</a>';
                   else
                      $link = $doc->Title;
             
             return $link;
          }
          
          private function uploadFolder() {
             $uploadFolder = 'quote-documents';
             
             if ($this->DocImageID > 0)
                if ($file = DataObject::get_one ('File', "ID = '" . $this->DocImageID . "'")) {
                   $pathInfo = pathinfo($file->Filename);
                   $uploadFolder = $pathInfo['dirname'];
                }
             
             return $uploadFolder;
          }
       
       }   // end class stQuoteDocuments
    }
    ?>

    315 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.