<commit_before><?php
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\File\Transfer\Adapter\Http  as HttpTransfer;
use Zend\Validator\File\Size;
use Zend\Validator\File\Extension;
use Admin\Model\Brand;
use Admin\Form\BrandForm;
use Admin\Traits\ModuleTablesTrait as AdminTablesTrait;
use Application\ConfigAwareInterface;

class BrandController extends AbstractActionController
implements ConfigAwareInterface
{
	use AdminTablesTrait;
	private $config;

	public function indexAction()
	{
		return new ViewModel(array(
			'brands' => $this->getBrandTable()->fetchAll(),
			'config' => $this->config
			));
	}

	public function addAction()
	{
		$form = new BrandForm();
		$request = $this->getRequest();

		if ($request->isPost()) {

			$brand = new Brand();
			$form->setInputFilter($brand->getInputFilter());

			$data = $request->getPost()->toArray();
			
			$form->setData($data);

			if ($form->isValid()) {

				$fileService = $this->getServiceLocator()->get('Admin\Service\FileService');

				$fileService->setDestination($this->config['component']['brand']['image_path']);
				$fileService->setSize($this->config['file_characteristics']['image']['size']);
				$fileService->setExtension($this->config['file_characteristics']['image']['extension']);

				$image = $fileService->copy($this->params()->fromFiles('image'));
				$backgroundImage = $fileService->copy($this->params()->fromFiles('background_image'));

				$data['image'] = $image ? $image : "" ;
				$data['background_image'] = $backgroundImage ? $backgroundImage : ""  ;

				$brand->exchangeArray($data);
				$this->getBrandTable()->save($brand);

				return $this->redirect()->toRoute('admin/brand');
			}
		}
		return array(
			'form' => $form,
			'config' => $this->config
			);
	}

	public function editAction()
	{
		$id = (int) $this->params()->fromRoute('id', 0);

		if (!$id)
			return $this->redirect()->toRoute('admin/brand', array('action' => 'add'));

		$brand = $this->getBrandTable()->get($id);
		$previousImage = $brand->getImage();
		$previousBackgroundImage = $brand->getBackgroundImage();

		$form  = new BrandForm();

		$form->get("name")->setValue($brand->getName());
		$form->get("description")->setValue($brand->getDescription());

		$request = $this->getRequest();
		if ($request->isPost()) {

			$form->setInputFilter($brand->getInputFilter());

			$data 				= $request->getPost()->toArray();
			$form->setData($data);

			if ($form->isValid()) {

				$brand->setName($request->getPost('name'));
				$brand->setDescription($request->getPost('description'));

				$fileService = $this->getServiceLocator()->get('Admin\Service\FileService');
				$fileService->setDestination($this->config['component']['brand']['image_path']);
				$fileService->setSize($this->config['file_characteristics']['image']['size']);
				$fileService->setExtension($this->config['file_characteristics']['image']['extension']);

				$image = $this->params()->fromFiles('image');
				$backgroundImage = $this->params()->fromFiles('background_image');

				if(isset($image['name']) && !empty($image['name'])) {
					$image = $fileService->copy($image);
					$brand->setImage($image);
					if(isset($previousImage) && !empty($previousImage))
					   unlink($this->config['component']['brand']['image_path']."/".$previousImage);
				}
				if(isset($backgroundImage['name']) && !empty($backgroundImage['name'])) {
					$backgroundImage = $fileService->copy($backgroundImage);
					$brand->setBackgroundImage($backgroundImage);

					if(isset($previousBackgroundImage) && !empty($previousBackgroundImage))
					   unlink($this->config['component']['brand']['image_path']."/".$previousBackgroundImage);
				}

				$this->getBrandTable()->save($brand);
				return $this->redirect()->toRoute('admin/brand');
			}
		}

		return array(
			'id' => $id,
			'image' => $previousImage,
			'backgroundImage' => $previousBackgroundImage,
			'form' => $form,
			'config' => $this->config,
			);
	}

	public function deleteAction()
	{
		$id = (int) $this->params()->fromRoute('id', 0);
		if (!$id) {
			return $this->redirect()->toRoute('admin/brand');
		}
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del', 'No');<commit_msg>refactoring code<commit_after><?php
namespace Admin\Controller;

use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\File\Transfer\Adapter\Http  as HttpTransfer;
use Zend\Validator\File\Size;
use Zend\Validator\File\Extension;
use Admin\Model\Brand;
use Admin\Form\BrandForm;
use Admin\Traits\ModuleTablesTrait as AdminTablesTrait;
use Application\ConfigAwareInterface;

class BrandController extends AbstractActionController
implements ConfigAwareInterface
{
	use AdminTablesTrait;
	private $config;

	public function indexAction()
	{
		return new ViewModel(array(
			'brands' => $this->getBrandTable()->fetchAll(),
			'config' => $this->config
			));
	}

	public function addAction()
	{
		$form = new BrandForm();
		$request = $this->getRequest();

		if ($request->isPost()) {

			$brand = new Brand();
			$form->setInputFilter($brand->getInputFilter());

			$data = $request->getPost()->toArray();
			
			$form->setData($data);

			if ($form->isValid()) {

				$fileService = $this->getServiceLocator()->get('Admin\Service\FileService');

				$fileService->setDestination($this->config['component']['brand']['image_path']);
				$fileService->setSize($this->config['file_characteristics']['image']['size']);
				$fileService->setExtension($this->config['file_characteristics']['image']['extension']);

				$image = $fileService->copy($this->params()->fromFiles('image'));
				$backgroundImage = $fileService->copy($this->params()->fromFiles('background_image'));

				$data['image'] = $image ? $image : "" ;
				$data['background_image'] = $backgroundImage ? $backgroundImage : ""  ;

				$brand->exchangeArray($data);
				$this->getBrandTable()->save($brand);

				return $this->redirect()->toRoute('admin/brand');
			}
		}
		return array(
			'form' => $form,
			'config' => $this->config
			);
	}

	public function editAction()
	{
		$id = (int) $this->params()->fromRoute('id', 0);

		if (!$id)
			return $this->redirect()->toRoute('admin/brand', array('action' => 'add'));

		$brand = $this->getBrandTable()->get($id);
		$previousImage = $brand->getImage();
		$previousBackgroundImage = $brand->getBackgroundImage();

		$form  = new BrandForm();

		$form->get("name")->setValue($brand->getName());
		$form->get("description")->setValue($brand->getDescription());

		$request = $this->getRequest();
		if ($request->isPost()) {

			$form->setInputFilter($brand->getInputFilter());

			$data 				= $request->getPost()->toArray();
			$form->setData($data);

			if ($form->isValid()) {

				$brand->setName($request->getPost('name'));
				$brand->setDescription($request->getPost('description'));

				$fileService = $this->getServiceLocator()->get('Admin\Service\FileService');
				$fileService->setDestination($this->config['component']['brand']['image_path']);
				$fileService->setSize($this->config['file_characteristics']['image']['size']);
				$fileService->setExtension($this->config['file_characteristics']['image']['extension']);

				$image = $this->params()->fromFiles('image');
				$backgroundImage = $this->params()->fromFiles('background_image');

				if(isset($image['name']) && !empty($image['name'])) {
					$image = $fileService->copy($image);
					$brand->setImage($image);
					if(isset($previousImage) && !empty($previousImage))
						unlink($this->config['component']['brand']['image_path']."/".$previousImage);
				}
				if(isset($backgroundImage['name']) && !empty($backgroundImage['name'])) {
					$backgroundImage = $fileService->copy($backgroundImage);
					$brand->setBackgroundImage($backgroundImage);
					if(isset($previousBackgroundImage) && !empty($previousBackgroundImage))
						unlink($this->config['component']['brand']['image_path']."/".$previousBackgroundImage);
				}

				$this->getBrandTable()->save($brand);
				return $this->redirect()->toRoute('admin/brand');
			}
		}

		return array(
			'id' => $id,
			'image' => $previousImage,
			'backgroundImage' => $previousBackgroundImage,
			'form' => $form,
			'config' => $this->config,
			);
	}

	public function deleteAction()
	{
		$id = (int) $this->params()->fromRoute('id', 0);
		if (!$id) {
			return $this->redirect()->toRoute('admin/brand');
		}
		$request = $this->getRequest();
		if ($request->isPost()) {
			$del = $request->getPost('del', 'No');
{'fraction_non_alphanumeric': 0.211803125728948, 'fraction_numerical': 0.00046652670865407047, 'mean_word_length': 5.103202846975089, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 197, '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': '2763546', 'n_tokens_mistral': 3017, 'n_tokens_neox': 2571, 'n_words': 366}