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.

DataObjectManager Module /

Discuss the DataObjectManager module, and the related ImageGallery module.

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

[solved] Adding Actions to To DataObjectManager


Go to End


6 Posts   1920 Views

Avatar
zenmonkey

Community Member, 545 Posts

27 August 2009 at 9:23am

Edited: 02/09/2009 6:43am

I posted this question in other Customizing CMS forum, with no response since I'm actually using the DataObjectManager I thought I'd post here.

I'm using the DataObjectManager to Create a User Application Form. I want to Add a Buttons to the Pop-Up that Either Delete the Application or Approve it, thus copying the Data to a member group.

Here is my UserApplication DataObject

class UserApplication extends DataObject {
	static $db = array(
			'FirstName' => 'Text',
			'LastName' => 'Text',
			'Email' => 'Text',
			'CompanyName' => 'Text',
			'CompanyType' => "Text",
			'CompanyURL' => 'Text',
			'CompanyPhone' => 'Text',
			'Address' => 'Text',
			'Address2' => 'Text',
			'City' => 'Text',
			'Country' => 'Text',
			'TaxIDNumber' => 'Text',
			'Distributor' => 'Text',
			'SalesRep' => 'Text',
			'Description' => 'Text',
			'ApprovedMember' => 'Boolean',
			'Password' => 'Text'
	);
	
	static $has_one = array(
		'RegistrationPage' => 'RegistrationPage',		   
	);
	
	public function getCMSFields_forPopup()
	{
		$companyTypeList = array(
			'Retail' => 'Retail',
			'Online' => 'Online',
			'Distributor' => 'Distributor',
			'Press' => 'Press'
		);
		
		return new FieldSet (
			new CheckBoxField('ApprovedMember', 'Approved Member'),
			new TextField('FirstName', 'First Name'),
			new TextField('LastName','Last Name'),
			new EmailField('Email'),
			new TextField('CompanyName','Company Name'),
			new DropdownField('CompanyType','Company Type',$companyTypeList),
			new TextField('CompanyURL','Company Website'),
			new TextField('CompanyPhone','CompanyPhone'),
			new TextField('Address','Address'),
			new TextField('Address2','Address Line 2'),
			new TextField('City','City'),
			new TextField('Country','Country'),
			new TextField('TaxIDNumber','TaxIDNumber'),
			new TextField('Distributor','Distributor'),
			new TextField('SalesRep','Sales Rep'),
			new TextareaField('Description','Description'),
			new ConfirmedPasswordField('Password')
		);
		
	}

}

And This is my Registration Page, I still need to create a a front end Form Element for End Users to Fill in.

class RegistrationPage extends Page 
{
	static $has_many = array (
		'UserApplications' => 'UserApplication'
	);
	
	public function getCMSFields()
	{
		$f = parent::getCMSFields();
		$applicationManager = new DataObjectManager(
			$this,
			'UserApplications',
			'UserApplication',
			array('FirstName' => 'FirstName','LastName' => 'LastName','Email' => 'Email','CompanyName' => 'CompanyName','CompanyType' => "CompanyType",'CompanyURL' => 'CompanyURL','CompanyPhone' => 'CompanyPhone','Address' => 'Address','Address2' => 'Address2','City' => 'City','Country' => 'Country','TaxIDNumber' => 'TaxIDNumber','Distributor' => 'Distributor','SalesRep' => 'SalesRep','Description' => 'Description','ApprovedMember' => 'ApprovedMember','Password' => 'Password'),
			'getCMSFields_forPopup'
		);
		
		$f->addFieldToTab("Root.Content.Applications", $applicationManager);
		
		$f->removeFieldFromTab('Root.Content.Main', 'Content');
		$f->removeFieldFromTab('Root.Content.Main', 'LifestyleText');
		
		return $f;
	}
	
}

class RegistrationPage_Controller extends Page_Controller
{
	
}

Would I use a standard form $actions in the DataObject FieldSet.

Incidentally, does the DataObjectManager not have a Delete like the FileDataObjectManager?

I really can't wait until the SilverStripe Book comes out LOL

Avatar
UncleCheese

Forum Moderator, 4102 Posts

27 August 2009 at 11:44am

The DOM should have delete buttons. ??? Not sure why you wouldn't see them.

Adding actions to the popup form is a tall order.

First, I would create a DataObjectManager subclass.

MyDataObjectManager.php

class MyDataObjectManager extends DataObjectManager
{
	public $popupClass = "MyDataObjectManager_Popup";
	public $itemClass = "MyDataObjectManager_Item";

}

Then, in the same file, you can create your popup and item subclasses.

class MyDataObjectManager_Popup extends DataObjectManager_Popup
{
function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) {
parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject);
$this->Actions()->push(
new FormAction('someaction','Some Label')
);
}

}

class MyDataObjectManager_Item extends DataObjectManager_Item
{
	function someaction($data, $form, $request) {
// do stuff
		Director::redirectBack();
	}

}

That's the basic idea.

Avatar
zenmonkey

Community Member, 545 Posts

28 August 2009 at 2:26am

Okay, would I then add new MyDataObjectManager_Popup to the UserApplication fieldset?

Avatar
UncleCheese

Forum Moderator, 4102 Posts

28 August 2009 at 5:57am

No, just use MyDataObjectMangaer to manage those objects instead of DataObjectManager.

Avatar
zenmonkey

Community Member, 545 Posts

28 August 2009 at 10:27am

When I added my action function to MyDataObjectManager_Item, I got:

[User Error] Uncaught Exception: Object->__call(): the method 'approveuser' does not exist on 'ApplicationDataObjectManager_Popup'

so I added the function to MyDataObjectManager_Popup

I'm still having problems with it but I think it has to go with the keys in the object and not the DataObjectManager so I've posted in another forum for help.

c<?php
class ApplicationDataObjectManager extends DataObjectManager 
{ 
   public $popupClass = "ApplicationDataObjectManager_Popup"; 
   public $itemClass = "ApplicationDataObjectManager_Item";

}


class ApplicationDataObjectManager_Popup extends DataObjectManager_Popup 
{ 
function __construct($controller, $name, $fields, $validator, $readonly, $dataObject) { 
parent::__construct($controller, $name, $fields, $validator, $readonly, $dataObject); 
$this->Actions()->push( 
new FormAction('ApproveUser','Approve User') 
);
}

   function ApproveUser($data, $form, $request) { 
// do stuff 
	 	$member = new Member();
		$form->saveInto($member);
		
		$groupID = 2;
		
		switch ($member->CompanyType) {
			case ("Retail"): $groupID = 3;
				break;
			case ("Distributor"): $groupID = 4;
				break;
			case ("Press"): $groupID = 5;
				break;
			case ("Online"): $groupID = 6;
				break;
		}
		
		/*$member->write();*/
	
		if($group = DataObject::get_one('Group', "ID = 2")) { 
			$member->Groups()->add($group);
			return print_r($member);	
			//Director::redirectBack(); 
		}
		else{
			return print_r($member->ID);	
		}
      
   }

}

class ApplicationDataObjectManager_Item extends DataObjectManager_Item 
{ 


}

Avatar
UncleCheese

Forum Moderator, 4102 Posts

28 August 2009 at 10:58am

It has to go in both places. Item class for edits and Popup class for adds.