getk2 / k2

K2 - the powerful content extension for Joomla
https://getk2.org
GNU General Public License v3.0
135 stars 96 forks source link

PHP notices and warnings #113

Closed vancanneyt closed 10 years ago

vancanneyt commented 10 years ago

Using latest dev build following PHP notices are showing up under PHP5.5:

PHP Notice: Undefined property: stdClass::$numOfComments in /var/www/html/components/com_k2/views/item/view.html.php on line 264 PHP Warning: Creating default object from empty value in /var/www/html/components/com_k2/models/item.php on line 442

kavadas commented 10 years ago

Hi. I cannot reproduce the issue. In which page do you get this notices ? Are you requesting a valid K2 item?

vancanneyt commented 10 years ago

on almost every page with K2 items, itemlist views and article views.

All are valid K2 items, the PHP error log is full of notices "Undefined property: stdClass::$numOfComments " The warning itself idem dito; a lot of hits too if you search for that one on google: https://www.google.be/search?hl=nl&q=Creating+default+object+from+empty+value+k2&sourceid=ie8&rls=com.microsoft:nl-BE:IE-Address&ie=&oe=#hl=nl&q=Creating+default+object+from+empty+value+k2%2Fmodels%2Fitem.php&rls=com.microsoft:nl-BE:IE-Address

tomconklin commented 10 years ago
// Comments
    $item->event->K2CommentsCounter = '';
    $item->event->K2CommentsBlock = '';
    if ($item->params->get('itemComments'))
    {
        // Trigger comments events
        $dispatcher = JDispatcher::getInstance();
        JPluginHelper::importPlugin('k2');
        $results = $dispatcher->trigger('onK2CommentsCounter', array(
            &$item,
            &$params,
            $limitstart
        ));
        $item->event->K2CommentsCounter = trim(implode("\n", $results));
        $results = $dispatcher->trigger('onK2CommentsBlock', array(
            &$item,
            &$params,
            $limitstart
        ));
        $item->event->K2CommentsBlock = trim(implode("\n", $results));

        // Load K2 native comments system only if there are no plugins overriding it
        if (empty($item->event->K2CommentsCounter) && empty($item->event->K2CommentsBlock))
        {

            // Load reCAPTCHA script
            if (!JRequest::getInt('print') && ($item->params->get('comments') == '1' || ($item->params->get('comments') == '2' && K2HelperPermissions::canAddComment($item->catid))))
            {

                if ($params->get('recaptcha') && ($user->guest || $params->get('recaptchaForRegistered', 1)))
                {
                    $document->addScript('https://www.google.com/recaptcha/api/js/recaptcha_ajax.js');
                    $js = '
                    function showRecaptcha(){
                        Recaptcha.create("'.$item->params->get('recaptcha_public_key').'", "recaptcha", {
                            theme: "'.$item->params->get('recaptcha_theme', 'clean').'"
                        });
                    }
                    $K2(window).load(function() {
                        showRecaptcha();
                    });
                    ';
                    $document->addScriptDeclaration($js);
                }
            }

            // Check for inline comment moderation
            if (!$user->guest && $user->id == $item->created_by && $params->get('inlineCommentsModeration'))
            {
                $inlineCommentsModeration = true;
                $commentsPublished = false;
            }
            else
            {
                $inlineCommentsModeration = false;
                $commentsPublished = true;
            }
            $this->assignRef('inlineCommentsModeration', $inlineCommentsModeration);

            // Flag spammer link
            $reportSpammerFlag = false;
            if (K2_JVERSION != '15')
            {
                if ($user->authorise('core.admin', 'com_k2'))
                {
                    $reportSpammerFlag = true;
                    $document = JFactory::getDocument();
                    $document->addScriptDeclaration('var K2Language = ["'.JText::_('K2_REPORT_USER_WARNING', true).'"];');
                }
            }
            else
            {
                if ($user->gid > 24)
                {
                    $reportSpammerFlag = true;
                }
            }

            $limit = $params->get('commentsLimit');
            $comments = $model->getItemComments($item->id, $limitstart, $limit, $commentsPublished);

            for ($i = 0; $i < sizeof($comments); $i++)
            {
                $comments[$i]->commentText = nl2br($comments[$i]->commentText);

                // Convert URLs to links properly
                $comments[$i]->commentText = preg_replace("/([^\w\/])(www\.[a-z0-9\-]+\.[a-z0-9\-]+)/i", "$1http://$2", $comments[$i]->commentText);
                $comments[$i]->commentText = preg_replace("/([\w]+:\/\/[\w-?&;#~=\.\/\@]+[\w\/])/i", "<a target=\"_blank\" rel=\"nofollow\" href=\"$1\">$1</A>", $comments[$i]->commentText);
                $comments[$i]->commentText = preg_replace("/([\w-?&;#~=\.\/]+\@(\[?)[a-zA-Z0-9\-\.]+\.([a-zA-Z]{2,3}|[0-9]{1,3})(\]?))/i", "<a href=\"mailto:$1\">$1</A>", $comments[$i]->commentText);

                $comments[$i]->userImage = K2HelperUtilities::getAvatar($comments[$i]->userID, $comments[$i]->commentEmail, $params->get('commenterImgWidth'));
                if ($comments[$i]->userID > 0)
                {
                    $comments[$i]->userLink = K2HelperRoute::getUserRoute($comments[$i]->userID);
                }
                else
                {
                    $comments[$i]->userLink = $comments[$i]->commentURL;
                }
                if ($reportSpammerFlag && $comments[$i]->userID > 0)
                {
                    $comments[$i]->reportUserLink = JRoute::_('index.php?option=com_k2&view=comments&task=reportSpammer&id='.$comments[$i]->userID.'&format=raw');
                }
                else
                {
                    $comments[$i]->reportUserLink = false;
                }
            }

            $item->comments = $comments;

            jimport('joomla.html.pagination');
            $total = $item->numOfComments;
            $pagination = new JPagination($total, $limitstart, $limit);

        }

    }

I deleted this portion of view.html.php and error go's away... Possible that this error is raised due to I just overwrite K2 files with git version (used extension manager discover) to correct backend save item error, which in fact is corrected yet introduced this error. I don't use K2 comments so not an issue for me.

spoxox commented 10 years ago

This error message showed up when I turned off comments. Using J! 3.2.3 using Yootheme Pace theme K2 2.6.8 PHP 5.5.9 IIS 7.0

Fixed with this one-line change on line 264:

$total = $item->numOfComments;

Change to

$total = (isset($item->numOfComments) ? $item->numOfComments : 0);
kavadas commented 10 years ago

Issue has already been fixed https://github.com/joomlaworks/k2/blob/master/components/com_k2/views/item/view.html.php#L263-L266 . I am closing this issue now. Please feel free to open a new one if needed.