<commit_before><?php
namespace CmsIr\Users\Controller;

use CmsIr\Users\Model\Users;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;
use Zend\Db\Sql\Predicate;
use CmsIr\Users\Form\UserForm;
use CmsIr\Users\Form\UserFormFilter;
use CmsIr\Users\Form\UsereditFormFilter;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;

use Zend\Mail\Message;

class UsersController extends AbstractActionController
{
    protected $usersTable;
    protected $uploadDir = 'public/files/users/';
    protected $appName = 'Cms-ir';

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

            $data = $this->getRequest()->getPost();
            $columns = array( 'name', 'surname', 'email');

            $listData = $this->getUsersTable()->getDatatables($columns,$data);
            $output = array(
                "sEcho" => $this->getRequest()->getPost('sEcho'),
                "iTotalRecords" => $listData['iTotalRecords'],
                "iTotalDisplayRecords" => $listData['iTotalDisplayRecords'],
                "aaData" => $listData['aaData']
            );

            $jsonObject = Json::encode($output, true);
            echo $jsonObject;
            return $this->response;
        }
		return new ViewModel();
	}

    public function previewAction()
    {
        $id = $this->params()->fromRoute('id');

        $user = $this->getUsersTable()->getUser($id);

        if(!$user) {
            return $this->redirect()->toRoute('users-list');
        }

        $form = new UserForm();

        $config = $this->getServiceLocator()->get('Config');
        $aclRoles = $config['acl']['roles'];

        $tmpArrayRoles = array();
        $i = 1;

        foreach ($aclRoles as $keyRole => $role) {
            if($user->getRole() == $i) {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                    'selected' => true
                );
            } else {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                );
            }
            array_push($tmpArrayRoles, $tmp);
            $i++;
        }
        $form->get('role')->setValueOptions($tmpArrayRoles);
        $form->bind($user);

        $viewParams = array();
        $viewParams['form'] = $form;
        return new ViewModel($viewParams);
    }

    public function createAction()
    {
        $form = new UserForm();

        $config = $this->getServiceLocator()->get('Config');
        $aclRoles = $config['acl']['roles'];

        $tmpArrayRoles = array();
        $i = 1;
        foreach ($aclRoles as $keyRole => $role) {
            $tmp = array(
                'value' => $i,
                'label' => ucfirst($keyRole)
            );
            array_push($tmpArrayRoles, $tmp);
            $i++;
        }
        $form->get('role')->setValueOptions($tmpArrayRoles);

        $request = $this->getRequest();

        if ($request->isPost()) {

            $form->setInputFilter(new UserFormFilter($this->getServiceLocator()));
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $form->getData();
                $data = $this->prepareData($data);

                $user = new Users();
                $user->exchangeArray($data[0]);
                $this->getUsersTable()->saveUser($user);
                $this->sendConfirmationEmail($user, $data[1]);
                $this->flashMessenger()->addMessage('Użytkownik został dodany poprawnie.');

                return $this->redirect()->toRoute('users-list');
            }
        }

        $viewParams = array();
        $viewParams['form'] = $form;
        return new ViewModel($viewParams);
    }

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

        $user = $this->getUsersTable()->getUser($id);

        if(!$user) {
            return $this->redirect()->toRoute('users-list');
        }

        $form = new UserForm();

        $config = $this->getServiceLocator()->get('Config');
        $aclRoles = $config['acl']['roles'];

        $tmpArrayRoles = array();
        $i = 1;

        foreach ($aclRoles as $keyRole => $role) {
            if($user->getRole() == $i) {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                    'selected' => true
                );
            } else {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                );
            }
            array_push($tmpArrayRoles, $tmp);
            $i++;
        }
        $form->get('role')->setValueOptions($tmpArrayRoles);
        $form->bind($user);

        $request = $this->getRequest();

        if ($request->isPost()) {

            $form->setInputFilter(new UsereditFormFilter($this->getServiceLocator()));
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $form->getData();

                $user->setName($data->name);
                $user->setSurname($data->surname);
                $user->setEmail($data->email);
                $user->setRole($data->role);
                $user->setFilename($data->filename);
                $this->getUsersTable()->saveUser($user);

                $this->flashMessenger()->addMessage('Użytkownik został zedytowany poprawnie.');
                return $this->redirect()->toRoute('users-list');
            }
        }
        $viewParams = array();
        $viewParams['form'] = $form;
        return new ViewModel($viewParams);
    }

    public function deleteAction()
    {
        $request = $this->getRequest();
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {<commit_msg>Naprawienie bledy zapisu uzytkownika<commit_after><?php
namespace CmsIr\Users\Controller;

use CmsIr\Users\Model\Users;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Zend\Json\Json;
use Zend\Db\Sql\Predicate;
use CmsIr\Users\Form\UserForm;
use CmsIr\Users\Form\UserFormFilter;
use CmsIr\Users\Form\UsereditFormFilter;
use Zend\Mime\Message as MimeMessage;
use Zend\Mime\Part as MimePart;

use Zend\Mail\Message;

class UsersController extends AbstractActionController
{
    protected $usersTable;
    protected $uploadDir = 'public/files/users/';
    protected $appName = 'Cms-ir';

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

            $data = $this->getRequest()->getPost();
            $columns = array( 'name', 'surname', 'email');

            $listData = $this->getUsersTable()->getDatatables($columns,$data);
            $output = array(
                "sEcho" => $this->getRequest()->getPost('sEcho'),
                "iTotalRecords" => $listData['iTotalRecords'],
                "iTotalDisplayRecords" => $listData['iTotalDisplayRecords'],
                "aaData" => $listData['aaData']
            );

            $jsonObject = Json::encode($output, true);
            echo $jsonObject;
            return $this->response;
        }
		return new ViewModel();
	}

    public function previewAction()
    {
        $id = $this->params()->fromRoute('id');

        $user = $this->getUsersTable()->getUser($id);

        if(!$user) {
            return $this->redirect()->toRoute('users-list');
        }

        $form = new UserForm();

        $config = $this->getServiceLocator()->get('Config');
        $aclRoles = $config['acl']['roles'];

        $tmpArrayRoles = array();
        $i = 1;

        foreach ($aclRoles as $keyRole => $role) {
            if($user->getRole() == $i) {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                    'selected' => true
                );
            } else {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                );
            }
            array_push($tmpArrayRoles, $tmp);
            $i++;
        }
        $form->get('role')->setValueOptions($tmpArrayRoles);
        $form->bind($user);

        $viewParams = array();
        $viewParams['form'] = $form;
        return new ViewModel($viewParams);
    }

    public function createAction()
    {
        $form = new UserForm();

        $config = $this->getServiceLocator()->get('Config');
        $aclRoles = $config['acl']['roles'];

        $tmpArrayRoles = array();
        $i = 1;
        foreach ($aclRoles as $keyRole => $role) {
            $tmp = array(
                'value' => $i,
                'label' => ucfirst($keyRole)
            );
            array_push($tmpArrayRoles, $tmp);
            $i++;
        }
        $form->get('role')->setValueOptions($tmpArrayRoles);

        $request = $this->getRequest();

        if ($request->isPost()) {

            $form->setInputFilter(new UserFormFilter($this->getServiceLocator()));
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $form->getData();
                $data = $this->prepareData($data);

                $user = new Users();
                $user->exchangeArray($data[0]);
                $this->getUsersTable()->saveUser($user);
                $this->sendConfirmationEmail($user, $data[1]);
                $this->flashMessenger()->addMessage('Użytkownik został dodany poprawnie.');

                return $this->redirect()->toRoute('users-list');
            }
        }

        $viewParams = array();
        $viewParams['form'] = $form;
        return new ViewModel($viewParams);
    }

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

        $user = $this->getUsersTable()->getUser($id);

        if(!$user) {
            return $this->redirect()->toRoute('users-list');
        }

        $form = new UserForm();

        $config = $this->getServiceLocator()->get('Config');
        $aclRoles = $config['acl']['roles'];

        $tmpArrayRoles = array();
        $i = 1;

        foreach ($aclRoles as $keyRole => $role) {
            if($user->getRole() == $i) {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                    'selected' => true
                );
            } else {
                $tmp = array(
                    'value' => $i,
                    'label' => ucfirst($keyRole),
                );
            }
            array_push($tmpArrayRoles, $tmp);
            $i++;
        }
        $form->get('role')->setValueOptions($tmpArrayRoles);
        $form->bind($user);

        $request = $this->getRequest();

        if ($request->isPost()) {

            $form->setInputFilter(new UsereditFormFilter($this->getServiceLocator()));
            $form->setData($request->getPost());

            if ($form->isValid()) {
                $data = $form->getData();
                //var_dump($data);die;
                $user->setName($data->getName());
                $user->setSurname($data->getSurname());
                $user->setEmail($data->getEmail());
                $user->setRole($data->getRole());
                $user->setFilename($data->getFilename());
                $this->getUsersTable()->saveUser($user);

                $this->flashMessenger()->addMessage('Użytkownik został zedytowany poprawnie.');
                return $this->redirect()->toRoute('users-list');
            }
        }
        $viewParams = array();
        $viewParams['form'] = $form;
        return new ViewModel($viewParams);
    }

    public function deleteAction()
    {
        $request = $this->getRequest();
        $id = (int) $this->params()->fromRoute('id', 0);
        if (!$id) {
{'fraction_non_alphanumeric': 0.16902228134353176, 'fraction_numerical': 0.0009976720984369804, 'mean_word_length': 1.7935438922433813, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 203, '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': '2888456', 'n_tokens_mistral': 3528, 'n_tokens_neox': 3363, 'n_words': 570}