Jump to content

Quick guide to the comments system


ryan

Recommended Posts

Hello @ all,

I have made my own custom comment form with Twitter Bootstrap markup in the template where the comment form is located. Unfortunately I am not able to store the entered data in the db. I have searched the forum and found some hints but it doesnt work.

Here is my code that should be executed if there are no errors:

   $c = new CommentRating();
    $c->text       = $commentsvalue;
    $c->cite       = $namevalue;
    $c->created    = time();
    $c->email      = $emailvalue;
    $c->title = $ratingtitle;
    $c->rating = $ratingvalue;
    $c->user_agent = $_SERVER['HTTP_USER_AGENT']; 
    $c->ip         = $_SERVER['REMOTE_ADDR'];
    $c->created_users_id = $user->id;
    $c->sort       = count($this->comments)+1;
    //$c->parent_id = (int) $data->parent_id;   
    $c->status     = 1; // just for testing
    $c->commentsrating->add($c);
    $c->save();

I use the modified version of the comment field with star rating (https://processwire.com/talk/topic/10829-comments-fieldtype-with-star-rating/

Therefore I call a new instance with the name "CommentRating" and not "Comment".

After that i add all fields that should be stored in the db.

Then I try to add it to my field "commentsrating" which is the comment field in my template. Here I always get the error for this function:

Fatal error: Call to a member function add() on null in .... on line 307 

Has anyone an idea how to store the values via API from the template correctly?

Best regards

EDIT: Nevermind, I did it this way https://processwire.com/talk/topic/696-quick-guide-to-the-comments-system/?p=41520

Link to comment
Share on other sites

Question: Has anyone tried to add a user image to the comments?

In my case people can upload a user image to their profile if they want. Now I also offer the possibility to add a comment to products (if you are a registered user or not).

My goal: If the user is logged in and he has a profile image added to his profile, this image should be shown next to his comment. If the user is not logged in or has no profile image than a avatar should be displayed instead.

I have checked the database columns for the comments and there is no column which stores f.e. the user id if a user was logged in.

My idea was to add a new column for user identification that stores f.e. the user id so the user is identified as a registered user and the profile image could be added via the id (because the user is identified).

Has anyone tried to achieve the same or has an better idea. I use a custom output for my comments so I can add everything I want.

SOLVED:

The user id will be stored. In my case an unregistered user has always the id "40" and a registered user has its own id. The value can be displayed in the comments by calling it inside the foreach loop with this name: created_users_id.

Example:

foreach ($comments as $comment) {
    $userid = $comment->created_users_id;
}

With the id it is possible to find and load the image of the user.

Best regards Jürgen

Link to comment
Share on other sites

Question: Has anyone tried to add a user image to the comments?

I have a pending PR that adds user images along with some other enhancements for logged in users:

https://github.com/ryancramerdesign/ProcessWire/pull/954

Maybe if you test out those changes and you like them you could add a comment to that PR to encourage Ryan to incorporate them :)

Link to comment
Share on other sites

Hello @ all,

I have tried to approve a comment via clicking the link in the notification email, but nothing happens. The comment is still in pending status.

Here is the approval link:

http://www.my-exampledomain.at/produkte/testprodukt/?field=commentsrating&page_id=2407&code=7yOVt2mdJGTcVnJoUZwdDe9piBaNpNWi_JbqcW7NLdI4e3PxOG_D71EFFlN3hm7RDXF9mMg6YbDcYCQJWvftaoSeGsnxDCt9vdqcfzoNAPDoR4DQHexVXQZhaEYcCgkr&comment_success=approve

- commentsfield name is "commentsrating"

- page id is "2407"

- the code is the same as in the database

- the URL to the page is correct (I only changed the true domain name to "my-exampledomain" for this posting)

So everything seems fine.

The only thing that comes to my mind is that I use the extended version with star rating https://processwire.com/talk/topic/10829-comments-fieldtype-with-star-rating/

- so the approval link could be not correct. I have tried "&commentrating_success=approve" and "&commentRating_success=approve" at the end of the link instead of the original "&comment_success=approve" but nothing happens.

What could be the cause?

Best regards Jürgen

Link to comment
Share on other sites

  • 1 month later...

I'm just reporting this here in case anyone else has same problem, as I've spent the last few hours trying to get comment reply notifications to work. Finally discovered that this line in the comments.js file causes a problem. It was nullifying the values in the reply form from a cookie of previous values. So instead of setting Off, Replies and All radios to 0 2 and 4 values. It was setting them to all to value = "" causing the replies notification to not work, so anyone with a set cookie couldn't use notifications. I think it happens when you had comment replies turned off in the field settings and make a comment, causing the browser to have a cookie, but then turning notifications on. Then and the radios get nullified values from the previously set cookie. Commenting out this line caused reply notifications to start working for me.

$form.find(".CommentFormNotify :input").val(values[3]);

Link to comment
Share on other sites

This seems to be fixed on the dev version, as the line does hold other code. Could you confirm that? Otherwise it would be nice if you would report that on github, so Ryan can fix any remaining issues: https://github.com/ryancramerdesign/ProcessWire/blob/dev/wire/modules/Fieldtype/FieldtypeComments/comments.js#L78 

Link to comment
Share on other sites

Hello @ all

Strange behavior:

Upgrading to dev version 21 (comments with star rating), doesnt grab configuration settings - star rating field and URL field will not be shown if set to yes.

post-2257-0-49210400-1445937454_thumb.pn

This ist the frontend with renderAll():

post-2257-0-60677100-1445937536_thumb.pn

As you can see no star rating or URL field will be shown.

Does anyone have the same problem?

Link to comment
Share on other sites

  • 1 month later...

Question: How can I approve comments via email if the comments list is custom?

Hello @ all,

I use a custom code to show the comments in my own markup as described here (documentation of the comments).

All works fine, but if a comment is approved via an email it doesnt change the status to approved.

In the commentlist.php the following lines of code could be relevant.

public function renderCheckActions() {

		$action = $this->wire('input')->get('comment_success');
		if(empty($action) || $action === "1") return '';

		if($action === '2' || $action === '3') {
			$message = $this->wire('session')->get('CommentApprovalMessage');
			if($message) {
				$this->wire('session')->remove('CommentApprovalMessage');
				$class = $action === '2' ? 'success' : 'error';
				$commentID = (int) $this->wire('input')->get('comment_id');
				$message = $this->wire('sanitizer')->entities($message);
				if($commentID) $message = str_replace($commentID, "<a href='#Comment$commentID'>$commentID</a>", $message);
				return "<p id='CommentApprovalMessage' class='$class'><strong>$message</strong></p>";
			}
		}

Does anyone have tried to make a custom comment list which works with email approval and how?

Best regards

Link to comment
Share on other sites

  • 2 weeks later...

New problem: comment form stops sending if email address for approval is in this syntax "id:fieldname".

If I enter the email address for comment approval like 1006:mail ("1006" is the id of the page which contains the email field and "mail" is the name of the email field) no email will be sent.

If I enter the email address as plain text like "myemail@abc.com" the email will be sent correctly.

I have updated to the lates dev 2.7.3. The syntax above has worked in the past.

Best regards

EDIT: I have created a new comment field and now it works again :)

Link to comment
Share on other sites

  • 5 weeks later...

In PW version 3.05 the following code doesnt work anymore to make a custom comment form.

require("./includes/Customcommentform.php"); 
$form = new Customcommentform($page, $page->comments); 
echo $form->render();

Is there another way to make this working again? Maybe it has something to do with the namespace.

Best regards

Link to comment
Share on other sites

In PW version 3.05 the following code doesnt work anymore to make a custom comment form.

require("./includes/Customcommentform.php"); 
$form = new Customcommentform($page, $page->comments); 
echo $form->render();

Does twiddling with the template's "Use compiled file" option help?

  • Like 1
Link to comment
Share on other sites

Does twiddling with the template's "Use compiled file" option help?

No this doesn´t work - independed of which setting is choosen. No usage of compiled file leads to error 500.

On the frontend the following error message will be displayed:

Error: Class 'Customcommentform' not found (line 263 of /home/.sites/24/......./products.php)
Link to comment
Share on other sites

I found the solution:

in the template file use the following code:

include("./inc/CustomCommentForm.php");
$form = new CommentForm($page, $page->comments);//HERE YOU HAVE TO USE THE DEFAULT CLASS COMMENTFORM INSTEAD OF CUSTOMCOMMENTFORM!!!!
echo $form->render();

Now it works! :)

Sorry but this doesnt work - it loads the default comment form :(

Link to comment
Share on other sites

This is how I got it working:

Use this code in the template

include("./includes/Customcommentform.php"); 
$form = new Customcommentform($page, $page->comments); 
echo $form->render();

Remove "namespace ProcessWire;" at the top of the file

Use the template compiler.

The reason was that the commentform.php was the only include file in the template which has the namespace commmand at the top. All the others dont have it at the top. By removing the namespace attribute the compiler works. Otherwise you will get an error message.

Hope this helps other who are struggeling with the same problem.

@BitPoet: You were right with your idea, but changing the settings in the template file doesnt work in this case, because you can change it only for all files in the template. In this special case it was necessary to adapt the commentform.php alone.

Link to comment
Share on other sites

hi there,

I'm testing the comment system and noticed a strange behaviour about the chronological order.

Let's say users posted three comments on the public page:

  1. comment A
  2. comment B
  3. comment C

now from my backoffice I manually delete comment B.

If a user posts a new comment (comment D) it doesn't follow the chronological order (going to the last position), but instead it goes in the second position:

  1. comment A
  2. comment D
  3. comment C

Also, is there a simple way to check if user submits a valid email address?

UPDATE: I noticed this check is already set!

Thanks!

Link to comment
Share on other sites

Has anyone tried to output only comments with a specific rating value (fe all comments which are rated with 5 stars)?

This is what I have tried, but I cannot get it to work.

$selector = "page=$page, sort=-created, start=$start,stars=5";//show all comments which were rated with 5 stars
$comments = FieldtypeComments::findComments("comments", $selector);//comments is the name of the field
foreach($comments as $c){
  echo $c->stars."<br />";
} 

All comments will be displayed instead of  only comments with 5 stars. Has anyone an idea how to filter comments correctly in this way?

Best regards Jürgen

Link to comment
Share on other sites

Can't you just do this?

$selector = ("page=$page, sort=-created, start=$start"); // Comment selector
$comments = FieldtypeComments::findComments("comments", $selector); //comments is the name of the field

foreach($comments as $c){
  // Only output those with 5 stars
  if($c->stars == 5) {
    echo "Stars = " . $c->stars .
         "<p> By: " . $c->createdUser->name . "</p>".
         "<p> Comment: " . $c->text . "</p>";
}
  • Like 1
Link to comment
Share on other sites

Hello GuroMeditation,

your idea is really good, but in my case I have a filter form where the user can choose if all comments should be displayed or only comments rated with fe 5 stars. In this case it would be better to use a selector to find only comment pages which match the query.

post-2257-0-74268900-1454565645_thumb.pn

Best regards

Link to comment
Share on other sites

It's probably just an oversight, so you should open an issue for it.

In the meantime, you can fix it per hand by changing this line in FieldtypeComments.module's find method (around line 1000):

if(in_array($f, array('id', 'status', 'created', 'pages_id', 'parent_id', 'created_users_id', 'upvotes', 'downvotes'))) {

and add the stars field:

if(in_array($f, array('id', 'status', 'created', 'pages_id', 'parent_id', 'created_users_id', 'upvotes', 'downvotes', 'stars'))) {
  • Like 1
Link to comment
Share on other sites

Hi Gents

Along similar lines to Marco's observation above, has anyone gotten threaded comments working in a fresh install of 2.7.3? 

I have tried entering 2 and 4 for the Reply depth value yet it still behaves as if it has been set to 0!

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
  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...