joomla/joomla-cms — pull request #5518 by jackkum (2014-12-25 04:46:59+00:00)
Title: Filter admin contact

On my project users sync with accounting software, creating user and contact from cli script.

// We are a valid entry point.
const _JEXEC = 1;

error_reporting(E_ALL | E_NOTICE);
ini_set('display_errors', 1);

// Load system defines
if (file_exists(dirname(__DIR__) . '/defines.php'))
{
require_once dirname(__DIR__) . '/defines.php';
}

if (!defined('_JDEFINES'))
{
define('JPATH_BASE', dirname(__DIR__));
require_once JPATH_BASE . '/includes/defines.php';
}

require_once JPATH_CONFIGURATION . '/configuration.php';

require_once JPATH_LIBRARIES . '/import.legacy.php';
require_once JPATH_LIBRARIES . '/cms.php';

$config = new JConfig;

error_reporting(E_ALL);
ini_set('display_errors', 1);

// im doing something wrong, without this have warnings
$_SERVER['HTTP_HOST'] = 'example.com';
$_SERVER['REQUEST_URI'] = NULL;

// init session
JFactory::getSession();
// admin app
JFactory::getApplication('administrator');

class JContactAddCli extends JApplicationCli
{
public function doExecute()
{
jimport('joomla.user.helper');
$user_id = $this->input->getInt('user');
$catid = $this->input->getInt('catid', 0);

if( ! $user_id){
$this->out("Input user", TRUE);
return FALSE;
}
// ...
$contactsModel = FOFModel::getAnInstance('Contacts', 'ContactModel');
$contactsModel->setState('filter.user_id', $user_id);
$contactsModel->getItems();
// ...
}
}

add to method ContactModelContacts::getListQuery() filter:
if (($user_id = $this->getState('filter.user_id')))
{
$query->where('a.user_id = ' . (int) $user_id);
}