<commit_before><?php
/**
 * Litus is a project by a group of students from the K.U.Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace CommonBundle\Controller\Admin;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    CommonBundle\Form\Admin\Role\Add as AddForm,
    CommonBundle\Form\Admin\Role\Edit as EditForm,
    CommonBundle\Entity\Acl\Action as AclAction,
    CommonBundle\Entity\Acl\Role,
    CommonBundle\Entity\Acl\Resource,
    Zend\View\Model\ViewModel;

/**
 * RoleController
 *
 * @author <NAME> <<EMAIL>>
 */
class RoleController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function manageAction()
    {
        $paginator = $this->paginator()->createFromEntity(
            'CommonBundle\Entity\Acl\Role',
            $this->getParam('page')
        );
        
        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
        );
    }

    public function addAction()
    {
        $form = new AddForm(
            $this->getEntityManager()
        );

        $roleCreated = false;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->post()->toArray();

            if ($form->isValid($formData)) {
                $parents = array();
                if (isset($formData['parents'])) {
                    foreach ($formData['parents'] as $parent) {
                        $parents[] = $this->getEntityManager()
                            ->getRepository('CommonBundle\Entity\Acl\Role')
                            ->findOneByName($parent);
                    }
                }
                
                $actions = array();
                if (isset($formData['actions'])) {
                    foreach ($formData['actions'] as $action) {
                        $actions[] = $this->getEntityManager()
                            ->getRepository('CommonBundle\Entity\Acl\Action')
                            ->findOneById($action);
                    }
                }
                
                $newRole = new Role(
                    $formData['name'], false, $parents, $actions
                );

                $this->getEntityManager()->persist($newRole);

                $this->getEntityManager()->flush();
                
                $form = new AddForm(
                    $this->getEntityManager()
                );
                
                $this->flashMessenger()->addMessage(
                    new FlashMessage(
                        FlashMessage::SUCCESS,
                        'Succes',
                        'The role was successfully created!'
                    )
                );

                $this->redirect()->toRoute(
                    'admin_role',
                    array(
                        'action' => 'add'
                    )
                );
                
                return new ViewModel();
            }
        }       
        
        return new ViewModel(
            array(
                'form' => $form,
                'roleCreated' => $roleCreated,
            )
        );
    }

    public function editAction()
    {
        if (!($role = $this->_getRole()))
            return new ViewModel();
        
        $form = new EditForm(
            $this->getEntityManager(), $role
        );

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->post()->toArray();
            
            if ($form->isValid($formData)) {
                if (isset($formData['parents'])) {
                    foreach ($formData['parents'] as $parent) {
                        $parents[] = $this->getEntityManager()
                            ->getRepository('CommonBundle\Entity\Acl\Role')
                            ->findOneByName($parent);
                    }
                }
                $role->setParents($parents);
                
                $actions = array();
                if (isset($formData['actions'])) {
                    foreach ($formData['actions'] as $action) {<commit_msg>Fixed a little bug when editing a role<commit_after><?php
/**
 * Litus is a project by a group of students from the K.U.Leuven. The goal is to create
 * various applications to support the IT needs of student unions.
 *
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 * @author <NAME> <<EMAIL>>
 *
 * @license http://litus.cc/LICENSE
 */

namespace CommonBundle\Controller\Admin;

use CommonBundle\Component\FlashMessenger\FlashMessage,
    CommonBundle\Form\Admin\Role\Add as AddForm,
    CommonBundle\Form\Admin\Role\Edit as EditForm,
    CommonBundle\Entity\Acl\Action as AclAction,
    CommonBundle\Entity\Acl\Role,
    CommonBundle\Entity\Acl\Resource,
    Zend\View\Model\ViewModel;

/**
 * RoleController
 *
 * @author <NAME> <<EMAIL>>
 */
class RoleController extends \CommonBundle\Component\Controller\ActionController\AdminController
{
    public function manageAction()
    {
        $paginator = $this->paginator()->createFromEntity(
            'CommonBundle\Entity\Acl\Role',
            $this->getParam('page')
        );
        
        return new ViewModel(
            array(
                'paginator' => $paginator,
                'paginationControl' => $this->paginator()->createControl(true),
            )
        );
    }

    public function addAction()
    {
        $form = new AddForm(
            $this->getEntityManager()
        );

        $roleCreated = false;
        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->post()->toArray();

            if ($form->isValid($formData)) {
                $parents = array();
                if (isset($formData['parents'])) {
                    foreach ($formData['parents'] as $parent) {
                        $parents[] = $this->getEntityManager()
                            ->getRepository('CommonBundle\Entity\Acl\Role')
                            ->findOneByName($parent);
                    }
                }
                
                $actions = array();
                if (isset($formData['actions'])) {
                    foreach ($formData['actions'] as $action) {
                        $actions[] = $this->getEntityManager()
                            ->getRepository('CommonBundle\Entity\Acl\Action')
                            ->findOneById($action);
                    }
                }
                
                $newRole = new Role(
                    $formData['name'], false, $parents, $actions
                );

                $this->getEntityManager()->persist($newRole);

                $this->getEntityManager()->flush();
                
                $form = new AddForm(
                    $this->getEntityManager()
                );
                
                $this->flashMessenger()->addMessage(
                    new FlashMessage(
                        FlashMessage::SUCCESS,
                        'Succes',
                        'The role was successfully created!'
                    )
                );

                $this->redirect()->toRoute(
                    'admin_role',
                    array(
                        'action' => 'add'
                    )
                );
                
                return new ViewModel();
            }
        }       
        
        return new ViewModel(
            array(
                'form' => $form,
                'roleCreated' => $roleCreated,
            )
        );
    }

    public function editAction()
    {
        if (!($role = $this->_getRole()))
            return new ViewModel();
        
        $form = new EditForm(
            $this->getEntityManager(), $role
        );

        if ($this->getRequest()->isPost()) {
            $formData = $this->getRequest()->post()->toArray();
            
            if ($form->isValid($formData)) {
                $parents = array();
                if (isset($formData['parents'])) {
                    foreach ($formData['parents'] as $parent) {
                        $parents[] = $this->getEntityManager()
                            ->getRepository('CommonBundle\Entity\Acl\Role')
                            ->findOneByName($parent);
                    }
                }
                $role->setParents($parents);
                
                $actions = array();
                if (isset($formData['actions'])) {
                    foreach ($formData['actions'] as $action) {
{'fraction_non_alphanumeric': 0.13161707344254986, 'fraction_numerical': 0.0, 'mean_word_length': 1.314676296105236, 'pattern_counts': {'":': 0, '<': 47, '<?xml version=': 0, '>': 133, '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': '6886968', 'n_tokens_mistral': 2274, 'n_tokens_neox': 2160, 'n_words': 417}