Jump to:

5539 Posts in 1738 Topics by 1224 members

Customising the CMS

SilverStripe Forums » Customising the CMS » using urlParams to view profile

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

Page: 1
Go to End
Author Topic: 2244 Views
  • McBainVT
    Avatar
    Community Member
    5 Posts

    using urlParams to view profile Link to this post

    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.

  • zenmonkey
    Avatar
    Community Member
    528 Posts

    Re: using urlParams to view profile Link to this post

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

  • McBainVT
    Avatar
    Community Member
    5 Posts

    Re: using urlParams to view profile Link to this post

    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.

    2244 Views
Page: 1
Go to Top

Want to know more about the company that brought you SilverStripe? Then check out SilverStripe.com

Comments on this website? Please give feedback.