Your IP : 216.73.216.85


Current Path : /home/smartconb/www/armencom33/administrator/components/com_jmailalerts/models/
Upload File :
Current File : /home/smartconb/www/armencom33/administrator/components/com_jmailalerts/models/subscriber.php

<?php
/**
 * @package     JMailAlerts
 * @subpackage  com_jmailalerts
 *
 * @author      Techjoomla <extensions@techjoomla.com>
 * @copyright   Copyright (C) 2009 - 2018 Techjoomla. All rights reserved.
 * @license     GNU General Public License version 2 or later; see LICENSE.txt
 */

// No direct access.
defined('_JEXEC') or die;
jimport('joomla.application.component.modeladmin');

/**
 * Subscriber model class.
 *
 * @package     JMailAlerts
 * @subpackage  com_jmailalerts
 * @since       2.6.1
 */
class JmailalertsModelsubscriber extends JModelAdmin
{
	/**
	 * @var		string	The prefix to use with controller messages.
	 * @since	1.6
	 */
	protected $text_prefix = 'COM_JMAILALERTS';

	/**
	 * Constructor.
	 *
	 * @param   array  $config  An optional associative array of configuration settings.
	 *
	 * @since   3.2
	 */
	public function __construct($config = array())
	{
		$config['event_after_delete'] = 'jmaOnAfterAlertSubscriptionDelete';

		parent::__construct($config);
	}

	/**
	 * Returns a Table object, always creating it.
	 *
	 * @param   string  $type    The table type to instantiate
	 * @param   string  $prefix  A prefix for the table class name. Optional.
	 * @param   array   $config  Configuration array for model. Optional.
	 *
	 * @return  JTable    A database object
	 */
	public function getTable($type = 'Subscriber', $prefix = 'JmailalertsTable', $config = array())
	{
		return JTable::getInstance($type, $prefix, $config);
	}

	/**
	 * Method to get the record form.
	 *
	 * @param   array    $data      Data for the form.
	 * @param   boolean  $loadData  True if the form is to load its own data (default case), false if not.
	 *
	 * @return  JForm|boolean  A JForm object on success, false on failure
	 *
	 * @since   1.6
	 */
	public function getForm($data = array(), $loadData = true)
	{
		// Initialise variables.
		$app	= JFactory::getApplication();

		// Get the form.
		$form = $this->loadForm('com_jmailalerts.subscriber', 'subscriber', array('control' => 'jform', 'load_data' => $loadData));

		if (empty($form))
		{
			return false;
		}

		return $form;
	}

	/**
	 * Method to get the data that should be injected in the form.
	 *
	 * @return  mixed  The data for the form.
	 *
	 * @since   1.6
	 */
	protected function loadFormData()
	{
		// Check the session for previously entered form data.
		$data = JFactory::getApplication()->getUserState('com_jmailalerts.edit.subscriber.data', array());

		if (empty($data))
		{
			$data = $this->getItem();
		}

		return $data;
	}

	/**
	 * Method to get a single record.
	 *
	 * @param   integer  $pk  The id of the primary key.
	 *
	 * @return  mixed  Object on success, false on failure.
	 */
	public function getItem($pk = null)
	{
		if ($item = parent::getItem($pk))
		{
			// Do any procesing on fields here if needed
		}

		return $item;
	}

	/**
	 * Prepare and sanitise the table data prior to saving.
	 *
	 * @param   JTable  $table  A JTable object.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	protected function prepareTable($table)
	{
		jimport('joomla.filter.output');

		if (empty($table->id))
		{
			// Set ordering to the last item if not set

			if (@$table->ordering === '')
			{
				$db = JFactory::getDbo();
				$db->setQuery('SELECT MAX(ordering) FROM #__jma_subscribers');
				$max = $db->loadResult();
				$table->ordering = $max + 1;
			}
		}
	}

	/**
	 * Method Preview.
	 *
	 * @return  void
	 *
	 * @since   1.6
	 */
	public function preview()
	{
		$input = JFactory::getApplication()->input;
		jimport('joomla.filesystem.file');
		require_once JPATH_SITE . '/components/com_jmailalerts/models/emails.php';
		require_once JPATH_SITE . '/components/com_jmailalerts/helpers/emailhelper.php';
		$jmailalertsemailhelper    = new jmailalertsemailhelper;
		$jmailalertsModelEmails    = new jmailalertsModelEmails;
		$nofrm                     = 0;
		$today                     = $input->get('select_date_box');
		$email_status              = 0;
		$target_user_id            = $input->get('user_id', '', 'INT');
		$alert_type_id             = $input->get('alert_id', '', 'INT');
		$target_email_id           = $input->get('email_id', '', 'STRING');
		$destination_email_address = $input->get('send_mail_to_box', '', 'STRING');
		$flag                      = $input->get('flag');

		// For registered user
		if ($target_user_id)
		{
			$query = "SELECT  u.id as user_id,u.name,u.email as email_id,a.template, a.email_subject, e.date,
			e.alert_id ,a.template_css, e.plugins_subscribed_to, a.respect_last_email_date"
					. " FROM #__users AS u, #__jma_subscribers AS e , #__jma_alerts AS a"
					. " WHERE e.user_id = " . $target_user_id
					. " AND e.alert_id = " . $alert_type_id
					. " AND u.id = e.user_id"
					. " AND a.id = e.alert_id";
		}
		// Guest user
		else
		{
			$query = "SELECT  e.user_id as user_id,e.name,e.email_id as email_id,
							a.template, a.email_subject,
							e.date, e.alert_id ,a.template_css, e.plugins_subscribed_to, a.respect_last_email_date"
					. " FROM #__jma_subscribers AS e , #__jma_alerts AS a"
					. " WHERE e.email_id = '" . $target_email_id
					. "' AND e.alert_id = " . $alert_type_id
					. " AND a.id = e.alert_id";
		}

		$this->_db->setQuery($query);
		$target_user_data = $this->_db->loadObjectList();

		$i = 0;

		foreach ($target_user_data as $data)
		{
			if ($data->date)
			{
				/*$data[$i]->date = $today;*/
				$data->date = ($today) ? $today:$data->date;
			}
			else
			{
				$data[$i]->date = ($today) ? $today:$data[$i]->date;
			}

			$i++;
		}

		if ($target_user_data)
		{
			$target_user_data[0]->email = $destination_email_address;

			// Get template from alert type
			$query = "SELECT template FROM #__jma_alerts WHERE id =$alert_type_id ";
			$this->_db->setQuery($query);
			$msg_body = $this->_db->loadResult();
			$skip_tags = array('[SITENAME]', '[NAME]', '[SITELINK]', '[PREFRENCES]', '[mailuser]');
			$tmpl_tags = $jmailalertsModelEmails->get_tmpl_tags($msg_body, $skip_tags);
			$remember_tags = $jmailalertsModelEmails->get_original_tmpl_tags($msg_body, $skip_tags);
			$response = $jmailalertsemailhelper->getMailcontent($target_user_data[0], $flag, $tmpl_tags, $remember_tags);

			if ($response[1] == 3)
			{
				echo JText::_('COM_JMAILALERTS_NO_MAIL_CONTENT');

				return;
			}

			return $response;
		}
	}

	/**
	 * Method to save an subscription data.
	 *
	 * @param   array  $data  data
	 *
	 * @return  mixed  Id on success and false on failure
	 *
	 * @since    1.6
	 */
	public function save($data)
	{
		if (!empty($data))
		{
			$isNew = true;
			$table = $this->getTable();

			if ($data['id'] != 0)
			{
				$isNew = false;
			}

			// Bind data
			if (!$table->bind($data))
			{
				$this->setError($table->getError());

				return false;
			}

			if (parent::save($data))
			{
				$id = (int) $this->getState($this->getName() . '.id');
				$data['subscriptionId'] = $id;

				$dispatcher = JDispatcher::getInstance();
				JPluginHelper::importPlugin('jmailalert');
				$dispatcher->trigger('jmaOnAfterAlertSubscriptionSave', array($data, $isNew));

				return $id;
			}
			else
			{
				return false;
			}
		}
	}
}