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.

Customising the CMS /

Advise requested -- SS 3.1 -- JSON error on UploadField


Reply


429 Views

Avatar
MJA

Community Member, 16 Posts

25 January 2014 at 2:59am

Edited: 25/01/2014 3:01am

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
}
?>