示例#1
0
    public function onContentPrepare($context, &$article, &$params, $page = 0)
    {
        $app = JFactory::getApplication();
        $menu = $app->getMenu();
        if ($menu->getActive() == $menu->getDefault() || $context != 'com_content.article' || $page > 0 || empty($article->id)) {
            return true;
        }
        $db = JFactory::getDbo();
        $document = JFactory::getDocument();
        $user = JFactory::getUser();
        $api = new CjLibApi();
        $article_url = JRoute::_(ContentHelperRoute::getArticleRoute($article->id . ':' . $article->alias, $article->catid . ':' . $article->category_alias));
        /******************************** TRIGGER BADGE RULE ******************************************/
        CjBlogApi::trigger_badge_rule('com_content.num_hits', array('num_hits' => $article->hits, 'ref_id' => $article->id), $article->created_by);
        /******************************* TRIGGER POINTS RULES ******************************************/
        $appparams = JComponentHelper::getParams('com_cjblog');
        if ($appparams->get('enable_points')) {
            CjBlogApi::award_points('com_content.hits', $user->id, 0, $article->id, JHtml::link($article_url, $article->title));
            CjBlogApi::award_points('com_content.popular_articles', $article->created_by, 0, $article->id, JHtml::link($article_url, $article->title), array('hits' => $article->hits));
        }
        // Check if the plugin is disabled for the article category
        $include_categories = $this->params->get('include_categories');
        $exclude_categories = $this->params->get('exclude_categories');
        $about_length = $this->params->get('about_length', 180);
        if (!empty($include_categories)) {
            $include_categories = explode(',', $include_categories);
            if (!in_array($article->catid, $include_categories)) {
                return true;
            }
        } else {
            if (!empty($exclude_categories)) {
                $exclude_categories = explode(',', $exclude_categories);
                if (in_array($article->catid, $exclude_categories)) {
                    return true;
                }
            }
        }
        $print = $app->input->getInt('print', 0);
        if ($print) {
            return true;
        }
        /********************************** CONTENT HEADER *********************************************/
        require_once JPATH_ROOT . '/components/com_cjblog/router.php';
        $CjLib = JPATH_ROOT . '/components/com_cjlib/framework.php';
        CJLib::import('corejoomla.framework.core');
        $custom_tag = false;
        JHtml::_('jquery.framework');
        CJLib::behavior('bscore', array('customtag' => $custom_tag));
        CJFunctions::load_jquery(array('libs' => array('rating', 'social', 'fontawesome'), 'custom_tag' => $custom_tag));
        $document->addScript(JUri::root(true) . '/media/com_cjblog/js/cjblog.min.js');
        $document->addStyleSheet(JUri::root(true) . '/media/com_cjblog/css/cjblog.min.css');
        $articles_itemid = CJFunctions::get_active_menu_id(true, 'index.php?option=com_cjblog&view=articles');
        $profile = CjBlogApi::get_user_profile($article->created_by);
        // 		$params->merge($appparams);
        $meta_header = '';
        $meta_stats = '';
        $meta_rating = '';
        $meta_rating_readonly = '';
        $social_buttons = '';
        $tags_html = '';
        $show_category = $appparams->get('show_category', 1);
        $show_date = $appparams->get('show_create_date', 1);
        $date_format = JText::_($appparams->get('date_format', 'd F Y'));
        $show_hits = $appparams->get('show_hits', 1);
        $show_author = $appparams->get('show_author', 1);
        $show_favoured = $appparams->get('show_favoured', 1);
        $show_rating = $appparams->get('show_rating', 1);
        $user_name = $appparams->get('user_display_name', 'name');
        $hot_hits = $appparams->get('hot_article_num_hits', 250);
        $social_buttons = $appparams->get('display_social_buttons', 1);
        $sharing_buttons_customtag = $appparams->get('sharing_buttons_customtag', '');
        $sharing_buttons_customtext = $appparams->get('sharing_buttons_customtext', '');
        $social_buttons_theme = $appparams->get('social_buttons_theme', 'jssocials-theme-classic.css');
        $sharing_position = $appparams->get('sharing_buttons_position', 'bottom');
        $jssocial_label = $appparams->get('jssocial_button_label', 'true');
        $jssocial_count = $appparams->get('jssocial_button_count', 'true');
        $social_buttons_via = $appparams->get('tweet_button_via', '');
        $social_buttons_hashtag = $appparams->get('tweet_button_hashtag', '');
        $jssocial_email = $appparams->get('jssocial_button_email', '1');
        $jssocial_twitter = $appparams->get('jssocial_button_twitter', '1');
        $jssocial_facebook = $appparams->get('jssocial_button_facebook', '1');
        $jssocial_googleplus = $appparams->get('jssocial_button_googleplus', '1');
        $jssocial_linkedin = $appparams->get('jssocial_button_linkedin', '1');
        $jssocial_pinterest = $appparams->get('jssocial_button_pinterest', '1');
        $profileApp = $appparams->get('profile_component', 'cjblog');
        $avatarApp = $appparams->get('avatar_component', 'cjblog');
        $showProfile = JFactory::getUser($article->created_by)->authorise('core.showprofile', 'com_cjblog');
        $bbcode = $appparams->get('default_editor', 'wysiwygbb') == 'wysiwygbb' ? true : false;
        $about = !empty($profile['about']) ? '<div>' . CJFunctions::preprocessHtml($profile['about'], false, $bbcode) . '</div>' : '';
        if ($about_length > 0) {
            $about = CJFunctions::substrws($about, $about_length);
        }
        if ($show_category && $show_date) {
            $cat_url = JRoute::_('index.php?option=com_cjblog&view=articles&task=latest&id=' . $article->catid . ':' . $article->category_alias . $articles_itemid);
            $cat_link = JHtml::link($cat_url, CJFunctions::escape($article->category_title));
            $formatted_date = CJFunctions::get_localized_date($article->created, $date_format);
            $meta_header = JText::sprintf('TXT_POSTED_IN_CATEGORY_ON', $cat_link, $formatted_date);
        } else {
            if ($show_category) {
                $cat_url = JRoute::_('index.php?option=com_cjblog&view=articles&task=latest&id=' . $article->catid . ':' . $article->category_alias . $articles_itemid);
                $cat_link = JHtml::link($cat_url, CJFunctions::escape($article->category_title));
                $meta_header = JText::sprintf('TXT_POSTED_IN_CATEGORY', $cat_link);
            } else {
                if ($show_date) {
                    $meta_header = JText::sprintf('TXT_POSTED_ON', CJFunctions::get_localized_date($article->created, $date_format));
                }
            }
        }
        if ($article->hits > $hot_hits) {
            $meta_stats = $meta_stats . '<span class="label label-important hot-article">' . JText::_('LBL_HOT') . '</span> ';
        }
        if ($show_hits) {
            $meta_stats = $meta_stats . '<span class="label label-info">' . JText::sprintf('TXT_NUM_HITS', $article->hits) . '</span> ';
        }
        if ($show_favoured) {
            $query = 'select favorites from ' . T_CJBLOG_CONTENT . ' where id = ' . $article->id;
            $db->setQuery($query);
            $favored = (int) $db->loadResult();
            if (!$user->guest) {
                $query = 'select count(*) from ' . T_CJBLOG_FAVORITES . ' where content_id = ' . $article->id . ' and user_id = ' . $user->id;
                $db->setQuery($query);
                $count = $db->loadResult();
                if ($count == 0) {
                    $meta_stats = $meta_stats . '
						<span class="label label-info favorites">' . JText::sprintf('TXT_NUM_FAVOURED', $favored) . '</span>
						<a id="btn-add-to-favorites"
							class="btn btn-mini tooltip-hover"
							href="' . JRoute::_('index.php?option=com_cjblog&view=articles&task=favorite&id=' . $article->id . $articles_itemid) . '"
							onclick="return false;"
							title="' . JText::_('LBL_ADD_TO_FAVORITES') . '">
							<i class="fa fa-star"></i>
						</a>';
                } else {
                    $meta_stats = $meta_stats . '
						<span class="label label-info favorites tooltip-hover" title="' . JText::_('LBL_YOU_ADDED_TO_FAVORITES') . '">
							<i class="fa fa-star"></i> ' . JText::sprintf('TXT_NUM_FAVOURED', $favored) . '
						</span>
						<a id="btn-remove-favorite"
							class="btn btn-mini tooltip-hover"
							href="' . JRoute::_('index.php?option=com_cjblog&view=articles&task=remove_favorite&id=' . $article->id . $articles_itemid) . '"
							onclick="return false;"
							title="' . JText::_('LBL_REMOVE_FAVORITE') . '">
							<i class="icon-remove"></i>
						</a>';
                }
            } else {
                $meta_stats = $meta_stats . '<span class="label label-info favorites">' . JText::sprintf('TXT_NUM_FAVOURED', $favored) . '</span>';
            }
        }
        $asset = 'com_content.article.' . $article->id;
        $redirect_url = base64_encode($article_url);
        if ($user->authorise('core.edit.own', $asset) && $article->created_by == $user->id || $user->authorise('core.manage')) {
            $meta_stats = $meta_stats . '
						<a id="btn-edit-article"
							class="btn btn-mini tooltip-hover"
							href="' . JRoute::_(CjBlogHelperRoute::getFormRoute($article->id) . '&return=' . $redirect_url) . '"
							title="' . JText::_('LBL_EDIT_ARTICLE') . '">
							<i class="fa fa-pencil"></i>
						</a>';
        }
        if ($user->authorise('core.edit.state')) {
            if ($article->state == 1) {
                $meta_stats = $meta_stats . '
						<a id="btn-publish-article"
							class="btn btn-mini tooltip-hover"
							href="' . JRoute::_('index.php?option=com_cjblog&view=articles&task=unpublish&id=' . $article->id . $articles_itemid . '&return=' . $redirect_url) . '"
							title="' . JText::_('LBL_PUBLISHED') . '">
							<i class="fa fa-check"></i>
						</a>';
            } else {
                $meta_stats = $meta_stats . '
						<a id="btn-publish-article"
							class="btn btn-mini tooltip-hover"
							href="' . JRoute::_('index.php?option=com_cjblog&view=articles&task=publish&id=' . $article->id . $articles_itemid . '&return=' . $redirect_url) . '"
							title="' . JText::_('LBL_UNPUBLISHED') . '">
							<i class="fa fa-ban"></i>
						</a>';
            }
        }
        if ($show_rating && $user->authorise('articles.rate', 'com_cjblog')) {
            $rating = CJFunctions::get_rating(CJBLOG_ASSET_ID, $article->id);
            $meta_rating_readonly = '
				<div class="pull-right">
					<span class="article-star-rating-readonly"
						data-rating-score="' . $rating['rating'] . '"
						data-rating-hints="' . JText::_('LBL_RATING_HINTS') . '"
						data-rating-noratemsg="' . JText::_('LBL_RATING_NORATE_HINT') . '"
						data-rating-cancelhint="' . JText::_('LBL_RATING_CANCEL_HINT') . '"></span>
					(' . JText::sprintf('TXT_RATINGS', $rating['total_ratings']) . ')
				</div>';
            $readonly = false;
            $hash = CJFunctions::get_hash('com_content.article.rating.item_' . $article->id);
            $cookie = $app->input->cookie->get($hash, null);
            if ($cookie) {
                $readonly = true;
            } else {
                if (!$user->guest) {
                    $query = 'select count(*) from ' . T_CJ_RATING_DETAILS . ' where item_id = ' . $article->id . ' and asset_id = 1 and created_by = ' . $user->id;
                    $db->setQuery($query);
                    $count = (int) $db->loadResult();
                    if ($count > 0) {
                        $app->input->cookie->set($hash, 1, time() + 60 * 60 * 24 * 365, $article_url);
                        $readonly = true;
                    }
                }
            }
            if (!$readonly) {
                $meta_rating = '
					<div>' . JText::_('LBL_RATE_THIS_ARTICLE') . ':</div>
					<span class="article-star-rating"
						data-rating-score="' . $rating['rating'] . '"
						data-rating-url="' . JRoute::_('index.php?option=com_cjblog&view=articles&task=rate&id=' . $article->id . $articles_itemid) . '"
						data-rating-hints="' . JText::_('LBL_RATING_HINTS') . '"
						data-rating-noratemsg="' . JText::_('LBL_RATING_NORATE_HINT') . '"
						data-rating-cancelhint="' . JText::_('LBL_RATING_CANCEL_HINT') . '"></span>
					(<span id="article-rating-info">' . JText::sprintf('TXT_RATING_INFO', $rating['rating'], $rating['total_ratings']) . '</span>)
					<hr>';
            }
        }
        if ($social_buttons == 1) {
            $params_image = new JRegistry();
            $params_image->loadString($article->images);
            $intro = $params_image->get('image_intro');
            $pinterest_image = false;
            if (!empty($intro)) {
                $pinterest_image = JUri::root() . $intro;
            } else {
                preg_match_all('/<img .*src=["|\']([^"|\']+)/i', $article->introtext . $article->fulltext, $matches);
                foreach ($matches[1] as $key => $file_path) {
                    $pinterest_image = JUri::root() . $file_path;
                    break;
                }
            }
            $document->addScript(JUri::root(true) . '/media/com_cjblog/js/jssocials.min.js');
            $document->addStyleSheet(JUri::root(true) . '/media/com_cjblog/css/jssocials.css');
            $document->addStyleSheet(JUri::root(true) . '/media/com_cjblog/css/' . $social_buttons_theme . '');
        }
        if ($social_buttons == 1 || $social_buttons == 2) {
            if ($sharing_buttons_customtext == '') {
                $sharing_buttons_customtext = JText::_('LBL_SOCIAL_SHARING_DESC');
            }
            $social_buttons_construct = '<div class="social-sharing"><p>' . $sharing_buttons_customtext . '</p></div>';
            if ($social_buttons == 1) {
                $social_buttons_jssocials = '
								showLabel: ' . $jssocial_label . ',
								showCount: ' . $jssocial_count . ',
								shares: [';
                if ($jssocial_twitter == "1") {
                    $social_buttons_jssocials = $social_buttons_jssocials . '
									{ share: "twitter",
										via: "' . $social_buttons_via . '",
										hashtags: "' . $social_buttons_hashtag . '",
										text: "' . $article->title . '"},';
                }
                if ($jssocial_facebook == "1") {
                    $social_buttons_jssocials = $social_buttons_jssocials . '
									{ share: "facebook"},';
                }
                if ($jssocial_googleplus == "1") {
                    $social_buttons_jssocials = $social_buttons_jssocials . '
									{ share: "googleplus"},';
                }
                if ($jssocial_linkedin == "1") {
                    $social_buttons_jssocials = $social_buttons_jssocials . '
									{ share: "linkedin"},';
                }
                if ($jssocial_pinterest == "1" && $pinterest_image) {
                    $social_buttons_jssocials = $social_buttons_jssocials . '
                                                                        { share: "pinterest",
                                                                                media: "' . $pinterest_image . '"},';
                }
                if ($jssocial_email == "1") {
                    $social_buttons_jssocials = $social_buttons_jssocials . '
									{ share: "email",
										text: "' . $article->title . '"},';
                }
                $social_buttons_jssocials = $social_buttons_jssocials . '
								]';
                $social_buttons_top = $social_buttons_construct . '
						<div id="share-top">
							<script type="text/javascript">
								jQuery("#share-top").jsSocials({' . $social_buttons_jssocials . '
								});
							</script>
						</div>';
                $social_buttons_bottom = $social_buttons_construct . '
						<div id="share-bottom">
							<script type="text/javascript">
								jQuery("#share-bottom").jsSocials({' . $social_buttons_jssocials . '
								});
							</script>
						</div>';
            } else {
                $social_buttons_top = $social_buttons_construct . $sharing_buttons_customtag;
                $social_buttons_bottom = $social_buttons_top;
            }
        }
        JLoader::import('joomla.application.component.model');
        JLoader::import('articles', JPATH_ROOT . '/components/com_cjblog/models');
        $model = JModelLegacy::getInstance('articles', 'CjBlogModel');
        $tags = $model->get_tags_by_itemids(array($article->id));
        if (!empty($tags)) {
            $tags_html = '<div class="tags-list margin-top-10">';
            foreach ($tags as $tag) {
                $tags_html .= '
						<a title="' . JText::sprintf('TXT_TAGGED_ARTICLES', CJFunctions::escape($tag->tag_text)) . ' - ' . CJFunctions::escape($tag->description) . '" class="tooltip-hover label tag-item"
							href="' . JRoute::_('index.php?option=com_cjblog&view=articles&task=tag&id=' . $tag->tag_id . ':' . $tag->alias . $articles_itemid) . '">
							<i class="fa fa-tags"></i> ' . CJFunctions::escape($tag->tag_text) . '
						</a>';
            }
            $tags_html .= '</div>';
        }
        $avatar = $api->getUserAvatarImage($avatarApp, $article->created_by, $profile['email'], 48);
        if ($showProfile) {
            $profileUrl = $api->getUserProfileUrl($profileApp, $article->created_by);
        } else {
            $profileUrl = "#";
        }
        $html_top = '
		<div class="well well-small">
			<div class="media clearfix" style="overflow: visible">
				<div class="pull-left"><a href="' . $profileUrl . '" class="thumbnail nomargin-bottom">
					<img class="img-avatar" src="' . $avatar . '" alt="' . CjLibUtils::escape($profile[$user_name]) . '" style="max-width: 48px;"></a>
				</div>
				<div class="media-body">
					<div class="muted">' . ($show_author == 1 ? JText::sprintf('TXT_WRITTEN_BY', JHtml::link($profileUrl, CjLibUtils::escape($profile[$user_name]))) . ' ' : '') . $meta_header . '</div>
					<div style="margin-top: 5px;" class="clearfix">
						<div class="pull-left">' . $meta_stats . '</div>
						' . $meta_rating_readonly . '
					</div>
				</div>
				' . ($sharing_position == 'top' || $sharing_position == 'both' ? '<hr/>' . $social_buttons_top : '') . '
			</div>
		</div>';
        $html_bottom = $tags_html . '
		<div class="panel panel-default">
			<div class="panel-body">
				<div class="media clearfix" style="overflow: visible">
					' . $meta_rating . '
					' . ($sharing_position == 'bottom' || $sharing_position == 'both' ? $social_buttons_bottom . '<hr/>' : '') . '
					<div class="pull-left"><a href="' . $profileUrl . '" class="thumbnail nomargin-bottom"><img class="img-avatar" src="' . $avatar . '" alt="' . $profile[$user_name] . '" style="max-width: 48px;"></a></div>
					<div class="media-body">
						<h4 class="media-heading">' . JText::sprintf('TXT_AUTHOR', CjLibUtils::escape($profile[$user_name])) . '</h4>
						<div style="margin: 5px 0;">' . CjBlogApi::get_user_badges_markup($profile) . '</div>
						' . (!empty($profile[$user_name] && $showProfile) ? $about : '') . '
					</div>
				</div>
				<input id="cjblog_page_id" value="article" type="hidden">
			</div>
		</div>';
        $article->text = '<div id="cj-wrapper">' . $html_top . $article->text . $html_bottom . '</div>';
    }
示例#2
0
    public function do_create_or_update_response($create_response = true)
    {
        $app = JFactory::getApplication();
        $user = JFactory::getUser();
        $config = JComponentHelper::getParams(S_APP_NAME);
        $sid = $app->input->getInt('id', 0);
        $key = trim($app->input->getCmd('key', null));
        $skey = trim($app->input->getCmd('skey', null));
        $response_id = 0;
        $obj_response = null;
        $survey = new stdClass();
        $survey->error = false;
        $user_id = $user->id;
        if (empty($skey) && empty($sid) && empty($key)) {
            $this->setError('Error: 10301 - ' . JText::_('MSG_SURVEY_NOT_FOUND'));
            $survey->error = 1;
            return $survey;
        }
        if (!empty($key)) {
            $query = 'select survey_id, user_id from #__survey_keys where key_name=' . $this->_db->quote($key);
            $this->_db->setQuery($query);
            $keyObj = $this->_db->loadObject();
            if ($keyObj) {
                $sid = $keyObj->survey_id;
                if (!empty($keyObj->user_id)) {
                    $user_id = $keyObj->user_id;
                }
            }
        } else {
            if (!empty($skey)) {
                $query = 'select id from #__survey where survey_key=' . $this->_db->quote($skey);
                $this->_db->setQuery($query);
                $sid = $this->_db->loadResult();
            }
        }
        // Get the survey.
        $survey = $this->get_survey_details($sid, 0, false, true, true);
        $survey->error = false;
        $survey->skey = $skey;
        $survey->key = $key;
        if (empty($survey) || !$survey->id) {
            // Looks like a deleted survey or some error occurred
            $this->setError('Error: 10305 - ' . JText::_('MSG_ERROR_PROCESSING'));
            $survey = new stdClass();
            $survey->error = 2;
            return $survey;
        }
        if ($survey->published != 1) {
            $survey->error = 3;
            return $survey;
        }
        if (!$user->authorise('core.respond', S_APP_NAME . '.category.' . $survey->catid)) {
            $survey->error = $user->guest ? 4 : 5;
            return $survey;
        }
        if ($survey->private_survey == '1' && empty($key) && empty($skey)) {
            $this->setError(JText::_('MSG_PRIVATE_SURVEY_WITH_NO_KEY') . '| SKey: ' . $skey . '| Key' . $key);
            $survey->error = 6;
            return $survey;
        }
        if ($survey->publish_up != '0000-00-00 00:00:00') {
            $date = JFactory::getDate($survey->publish_up);
            $compareTo = JFactory::getDate();
            if ($compareTo->toUnix() - $date->toUnix() < 0) {
                $this->setError(JText::_('MSG_SURVEY_NOT_YET_UP'));
                $survey->error = 7;
                return $survey;
            }
        }
        if ($survey->publish_down != '0000-00-00 00:00:00') {
            $date = JFactory::getDate($survey->publish_down);
            $compareTo = JFactory::getDate();
            if ($compareTo->toUnix() - $date->toUnix() > 0) {
                $this->setError(JText::_('MSG_SURVEY_CLOSED'));
                $survey->error = 8;
                return $survey;
            }
        }
        // 		if(!$this->checkUserGroupsLimit($survey->id))
        // 		{
        // 			$survey->error = 19;
        // 			return $survey;
        // 		}
        if ($create_response == false) {
            // do not create or check for the response
            return $survey;
        }
        if (!empty($key)) {
            $query = 'select id, completed, created, completed > created as completed, survey_key, created_by ' . 'from #__survey_responses where survey_key=' . $this->_db->quote($key) . ' order by created desc';
            $this->_db->setQuery($query);
            $obj_response = $this->_db->loadObject();
            if (!empty($obj_response) && $obj_response->completed == 1) {
                $this->setError('Error: 10304 - ' . JText::_('MSG_SURVEY_ALREADY_TAKEN') . (S_DEBUG_ENABLED ? print_r($obj_response, true) : ''));
                $survey->response_id = $obj_response->id;
                $survey->error = 9;
                return $survey;
            }
        } else {
            // now let us find the response id based on the restriction method used
            if (empty($obj_response) && strpos($survey->restriction, 'cookie') !== false) {
                //now we have the sid and we need response id, first check if there is a key in cookie with the sid
                $cookieName = trim(CJFunctions::get_hash($app->getName() . S_COOKIE_PREFIX . $sid));
                $cookie_key = trim(JRequest::getVar($cookieName, null, 'COOKIE', 'CMD'));
                if (!empty($cookie_key)) {
                    $query = 'select id, completed > created as completed, survey_key, created_by from #__survey_responses where survey_key=' . $this->_db->quote($cookie_key);
                    $this->_db->setQuery($query);
                    $obj_response = $this->_db->loadObject();
                }
                if (!empty($obj_response) && $obj_response->completed == 1) {
                    $this->setError('Error: 103041 - ' . JText::_('MSG_SURVEY_ALREADY_TAKEN') . (S_DEBUG_ENABLED ? print_r($obj_response, true) : ''));
                    $survey->response_id = $obj_response->id;
                    $survey->error = 10;
                    return $survey;
                }
            }
            if (empty($obj_response) && strpos($survey->restriction, 'ip') !== false) {
                $ip_address = CJFunctions::get_user_ip_address();
                $query = '
						select
							id, completed > created as completed, survey_key, created_by
						from
							#__survey_responses
						where
							survey_id=' . $sid . ' and ip_address=' . $this->_db->quote($ip_address);
                $this->_db->setQuery($query);
                $obj_response = $this->_db->loadObject();
                if (!empty($obj_response) && $obj_response->completed == 1) {
                    $this->setError('Error: 103042 - ' . JText::_('MSG_SURVEY_ALREADY_TAKEN'));
                    $survey->error = 11;
                    return $survey;
                }
            }
            if (!$user->guest && empty($obj_response)) {
                $query = '
						select
							id, completed > created as completed, survey_key, created_by
						from
							#__survey_responses
						where
							survey_id=' . $sid . ' and created_by=' . $user->id . '
						order by
							created desc';
                $this->_db->setQuery($query);
                $obj_response = $this->_db->loadObject();
                if (!empty($obj_response) && $obj_response->completed == 1) {
                    if (strpos($survey->restriction, 'username') !== false) {
                        $this->setError('Error: 103043 - ' . JText::_('MSG_SURVEY_ALREADY_TAKEN'));
                        $survey->error = 12;
                        return $survey;
                    } else {
                        $obj_response = null;
                    }
                }
            }
        }
        if (!empty($obj_response)) {
            if ($survey->anonymous == 1 || $obj_response->created_by == $user_id) {
                $response_id = $obj_response->id;
                $key = $obj_response->survey_key;
            }
        } else {
            if (!empty($key)) {
                // new survey with created key from invite page. no response should exist with this key so new response is created next
                // check if this key is legimate
                $query = 'select count(*) from #__survey_keys where key_name = ' . $this->_db->quote($key);
                $this->_db->setQuery($query);
                $count = $this->_db->loadResult();
                if ($count <= 0) {
                    $this->setError('Error: 103044 - ' . JText::_('MSG_ERROR_PROCESSING'));
                    $survey->error = 13;
                    return $survey;
                }
            }
        }
        if (!$response_id || empty($key)) {
            // No response id, so there is no way to track if he has responded. Create a survey response now.
            $query = 'select count(*) from #__survey_responses where survey_id=' . $sid;
            $this->_db->setQuery($query);
            $max_responses = $this->_db->loadResult();
            if ($survey->max_responses > 0 && $survey->max_responses <= $max_responses) {
                $this->setError(JText::_('MSG_EXCEED_RESPONSE_LIMIT'));
                $survey->error = 14;
                return $survey;
            }
            if (!$key) {
                $key = $this->create_survey_keys($sid, 1, false, true);
            }
            if (empty($key) || count($key) == 0) {
                $this->setError(JText::_('MSG_NO_CREDITS'));
                $survey->error = 15;
                return $survey;
            } else {
                $key = is_array($key) ? $key[0] : $key;
                $cookieName = CJFunctions::get_hash($app->getName() . S_COOKIE_PREFIX . $sid);
                $expire = time() + 60 * 60 * 24 * intval($config->get('cookie_expiration_days', 365));
                setcookie($cookieName, $key, $expire, '/');
            }
            $createdate = JFactory::getDate()->toSql();
            $ip_address = CJFunctions::get_user_ip_address();
            $location = CJFunctions::get_user_location($ip_address);
            $browser = CJFunctions::get_browser();
            if ($survey->anonymous) {
                $query = 'insert into #__survey_responses(survey_id, created, survey_key, country, browser, os) ' . 'values (' . $sid . ',' . $this->_db->quote($createdate) . ',' . $this->_db->quote($key) . ',' . $this->_db->quote($location['country_code']) . ',' . $this->_db->quote($browser['name'] . ' ' . $browser['version']) . ',' . $this->_db->quote($browser['platform']) . ')';
            } else {
                $query = 'insert into #__survey_responses(survey_id, created, survey_key, country, city, created_by, ip_address, browser, os) values (' . $sid . ',' . $this->_db->quote($createdate) . ',' . $this->_db->quote($key) . ',' . $this->_db->quote($location['country_code']) . ',' . $this->_db->quote($location['city']) . ',' . $user_id . ',' . $this->_db->quote($ip_address) . ',' . $this->_db->quote($browser['name'] . ' ' . $browser['version']) . ',' . $this->_db->quote($browser['platform']) . ')';
            }
            $this->_db->setQuery($query);
            if ($this->_db->query()) {
                $response_id = $this->_db->insertid();
                $query = '
					update
						#__survey_keys
					set
						response_id=' . $response_id . ',
						response_status = 2
					where
						key_name=' . $this->_db->quote($key) . ' and survey_id=' . $sid;
                $this->_db->setQuery($query);
                if (!$this->_db->query()) {
                    $this->setError('Error: 10310 - ' . JText::_('MSG_ERROR_PROCESSING'));
                    $survey->error = 16;
                    return $survey;
                }
            } else {
                $this->setError('Error: 10311 - ' . JText::_('MSG_ERROR_PROCESSING') . (S_DEBUG_ENABLED ? $this->_db->getErrorMsg() : ''));
                $survey->error = 17;
                return $survey;
            }
        }
        $survey->response_id = $response_id;
        $survey->key = $key;
        if (!$survey->response_id || empty($survey->key)) {
            $survey->error = 18;
        }
        return $survey;
    }
示例#3
0
 function rate_article()
 {
     $user = JFactory::getUser();
     if (!$user->authorise('articles.rate', CJBLOG)) {
         echo json_encode(array('error' => JText::_('JERROR_ALERTNOAUTHOR')));
     } else {
         $app = JFactory::getApplication();
         $id = $app->input->getInt('id');
         $rating = $app->input->getFloat('rating');
         if ($id > 0 && $rating > 0) {
             $return = CJFunctions::store_rating(CJBLOG_ASSET_ID, $id, $rating, $user->id);
             if ($return) {
                 $hash = CJFunctions::get_hash('com_content.article.rating.item_' . $id);
                 $domain = JRoute::_(ContentHelperRoute::getArticleRoute($id));
                 $app->input->cookie->set($hash, 1, time() + 60 * 60 * 24 * 365, $domain);
                 echo json_encode(array('data' => JText::_('MSG_THANK_YOU_FOR_RATING')));
             } else {
                 if ($return == -1) {
                     echo json_encode(array('error' => JText::_('MSG_ALREADY_RATED')));
                 } else {
                     echo json_encode(array('error' => JText::_('MSG_ERROR_PROCESSING') . ' - Error Code: 1.'));
                 }
             }
         } else {
             echo json_encode(array('error' => ''));
         }
     }
     jexit();
 }