Get page owner ID and Name

Permalink
I cannot find anywhere in the Concrete5 code where one may retrieve the ID and Name of a page's owner.

The files "/concrete/models/page.php", "/concrete/models/collection.php" and "/concrete/models/collection_version.php" contain the functions "getCollectionUserID()", "getVersionAuthorUserID()", "getVersionAuthorUserName()" and a few more user related methods.

Now, think of a Blog - or even better, a News Item page. A News Item can be posted by any user, and this very user should remain the owner (and essentially author). However, if another site administrator discovers e.g. spelling errors in the text on the page - the News Item - this secondary site administrator may check out the page, edit the contents and publish it again. If using the functions mentioned above, this secondary site administrator will now stand as the News Item's author, which is incorrect. This is of course due to the functions being tied to the Collection and not the Page itself.

Therefore I'm interested in retrieving the page's owner, which is set (changed) in the Properties of the page. This name should remain static, unless it is changed intentionally through the Properties.

I have not been able to find any functions for this purpose - does anyone know if they exist?

For now I copied the file "/concrete/models/page.php" to "/models/page.php", and added the two functions below:

/** 
   * Gets the user id of the page owner. 
   * $return int $uID
   */
   public function getPageOwnerID() {
      $db = Loader::db();
      $query = "select uID from Pages where cID = ?";
      $vals = array($this->cID);
      $uID = $db->getOne($query, $vals);
      return $uID;
   }
   /** 
   * Gets the user name of the page owner. 
   * $return string $name
   */


Thanks in advance!

/Kafoso

View Replies: View Best Answer
Mnkras replied on at Permalink Reply
Mnkras
You should be able to do something like:

$uID = $c->getVersionAuthorUserID();
$ui = UserInfo::getByID($uID);
if(is_object($ui)) {
    echo $ui->getUserName();
} else {
    echo t('Unknown');
}
Kafoso replied on at Permalink Reply
As implied by the function's name it returns the uID of the last user to edit the page in question (the current version).

I'm looking for the page's "owner", not the "author". Thanks, though :)
Shotster replied on at Permalink Best Answer Reply
Shotster
> I'm looking for the page's "owner"...

It appears the getCollectionUserID() method of the Page class might be what you're after.

-Steve
Kafoso replied on at Permalink Reply
That's exactly it! Thanks, Steve.

Here's the code, for others to enjoy:

$ownerID = $cobj->getCollectionUserID();
$ui = UserInfo::getByID($ownerID);
$ownerName = $ui->getUserName();
foster replied on at Permalink Reply
foster
Thanks for the snippet - had found a few older ones that were only returning the author of the most recent version, but this give the page creator. Perfect!
becki91 replied on at Permalink Reply
Hi Kafoso,

your solution seems quite nice. One thing I can't figure out. What does your final function in "/models/page.php" look like? Could you maybe post the whole funtion you added there for me to understand the implementation? Great thanks in advance.
Kafoso replied on at Permalink Reply
Hello @becki91

The two functions in my first post aren't needed. I merely created those because I did not know that getVersionAuthorUserID() would return the UserID of the page's author/owner. In my mind the "Version" part of that function indicates that the UserID being return is that of the user who edited a specific version of the page or the last version of the page. That is why I got confused.

You do not have to change anything in the model class as the method mentioned above is default in Concrete5 - simply use the code from my previous reply in the respective file(s) in your theme.

Note: Below the code block in the first post you'll see the text "Viewing 15 lines of 23 lines. View entire code block." Clicking the link "View entire code block" will display all the code.
becki91 replied on at Permalink Reply
Hi @Kafoso,

thanks for your help. I finally found the time to update my site this weekend. It´s working out great. Thanks for your help.

Regards
Cahueya replied on at Permalink Reply
This does not seem to work anymore in 5.6.3.1 - any Idea why? Would love to use it :)
drbiskit replied on at Permalink Reply
drbiskit
Try this:
$ownerID = $c->getCollectionUserID();
$ui = UserInfo::getByID($ownerID);
$ownerName = $ui->getUserName();
echo $ownerName;

or, to extend slightly further - if you have an attribute you want to use (if available - eg 'name'), you could do this:
$ownerID = $c->getCollectionUserID();
$ui = UserInfo::getByID($ownerID);
$ownerName = $ui->getUserName();
$name = $ui->getAttribute('name');
if ($name):
   echo $name;
else :
   echo $ownerName;
endif;

That should work.
drallewellyn replied on at Permalink Reply
drallewellyn
Hi

I'm wondering if anyone has solved this particular problem for Concrete 5.7?

We have a multi-author site with a group of editors who are responsible for editing content and posting.

I need the page owner name to be able to be changed in the Page Attributes by the editors and then show up correctly in the blog_entry.php file.

Thanks
campbell replied on at Permalink Reply
campbell
I've been trying to do something similar, and I was able to return the page owner and email address using the following (I'm just a lowly tech writer, so this might be sloppy):

<?php
   $page = Page::getCurrentPage();
   $ownerID = UserInfo::getByID($page->getCollectionUserID());
   $name = $ownerID->getUserName(); 
   $email = $ownerID->getUserEmail();
echo '<p>If you have questions about this page, please contact the page owner $name at $email.</p>';
?>


Maybe that will help you out a bit?