0

*EDIT / FINISHED SOLUTION / WORKING CODE

So, this is what a friend of mine helped me come up with.

Here is the part I use in my K2 "items.php" file:

<div class="fb-comments" data-href="<?php echo JURI::current(); ?>" data-num-posts="8" notify="true" data-width="580"></div>

<input id="authname" style="display: none;" type="text" value="<?php echo $this->item->author->name; ?>" />
<input id="authmail" style="display: none;" type="text" value="<?php echo $this->item->author->email; ?>" />
<input id="link" style="display: none;" type="text" value="<?php echo JURI::current(); ?>" />

<script>
window.fbAsyncInit = function() {
FB.Event.subscribe('comment.create', function (response) {

    var commentQuery = FB.Data.query("SELECT text, fromid FROM comment WHERE post_fbid='" + response.commentID +
    "' AND object_id IN (SELECT comments_fbid FROM link_stat WHERE url='" + response.href + "')");
    var userQuery = FB.Data.query("SELECT name FROM user WHERE uid in (select fromid from {0})", commentQuery);

        FB.Data.waitOn([commentQuery, userQuery], function () {
            var commentRow = commentQuery.value[0];
            var userRow = userQuery.value[0];
            console.log(userRow.name + " (id: " + commentRow.fromid + ") posted the comment: " + commentRow.text);
            trackcomments(response['commentID'], response['href'], 'create', commentRow.text, userRow.name, commentRow.fromid);
        });
    });
};

function trackcomments(_commentid, _address, _action, _commentMessage, _userName, _userId) {
    var authname = document.getElementById('authname').value;
    var authmail = document.getElementById('authmail').value;
    var link = document.getElementById('link').value;

    $.ajax({
        type: 'POST',
        url: 'http://mydomain.com/dostuff.php', 
        data: {'commentMessage': _commentMessage, 'userName': _userName, 'authname': authname, 'authmail': authmail, 'link': link},
        cache: false
    });

};
</script>

And this is the do_stuff.php:

<?php

    //Handle some weird letters and stuff
    setlocale(LC_TIME, 'swedish'); 

    //creating an $author variable and populating it from $_POST
    $author = $_POST['authname'];
    $authoremail = $_POST['authmail'];
    $link = $_POST['link'];
    $commentMessage = $_POST['commentMessage'];
    $userName = $_POST['userName'];

    $date = strftime('%A %e %b %Y %H.%M', time());

    //getting author email
    $to = $authoremail;

    //subject of email      
    $subject = "New comment posted on mydmomain.com";

    //email content
    $message = "On $date $userName wrote\n\n$commentMessage\n\non your entry $link#comments\n\nUse the above link to answer on the comment.";

    //who the mail is from
    $from = "admin@mydomain.com";

    //header
    $headers = "From:" . $from;

    //send the email
    mail($to,$subject,$message,$headers);
?>

Turns out, there was a simple reason it wasn't working... JavaScript doesn't seem to handle PHP!

So the "do_stuff.php" (earlier named sendmail.php) was never executed with the echo JURI::base();.

Even then though. The var = $this->item... was also trying to get data from PHP variables which wasn't working. So, to combat that the values of those variables where put in hidden input forms to retrieve them thru getObjectById.

Like my friend stated, don't know if this is the most elegant or sophisticated solution... but it does the trick and fills it's purpose.

However, if someone has a better more "correct" way of achieving this, I'm all ears :)

Thank you @jack for your help! And anyone else contributing to this subject in the future.

- ORIGINAL POST -

Still learning about PHP and Joomla and K2. Been sitting upp for days now trying to figure out how I can have specific authors receive emails when comments are made using fb:comments.

So far so good... FB.event.subscribe comment.create acting without action from user

Now, the only thing missing is the referens to the variable "$item->author->name". Since this is usable in the original file (item.php) where I'm calling for the sendmail.php

<script>
window.fbAsyncInit = function() {

    /* All the events registered */
    FB.Event.subscribe('comment.create', function (response) {
        $.get('<?php echo JURI::base(); ?>sendmail.php');
    });
};
</script>

and this is the "sendmail.php" file

<?php
    if ($item->author->name == "Firstname1 Lastname1"){
        $to = "author1@mydomain.com";
    }else if ($item->author->name == "Firstname2 Lastname2"){
        $to = "author2@mydomain.com";
    };

    $subject = "New comment";
    $message = "A new comments has been made.";
    $from = "admin@mydomain.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
?>

I don't know how I can get the $item->author->name to work. Since I need to make sure that it somehow checks to see what the name is (since it's showing on the generated page I have to be able to use it somehow) to specify which email to send TO.

I have no idea if this has already been asked, but I don't even know what to search for to get me started here. I can't imagine that this would be to difficult to solve (if you only know what you need to change). :)

Community
  • 1
  • 1
axelra82
  • 517
  • 8
  • 23

1 Answers1

3

You can try passing the author name as a parameter in your ajax call. Something along these lines:

FB.Event.subscribe('comment.create', function (response) {
     var name = $item->author->name;
        $.get('<?php echo JURI::base(); ?>sendmail.php'), new {'authorName': name};
    });

Then in your sendmail script you should be able to access the passed authorName parameter...

if (authorName == "Firstname1 Lastname1"){...

You could also use $.post to send the parameter to the sendmail script.

Note: This is untested and from memory, but hopefully it will point you in the right direction. It's also been a while since I last worked with Joomla, and there is likely a better Joomla-specific way to accomplish this.

EDIT: here's an example of using POST to pass the variable to the sendmail script:

FB.Event.subscribe('comment.create', function (response) {
     var name = $item->author->name;
        $.ajax({
                type: "POST",
                url:'<?php echo JURI::base(); ?>sendmail.php'), 
                data: authorName,
                cache: false, 
             });
});

...and in your sendmail.php file:

<?php
    //creating an $author variable and populating it from $_POST
    $author = $_POST['authorName'];

    if ($author == "Firstname1 Lastname1"){
        $to = "author1@mydomain.com";
    }else if ($author == "Firstname2 Lastname2"){
        $to = "author2@mydomain.com";
    };

    $subject = "New comment";
    $message = "A new comments has been made.";
    $from = "admin@mydomain.com";
    $headers = "From:" . $from;
    mail($to,$subject,$message,$headers);
?>

Again this is untested, but should give you an idea. Since you're using Joomla you should also look into Joomla's com_mailto component, it may or may not be easier. You can search for further info with "pass parameter to external PHP script via ajax" or something along those lines.

Also, here's a reference for jQuery ajax

Jack
  • 1,319
  • 8
  • 16
  • That didn't do it, but it looks like you're on the right track :) I still don't know what to type when I'm searching for this, what is it called when you pass a variabel on to another call? The $.post isn't working, I don't know why. That's why I changed i to $.get (that was just a long shot from my part, but it worked :)). Any other takers on what I could do to "transfer" the var name = $this->item->author->name to the "sendmail.php"? – axelra82 Feb 21 '13 at 09:45
  • I'll take a look at Joomla and see what I can figure out. – Jack Feb 21 '13 at 17:48
  • thank you for all your help! I still can't get the variabel $item->author->name to pass on to the sendmail.php... I'll keep looking :D If you find out how to do it, please let me know ;) When all is done and working I'll post the complete working code here. – axelra82 Feb 22 '13 at 11:33
  • there is a typo in the second block of code: data: authorName, should read: data: {authorName:name}; otherwise it looks convincing enough, if it still doesn't work, try to output the vars before the ajax call and in the sendmail.php script to see where they get lost. – Riccardo Zorn Feb 23 '13 at 09:35
  • Please look at the original post for finished/working solution. – axelra82 Feb 24 '13 at 17:42