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.

Data Model Questions /

TableListField to show uploaded file link?


Reply


4 Posts   2267 Views

Avatar
danietsai

Community Member, 2 Posts

15 April 2009 at 5:30pm

Edited: 15/04/2009 5:32pm

Hi everyone,

I have tried to search for an answer of my question in the forum or document, but could not find one.

I have a custom form (a contact form for my company website) which includes a File field. I want to show the list of all submissions in the admin page using TableListField. My code is listed below. I got the following error message "__call() Method 'forTemplate' not found in class 'File'". If I remove Attachment from below, it just works perfectly fine.

I understand that a File field doesn't have forTemplate method. The problem is how can I get the file download link correctly?

Thanks in advance if anyone can give me a hand. Appreciate that.

function getReportField() {      
   $resultSet = new DataObjectSet();
   $filter = '';
   $sort = "ContactPageSubmission.ID ASC";
   $join = '';
   $instance = singleton('ContactPageSubmission');      
   $query = $instance->buildSQL($filter, $sort, null, $join);

   $report = new TableListField(
      'ContactPageSubmissionReport',
      'ContactPageSubmission',
      array(
         'ID' => 'ID',
         'Created' => 'Date/Time',
         'FirstName' => 'First Name',
         'LastName' => 'Last Name',
         'Email' => 'Email Address',
         'Attachment' => ''
      )
   );
      
   $report->setCustomQuery($query);
      
   $report->setFieldFormatting(array(
      'Email' => '<a href=\"mailto: $Email\" title=\"Email $FirstName\">$Email</a>',
      'Attachment' => '<a href=\"$Attachment.URL\" title=\"$Attachment.Title\">$Attachment.Title</a>'
   ));
      
   $report->setFieldCasting(array(
      'Created' => 'Date->Nice'
   ));
      
   $report->setShowPagination(true);
   if(isset($_REQUEST['printable'])) {
      $report->setPageSize(false);
   } else {
      $report->setPageSize(50);
   }
      
   $report->setPermissions(array(
      'export',
      'delete',
      'print'
   ));
      
   return $report;
}

Avatar
danietsai

Community Member, 2 Posts

19 April 2009 at 12:21pm

Edited: 19/04/2009 12:26pm

I should have provided my data model class as below:

class ContactPageSubmission extends DataObject {
static $db = array(
'FirstName' => 'Text',
'LastName' => 'Text',
'Email' => 'Text',
'Comments' => 'Text'
);

static $has_one = array(
'Attachment' => 'File',
);
}

Avatar
Ben_W

Community Member, 80 Posts

2 June 2009 at 10:57am

change the following line:

<a href=\"$Attachment.URL\" title=\"$Attachment.Title\">$Attachment.Title</a>

To

<a href=\"$Attachment.Filename\" title=\"$Attachment.Title\">$Attachment.Title</a>

Avatar
Ben_W

Community Member, 80 Posts

2 June 2009 at 12:35pm

By the way, I forgot to ask, where do you put the 'function getReportField()', is it in ContactUsPage.php ( class ContactUsPage extends Page )? assuming ContactUsPage.php is the page which contains the form. Thanks!