1. Fabrik 3.9 has been released. If you have updated Joomla to 3.9, this is a required update.
    Dismiss Notice

Bug with JComments

Discussion in 'Fabrik 3.x Testing' started by mhelm, May 7, 2012.

  1. mhelm

    mhelm Member

    Level: Community
    Hi,
    I use JComments with the latest GitHub-Update and Joomla 2.5.4. I activated the form-Plugin (do COMMENT in FRONTEND on EDIT, JComments) and experience the following:
    1. commenting in the form works ==> :)
    2. When adding a new entry in Frontend ALL Comments are shown at the bottom ==> :(
    3. JComments shows right before the comments a small "#" including the link to this comment. This link is wrong: It's www.mysite.com#comment-33, but should be something like www.mysite.com/index.php?option=com_fabrik&view=details&formid=5&rowid=31#comment-33 or www.mysite.com/details/5/31#comment-33) ==> :mad:
    4. email-notification of JComments works great, but the link to the corresponding record in the list is wrong ==> :mad:

    IMHO the reason for this bevaviour could be in table ##_jcomments_objects: The links saved here are wrong (www.mysite.com instead of www.mysite.com/index.php?option=com_fabrik&view=details&formid=5&rowid=31)
    Is this an issue of JComments (and must be fixed there) or by Fabrik?

    5. I have a user-element in my list. On creation of a new record I want that user to auto-subscribe to any comments. Thought of adding a record via PHP to ##__jcomments_subscriptions - did anyone do that already and is willing to share some code?

    Thanks for help
    mhelm
     
  2. rob

    rob Administrator Staff Member

    Level: Community
    2 / the plugin should now not render when adding a record.

    For the issues 3 & 4 I concur that the link entry in #__jcomments_objects should have the full url. I'm not sure what needs to be done there to fix it - I had a look at their code but couldn't see a way of setting this.

    5/ is only possible with the local commenting system in conjunction with the notifications cron plugin.
     
    1 person likes this.
  3. mhelm

    mhelm Member

    Level: Community
    /2 confirmed: No comments shown - thanks!
     
  4. mhelm

    mhelm Member

    Level: Community
    Just in case someone has the same problems with the JComment-Link:
    You need a JComments plugin for your form (see http://www.joomlatune.com/jcomments-developers-manual.html

    Example: Given a fabrik form with ID 5 (a the corresponding list with ID 5)
    Create a file /components/com_jcomments/plugins/com_fabrik_5.plugin.php

    PHP:
    <?php
    class jc_com_fabrik_5 extends JCommentsPlugin
    {
        function getObjectTitle($id)
        {
            $title = "Item number ".$id;
            return $title;
        }
     
        function getObjectLink($id)
        {
            $link = JoomlaTuneRoute::_('index.php?option=com_fabrik&amp;view=form&formid=5&rowid=' .$id. '&listid=5');      
            return $link;
        }
     
        function getObjectOwner($id)
        {
            $user =& JFactory::getUser();
            return $user->id;
        }
    }
    ?>
    Remember to change "5" to your id. Just play a bit with the link and the title...that should do it.
     
  5. mhelm

    mhelm Member

    Level: Community
    Some lines of code autosubscribe the "owner" of a record to JComments:

    Create a file in /plugins/fabrik_form/php/scripts/JS_AutoSubscribe_Form5.php"

    PHP:
    <?php
    defined('_JEXEC') or die();
    require_once (JCOMMENTS_BASE.DS.'jcomments.subscription.php');
    require_once (JCOMMENTS_BASE.DS.'jcomments.class.php');
    $rowid = JRequest::getVar('rowid',0);
    $objgroup = 'com_fabrik_5'; // form # 5 is used in this case
    $language = 'en-EN'; // should be fetched from Joomla...just for testing purposes

    if ($rowid > 0) {    
        // add subscription    
        $manager = JCommentsSubscriptionManager::getInstance();
        $user =& JFactory::getUser();
        $manager->subscribe($rowid, $objgroup, $user->id, $user->email, $user->name, $language);
    }
    ?>
    - Add a plugin to your fabrik-form (do "php" in "FrontEnd" on "New")
    - Process "After Data Stored, before calc..."
    - Add the script "JS_AutoSubscribe_Form5.php"
     
  6. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    OK, fixed this one.

    1) In the jc_com_fabrik_5 plugin code, the links in getObjectLink() needed to use 8, not 5.

    2) Same file, there's a missing ; at the end of getObjectOwner()

    3) There seems to be a bug in com_jcomments, in ./components/com_jcomments/helpers/notifications.php, line 38 needs to check if $subscriber->userid is set, like this:

    PHP:
                        if (isset($subscriber->userid) && ($data['comment']->email <> $subscriber->email) && ($user->email <> $subscriber->email)) {
    ... otherwise for (I think) guest comments it pitches a warning, which blows up the AJAX response when posting a followup comment.

    Seems to all be working now.

    Thanks to mhelm for the guidance on doing this. Maybe someone could make a wiki entry for this soewhere? Not sure what category to put it under ...

    -- hugh
     
  7. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Ooops, note that in my last post, point 1), "8 not 5", I was answering someone in Skype who pointed to this thread. Their form is ID 8, not 5. I thought this was their thread, but it isn't. So ignore that, and obviously use the form ID for your form.

    But my 2) and 3) still stand.

    -- hugh
     
  8. mhelm

    mhelm Member

    Level: Community
    You're right - fixed 2) !
     
  9. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Just FYI, here's the code I ended up with which will auto-subscribe users to a JComment thread when posting a new form. The main wrinkle is that when the form is new, there is no JComment 'object', so you can't auto-subscribe until that's been created. And as the code to do that in JComments isn't exposed, I had to copy soe chunks of their code in to a couple of functions.

    The following would need a little tweaking to work for another form, but really only the element names / form ID.

    PHP:

    <?php
     
    defined('_JEXEC') or die();
     
    require_once (JCOMMENTS_BASE.DS.'jcomments.subscription.php');
    require_once (JCOMMENTS_BASE.DS.'jcomments.class.php');
     
    $debug = true;
     
    $rowid = $formModel->getRowId();
    if (empty($rowid)) {
    $rowid = JArrayHelper::getValue($formModel->_formData, 'repairorders___id_raw', 0);
    }
     
    //var_dump($rowid, $formModel->_formData);exit;
     
    $my_jc_object_group = 'com_fabrik_8';
    $my_jc_language = 'en-GB';
    $my_jc_object_id = $rowid;
     
    // var_dump($formModel->_formData);exit;
     
    function my_jc_getObjectTitle($id, $language)
    {
    $title = "Assistenza ".$id;
    return $title;
    }
     
    function my_jc_getObjectLink($id, $language)
    {
    $link = JRoute::_('index.php?option=com_fabrik&amp;view=form&formid=8&rowid=' .$id. '&listid=8');
    return $link;
    }
     
    function my_jc_getObjectOwner($id, $language)
    {
    $user =& JFactory::getUser();
    return $user->get('id');
    }
     
    function my_jc_getObjectInfo($object_id, $object_group, $language)
    {
    $db = JFactory::getDbo();
     
    $query = "SELECT * "
    . " FROM `#__jcomments_objects`"
    . " WHERE `object_id` = " . $db->Quote($object_id)
    . " AND `object_group` = " . $db->Quote($object_group)
    . " AND `lang` = " . $db->Quote($language)
    ;
     
    $db->setQuery($query);
    $info = $db->loadObject();
     
    return empty($info) ? false : $info;
    }
     
    function my_jc_setObjectInfo($objectId, $info)
    {
    $db = JFactory::getDbo();
     
    if (!empty($objectId)) {
    $query = "UPDATE #__jcomments_objects"
    . " SET "
    . "  `access` = " . (int) $info->access
    . ", `userid` = " . (int) $info->userid
    . ", `expired` = 0"
    . ", `modified` = " . $db->Quote(JFactory::getDate()->toSql())
    . (empty($info->title) ? "" : ", `title` = " . $db->Quote($info->title))
    . (empty($info->link) ? "" : ", `link` = " . $db->Quote($info->link))
    . (empty($info->category_id) ? "" : ", `category_id` = " . (int) $info->category_id)
    . " WHERE `id` = " . (int) $objectId . ";"
    ;
    } else {
    $query = "INSERT INTO #__jcomments_objects"
    . " SET "
    . "  `object_id` = " . (int) $info->object_id
    . ", `object_group` = " . $db->Quote($info->object_group)
    . ", `category_id` = " . (int) $info->category_id
    . ", `lang` = " . $db->Quote($info->lang)
    . ", `title` = " . $db->Quote($info->title)
    . ", `link` = " . $db->Quote($info->link)
    . ", `access` = " . (int) $info->access
    . ", `userid` = " . (int) $info->userid
    . ", `expired` = 0"
    . ", `modified` = " . $db->Quote(JFactory::getDate()->toSql())
    ;
    }
     
    $db->setQuery($query);
    $db->execute();
    }
     
    function my_jc_upsertObject($object_id, $object_group, $language) {
    $jObjectId = '';
    $info = my_jc_getObjectInfo($object_id, $object_group, $language);
     
    if ($info === false) {
    $info = new stdClass();
    }
    else {
    $jObjectId = $info->id;
    }
     
    $info->title = my_jc_getObjectTitle($object_id, $language);
    $info->link = my_jc_getObjectLink($object_id, $language);
    $info->userid = my_jc_getObjectOwner($object_id, $language);
    $info->lang = $language;
    $info->object_id = $object_id;
    $info->object_group = $object_group;
    $info->access = 1;
    $info->category_id = '';
    $info->expired = 0;
    //var_dump($info, $jObjectId);exit;
    my_jc_setObjectInfo($jObjectId, $info);
    }
     
    $app = jFactory::getApplication();
     
    //var_dump($rowid);exit;
    if (!empty($rowid)) {
     
    if ($debug) {
    $app->enqueueMessage("auto subscribe: got rowid, about to call upsertObject: $my_jc_object_id, $my_jc_object_group, $my_jc_language");
    }
     
    my_jc_upsertObject($my_jc_object_id, $my_jc_object_group, $my_jc_language);
     
        // add subscription    
        $manager = JCommentsSubscriptionManager::getInstance();
    /*
        $user =& JFactory::getUser();
        $manager->subscribe($rowid, $objgroup, $user->id, $user->email, $user->name, $language);
    */

    $shop_user_id = (int) $formModel->getElementData('loadshopid', true, 0);
    $customer_user_id = (int) $formModel->getElementData('customer_user_id', true, 0);
     
    if ($debug) {
    $app->enqueueMessage("auto subscribe: got shop user, customer: $shop_user_id, $customer_user_id");
    }
    //var_dump($formModel->_formData, $shop_user_id, $customer_user_id);exit;
    if ($shop_user_id > 0) {
    if ($debug) {
    $app->enqueueMessage("auto subscribe: subscribing shop user: $shop_user_id");
    }
    $user =& JFactory::getUser($shop_user_id);
    $manager->subscribe($my_jc_object_id, $my_jc_object_group, $user->id, $user->email, $user->name, $my_jc_language);
    }
     
    if ($customer_user_id > 0) {
    if ($debug) {
    $app->enqueueMessage("auto subscribe: subscribing customer user: $customer_user_id");
    }
    $user =& JFactory::getUser($customer_user_id);
    $manager->subscribe($my_jc_object_id, $my_jc_object_group, $user->id, $user->email, $user->name, $my_jc_language);
    }
    }
    else {
    if ($debug) {
    $app->enqueueMessage("auto subscribe: no rowid, not subscribing");
    }
    }
     
    ?>
     
    I should probably not duplicate those first three functions, which are in the plugin from post #4, and instead work out how to call those directly, but I was in a hurry.

    Also, should really work out where in the wiki all this stuff could go, but I'm not entirely sure.

    And of course, best of all worlds would be to fold this stuff into the 'comments' form plugin, with the elements to auto-subscribe being configurable ... one of these days ...

    -- hugh
     
  10. cheesegrits

    cheesegrits Support Gopher Staff Member

    Level: Community
    Hmmm, not sure why all my indenting got stripped out using the PHP bbcode.

    -- hugh
     
  11. Borox

    Borox Member

    Level: Community
    Hi,

    I use JComments 3.0.5 and fabrik 3.4.2, I've done another way to get the proper link and title

    PHP:
    <?php
    //Plugin for each fabrik list/form where we want use jcomments
    //This way the title and the link is OK for jcomments and we can use such module as 'JComments Latest Commented'

    //Just replace '5' in line 7 and 11 by your fabrik id and adapt the query

    class jc_com_fabrik_5 extends JCommentsPlugin
    {
        function getObjectInfo($id, $language = null)
        {
            $No_id_Fabrik='5';
            echo $No_id_Fabrik;
       
            $info = new JCommentsObjectInfo();

            $router = JPATH_ROOT.'/components/com_fabrik/router.php';
            if (is_file($router)) {
                require_once($router);

                $db = JFactory::getDbo();
                $query = $db->getQuery(true);

                //Adapt the query to get the proper title
                $query->select('id, travaux_nom');
                $query->from('travaux');
                $query->where('id = ' . (int) $id);
           
                $db->setQuery($query);
                $row = $db->loadObject();

                if (!empty($row)) {
                    $Itemid = self::getItemid('com_fabrik', 'index.php?option=com_fabrik&amp;view=form&formid='.$No_id_Fabrik);
                    $Itemid = $Itemid > 0 ? '&Itemid=' . $Itemid : '';

                    //adapt the title regarding your query
                    $info->title = 'Travaux - '.$row->travaux_nom;
                    $info->link = JRoute::_('index.php?option=com_fabrik&amp;view=form&formid='.$No_id_Fabrik.'&rowid=' .$id. '&listid='.$No_id_Fabrik);
                }
            }

            return $info;
        }
    }
    ?>
    It works fine for me ( the module 'JComments Latest Commented' displays the right informations).
     
    Last edited: Feb 22, 2016
    cheesegrits likes this.
  12. rob

    rob Administrator Staff Member

    Level: Community
    Borox likes this.

Share This Page