<commit_before><?php
namespace Chalk\Core\Controller;

use Chalk\Chalk,
	Chalk\Core,
	FileUpload\FileUpload,
	FileUpload\PathResolver,
	FileUpload\FileSystem,
	Chalk\Controller\Basic,
	Coast\Request,
	Coast\Response;

class Content extends Basic
{
	public function preDispatch(Request $req, Response $res)
	{
		$req->view->info
			= $req->info
			= Chalk::info($req->entity ? $req->entity : 'Chalk\Core\Content');
	}

	public function index(Request $req, Response $res)
	{
		if (!$req->entity) {
			$contents = $this->app->fire('Chalk\Core\Event\ListContents')->contents();
			return $res->redirect($this->url([
				'entity' => Chalk::info($contents[0])->name,
			]));
		}

		$wrap = $this->em->wrap($index = new \Chalk\Core\Model\Index());
		$wrap->graphFromArray($req->queryParams());
		$req->view->index = $wrap;
	}

	public function select(Request $req, Response $res)
	{
		$wrap = $this->em->wrap($index = new \Chalk\Core\Model\Index());
		$wrap->graphFromArray($req->bodyParams());
		$req->view->index = $wrap;

		if (count($index->contents)) {
			$contents = [];
			foreach ($index->contents as $content) {
				$contents[] = [
					'id'	=> $content->master->id,
					'name'	=> $content->master->name,
					'card'	=> $this->view->render('content/card', ['content' => $content])->toString(),
				];
			}
			return $res->json(['contents' => $contents]);
		}
	}

	public function edit(Request $req, Response $res)
	{
		$content = isset($req->route['params']['content'])
			? $this->em($req->info)->id($req->route['params']['content'])
			: $this->em($req->info)->create();
		$req->view->content = $wrap = $this->em->wrap($content);

		if (!$req->isPost()) {
			return;
		}

		$params = $req->bodyParams();
		$status = $params['status'];
		unset($params['status']);
		$wrap->graphFromArray($params);
		$wrap->graphFromArray(['status' => $status]); // @hack setting dates should be a listener
		if (!$wrap->isValid()) {
			return;
		}

		if (!$this->em->isPersisted($content)) {
			$this->em->persist($content);
		}
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was saved successfully", 'positive');
		return $res->redirect($this->url(array(
			'action'	=> 'edit',
			'content'	=> $content->id,
		)));
	}

	public function upload(Request $req, Response $res)
	{
		if (!$req->isPost()) {
			throw new \Chalk\Exception("Upload action only accepts POST requests");
		}

		$dir      = $this->frontend->dir('data/upload', true);
		$uploader = new FileUpload($req->bodyParam('files'), $req->servers());
		$uploader->setPathResolver(new PathResolver\Simple($dir->name()));
		$uploader->setFileSystem(new FileSystem\Simple());

		list($uploads, $headers) = $uploader->processAll();
		foreach ($uploads as $upload) {
			if (isset($upload->path)) {
				$content = new \Chalk\Core\File();
				$content->newFile = new \Coast\File($upload->path);
				$this->em->persist($content);
				$this->em->flush();
				unset($upload->path);
				$upload->html = $this->view->render('content/thumb', [
					'content'	=> $content,
					'covered'	=> true,
				] + (array) $req->view)->toString();
			}
		}

		return $res
			->headers($headers)
			->json(['files' => $uploads]);
	}

	public function archive(Request $req, Response $res)
	{
		$content = $this->em($req->info)->find($req->content);

		$content->status = \Chalk\Chalk::STATUS_ARCHIVED;
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was archived successfully", 'positive');
		if (isset($req->redirect)) {
			return $res->redirect($req->redirect);
		} else {
			return $res->redirect($this->url(array(
				'action'	=> 'edit',
				'content'	=> $content->id,
			)));
		}
	}

	public function restore(Request $req, Response $res)
	{
		$content = $this->em($req->info)->find($req->content);

		$content->restore();
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was restored successfully", 'positive');
		if (isset($req->redirect)) {
			return $res->redirect($req->redirect);
		} else {
			return $res->redirect($this->url(array(
				'action'	=> 'edit',
				'content'	=> $content->id,
			)));
		}
	}

	public function delete(Request $req, Response $res)
	{
		$content = $this->em($req->info)->find($req->content);

		$this->em->remove($content);
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was deleted successfully", 'positive');
		return $res->redirect($this->url(array(
			'action'	=> 'index',
			'id'		=> null,
		)));
	}
}<commit_msg>Fix issue with ID being passed through when deleting content.<commit_after><?php
namespace Chalk\Core\Controller;

use Chalk\Chalk,
	Chalk\Core,
	FileUpload\FileUpload,
	FileUpload\PathResolver,
	FileUpload\FileSystem,
	Chalk\Controller\Basic,
	Coast\Request,
	Coast\Response;

class Content extends Basic
{
	public function preDispatch(Request $req, Response $res)
	{
		$req->view->info
			= $req->info
			= Chalk::info($req->entity ? $req->entity : 'Chalk\Core\Content');
	}

	public function index(Request $req, Response $res)
	{
		if (!$req->entity) {
			$contents = $this->app->fire('Chalk\Core\Event\ListContents')->contents();
			return $res->redirect($this->url([
				'entity' => Chalk::info($contents[0])->name,
			]));
		}

		$wrap = $this->em->wrap($index = new \Chalk\Core\Model\Index());
		$wrap->graphFromArray($req->queryParams());
		$req->view->index = $wrap;
	}

	public function select(Request $req, Response $res)
	{
		$wrap = $this->em->wrap($index = new \Chalk\Core\Model\Index());
		$wrap->graphFromArray($req->bodyParams());
		$req->view->index = $wrap;

		if (count($index->contents)) {
			$contents = [];
			foreach ($index->contents as $content) {
				$contents[] = [
					'id'	=> $content->master->id,
					'name'	=> $content->master->name,
					'card'	=> $this->view->render('content/card', ['content' => $content])->toString(),
				];
			}
			return $res->json(['contents' => $contents]);
		}
	}

	public function edit(Request $req, Response $res)
	{
		$content = isset($req->route['params']['content'])
			? $this->em($req->info)->id($req->route['params']['content'])
			: $this->em($req->info)->create();
		$req->view->content = $wrap = $this->em->wrap($content);

		if (!$req->isPost()) {
			return;
		}

		$params = $req->bodyParams();
		$status = $params['status'];
		unset($params['status']);
		$wrap->graphFromArray($params);
		$wrap->graphFromArray(['status' => $status]); // @hack setting dates should be a listener
		if (!$wrap->isValid()) {
			return;
		}

		if (!$this->em->isPersisted($content)) {
			$this->em->persist($content);
		}
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was saved successfully", 'positive');
		return $res->redirect($this->url(array(
			'action'	=> 'edit',
			'content'	=> $content->id,
		)));
	}

	public function upload(Request $req, Response $res)
	{
		if (!$req->isPost()) {
			throw new \Chalk\Exception("Upload action only accepts POST requests");
		}

		$dir      = $this->frontend->dir('data/upload', true);
		$uploader = new FileUpload($req->bodyParam('files'), $req->servers());
		$uploader->setPathResolver(new PathResolver\Simple($dir->name()));
		$uploader->setFileSystem(new FileSystem\Simple());

		list($uploads, $headers) = $uploader->processAll();
		foreach ($uploads as $upload) {
			if (isset($upload->path)) {
				$content = new \Chalk\Core\File();
				$content->newFile = new \Coast\File($upload->path);
				$this->em->persist($content);
				$this->em->flush();
				unset($upload->path);
				$upload->html = $this->view->render('content/thumb', [
					'content'	=> $content,
					'covered'	=> true,
				] + (array) $req->view)->toString();
			}
		}

		return $res
			->headers($headers)
			->json(['files' => $uploads]);
	}

	public function archive(Request $req, Response $res)
	{
		$content = $this->em($req->info)->find($req->content);

		$content->status = \Chalk\Chalk::STATUS_ARCHIVED;
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was archived successfully", 'positive');
		if (isset($req->redirect)) {
			return $res->redirect($req->redirect);
		} else {
			return $res->redirect($this->url(array(
				'action'	=> 'edit',
				'content'	=> $content->id,
			)));
		}
	}

	public function restore(Request $req, Response $res)
	{
		$content = $this->em($req->info)->find($req->content);

		$content->restore();
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was restored successfully", 'positive');
		if (isset($req->redirect)) {
			return $res->redirect($req->redirect);
		} else {
			return $res->redirect($this->url(array(
				'action'	=> 'edit',
				'content'	=> $content->id,
			)));
		}
	}

	public function delete(Request $req, Response $res)
	{
		$content = $this->em($req->info)->find($req->content);

		$this->em->remove($content);
		$this->em->flush();

		$this->notify("{$req->info->singular} <strong>{$content->name}</strong> was deleted successfully", 'positive');
		return $res->redirect($this->url(array(
			'action'	=> 'index',
			'content'	=> null,
		)));
	}
}
{'fraction_non_alphanumeric': 0.23015873015873015, 'fraction_numerical': 0.00021743857360295715, 'mean_word_length': 4.121937639198218, 'pattern_counts': {'":': 0, '<': 21, '<?xml version=': 0, '>': 327, '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': '5408962', 'n_tokens_mistral': 3522, 'n_tokens_neox': 3113, 'n_words': 539}