<?php
/**
 * ------------------------------------------------------------------------
 * Plugin Ajax JA Content Type
 * ------------------------------------------------------------------------
 * Copyright (C) 2004-2018 J.O.O.M Solutions Co., Ltd. All Rights Reserved.
 * @license - GNU/GPL, http://www.gnu.org/licenses/gpl.html
 * Author: J.O.O.M Solutions Co., Ltd
 * Websites: http://www.joomlart.com - http://www.joomlancers.com
 * ------------------------------------------------------------------------
 */

defined('_JEXEC') or die;

use Joomla\CMS\Factory;
use Joomla\CMS\Uri\Uri;
use Joomla\CMS\HTML\HTMLHelper;
use Joomla\CMS\Plugin\CMSPlugin;
use Joomla\CMS\Layout\LayoutHelper;
use Joomla\CMS\Client\ClientHelper;
use Joomla\CMS\Pagination\Pagination;
use Joomla\CMS\Component\ComponentHelper;

jimport('joomla.filesystem.folder');
jimport('joomla.filesystem.file');
/**
 *
 * @package     Joomla.Plugin
 * @subpackage  Content.Jacontenttype
 * @since       1.5
 */
class PlgAjaxJacontenttype extends CMSPlugin
{
	protected $layoutBasePath;
	public function __construct(& $subject, $config)
	{
		parent::__construct($subject, $config);
		$this->layoutBasePath = JPATH_ROOT . '/plugins/ajax/jacontenttype/layouts';
	}

	public function onAjaxJacontenttype() {
		$app = Factory::getApplication();
		$view = $app->input->getCmd('view', '');
		switch($view) {
			case 'contacts':
				$this->getContacts();
				break;
			case 'items':
				$this->getItems();
				break;
			case 'media':
				$this->getMedia();
				break;
			case 'mediaList':
				$this->getMediaList();
				break;
		}
	}

	protected function  getItems() {
		JLoader::register('JAContentTypeModelItems', JPATH_ROOT . '/plugins/system/jacontenttype/models/items.php');
		$model = new JAContentTypeModelItems();

		$app = Factory::getApplication();
		$lang	= Factory::getLanguage();
		$lang->load('com_content', JPATH_ADMINISTRATOR);
		$content_type = $app->input->get('content_type', '');

		$items = $model->getMetaItems($content_type);
		$state = $model->getState();

		$limit = (int) $model->getState('list.limit') - (int) $model->getState('list.links');
		$pagination = new Pagination($model->getTotal(), $model->getStart(), $limit);

		$options = array('items' => $items, 'state' => $state, 'pagination' => $pagination);
		echo LayoutHelper::render('contenttype.items', $options, $this->layoutBasePath);
	}

	protected function  getMedia() {
		$input = Factory::getApplication()->input;
		$params     = ComponentHelper::getParams('com_media');

		Factory::getDocument()->addScriptOptions(
			'mediamanager', array(
				'base'   => $params->get('image_path', 'images') . '/',
				'asset'  => $input->get('asset'),
				'author' => $input->get('author')
			)
		);
		
		JLoader::register('MediaModelManager', JPATH_ADMINISTRATOR. '/components/com_media/models/manager.php');
		$config = ComponentHelper::getParams('com_media');
		$lang	= Factory::getLanguage();
		$model = new MediaModelManager();

		$lang->load('com_media', JPATH_ADMINISTRATOR);

		$path = 'file_path';
		define('COM_MEDIA_BASE',    JPATH_ROOT . '/' . $config->get($path, 'images'));
		define('COM_MEDIA_BASEURL', Uri::root() . $config->get($path, 'images'));
		// Include jQuery
		HTMLHelper::_('jquery.framework');
		HTMLHelper::_('script', 'media/popup-imagemanager.js', true, true);
		HTMLHelper::_('stylesheet', 'media/popup-imagemanager.css', array(), true);

		if ($lang->isRTL())
		{
			HTMLHelper::_('stylesheet', 'media/popup-imagemanager_rtl.css', array(), true);
		}

		/*
		 * Display form for FTP credentials?
		 * Don't set them here, as there are other functions called before this one if there is any file write operation
		 */
		$ftp = !ClientHelper::hasCredentials('ftp');


		$options = array(
			'session' => Factory::getSession(),
			'config' => $config,
			'state' => $model->getState(),
			'folderList' => $model->getFolderList(),
			'require_ftp' => $ftp
		);
		echo LayoutHelper::render('contenttype.media', $options, $this->layoutBasePath);
	}

	protected function  getMediaList() {
		JLoader::register('MediaHelper', JPATH_ADMINISTRATOR. '/components/com_media/helpers/media.php');
		JLoader::register('MediaModelList', JPATH_ADMINISTRATOR. '/components/com_media/models/list.php');
		$config = ComponentHelper::getParams('com_media');

		// Do not allow cache
		Factory::getApplication()->allowCache(false);

		$lang	= Factory::getLanguage();
		$lang->load('com_media', JPATH_ADMINISTRATOR);
		$path = 'file_path';
		define('COM_MEDIA_BASE',    JPATH_ROOT . '/' . $config->get($path, 'images'));
		define('COM_MEDIA_BASEURL', Uri::root() . $config->get($path, 'images'));

		HTMLHelper::_('stylesheet', 'media/popup-imagelist.css', array(), true);
		if ($lang->isRTL()) {
			HTMLHelper::_('stylesheet', 'media/popup-imagelist_rtl.css', array(), true);
		}

		$document = Factory::getDocument();
		$document->addScriptDeclaration("var ImageManager = window.parent.ImageManager;");

		$model = new MediaModelList();

		$docs = method_exists( $model, 'getVideos') ? array_merge( $model->getDocuments(), $model->getVideos() ) : $model->getDocuments();
		$images = $model->getDocuments();
		$folders = $model->getFOlders();
		$state = $model->getState();

		$options = array(
			'baseURL' => COM_MEDIA_BASEURL,
			'docs' => $docs,
			'images' => $images,
			'folders' => $folders,
			'state' => $state
		);
		echo LayoutHelper::render('contenttype.media_list', $options, $this->layoutBasePath);
	}

	protected function  getContacts() {
		if (version_compare(JVERSION, '4.0', 'ge')) {
			$model = new Joomla\Component\Contact\Administrator\Model\ContactsModel(array()); 
		} else {
			JLoader::register('ContactModelContacts', JPATH_ADMINISTRATOR . '/components/com_contact/models/contacts.php');
			$model = new ContactModelContacts(array());
		}

		$lang	= Factory::getLanguage();
		$lang->load('com_contact', JPATH_ADMINISTRATOR);

		$items = $model->getItems();
		$state = $model->getState();

		$limit = (int) $model->getState('list.limit') - (int) $model->getState('list.links');
		$pagination = new Pagination($model->getTotal(), $model->getStart(), $limit);

		$options = array('items' => $items, 'state' => $state, 'pagination' => $pagination);
		echo LayoutHelper::render('contact.items', $options, $this->layoutBasePath);
	}
}