<commit_before><?php namespace Frozennode\Administrator;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Routing\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;
use Symfony\Component\HttpFoundation\File\File as SFile;
use Illuminate\Support\Facades\Validator as LValidator;
use Frozennode\Administrator\Fields\Field;

/**
 * Handles all requests related to managing the data models
 */
class AdminController extends Controller
{

	protected $layout = "administrator::layouts.default";

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}

	/**
	 * The main view for any of the data models
	 *
	 * @return Response
	 */
	public function index()
	{
		//set the layout content and title
		$this->layout->content = View::make("administrator::index");
	}


	/**
	 * Gets the item edit page / information
	 *
	 * @param string		$modelName
	 * @param mixed			$itemId
	 */
	public function item($modelName, $itemId = false)
	{
		$config = App::make('itemconfig');
		$fieldFactory = App::make('admin_field_factory');
		$actionFactory = App::make('admin_action_factory');
		$columnFactory = App::make('admin_column_factory');
		$actionPermissions = $actionFactory->getActionPermissions();
		$fields = $fieldFactory->getEditFields();

		//if it's ajax, we just return the item information as json
		if (Request::ajax())
		{
			//try to get the object
			$model = $config->getModel($itemId, $fields, $columnFactory->getIncludedColumns($fields));

			if ($model->exists)
			{
				$model = $config->updateModel($model, $fieldFactory, $actionFactory);
			}

			return $actionPermissions['view'] ? $model->toJson() : Response::json(array(
				'success' => false,
				'errors' => "You do not have permission to view this item",
			));
		}
		else
		{
			$view = View::make("administrator::index", array(
				'itemId' => $itemId,
			));

			//set the layout content and title
			$this->layout->content = $view;
		}
	}

	/**
	 * POST save method that accepts data via JSON POST and either saves an old item (if id is valid) or creates a new one
	 *
	 * @param string		$modelName
	 * @param int			$id
	 *
	 * @return JSON
	 */
	public function save($modelName, $id = false)
	{
		$config = App::make('itemconfig');
		$fieldFactory = App::make('admin_field_factory');
		$actionFactory = App::make('admin_action_factory');
		$save = $config->save(App::make('request'), $fieldFactory->getEditFields(), $actionFactory->getActionPermissions(), $id);

		if (is_string($save))
		{
			return Response::json(array(
				'success' => false,
				'errors' => $save,
			));
		}
		else
		{
			$columnFactory = App::make('admin_column_factory');
			$fields = $fieldFactory->getEditFields();
			$model = $config->getModel($id, $fields, $columnFactory->getIncludedColumns($fields));

			if ($model->exists)
			{
				$model = $config->updateModel($model, $fieldFactory, $actionFactory);
			}

			return Response::json(array(
				'success' => true,
				'data' => $model->toArray(),
			));
		}
	}

	/**
	 * POST delete method that accepts data via JSON POST and either saves an old
	 *
	 * @param string		$modelName
	 * @param int			$id
	 *
	 * @return JSON
	 */
	public function delete($modelName, $id)
	{
		$config = App::make('itemconfig');
		$actionFactory = App::make('admin_action_factory');
		$baseModel = $config->getDataModel();
		$model = $baseModel::find($id);
		$errorResponse = array(
			'success' => false,
			'error' => "There was an error deleting this item. Please reload the page and try again.",
		);

		//if the model or the id don't exist, send back 404
		$permissions = $actionFactory->getActionPermissions()
		if (!$model->exists || !$permissions['delete'])
		{
			return Response::json($errorResponse);
		}

		//delete the model
		if ($model->delete())
		{
			return Response::json(array(<commit_msg>Compatibility fix in delete function<commit_after><?php namespace Frozennode\Administrator;

use Illuminate\Support\Facades\App;
use Illuminate\Support\Facades\Config;
use Illuminate\Routing\Controllers\Controller;
use Illuminate\Support\Facades\Redirect;
use Illuminate\Support\Facades\Response;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\View;
use Illuminate\Support\Facades\Input;
use Illuminate\Support\Facades\File;
use Illuminate\Support\Facades\Session;
use Symfony\Component\HttpFoundation\File\File as SFile;
use Illuminate\Support\Facades\Validator as LValidator;
use Frozennode\Administrator\Fields\Field;

/**
 * Handles all requests related to managing the data models
 */
class AdminController extends Controller
{

	protected $layout = "administrator::layouts.default";

	/**
	 * Setup the layout used by the controller.
	 *
	 * @return void
	 */
	protected function setupLayout()
	{
		if ( ! is_null($this->layout))
		{
			$this->layout = View::make($this->layout);
		}
	}

	/**
	 * The main view for any of the data models
	 *
	 * @return Response
	 */
	public function index()
	{
		//set the layout content and title
		$this->layout->content = View::make("administrator::index");
	}


	/**
	 * Gets the item edit page / information
	 *
	 * @param string		$modelName
	 * @param mixed			$itemId
	 */
	public function item($modelName, $itemId = false)
	{
		$config = App::make('itemconfig');
		$fieldFactory = App::make('admin_field_factory');
		$actionFactory = App::make('admin_action_factory');
		$columnFactory = App::make('admin_column_factory');
		$actionPermissions = $actionFactory->getActionPermissions();
		$fields = $fieldFactory->getEditFields();

		//if it's ajax, we just return the item information as json
		if (Request::ajax())
		{
			//try to get the object
			$model = $config->getModel($itemId, $fields, $columnFactory->getIncludedColumns($fields));

			if ($model->exists)
			{
				$model = $config->updateModel($model, $fieldFactory, $actionFactory);
			}

			return $actionPermissions['view'] ? $model->toJson() : Response::json(array(
				'success' => false,
				'errors' => "You do not have permission to view this item",
			));
		}
		else
		{
			$view = View::make("administrator::index", array(
				'itemId' => $itemId,
			));

			//set the layout content and title
			$this->layout->content = $view;
		}
	}

	/**
	 * POST save method that accepts data via JSON POST and either saves an old item (if id is valid) or creates a new one
	 *
	 * @param string		$modelName
	 * @param int			$id
	 *
	 * @return JSON
	 */
	public function save($modelName, $id = false)
	{
		$config = App::make('itemconfig');
		$fieldFactory = App::make('admin_field_factory');
		$actionFactory = App::make('admin_action_factory');
		$save = $config->save(App::make('request'), $fieldFactory->getEditFields(), $actionFactory->getActionPermissions(), $id);

		if (is_string($save))
		{
			return Response::json(array(
				'success' => false,
				'errors' => $save,
			));
		}
		else
		{
			$columnFactory = App::make('admin_column_factory');
			$fields = $fieldFactory->getEditFields();
			$model = $config->getModel($id, $fields, $columnFactory->getIncludedColumns($fields));

			if ($model->exists)
			{
				$model = $config->updateModel($model, $fieldFactory, $actionFactory);
			}

			return Response::json(array(
				'success' => true,
				'data' => $model->toArray(),
			));
		}
	}

	/**
	 * POST delete method that accepts data via JSON POST and either saves an old
	 *
	 * @param string		$modelName
	 * @param int			$id
	 *
	 * @return JSON
	 */
	public function delete($modelName, $id)
	{
		$config = App::make('itemconfig');
		$actionFactory = App::make('admin_action_factory');
		$baseModel = $config->getDataModel();
		$model = $baseModel::find($id);
		$errorResponse = array(
			'success' => false,
			'error' => "There was an error deleting this item. Please reload the page and try again.",
		);

		//if the model or the id don't exist, send back 404
		$permissions = $actionFactory->getActionPermissions();
		
		if (!$model->exists || !$permissions['delete'])
		{
			return Response::json($errorResponse);
		}

		//delete the model
		if ($model->delete())
		{
			return Response::json(array(
{'fraction_non_alphanumeric': 0.15064291612598796, 'fraction_numerical': 0.00070779756989501, 'mean_word_length': 4.040428061831153, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 75, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '3380801', 'n_tokens_mistral': 2939, 'n_tokens_neox': 2644, 'n_words': 661}