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.

Customising the CMS /

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

File Upload Field in ManyManyComplexTableField popup


Go to End


21 Posts   5660 Views

Avatar
kevino

Community Member, 30 Posts

7 November 2009 at 7:34am

Edited: 07/11/2009 7:35am

I'm trying to add a group of text fields plus some file fields to a ManyManyComplexTableField.
From what I can fathom, I've done the logcal thing (to me), but it fails.
I can get the upload brows boxes to show, but it doesn't upload.

Can anyone see a blunder in this code?

-----
UnitList.php

<?php

class UnitList extends DataObject {

	static $db = array(
				'UnitNumber' => 'Text',
				'UnitType' => 'Text',
				'UnitArea' => 'Text',
				'UnitRent' => 'Text'
	);
	
	static $has_one = array(
				'UnitSpec' => 'File',
				'UnitLease' => 'File'
	);
	
	static $belongs_many_many = array(
		'AvailableUnitss' => 'AvailableUnits'
	);


	function getCMSFields_forPopup() {
		$fields = new FieldSet();
		$fields->push( new TextField( 'UnitNumber', 'UnitNumber' ) );
		$fields->push( new TextField( 'UnitType', 'Unit Type' ) );
		$fields->push( new TextField( 'UnitArea', 'Unit Area' ) );
		$fields->push( new TextField( 'UnitRent', 'Unit Rent' ) );
		$fields->push( new FileField( 'UnitSpec', 'Unit Specsheet' ) );
		$fields->push( new FileField( 'UnitLease', 'Unit Lease Document' ) );
		return $fields;
	}

}
?>

PropertyPage.php

<?php
/**
 * Defines the PropertyPage page type
 */
class PropertyPage extends Page {
   static $db = array(
   'AvailableSpace' => 'Text',
   'UnitSizeRange' => 'Text'
	);
   
   static $has_one = array(
   );

	static $many_many = array(
		'AvailableUnits' => 'UnitList'
	);
   
   function getCMSFields() {
   $fields = parent::getCMSFields();
 
   $fields->addFieldToTab('Root.Content.Main', new TextField('AvailableSpace'), 'Content');
   $fields->addFieldToTab('Root.Content.Main', new TextField('UnitSizeRange'), 'Content');
		
		$Unitstablefield = new ManyManyComplexTableField(
			$this,
			'AvailableUnits',
			'UnitList',
			array(
				'UnitNumber' => 'UnitNumber',
				'UnitType' => 'Unit Type',
				'UnitArea' => 'Unit Area',
				'UnitRent' => 'Unit Rent',
				'UnitSpec' => 'Unit Specsheet',
				'UnitLease' => 'Unit Lease Document'
			),
			'getCMSFields_forPopup'
		);
		$Unitstablefield->setAddTitle( 'A Unit' );
		$Unitstablefield->setPageSize(100);

		$fields->addFieldToTab( 'Root.Content.Units', $Unitstablefield );
    	
   return $fields;
	}
	
	 static $icon = "themes/tutorial/images/treeicons/news";
	 
	 static $defaults = array('ProvideComments' => false);
   
}
 
class PropertyPage_Controller extends Page_Controller {
 
}
 
?>

Best wishes,
K

Avatar
Carbon Crayon

Community Member, 598 Posts

9 November 2009 at 12:57am

Hi Kevino

Try using a FileIframeField, that has always worked for me. Alternatively use the Data Object Manager Module which it certainly works with and is a lot more feature rich.

Aram

Avatar
kevino

Community Member, 30 Posts

9 November 2009 at 12:32pm

Thanks for the reply.
I'm pretty sure I tried the FileIframeField, which didn't work either.
I assumed this is because it was trying to work from a pop-up?

Avatar
Carbon Crayon

Community Member, 598 Posts

9 November 2009 at 12:55pm

Hmm, must be a problem in your code, as FileIFrameField has always worked fine in a popup. I havn't used a complex table field in quite a while, I only ever use the DataObjectmanager thesedays but I can't think of any reason it would have stopped working.

I would give ti another go, and if it still doesnt work, post ur code and I'll take a look :)

Avatar
kevino

Community Member, 30 Posts

10 November 2009 at 12:03am

I think there must be a problem with my code to some extent. Or maybe I'm just going about it the complete wrond way.
Does the code look plausible?

From what I can gather, if I change the upload fields to text fields instead, I can get a list of text fields into my db..?

Avatar
Carbon Crayon

Community Member, 598 Posts

10 November 2009 at 1:28am

Ok, so here is some slightly adapted code for your dataobject (your belongs_many_many relationshop was not referencing the page and I changed the fields to IFrameFields):

<?php

class UnitList extends DataObject {

   static $db = array(
            'UnitNumber' => 'Text',
            'UnitType' => 'Text',
            'UnitArea' => 'Text',
            'UnitRent' => 'Text'
   );
   
   static $has_one = array(
            'UnitSpec' => 'File',
            'UnitLease' => 'File'
   );
   
   static $belongs_many_many = array(
      'PropertyPages' => 'PropertyPage'
   );

   function getCMSFields_forPopup() {
      return new FieldSet(
	  		new TextField( 'UnitNumber', 'UnitNumber' ),
			new TextField( 'UnitArea', 'Unit Area' ),
			new TextField( 'UnitRent', 'Unit Rent' ),
			new FileIFrameField( 'UnitSpec', 'Unit Specsheet' ),
			new FileIFrameField( 'UnitLease', 'Unit Lease Document' )
	  );

   }

}
?>

And here is the CTF code (you don't put the file field in the display array as it doesn't know what to do with it):
      $Unitstablefield = new ManyManyComplexTableField(
         $this,
         'AvailableUnits',
         'UnitList',
         array(
            'UnitNumber' => 'UnitNumber',
            'UnitType' => 'Unit Type',
            'UnitArea' => 'Unit Area',
            'UnitRent' => 'Unit Rent'
         ),
         'getCMSFields_forPopup'
      );
      $Unitstablefield->setAddTitle( 'A Unit' );
      $Unitstablefield->setPageSize(100);
      $fields->addFieldToTab( 'Root.Content.Units', $Unitstablefield ); 

As far as I can see this should work...

Avatar
kevino

Community Member, 30 Posts

11 November 2009 at 3:06am

I've tried to give the DOM a go, and couldn't get it working as I hoped.

The code that you've modified, aram, has worked perfectly. Thank you very much.

I'm actually struggling to get this to sit in my template however.

This is what my controlling code is at the moment


				<li class="available-units">
					<ul>
					<% control AvailableUnits %>
					<li>$UnitNumber $UnitType $UnitArea $UnitRent <a href="$UnitSpec.URL" title="Download Unit Specification Sheet PDF">Unit Specification</a> <a href="$UnitLease.URL" title="Download Unit Lease Document PDF">Unit Lease Document</a></li> 
					<% end_control %> 
					</ul>
				</li>

So I'm not sure if I'm just doing the control wrong, or have an error all on the db side.

Avatar
Carbon Crayon

Community Member, 598 Posts

11 November 2009 at 7:27am

Edited: 11/11/2009 7:27am

hmm, looks ok to me.....not sure what to suggest. Are you getting anything at all in the template?

Try adding a conditional in there to see if AvailableUnits() is actually returning anything:

               <% if AvailableUnits %>
               <p>FOO</p>
               <% end_if %>

Go to Top