Your IP : 216.73.216.85


Current Path : /home/smartconb/www/armencom33/plugins/emailalerts/jma_latestnews_js/
Upload File :
Current File : /home/smartconb/www/armencom33/plugins/emailalerts/jma_latestnews_js/jma_latestnews_js.php

<?php
/**
 * @version     SVN: <svn_id>
 * @package     JMailAlerts
 * @subpackage  jma_latestnews_js
 * @author      Techjoomla <extensions@techjoomla.com>
 * @copyright   Copyright (c) 2009-2015 TechJoomla. All rights reserved.
 * @license     GNU General Public License version 2 or later.
 */

// No direct access.
defined('_JEXEC') or die();

jimport('joomla.plugin.plugin');

// Load language file for plugin frontend
$lang = JFactory::getLanguage();
$lang->load('plg_emailalerts_jma_latestnews_js', JPATH_ADMINISTRATOR);

// Include plugin helper file.
$jma_helper = JPATH_SITE . '/components/com_jmailalerts/helpers/plugins.php';

if (JFile::exists($jma_helper))
{
	include_once $jma_helper;
}
// This is needed when JMA integration plugin is used on sites where JMA is not installed.
else
{
	$jma_integration_helper = JPATH_SITE . '/plugins/system/plg_sys_jma_integration/plg_sys_jma_integration/plugins.php';

	if (JFile::exists($jma_integration_helper))
	{
		include_once $jma_integration_helper;
	}
}

/**
 * Latest news plugin for JMailAlerts Component.
 *
 * @package     JMailAlerts
 * @subpackage  Emailalerts.LatestNews
 * @since       2.5.1
 */
class PlgEmailalertsJma_Latestnews_Js extends JPlugin
{
	/**
	 * Constructor
	 *
	 * @param   object  &$subject  The object to observe
	 * @param   array   $config    An array that holds the plugin configuration
	 *
	 * @since   2.5
	 */
	public function __construct(&$subject, $config)
	{
		parent::__construct($subject, $config);
		$this->loadLanguage();

		if ($this->params === false)
		{
			$jPlugin = JPluginHelper::getPlugin('emailalerts', 'jma_latestnews_js');
			$this->params = new JParameter($jPlugin->params);
		}
	}

	/**
	 * Method to get the HTML and CSS output for this plugin.
	 *
	 * @param   string   $id                 The userid or email of the user to whom email is being sent.
	 * @param   string   $date               The date when last email alert of which this plugins is part of was sent to this user.
	 * @param   array    $userparam          The final array of plugin parameters which are derieved
	 * in the order of increasing priority of - plugin params, user params, data tags.
	 * @param   integer  $fetch_only_latest  Parameter that decides if only the latest content is to be sent out in the email.
	 *
	 * @return  array
	 *
	 * @since   2.5.1
	 */
	public function onEmail_jma_latestnews_js($id, $date, $userparam, $fetch_only_latest)
	{
		$areturn = array();

		// If no userid or no guest user, return blank array for html and css.
		if ($id === null)
		{
			$areturn[0] = $this->_name;
			$areturn[1] = '';
			$areturn[2] = '';

			return $areturn;
		}

		$list = $this->getList($id, $date, $userparam, $fetch_only_latest);

		$areturn[0] = $this->_name;

		if (empty($list))
		{
			// If no output is found, return array with 2 indexes with NO values.
			$areturn[1] = '';
			$areturn[2] = '';
		}
		else
		{
			// Get all plugin parameters in the variable, this will be passed to plugin helper function.
			$plugin_params = $this->params;

			// Create object for helper class.
			$helper = new pluginHelper;

			// Call helper function to get plugin layout.
			$ht         = $helper->getLayout($this->_name, $list, $plugin_params);
			$areturn[1] = $ht;

			// Call helper function to get plugin CSS layout path
			$cssfile    = $helper->getCSSLayoutPath($this->_name, $plugin_params);
			$cssdata    = file_get_contents($cssfile);
			$areturn[2] = $cssdata;
		}

		return $areturn;
	}

	/**
	 * Method to get the list of articles based on user preferences.
	 *
	 * @param   string   $id                 The userid or email of the user to whom email is being sent.
	 * @param   string   $last_alert_date    The date when last email alert of which this plugins is part of was sent to this user.
	 * @param   array    $userparam          The final array of plugin parameters which are derieved
	 * in the order of increasing priority of - plugin params, user params, data tags.
	 * @param   integer  $fetch_only_latest  Parameter that decides if only the latest content is to be sent out in the email.
	 *
	 * @return  array
	 *
	 * @since   2.5.1
	 */
	private function getList($id, $last_alert_date, $userparam, $fetch_only_latest)
	{
		if ($id === null)
		{
			return false;
		}

		require_once JPATH_SITE . DS . 'components' . DS . 'com_content' . DS . 'helpers' . DS . 'route.php';

		$mainframe = JFactory::getApplication();
		$db        = JFactory::getDBO();

		if ($id === 0)
		{
			$user    = JFactory::getUser($id);
			$userId  = 0;
		}
		else
		{
			$user   = JFactory::getUser($id);
			$userId = (int) $user->get('id');
		}

		// Get all plugin parameters in the variable, these will be passed to plugin helper function.
		$plugin_params = $this->params;

		$count   = (int) $this->params->get('count');
		$catid   = $this->params->get('catid', '');
		$author = (int) $this->params->get('user_id', '');

		if ($id != null)
		{
			// Get user preferences for this plugin parameters(shown in frontend).
			if (isset($userparam['count']))
			{
				$count = (int) $userparam['count'];
			}

			if (isset($userparam['catid']))
			{
				$catid = trim($userparam['catid']);
			}

			if (isset($userparam['user_id']))
			{
				$author = $userparam['user_id'];
			}
		}

		// Get plugin parameters (not shown in frontend).
		$ordering          = $this->params->get('ordering');
		$show_front        = $this->params->get('show_front', 0);
		$introtext_count   = (int) $this->params->get('introtext_count', 200);
		$show_introtext    = (int) $this->params->get('show_introtext', 0);
		$show_date         = (int) $this->params->get('show_date', 0);
		$show_author       = (int) $this->params->get('show_author', 0);
		$show_author_alias = (int) $this->params->get('show_author_alias', 0);
		$show_category     = (int) $this->params->get('show_category', 0);

		$contentConfig = JComponentHelper::getParams('com_content');
		$access       = !$contentConfig->get('show_noauth');

		$nullDate = $db->getNullDate();
		$date     = JFactory::getDate();
		$now      = $date->toSql();

		$replace = JUri::root();

		// Date filter
		$where = 'a.state = 1'
		. ' AND ( a.publish_up = ' . $db->Quote($nullDate) . ' OR a.publish_up <= ' . $db->Quote($now) . ' )'
		. ' AND ( a.publish_down = ' . $db->Quote($nullDate) . ' OR a.publish_down >= ' . $db->Quote($now) . ' )';

		// Author Filter
		switch ($author)
		{
			case 'by_me':
				if ($userId)
				{
					$where .= ' AND (created_by = ' . (int) $userId . ' OR modified_by = ' . (int) $userId . ')';
				}
				break;

			case 'not_me':
				$where .= ' AND (created_by <> ' . (int) $userId . ' AND modified_by <> ' . (int) $userId . ')';
				break;
		}

		// Ordering
		if ($show_category)
		{
			// First sort by category name
			$where_ordering = ' category ASC, ';
		}
		else
		{
			$where_ordering = ' ';
		}

		switch ($ordering)
		{
			case 'm_dsc':
				$where_ordering .= ' a.modified DESC';
				break;

			case 'c_dsc':
			default:
				$where_ordering .= ' a.created DESC';
				break;
		}

		// Category filter
		if ($catid)
		{
			$ids = explode(',', $catid);
			JArrayHelper::toInteger($ids);
			$catCondition = ' AND (cc.id=' . implode(' OR cc.id=', $ids) . ')';
		}

		// Introtext filter
		$intro = '';

		if ($show_introtext)
		{
			$intro = " a.introtext AS intro, ";
		}

		// Get content items/articles
		$groups   = implode(',', $user->getAuthorisedViewLevels());
		$checkacc = ' a.access IN (' . $groups . ')';

		$query = 'SELECT ' . $intro . ' a.id, a.catid, a.title, a.created, a.created_by_alias, u.name, u.username, cc.access, cc.title as category, ' .
		' CASE WHEN CHAR_LENGTH(a.alias) THEN CONCAT_WS(":", a.id, a.alias) ELSE a.id END as slug, ' .
		' CASE WHEN CHAR_LENGTH(cc.alias) THEN CONCAT_WS(":", cc.id, cc.alias) ELSE cc.id END as catslug ' .
		' FROM #__content AS a ' .
		' LEFT JOIN #__users AS u ON u.id=a.created_by ' .
		($show_front == '0' ? ' LEFT JOIN #__content_frontpage AS f ON f.content_id = a.id' : '') .
		' INNER JOIN #__categories AS cc ON cc.id = a.catid' .
		' WHERE ' . $where .
		($access ? ' AND ' . $checkacc : '') .
		($catid ? $catCondition : '') .
		($show_front == '0' ? ' AND f.content_id IS null ' : '') .
		' AND cc.published = 1';

		// Get only the fresh content
		if ($fetch_only_latest)
		{
			$query .= " AND a.created >= ";
			$query .= $db->Quote($last_alert_date);
		}

		// Sort by
		$query .= ' ORDER BY ' . $where_ordering;

		// Use user's preferred value for count
		$db->setQuery($query, 0, $count);
		$rows = $db->loadObjectList();

		// No output.
		if (!$rows)
		{
			return false;
		}

		// Create object for helper class
		$helper = new pluginHelper;

		// Call plugin function to sort output by category
		// $rows = $helper->multi_d_sort($rows, 'catid', 0);

		$i = 0;
		$lists = array();

		// If email is previewed from backend, do not generate sef urls as it won't work
		if ($mainframe->isAdmin())
		{
			foreach ($rows as $row)
			{
				$lists[$i] = new stdclass;

				$lists[$i]->link  = JRoute::_($replace . ContentHelperRoute::getArticleRoute($row->slug, $row->catslug));
				$lists[$i]->link  = str_replace("&", "&amp;", $lists[$i]->link);
				$lists[$i]->title = htmlspecialchars($row->title);

				if ($show_author_alias && $row->created_by_alias)
				{
					$lists[$i]->author = htmlspecialchars($row->created_by_alias);
				}
				else
				{
					$lists[$i]->author = htmlspecialchars($row->name);
				}

				$lists[$i]->date     = htmlspecialchars($row->created);
				$lists[$i]->catid    = htmlspecialchars($row->catid);
				$lists[$i]->category = htmlspecialchars($row->category);

				if ($show_introtext)
				{
					$lists[$i]->intro = substr(strip_tags($row->intro), 0, $introtext_count) . " ...";
				}

				$i++;
			}
		}
		// If email is previewed/generated from frontend, generate sef urls.
		else
		{
			foreach ($rows as $row)
			{
				$lists[$i] = new stdclass;

				$lists[$i]->link = JUri::root() .
				substr(JRoute::_(ContentHelperRoute::getArticleRoute($row->slug, $row->catslug)), strlen(JUri::base(true)) + 1);
				$lists[$i]->title = htmlspecialchars($row->title);

				if ($show_author_alias && $row->created_by_alias)
				{
					$lists[$i]->author = htmlspecialchars($row->created_by_alias);
				}
				else
				{
					$lists[$i]->author = htmlspecialchars($row->name);
				}

				$lists[$i]->date     = htmlspecialchars($row->created);
				$lists[$i]->catid    = htmlspecialchars($row->catid);
				$lists[$i]->category = htmlspecialchars($row->category);

				if ($show_introtext)
				{
					$lists[$i]->intro = substr(strip_tags($row->intro), 0, $introtext_count) . " ...";
				}

				$i++;
			}
		}

		return $lists;
	}
}