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:

Customising the CMS /

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

using urlParams to view profile


Go to End


3 Posts   5857 Views

Avatar
McBainVT

Community Member, 5 Posts

25 September 2009 at 7:27am

I have extended the member class using dataobjectdecorator successfully, created custom login, registration and edit profile pages; however I am having a little trouble figuring out how I would create a view profile page.

I have created a viewprofile.php and viewprofile.ss page, setup similar to what exists in the forum module. Here is what I have in my viewprofile.php page.

<code>
class ViewProfile extends Page {

}

class ViewProfile_Controller extends Page_Controller {

function init() {
$member = $this->Member() ? $this->Member() : null;
$usernameText = ($member) ? ($member->Username . '\'s ') : '';

$this->Title = $usernameText . 'User Profile';

parent::init();
}

function Member() {
$member = null;

if(is_numeric($this->urlParams['ID'])) {
$member = DataObject::get_by_id('Member', $this->urlParams['ID']);
} else {
$member = Member::currentUser();
}

return $member;
}

} </code>

Here is what I have in my viewprofile.ss

<code>
<% control Member %>
<h1>$Username&#39;s <% _t('PROFILE','Profile') %></h1>
<p><b>Username: </b>$Username</p>
<% if FirstName %>
<p><b>First Name: </b>$FirstName</p>
<% end_if %>
<% if Surname %>
<p><b>Last Name: </b>$Surname</p>
<% end_if %>
<% if Website %>
<p><b>Website: </b>$Website</p>
<% end_if %>
<% end_control %></code>

At this point if I go to www.url.com/viewprofile/ when I am logged in it will show my profile. However I havent figured out how to display other users profiles by using urlParams. www.url.com/viewprofile/1 or any ID number doesnt return anything but a blank page. So it seems I am having a bit of trouble figuring out how to use urlParams to pass a member ID.

I have looked through the documentation but could use some more explanation of how urlParams work and how I can create a view profile page that will display any members profile by simply adding an ID number like this "www.url.com/viewprofile/1" where 1 is the member ID number.

Any help would be greatly appreciated.

Avatar
zenmonkey

Community Member, 545 Posts

26 September 2009 at 1:31pm

I've been trying to find documentation as well. the recipes:page_action_id is blank which in theory should explain it :)

Avatar
McBainVT

Community Member, 5 Posts

1 October 2009 at 5:46am

Managed to get it working fine.

Here is the code for viewprofile.php

<?php

class ViewProfile extends Page {
	

}
 
class ViewProfile_Controller extends Page_Controller {
	
	function init() {
		
		$params = Director::urlParams(); 
   		$id = (int)$params['ID']; 

   		$member = DataObject::get_by_id('Member',$id); 
		
		$usernameText = ($member) ? ($member->Username . '\'s ') : '';
		
		$this->Title = $usernameText . 'User Profile';
		
		parent::init();
 	}
 
	
	
	function Member($id = null) { 
       
   	$params = Director::urlParams(); 
   	$id = (int)$params['ID']; 

   	$object = DataObject::get_by_id('Member',$id); 

   		if($object) { 
      		return $this 
         	->customise(array('Member' => $object)) 
         	->renderWith(array('ViewProfile', 'Page')); 
   		} else { 
      		Director::redirect(Director::absoluteBaseURL() . '404'); 
   		} 
	}     

	
}
 

?>

Here is the code for memberdecorator.php

<?php 

class MemberDecorator extends DataObjectDecorator { 

	function augmentSQL(SQLQuery &$query) {} 

	function extraStatics() { 
    
		return array( 
			'db' => array( 
				"Website" => "Text", 
      			"Username" => "Text"       
			), 
			'has_one' => array( 
				"Avatar" => "Image" 
			), 
		); 
	}

	function getCMSFields() { 
		$fields = parent::getCMSFields();
		$this->extend('updateCMSFields', $fields); 
		return $fields; 
	} 

	function updateCMSFields(FieldSet &$fields) { 
		$fields->push(new TextField("Username", "Username"), 'Members'); 
		$fields->push(new TextField("Website", "Website URL"), 'Members'); 
		$fields->push(new ImageField("Avatar", "Profile Image"), 'Members'); 
	}
	
	function Link() { 
      	return Director::absoluteBaseURL() . SSViewer::topLevel()->URLSegment . "/member/" . $this->ID; 
   	} 
	
}

?>

The code for viewprofile.ss is still the same. Hope this helps.