Your IP : 10.10.0.253


Current Path : /var/www/components/com_contactpush/models/
Upload File :
Current File : /var/www/components/com_contactpush/models/messages.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 List Model
*
* @package	Contactpush
* @subpackage	Classes
*/
class ContactpushCkModelMessages extends ContactpushClassModelList
{
	/**
	* The URL view item variable.
	*
	* @var string
	*/
	protected $view_item = 'message';

	/**
	* Constructor
	*
	* @access	public
	* @param	array	$config	An optional associative array of configuration settings.
	*
	* @return	void
	*/
	public function __construct($config = array())
	{
		//Define the sortables fields (in lists)
		if (empty($config['filter_fields'])) {
			$config['filter_fields'] = array(
				'creation_date', 'creation_date',
				'_created_by_name', 'created_by.name',
				'title', 'title',
				'priority', 'priority',
				'status', 'status',

			);
		}

		//Define the filterable fields
		$this->set('filter_vars', array(
			'published' => 'cmd',
			'sortTable' => 'cmd',
			'directionTable' => 'cmd',
			'limit' => 'cmd',
			'created_by' => 'cmd',
			'creation_date_from' => 'varchar',
			'creation_date_to' => 'varchar'
				));

		//Define the searchable fields
		$this->set('search_vars', array(
			'search' => 'string'
				));


		parent::__construct($config);

		$this->hasOne('created_by', // name
			'.users', // foreignModelClass
			'created_by', // localKey
			'id' // foreignKey
		);

		$this->hasOne('access', // name
			'.viewlevels', // foreignModelClass
			'access', // localKey
			'id' // foreignKey
		);

		$this->belongsToMany('pushuids', // name
			'pushuids', // foreignModelClass
			'id', // localKey
			'id', // foreignKey,
			'messagerecipients', // pivotModelClass,
			'message', // pivotLocalKey
			'user' // pivotForeignKey
		);
	}

	/**
	* Method to get the layout (including default).
	*
	* @access	public
	*
	* @return	string	The layout alias.
	*/
	public function getLayout()
	{
		$jinput = JFactory::getApplication()->input;
		return $jinput->get('layout', 'default', 'STRING');
	}

	/**
	* Method to get a store id based on model configuration state.
	* 
	* This is necessary because the model is used by the component and different
	* modules that might need different sets of data or differen ordering
	* requirements.
	*
	* @access	protected
	* @param	string	$id	A prefix for the store id.
	*
	*
	* @since	1.6
	*
	* @return	void
	*/
	protected function getStoreId($id = '')
	{
		// Compile the store id.

		$id	.= ':'.$this->getState('sortTable');
		$id	.= ':'.$this->getState('directionTable');
		$id	.= ':'.$this->getState('limit');
		$id	.= ':'.$this->getState('filter.created_by');
		$id	.= ':'.$this->getState('filter.creation_date');
		$id	.= ':'.$this->getState('search.search');
		$id	.= ':'.$this->getState('filter.published');
		return parent::getStoreId($id);
	}

	/**
	* Preparation of the list query.
	*
	* @access	protected
	* @param	object	&$query	returns a filled query object.
	*
	* @return	void
	*/
	protected function prepareQuery(&$query)
	{
		//FROM : Main table
		$query->from('#__contactpush_messages AS a');

		// Primary Key is always required
		$this->addSelect('a.id');


		switch($this->getState('context', 'all'))
		{
			case 'layout.default':

				$this->orm->select(array(
					'created_by',
					'created_by.name',
					'creation_date',
					'message',
					'priority',
					'status',
					'title'
				));
				break;

			case 'all':
				//SELECT : raw complete query without joins
				$this->addSelect('a.*');

				// Disable the pagination
				$this->setState('list.limit', null);
				$this->setState('list.start', null);
				break;
		}

		// SELECT required fields for all profiles
		$this->orm->select(array(
			'access',
			'created_by',
			'published'
		));

		// ACCESS : Restricts accesses over the local table
		$this->orm->access('a', array(
			'access' => 'access',
			'publish' => 'published',
			'author' => 'created_by'
		));

        if(!$this->isAdmin())
        {
            if($this->getPrivyList())
            {
                $this->addWhere("( a.id IN (" . $this->getPrivyList() . ") || a.created_by = " . $this->myPushId() . ")");
            }
            else
            {
                $this->addWhere("a.created_by = " . $this->myPushId());
            }

        }

		// FILTER : Created By > Name
		if($filter_created_by = $this->getState('filter.created_by'))
		{
			if ($filter_created_by == 'auto'){
				$this->addWhere('a.created_by = ' . (int)JFactory::getUser()->get('id'));
			}
			else 
			if ($filter_created_by > 0){
				$this->addWhere("a.created_by = " . (int)$filter_created_by);
			}
		}

		// FILTER (Range) : Creation date
		if($filter_creation_date_from = $this->getState('filter.creation_date_from'))
		{
			if ($filter_creation_date_from !== null){
				$this->addWhere("a.creation_date >= " . $this->_db->Quote($filter_creation_date_from));
			}
		}

		// FILTER (Range) : Creation date
		if($filter_creation_date_to = $this->getState('filter.creation_date_to'))
		{
			if ($filter_creation_date_to !== null){
				$this->addWhere("a.creation_date <= " . $this->_db->Quote($filter_creation_date_to));
			}
		}

		// SEARCH : Search...
		$this->orm->search('search', array(
			'on' => array(
				'title' => 'like',
				'message' => 'like'
			)
		));

		//WHERE - FILTER : Publish state
		$published = $this->getState('filter.published');
		if (is_numeric($published))
		{
			$allowAuthor = '';
			if (($published == 1) && !$acl->get('core.edit.state')) //ACL Limit to publish = 1
			{
				//Allow the author to see its own unpublished/archived/trashed items
				if ($acl->get('core.edit.own') || $acl->get('core.view.own'))
					$allowAuthor = ' OR a.created_by = ' . (int)JFactory::getUser()->get('id');
			}
			$query->where('(a.published = ' . (int) $published . $allowAuthor . ')');
		}
		elseif (!$published)
		{
			$query->where('(a.published = 0 OR a.published = 1 OR a.published IS NULL)');
		}

		// ORDERING
		$orderCol = $this->getState('list.ordering', 'created_by');
		$orderDir = $this->getState('list.direction', 'ASC');

		if ($orderCol)
			$this->orm->order(array($orderCol => $orderDir));

        $query->order('a.creation_date DESC');


		// Apply all SQL directives to the query
		$this->applySqlStates($query);

		//die($query);
	}

    public function isAdmin()
    {
        $user  = JFactory::getUser();
        $user_groups = $user->groups;
        if(in_array('7',$user_groups) || in_array('8',$user_groups))
        {
            return true;
        }
        else{
            return false;
        }
    }

    public function getPrivyList()
    {
        $user  = JFactory::getUser();
        //getPushUseID
        $db = JFactory::getDbo();
        $query = "SELECT id FROM #__contactpush_pushuids WHERE user =" . $user->id;
        $db->setQuery($query);

        $pid = $db->loadResult();

        $query = "SELECT distinct(message) FROM #__contactpush_messagerecipients WHERE user = " . $pid;
        //die($query);
        $db->setQuery($query);
        $msgs = $db->loadColumn();
        if(!$msgs)
        {
            return false;
        }
        $m = implode(',',$msgs);
        return $m;
    }

    public function myPushId()
    {
        $user  = JFactory::getUser();
        //getPushUseID
        $db = JFactory::getDbo();
        $query = "SELECT id FROM #__contactpush_pushuids WHERE user =" . $user->id;
        $db->setQuery($query);
        return $db->loadResult();
    }


}

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

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