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 /

Getting Data for CMS PopUp from more than one has_one relation


Reply


2 Posts   353 Views

Avatar
Vix

Community Member, 25 Posts

18 June 2013 at 8:21pm

I am new to this so forgive me if the subject is not too clear.

I have a site I am building in 2.10 and I am not sure how to go about getting the following information in the CMS. Basically I have a 'SlipBookingPage' that has many 'SlipBookings'

...
public static $has_many = array(
      'SlipBookings' => 'SlipBooking'
   );
...

A SlipBooking has one Boat
SlipBooking code:

...
public static $db = array (
      'DateIn' => 'Date',
      'DateOut' => 'Date',
      'CradleNumber' => 'Varchar(100)',
      'Confirmed' => 'Boolean'      
   );
         
   public static $has_one = array(
      'Boat' => 'Boat',
      'SlipBookingPage' => 'Page'
         
   );
...

And a boat has one member

Boat code

class Boat extends DataObject {
   public static $db = array (
         'BoatName' => 'Varchar(100)',
         'BoatRegistration' => 'Varchar(20)',
         'SYCPenned' => 'Int',
         'BoatLength' => 'Varchar(100)',
         'BoatBeam' => 'Varchar(100)',
         'BoatWeight' => 'Varchar(100)',
         'KeelType' => 'Enum("Kiel, No Keel, Yacht Keel")',
         'ShaftType' => 'Enum("Shaft, Twin Shaft")',
         'VDrive' => 'Boolean',
         'Propulsion' => 'Enum("Inboard-Outboard(Leg), Twin Inboard-Outboard(Legs),Outboard, Twin Outboard")',
         'CraneAccess' => 'Boolean'
         );
         
   public static $has_one = array(
      'Member' => 'Member'
         
   );
   
   //Fields to show in the DOM table
static $summary_fields = array(
'BoatName' => 'BoatName',
'BoatRegistration' => 'BoatRegistration'
);
      
   
}

The problem I am having is getting information from the member and the boat to display in the CMS in the Popup for the HasManyComplexTableField. In the summary information I have gotten the members name and boat rego, but can't seem to get more information (eg all the boat details) in the pop up (so in the code below the mobile phone and boat length do not display in the pop up)

HasManyComplexTableField code:

...
$tablefield = new HasManyComplexTableField(
$this,
'SlipBookings',
'SlipBooking',
array(
            'Member.FirstName' => 'First Name',
            'Member.Surname' => 'Surname',
            'Boat.BoatRegistration' => 'Boat Rego',
            'DateIn' => 'Date In',
            'DateOut' => 'Date Out',
            'CradleNumber' => 'Cradle Number',
'Confirmed' => 'Confirmed'
),
new FieldSet(
          new TextField( 'CradleNumber', 'Cradle Number'),
          $dateField,
          $dateField2,
          new CheckboxField( 'Confirmed', 'Confirmed' ),
          new TextField( 'Member.MobilePhone', 'Mobile'),
          new TextField( 'BoatLength', 'Length'))
          );
....

If anyone can point me in the right direction of how to achieve this, that would be awesome!

Thanks

Avatar
Vix

Community Member, 25 Posts

25 June 2013 at 5:20pm

Figured I might as well post my solution in case someone else needs it. (Or sometime in the future I need it and stumble across this again! :) )

In the SlipBooking DataObject I added the following code:

function getCMSFields_forPopup(){
      $fields = new FieldSet();
      $boat_id = $this->BoatID;
      $member_id = $this->MemberID;      
      if($member_id){
         $member = DataObject::get_by_id('Member', $member_id);
         $membertable = '<table width="100%" class="boatDetails">
                  <tr><td width="30%" align="left">Member Name:</td><td align="left">'.$member->FirstName.' '.$member->Surname.'</td></tr>
                  <tr><td width="30%" align="left">Email:</td><td align="left">'.$member->Email.'</td></tr>
                  <tr><td width="30%" align="left">Mobile: </td><td align="left">'.$member->MobilePhone.'</td></tr>
                  <tr><td width="30%" align="left">Home Phone: </td><td align="left">'.$member->HomePhone.'</td></tr>
                  <tr><td width="30%" align="left">Work Phone: </td><td align="left">'.$member->WorkPhone.'</td></tr>
                  <tr><td width="30%" align="left" colspan="2">For full contact details view the member under the security tab</td></tr>                  
                </table>';
         $fields->push(new LiteralField('','<h2>Member Details</h2>'));
         $fields->push(new LiteralField('',$membertable));
         
      }   
      if($boat_id){
         $boat = DataObject::get_by_id('Boat', $boat_id);
         $penned = $boat->SYCPenned;
         if($penned == 1){
            $penned = 'Yes';
         } else {
            $penned = 'No';
         }
         $vdrive = $boat->VDrive;
         if($vdrive == 1){
            $vdrive = 'Yes';
         } else {
            $vdrive = 'No';
         }
         $crane = $boat->CraneAccess;
         if($crane == 1){
            $crane = 'Yes';
         } else {
            $crane = 'No';
         }
         $table = '<table width="100%" class="boatDetails">
                  <tr><td width="30%" align="left">Boat Name:</td><td align="left">'.$boat->BoatName.'</td></tr>
                  <tr><td width="30%" align="left">Boat Registration:</td><td align="left">'.$boat->BoatRegistration.'</td></tr>
                  <tr><td width="30%" align="left">Penned at SYC? </td><td align="left">'.$penned.'</td></tr>
                  <tr><td width="30%" align="left">Boat Length </td><td align="left">'.$boat->BoatLength.'</td></tr>
                  <tr><td width="30%" align="left">Boat Beam </td><td align="left">'.$boat->BoatBeam.'</td></tr>
                  <tr><td width="30%" align="left">Boat Weight </td><td align="left">'.$boat->BoatWeight.'</td></tr>
                  <tr><td width="30%" align="left">Keel Type </td><td align="left">'.$boat->KeelType.'</td></tr>
                  <tr><td width="30%" align="left">Shaft Type </td><td align="left">'.$boat->ShaftType.'</td></tr>
                  <tr><td width="30%" align="left">V Drive? </td><td align="left">'.$vdrive.'</td></tr>
                  <tr><td width="30%" align="left">Propulsion </td><td align="left">'.$boat->Propulsion.'</td></tr>
                  <tr><td width="30%" align="left">Crane Required? </td><td align="left">'.$crane.'</td></tr>
                </table>';
         
         $fields->push(new LiteralField('','<h2>Boat Details</h2>'));         
         $fields->push(new LiteralField('', $table));
         if($boatdo = DataObject::get('Boat', '', 'BoatRegistration, BoatName')){
            $boatdo = $boatdo->toDropdownMap('ID', 'BoatRegistration', '(Select one)', true);            
         }
         $fields->push(new DropDownField('BoatID', 'Change Boat', $boatdo));
      }
      
      $fields->push(new LiteralField('','<h2>Booking Request</h2>'));
      $fields->push(new TextField('DateIn', 'Date In'));
      $fields->push(new TextField('DateOut', 'Date Out'));
      $fields->push(new TextField('CradleNumber', 'Cradle'));
      $fields->push(new CheckboxField('Confirmed', 'Confirmed?'));
      return $fields;
   }

And then called the getCMSFields_forPopup function in the HasManyComplexTableField of the SlipBookingPage.

I am sure there is a cleaner way of doing this, but this worked for me. Any pointers in how to make it better would be great.

For me this is an acceptable solution as I did not want the member/boat being editable from within this popup, so not sure how it might work to actually make these editable if you wanted them to be.