Jump to content

[SOLVED] Comments only by Users


Vineet Sawant
 Share

Recommended Posts

Hello All,

This question is a part of my series of questions that I'm coming up with while building a drama ticket booking/selling website.

So for today my question is, I want that only registered users should be able to review(comment) any drama. Others who wish to comment/review the drama should first signup and then add the comment.

I've figured this is possible if I add a new permission such as add-review and link to a role (eg, in my case registered is a role).

But, how exactly will the system know that add-review refers to adding comments?

Which step am I missing here? Or is this not the way to do it?

Please, I need some help here. :)

Thank you all in advance.

Link to comment
Share on other sites

Not sure that you need to get that complex. I did the following. Not sure how you handle front end logins or redirects from the login page, but you get the idea.

    if($user->isLoggedin()){
        echo $page->comments->renderForm();
    }
    else{
        echo '<hr /><br /><h3>Post Comment</h3><p>To leave a comment, you must be <a href="/login/?pid='.$page->id.'">signed in</a></p>';
    }
  • Like 2
Link to comment
Share on other sites

I've figured this is possible if I add a new permission such as add-review and link to a role (eg, in my case registered is a role).

But, how exactly will the system know that add-review refers to adding comments?

Hi, I don't know if it is the right way to add a permission, or if it just would be enough to check a users role.

But I think the system only knows what you implements :)

If you use permissions, you want to check: $user->hasPermission($permission)

If you use roles, you want to check: $user->hasRole($role)

More variations can be found in CheatSheet under user, roles, permission. (But don't forget to click the advanced-button! in TopNav the right one!)

  • Like 1
Link to comment
Share on other sites

@Adrian

Cool, that's a very simple & straightforward solution. I think it get's the job done, I'll implement that. Thanks a lot.

Now I feel dumb, why I didn't think about that. :P

But now that we're anyways talking about adding permissions to roles, I'd like to know how it can be done. Let's keep hunting for it.

  • Like 1
Link to comment
Share on other sites

@horst, thanks for your reply. I always have Cheatsheet open, it's like an ultimate map of processwire's world.

 
But I think the system only knows what you implements :)
 
If you use permissions, you want to check: $user->hasPermission($permission)
If you use roles, you want to check: $user->hasRole($role)
 

This gives me a clue. May be I can add a new permission as "add-review" and link it to a role called "registered" and just check it with users.

That should work, isn't it? That's what you are suggesting right?

Will give it a try and let you know.

Thanks again :D

Link to comment
Share on other sites

Thank you guys, my problem is solved.

All I did was created a new permission called as "add-review" and added it to the user role called "registered". Using code given below, I checked if the current user has the permission to add review.

For future reference of anyone like me, following is the code I used, pretty simple: 

echo "<div id='reviews'><h3>Viewer Reviews</h3>";
		echo "<div class='CommentList'>";
		foreach($page->drama_reviews as $comment) {
		    if($comment->status < 1) continue; // skip unapproved or spam comments
		    $cite = htmlentities($comment->cite); // make sure output is entity encoded
		    $text = htmlentities($comment->text);
		    $date = date('m/d/y g:ia', $comment->created); // format the date
		    echo "<div class='CommentListItem'><p><strong>Posted by $cite on $date</strong><br />$text</p></div>";
		}
		echo "</div>";

		$perm_to = $permissions->get("name=add-reviews");
		if($user->hasPermission($perm_to)){
			echo $page->drama_reviews->renderForm();
		}else{
			echo "<h4 class='center-text'>You need to be logged in to write a review. Please <a href='{$config->urls->root}profile/login/'>Log In</a>.</h4>";
		}

		echo "</div><!--#reviews-->";

Thanks to horst & andrian for help.

Edited by vineonardo
  • Like 3
Link to comment
Share on other sites

This gives me a clue. May be I can add a new permission as "add-review" and link it to a role called "registered" and just check it with users.

That should work, isn't it? That's what you are suggesting right?

That should work, yes!

But that's not exactly what I am suggesting, no!

It depends on if you have different kind of registered users. If all registered users are allowed to add-review, you don't need extra permission, you only check if user has the role.

And if you have registered users that are allowed and some that are not allowed, you need other rules to check.

As an example: If you have registered users that have allready bought tickets and users that have not, and only the users that have bought tickets should be allowed to add-review, you want to check for: 

$user->hasRole($role) && count($pages->find("ticket_reference=$user")) > 0

I think you only would check for a permission if you have different roles, and some have the permission and some have not. So you have to check only one thing.

EDIT: sorry, last post has taken a bit time, (has to do a phone call), and now you are allready done. :)

Edited by horst
  • Like 2
Link to comment
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now
 Share

×
×
  • Create New...