<?php
/** 
 * @package     VikRestaurants
 * @subpackage  core
 * @author      E4J s.r.l.
 * @copyright   Copyright (C) 2023 E4J s.r.l. All Rights Reserved.
 * @license     http://www.gnu.org/licenses/gpl-2.0.html GNU/GPL
 * @link        https://vikwp.com
 */

// No direct access
defined('ABSPATH') or die('No script kiddies please!');

VRELoader::import('library.mvc.controllers.admin');

/**
 * VikRestaurants API plugin controller.
 *
 * @since 1.8
 */
class VikRestaurantsControllerApiplugin extends VREControllerAdmin
{
	/**
	 * Task used to access the management page of an existing record.
	 *
	 * @return 	boolean
	 */
	public function edit()
	{
		$app = JFactory::getApplication();

		// unset user state for being recovered again
		$app->setUserState('vre.apiplugin.data', array());

		// check user permissions
		if (!JFactory::getUser()->authorise('core.edit', 'com_vikrestaurants'))
		{
			// back to main list, not authorised to edit records
			$app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
			$this->cancel();

			return false;
		}

		$cid = $app->input->getString('cid', array(''));

		$this->setRedirect('index.php?option=com_vikrestaurants&view=manageapiplugin&cid[]=' . $cid[0]);

		return true;
	}

	/**
	 * Deletes a list of records set in the request.
	 *
	 * @return 	boolean
	 */
	public function delete()
	{
		$app  = JFactory::getApplication();
		$cid  = $app->input->get('cid', array(), 'string');

		// check user permissions
		if (!JFactory::getUser()->authorise('core.delete', 'com_vikrestaurants'))
		{
			// back to main list, not authorised to delete records
			$app->enqueueMessage(JText::translate('JERROR_ALERTNOAUTHOR'), 'error');
			$this->cancel();

			return false;
		}

		// delete selected records
		$res = JTableVRE::getInstance('apiplugin', 'VRETable')->delete($cid);

		// back to main list
		$this->cancel();

		return true;
	}

	/**
	 * Redirects the users to the main records list.
	 *
	 * @return 	void
	 */
	public function cancel()
	{
		$this->setRedirect('index.php?option=com_vikrestaurants&view=apiplugins');
	}

	/**
	 * AJAX end-point used to load the supported columns of the given model.
	 * In case the given model doesn't support tables, an empty object will
	 * be returned.
	 *
	 * This task expects the following arguments to be set in request.
	 *
	 * @param 	string 	model  The model to load.
	 *
	 * @return 	void
	 * 
	 * @since   1.11.1
	 */
	public function tableajax()
	{
		$app = JFactory::getApplication();

		if (!JSession::checkToken())
		{
			// missing CSRF-proof token
			E4J\VikRestaurants\Http\Document::getInstance($app)->close(403, JText::translate('JINVALID_TOKEN'));
		}

		// load model from request
		$modelName = $app->input->get('model');

		if (!$modelName)
		{
			E4J\VikRestaurants\Http\Document::getInstance($app)->close(400, 'Missing model name');
		}

		// load model
		$model = JModelVRE::getInstance($modelName);

		if (!$model)
		{
			E4J\VikRestaurants\Http\Document::getInstance($app)->close(404, sprintf('Model [%s] not found', $modelName));
		}

		try
		{
			// send default item details
			$this->sendJSON($model->getItem(0, true));
		}
		catch (Exception $e)
		{
			// an error occurred, send empty object
			$this->sendJSON(new stdClass);
		}
	}
}
