<commit_before><?php
/**
 * Created by JetBrains PhpStorm.
 * User: zenkovnick
 * Date: 24.05.13
 * Time: 12:19
 * To change this template use File | Settings | File Templates.
 */

class registrationActions extends sfActions
{
    public function executeIndex(sfWebRequest $request) {

    }

    public function executeSignin(sfWebRequest $request) {
        if($this->getUser()->isAuthenticated()){
            $accounts = AccountTable::getAllUserAccounts($this->getUser()->getGuardUser());
            if($accounts->count() >= 1) {
                $this->redirect("@dashboard?account_id={$accounts->getFirst()->getId()}");

            } else {
                $this->redirect("@select_account");
            }

        }
        else
        {
            if($request->getParameter('redirect_to')){
                $this->getUser()->setAttribute('redirect_to', $request->getParameter('redirect_to'));
            }
            $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
            $this->form = new $class();

        }
    }


    public function executeSignup(sfWebRequest $request) {
        if($this->getUser()->isAuthenticated()){
            $this->redirect("@select_account");
        }
        if($request->getParameter('token')){
            $this->forward404Unless($user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
            $invited_user = $user_account->getUser();
            if($invited_user){
                $this->form = new RegistrationForm($invited_user);
            }
        } else {
            $this->form = new RegistrationForm();
        }
        if($request->isMethod('POST')){
            $this->form->bind($request->getPostParameter($this->form->getName()));
            if($this->form->isValid()){
                $user = $this->form->save();
                if($request->getParameter('token')){
                    $this->forward404Unless($user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
                    $user_account->setIsActive(true);
                    $user_account->save();
                    $this->getUser()->signIn($user);
                    $this->redirect("@dashboard?account_id=".$user_account->getAccount()->getId());
                } else {
                    $this->getUser()->signIn($user);
                    $this->redirect('@create_account');
                }
            }/* else {
            $errors = $this->form->getErrorSchema()->getErrors();
            $error_fields = array();
            foreach($errors as $key => $error){
                $error_fields[] = $key;
            }
            echo json_encode(
                array(
                    'result' => 'Failed',
                    'error_fields' => $error_fields,
                    'new_form_num' => $request->getParameter('new_form_num'),
                )
            );

            }*/
        }
    }

    public function executeApproveAccount(sfWebRequest $request) {
        $this->getUser()->setAttribute('approve', $request->getUri());
        if($this->getUser()->isAuthenticated()){
            $this->user = $this->getUser()->getGuardUser();
            if($request->getParameter('token')){
                $this->forward404Unless($this->user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
                if($this->user_account->getUserId() != $this->user->getId()){
                    $this->redirect("@select_account");
                } else {
                    $this->account = $this->user_account->getAccount();
                }
            } else {
                $this->redirect("@select_account");
            }

        } else {
            $this->redirect("@signin");
        }
    }

    public function executeApproveAccountProcess(sfWebRequest $request) {
        if($request->getParameter('token')){
            $this->forward404Unless($this->user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
            if($request->getParameter('status') == 'approve') {
                $this->user_account->setIsActive(true);
                $this->user_account->setInviteToken(null);
                $this->user_account->save();
                $this->getUser()->getAttributeHolder()->remove('approve');
                $this->redirect("@dashboard?account_id=".$this->user_account->getAccount()->getId());
            } else {
                $this->user_account->delete();
                $this->redirect("@select_account");
            }
        } else {
            $this->redirect("@select_account");
        }
    }



    public function executeSelectAccount(sfWebRequest $request) {
        $this->accounts = AccountTable::getAllUserAccounts($this->getUser()->getGuardUser());
        if($this->accounts->count() == 0){
            $this->redirect('@create_account');
        }
    }

    public function executeCreateAccount(sfWebRequest $request) {
        if($this->getUser()->isAuthenticated()){
            $this->user = $this->getUser()->getGuardUser();
            $this->form = new AccountForm(null, array('curr_user' => $this->user));
            if($request->isMethod('POST')){
                $this->form->bind($request->getPostParameter($this->form->getName()),$request->getFiles($this->form->getName()));
                if($this->form->isValid()){
                    $values = $this->form->getTaintedValues();
                    $chief_exists = $values['chief_pilot_id'] ? true : false;
                    $account = $this->form->save();
                    $account->setManager($this->user);
                    $user_account = UserAccountTable::getUserAccount($this->user->getId(), $account->getId());
                    if(!$user_account){
                        $user_account = new UserAccount();
                        $user_account->setAccount($account);
                        $user_account->setUser($this->user);
                        $user_account->setIsManager(true);
                        $user_account->setIsPic(true);
                        $user_account->setIsSic(true);
                        $user_account->setIsActive(true);
                        $user_account->save();
                    }

                    $risk_builder = new RiskBuilder();
                    $risk_builder->createDefaultForm($account);

                    $chief_pilot = $account->getChiefPilot();
                    if($chief_pilot && $chief_pilot->getId() && $chief_pilot->getId() != $this->user->getId()){
                        $chief_user_account = UserAccountTable::getUserAccount($chief_pilot->getId(), $account->getId());
<commit_msg>Fix create account<commit_after><?php
/**
 * Created by JetBrains PhpStorm.
 * User: zenkovnick
 * Date: 24.05.13
 * Time: 12:19
 * To change this template use File | Settings | File Templates.
 */

class registrationActions extends sfActions
{
    public function executeIndex(sfWebRequest $request) {

    }

    public function executeSignin(sfWebRequest $request) {
        if($this->getUser()->isAuthenticated()){
            $accounts = AccountTable::getAllUserAccounts($this->getUser()->getGuardUser());
            if($accounts->count() >= 1) {
                $this->redirect("@dashboard?account_id={$accounts->getFirst()->getId()}");

            } else {
                $this->redirect("@select_account");
            }

        }
        else
        {
            if($request->getParameter('redirect_to')){
                $this->getUser()->setAttribute('redirect_to', $request->getParameter('redirect_to'));
            }
            $class = sfConfig::get('app_sf_guard_plugin_signin_form', 'sfGuardFormSignin');
            $this->form = new $class();

        }
    }


    public function executeSignup(sfWebRequest $request) {
        if($this->getUser()->isAuthenticated()){
            $this->redirect("@select_account");
        }
        if($request->getParameter('token')){
            $this->forward404Unless($user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
            $invited_user = $user_account->getUser();
            if($invited_user){
                $this->form = new RegistrationForm($invited_user);
            }
        } else {
            $this->form = new RegistrationForm();
        }
        if($request->isMethod('POST')){
            $this->form->bind($request->getPostParameter($this->form->getName()));
            if($this->form->isValid()){
                $user = $this->form->save();
                if($request->getParameter('token')){
                    $this->forward404Unless($user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
                    $user_account->setIsActive(true);
                    $user_account->save();
                    $this->getUser()->signIn($user);
                    $this->redirect("@dashboard?account_id=".$user_account->getAccount()->getId());
                } else {
                    $this->getUser()->signIn($user);
                    $this->redirect('@create_account');
                }
            }/* else {
            $errors = $this->form->getErrorSchema()->getErrors();
            $error_fields = array();
            foreach($errors as $key => $error){
                $error_fields[] = $key;
            }
            echo json_encode(
                array(
                    'result' => 'Failed',
                    'error_fields' => $error_fields,
                    'new_form_num' => $request->getParameter('new_form_num'),
                )
            );

            }*/
        }
    }

    public function executeApproveAccount(sfWebRequest $request) {
        $this->getUser()->setAttribute('approve', $request->getUri());
        if($this->getUser()->isAuthenticated()){
            $this->user = $this->getUser()->getGuardUser();
            if($request->getParameter('token')){
                $this->forward404Unless($this->user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
                if($this->user_account->getUserId() != $this->user->getId()){
                    $this->redirect("@select_account");
                } else {
                    $this->account = $this->user_account->getAccount();
                }
            } else {
                $this->redirect("@select_account");
            }

        } else {
            $this->redirect("@signin");
        }
    }

    public function executeApproveAccountProcess(sfWebRequest $request) {
        if($request->getParameter('token')){
            $this->forward404Unless($this->user_account = Doctrine_Core::getTable('UserAccount')->findOneBy('invite_token', $request->getParameter('token')));
            if($request->getParameter('status') == 'approve') {
                $this->user_account->setIsActive(true);
                $this->user_account->setInviteToken(null);
                $this->user_account->save();
                $this->getUser()->getAttributeHolder()->remove('approve');
                $this->redirect("@dashboard?account_id=".$this->user_account->getAccount()->getId());
            } else {
                $this->user_account->delete();
                $this->redirect("@select_account");
            }
        } else {
            $this->redirect("@select_account");
        }
    }



    public function executeSelectAccount(sfWebRequest $request) {
        $this->accounts = AccountTable::getAllUserAccounts($this->getUser()->getGuardUser());
        if($this->accounts->count() == 0){
            $this->redirect('@create_account');
        }
    }

    public function executeCreateAccount(sfWebRequest $request) {
        if($this->getUser()->isAuthenticated()){
            $this->user = $this->getUser()->getGuardUser();
            $this->form = new AccountForm(null, array('curr_user' => $this->user));
            if($request->isMethod('POST')){
                $this->form->bind($request->getPostParameter($this->form->getName()),$request->getFiles($this->form->getName()));
                if($this->form->isValid()){
                    $values = $this->form->getTaintedValues();
                    $chief_exists = $values['chief_pilot_id'] ? true : false;
                    $account = $this->form->save();
                    $account->setManager($this->user);
                    $user_account = UserAccountTable::getUserAccount($this->user->getId(), $account->getId());
                    if(!$user_account){
                        $user_account = new UserAccount();
                        $user_account->setAccount($account);
                        $user_account->setUser($this->user);
                        $user_account->setIsManager(true);
                        $user_account->setRole('both');
                        $user_account->setIsActive(true);
                        $user_account->save();
                    }

                    $risk_builder = new RiskBuilder();
                    $risk_builder->createDefaultForm($account);

                    $chief_pilot = $account->getChiefPilot();
                    if($chief_pilot && $chief_pilot->getId() && $chief_pilot->getId() != $this->user->getId()){
                        $chief_user_account = UserAccountTable::getUserAccount($chief_pilot->getId(), $account->getId());

{'fraction_non_alphanumeric': 0.16418457926784535, 'fraction_numerical': 0.0035213850781307316, 'mean_word_length': 2.027315123251166, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 308, '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': '6539160', 'n_tokens_mistral': 3716, 'n_tokens_neox': 3529, 'n_words': 419}