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're retiring the forums!

The SilverStripe forums have passed their heyday. They'll stick around, but will be read only. We'd encourage you to get involved in the community via the following channels instead:

Archive /

Our old forums are still available as a read-only archive.

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

extending PM


Go to End


2 Posts   1104 Views

Avatar
eoor

Community Member, 5 Posts

2 September 2008 at 10:51pm

Hi, i'm trying to develop provate message module. This is my class that extends Page_Controller

<?php

class PrivateMessage extends Page_Controller {
	public $URLSegment = "PrivateMessage";
	
	function __construct() {
		return parent::__construct(null);
	}
	
	function init() {
		if(!Member::currentUserID()) Security::permissionFailure();
		parent::init();
	}
	
	
	function InboxItems() {
		$myID=Member::currentUserID();
		return DataObject::get("PrivateMessage", "ToID = $myID  AND (Status = 'Unread' OR Status ='Read')", "Status, Created DESC");
	}
	
	function SentItems() {
		$myID=Member::currentUserID();
		return DataObject::get("PrivateMessage", "FromID = $myID AND Status = 'Sent'", "Created DESC");
	}
	
	function DraftItems() {
		$myID=Member::currentUserID();
		return DataObject::get("PrivateMessage", "ToID = $myID AND Status = 'Draft'", "Created DESC");
	}
	
	function GetNewMessages() {
		$myID=Member::currentUserID();
		return DataObject::get("PrivateMessage", "ToID =  $myID AND Status = 'Unread'",  "Created DESC");
	}
	
	
	function PostMessageForm() {
		$members = DataObject::get("Member", "", "Firstname, Surname","INNER JOIN Group_Members ON Group_Members.GroupID IN (1,2,3) AND Group_Members.MemberID = Member.ID");
		$allMembers = array();
		foreach($members as $member) {
			$allMembers[$member->ID] = "$member->Nickname"; 
		}
		$me = Member::currentUser();
		return new Form($this, "PostMessageForm", new FieldSet(
			new ReadonlyField("From", "Od", "$me->NickName"),
			new DropdownField("ToID", "Do", $allMembers),
			new TextField("Subject", "Temat"),
			new TextareaField("Body", "Tresc")

			), new FieldSet(
			new FormAction("doPostMessage", "Send")
		)
		,new RequiredFields("ToID", "Subject", "Body")
		);
	}
	
	function ReplayMessageForm() {
		$message = DataObject::get_by_id("PrivateMessage", $this->urlParams['ID']);
		
		$me = Member::currentUser();
		return new Form($this, "ReplayMessageForm", new FieldSet(
			new ReadonlyField("From", "Od", "$me->NickName"),
			new ReadonlyField("ToID", "Do", $message->FromID),
			new TextField("Subject", "Temat", "Re:".$message->Subject),
			new TextareaField("Body", "Tresc")

			), new FieldSet(
			new FormAction("doPostMessage", "Send")
		)
		,new RequiredFields("Subject", "Body")
		);
	}
	
	
	
	function doPostMessage($data, $form) {
		$message = new PrivateMessage();
		$message2 = new PrivateMessage();
		$form->saveInto($message);
		$message->FromID = Member::currentUserID();
		
		$touser = DataObject::get_by_id("Member", $message->ToID);
		$message->To = $touser->Nickname;
		$message->write();
		$message2 = $message->duplicate(false);
		$message2->MarkSent();
		$message2->write();
		Director::redirect($this->Link().'thanks');
	}
	
	function thanks() {
		return array(
				'Content' => "widomosc zostala wyslana"		
			);
	}
	
	function message() {
		$message = DataObject::get_by_id("PrivateMessage", $this->urlParams['ID']);
		if(($message->ToID == Member::currentUserID()) or ($message->FromID ==Member::currentUserID() and $message->Status == 'Sent')) {
			if($message->Status=="Unread")
			$message->MarkRead();
			return array(
				'CurrentMessage' => $message			
			);
		} else {
			return array();
		}
	}
	
	function draft() {
		$message = DataObject::get_by_id("PrivateMessage", $this->urlParams['ID']);
		if($message->ToID == Member::currentUserID() or ($message->FromID ==Member::currentUserID() and $message->Status == 'Sent') ) {
			$message->MarkDraft();
			return array(
				'Content' => "Wiadomosc zostala przeniesiona do kosza");
		}
	}
	
	function delete() {
		$message = DataObject::get_by_id("PrivateMessage", $this->urlParams['ID']);
		if(($message->ToID == Member::currentUserID() and  $message->Status == 'Draft') or ($message->FromID ==Member::currentUserID() and $message->Status == 'Sent')) {
			DataObject::delete_by_id("PrivateMessage", $this->urlParams['ID']);
		return array(
				'Content' => "Wiadomosc zostala usunieta");
		}
			
		
	}

	function ReplayAble()
	{
		$message = DataObject::get_by_id("PrivateMessage", $this->urlParams['ID']);
		if($message->Status == 'Read')
		return true;
		
	}

	
	


}

?>

and when i try to use any of this method in my .ss file I'm getting error:

FATAL ERROR: Object::__call() Method 'instance_get' not found in class 'PrivateMessage'
At line 199 in /var/www/pm/silverstripe/sapphire/core/Object.php

What's wrong?

Avatar
ajshort

Community Member, 244 Posts

2 September 2008 at 11:57pm

Hi eoor,

The problem is that your trying to use a controller as a model - as you can see, your extending Page_Controller rather than using a model class. What you should do, is rename your PrivateMessage to PrivateMessage_Controller. You could then create a model class for the actual PrivateMessage - something like:

class PrivateMessage extends DataObject {

    public static $db = array(
       'Message' => 'Text'
    );

    public static $has_one = array(
        'Sender' => 'Member',
        'Receiver' => 'Member'
    );

    // etc...

}