Your IP : 10.10.0.253


Current Path : /var/www/components/com_contactpush/controllers/
Upload File :
Current File : /var/www/components/com_contactpush/controllers/message.php

<?php
/**                               ______________________________________________
*                          o O   |                                              |
*                 (((((  o      <    Generated with Cook Self Service  V3.1     |
*                ( o o )         |______________________________________________|
* --------oOOO-----(_)-----OOOo---------------------------------- www.j-cook.pro --- +
* @version		1.0.0
* @package		Contact Push
* @subpackage	Messages
* @copyright	Netamity 2017
* @author		Andy Hickey - www.netamity.com - andy@netamity.com
* @license		GPL V2+
*
*             .oooO  Oooo.
*             (   )  (   )
* -------------\ (----) /----------------------------------------------------------- +
*               \_)  (_/
*/

// no direct access
defined('_JEXEC') or die('Restricted access');



/**
* Contactpush Message Controller
*
* @package	Contactpush
* @subpackage	Message
*/
class ContactpushCkControllerMessage extends ContactpushClassControllerItem
{
	/**
	* The context for storing internal data, e.g. record.
	*
	* @var string
	*/
	protected $context = 'message';

	/**
	* The URL view item variable.
	*
	* @var string
	*/
	protected $view_item = 'message';

	/**
	* The URL view list variable.
	*
	* @var string
	*/
	protected $view_list = 'messages';

	/**
	* Constructor
	*
	* @access	public
	* @param	array	$config	An optional associative array of configuration settings.
	*
	* @return	void
	*/
	public function __construct($config = array())
	{
		parent::__construct($config);
		$app = JFactory::getApplication();

	}

	/**
	* Method to add an element.
	*
	* @access	public
	*
	* @return	void
	*/
	public function add()
	{
		JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
		$this->_result = $result = parent::add();
		$model = $this->getModel();

		//Define the redirections
		switch($this->getLayout() .'.'. $this->getTask())
		{
			case 'default.add':
				$this->applyRedirection($result, array(
					'stay',
					'com_contactpush.message.newmessage'
				), array(
			
				));
				break;

			default:
				$this->applyRedirection($result, array(
					'stay',
					'com_contactpush.message.newmessage'
				));
				break;
		}
	}

	/**
	* Override method when the author allowed to delete own.
	*
	* @access	protected
	* @param	array	$data	An array of input data.
	* @param	string	$key	The name of the key for the primary key; default is id..
	*
	* @return	boolean	True on success
	*/
	protected function allowDelete($data = array(), $key = id)
	{
		return parent::allowDelete($data, $key, array(
		'key_author' => 'created_by'
		));
	}

	/**
	* Override method when the author allowed to edit own.
	*
	* @access	protected
	* @param	array	$data	An array of input data.
	* @param	string	$key	The name of the key for the primary key; default is id..
	*
	* @return	boolean	True on success
	*/
	protected function allowEdit($data = array(), $key = id)
	{
		return parent::allowEdit($data, $key, array(
		'key_author' => 'created_by'
		));
	}

	/**
	* Method to cancel an element.
	*
	* @access	public
	* @param	string	$key	The name of the primary key of the URL variable.
	*
	* @return	void
	*/
	public function cancel($key = null)
	{
		$this->_result = $result = parent::cancel();
		$model = $this->getModel();

		//Define the redirections
		switch($this->getLayout() .'.'. $this->getTask())
		{
			case 'newmessage.cancel':
				$this->applyRedirection($result, array(
					'stay',
					'com_contactpush.messages.default'
				), array(
					'cid[]' => null
				));
				break;

			default:
				$this->applyRedirection($result, array(
					'stay',
					'com_contactpush.messages.default'
				));
				break;
		}
	}

	/**
	* Return the current layout.
	*
	* @access	protected
	* @param	bool	$default	If true, return the default layout.
	*
	* @return	string	Requested layout or default layout
	*/
	protected function getLayout($default = null)
	{
		if ($default === 'edit')
			return 'newmessage';

		if ($default)
			return 'newmessage';

		$jinput = JFactory::getApplication()->input;
		return $jinput->get('layout', 'newmessage', 'CMD');
	}

	/**
	* Method to save an element.
	*
	* @access	public
	* @param	string	$key	The name of the primary key of the URL variable.
	* @param	string	$urlVar	The name of the URL variable if different from the primary key (sometimes required to avoid router collisions).
	*
	* @return	void
	*/
	public function save($key = null, $urlVar = null)
	{
		JSession::checkToken() or JSession::checkToken('get') or jexit(JText::_('JINVALID_TOKEN'));
		//Check the ACLs
		$model = $this->getModel();
		$item = $model->getItem();
		$result = false;
		if ($model->canEdit($item, true))
		{
			$result = parent::save();
			//Get the model through postSaveHook()
			if ($this->model)
			{
				$model = $this->model;
				$item = $model->getItem();	
			}
		}
		else
			JError::raiseWarning( 403, JText::sprintf('ACL_UNAUTORIZED_TASK', JText::_('CONTACTPUSH_JTOOLBAR_SAVE')) );

		$this->_result = $result;

		//send the Push message
        $this->sendPushoverMessage();

		//Define the redirections
		switch($this->getLayout() .'.'. $this->getTask())
		{
			case 'newmessage.save':
				$this->applyRedirection($result, array(
					'stay',
					'com_contactpush.messages.default'
				), array(
					'cid[]' => null
				));
				break;

			default:
				$this->applyRedirection($result, array(
					'stay',
					'com_contactpush.messages.default'
				));
				break;
		}
	}

	public function setMessageStatus($id, $response)
    {
        $db = JFactory::getDbo();
        $query = "UPDATE #__contactpush_messages SET status = ".$response." WHERE id=". $id;
        $db->setQuery($query);
        $db->execute();
        return;
    }

    public function getPushUsers($recipients)
    {
        $db = JFactory::getDbo();
        $query = "SELECT push_user_id FROM #__contactpush_pushuids WHERE id IN (". implode(',',$recipients).")";
        $db->setQuery($query);
        $n = $db->loadColumn();
        return implode(',',$n);
    }

    public function sendPushoverMessage()
    {
        $session = JFactory::getSession();
        $input = JFactory::getApplication()->input;

        $formData = new JRegistry($input->get('jform', '', 'array'));
        $recipients = $formData->get('pushuids');
        $session->set('seepost', $formData);

        $params = JComponentHelper::getParams('com_contactpush');
        $appid = $params->get('pushover_appid');
        $retry = $params->get('retry');
        $expire = $params->get('expire');
        $footer = $params->get('footer');
        $session->set('appid', $appid);

        $pids = $this->getPushUsers($recipients);
        $session->set('pids', $pids);

        curl_setopt_array($ch = curl_init(), array(
            CURLOPT_URL => "https://api.pushover.net/1/messages.json",
            CURLOPT_POSTFIELDS => array(
                "token" => $appid,
                "user" => $pids,
                "html" => 1,
                "title" => $formData->get('title'),
                "priority" => $formData->get('priority'),
                "message" => $formData->get('message') . "\r\n" . $footer,
                "retry" => $retry,
                "expire" => $expire,
            ),
            CURLOPT_SAFE_UPLOAD => true,
        ));
        curl_exec($ch);
        curl_close($ch);

        return;
    }


}

// Load the fork
ContactpushHelper::loadFork(__FILE__);

// Fallback if no fork has been found
if (!class_exists('ContactpushControllerMessage')){ class ContactpushControllerMessage extends ContactpushCkControllerMessage{} }