<?php 

/** 
 *------------------------------------------------------------------------------
 * @package       T4 Page Builder for Joomla!
 *------------------------------------------------------------------------------
 * @copyright     Copyright (C) 2004-2020 JoomlArt.com. All Rights Reserved.
 * @license       GNU General Public License version 2 or later; see LICENSE.txt
 * @authors       JoomlArt, JoomlaBamboo, (contribute to this project at github 
 *                & Google group to become co-author)
 *------------------------------------------------------------------------------
 */
use JPB\Helper\Item as JPBItem;
use Joomla\CMS\Factory;
use Joomla\CMS\Router\Route;
use Joomla\CMS\Response\JsonResponse;
use Joomla\CMS\MVC\Controller\FormController;

defined('_JEXEC') or die;
/**
 * Pages list controller class.
 *
 * @since  1.6
 */
class T4pagebuilderControllerPage extends FormController
{
 	public function __construct($config = array())
	{
		parent::__construct($config);
		// module save2copy should be standard on forms.
		$this->registerTask('module2copy', 'module2copy');
	}
	/**
	 * Method override to check if you can add a new record.
	 *
	 * @param   array  $data  An array of input data.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	protected function allowAdd($data = array())
	{
		// In the absense of better information, revert to the component permissions.
			return parent::allowAdd();
	}

	/**
	 * Method override to check if you can edit an existing record.
	 *
	 * @param   array   $data  An array of input data.
	 * @param   string  $key   The name of the key for the primary key.
	 *
	 * @return  boolean
	 *
	 * @since   1.6
	 */
	protected function allowEdit($data = array(), $key = 'id')
	{
		$recordId = (int) isset($data[$key]) ? $data[$key] : 0;
		$user = Factory::getUser();

		// Zero record (id:0), return component edit permission by calling parent controller method
		if (!$recordId)
		{
			return parent::allowEdit($data, $key);
		}

		// Check edit on the record asset (explicit or inherited)
		if ($user->authorise('core.edit', 'com_t4pagebuilder.page.' . $recordId))
		{
			return true;
		}

		// Check edit own on the record asset (explicit or inherited)
		if ($user->authorise('core.edit.own', 'com_t4pagebuilder.page.' . $recordId))
		{
			// Existing record already has an owner, get it
			$record = $this->getModel()->getItem($recordId);

			if (empty($record))
			{
				return false;
			}

			// Grant if current user is owner of the record
			return $user->id == $record->created_by;
		}

		return false;
	}
	protected function getRedirectToItemAppend($recordId = null, $urlVar = 'id')
	{
		$append = parent::getRedirectToItemAppend($recordId, $urlVar);
	
		if ($editInline = $this->input->get('inline', '', 'cmd')) {
			$append .= '&inline=' . $editInline;
		}
		return $append;
	}
	protected function getRedirectToListAppend()
	{
		$append = parent::getRedirectToListAppend();
		if ($inline = $this->input->get('inline', '', 'cmd')) {
			$append .= '&inline=' . $inline;
		}
		return $append;
	}
	/**
	 * Method to run batch operations.
	 *
	 * @param   object  $model  The model.
	 *
	 * @return  boolean   True if successful, false otherwise and internal error is set.
	 *
	 * @since   1.6
	 */
	public function batch($model = null)
	{
		$this->checkToken();

		// Set the model
		/** @var T4pagebuildertModelArticle $model */
		$model = $this->getModel('Page', '', array());

		// Preset the redirect
		$this->setRedirect(Route::_('index.php?option=com_t4pagebuilder&view=pages' . $this->getRedirectToListAppend(), false));

		return parent::batch($model);
	}
	public function module2copy()
	{
	
		try {
			$id = Factory::getApplication()->input->get('id', 0, 'int');
			$ext = Factory::getApplication()->input->get('ext', "", 'string');
			$data = JPBItem::load($id);
			$data->id = null;
			$Extension_id = JPBItem::getNextExtensionId($ext);
			$data->asset_id = $Extension_id + 1;
			$result = $this->getModel()->save((array)$data);
			echo new JsonResponse($result);
		}
		catch (Exception $e) {
			echo new JsonResponse($e);
		}
		exit;

	}
}
?>