<commit_before><?php

defined('SYSPATH') or die('No direct script access.');

class Model_Account_Email extends Model_Master {

    protected $_table_name = 'account_email';
    protected $_db_group = 'mship';
    protected $_primary_key = 'id';
    protected $_table_columns = array(
        'id' => array('data_type' => 'string'),
        'account_id' => array('data_type' => 'bigint'),
        'email' => array('data_type' => 'string'),
        'primary' => array('data_type' => 'boolean', 'is_nullable' => TRUE),
        'verified' => array('data_type' => 'timestamp', 'is_nullable' => TRUE),
        'created' => array('data_type' => 'timestamp', 'is_nullable' => TRUE),
        'deleted' => array('data_type' => 'timestamp', 'is_nullable' => TRUE),
    );
    // fields mentioned here can be accessed like properties, but will not be referenced in write operations
    protected $_ignored_columns = array(
    );
    // Belongs to relationships
    protected $_belongs_to = array(
        'account' => array(
            'model' => 'Account_Main',
            'foreign_key' => 'account_id',
        ),
    );
    // Has man relationships
    protected $_has_many = array();
    
    // Has one relationship
    protected $_has_one = array(
        'sso_email' => array(
            'model' => 'Sso_Email',
            'foreign_key' => 'account_email_id',
        ),
    );

    // Validation rules
    public function rules() {
        return array(
            'email' => array(
                array('not_empty'),
                array('email', array(':value', true)),
                //array(array($this, 'email_check_unique')),
            ),
        );
    }

    // Data filters
    public function filters() {
        return array(
            'email' => array(
                array('trim'),
                array('strtolower'),
            )
        );
    }
    
    /**
     * Check whether the current email has been assigned to an SSO system.
     * 
     * @param string $system The system to check for an assignment for.
     * @param boolean $returnEmail If set to TRUE the email will be returned, instead of true/false.
     * @return boolean TRUE if this system has an email assignment. FALSE otherwise.
     */
    public function assigned_to_sso($system, $returnEmail=false){
        // Let's loop through ALL emails
        foreach($this->where("deleted", "IS", NULL)->find_all() as $email){
            if($email->sso_email->loaded() && $email->sso_email->sso_system == $system){
                if($returnEmail){
                    return $email->email;
                } else {
                    return true;
                }
            }
        }
        return false;
    }
    
    // Check email is unique in the database.
    public function email_check_unique($email) {
        return false;
        return !(bool) ORM::factory("Account_Email")
                        ->where("id", "!=", $this->id)
                        ->where("email", "=", $email)
                        ->where("deleted", "=", NULL)
                        ->count_all() > 0;
    }

    // Gotta love __toString!
    public function __toString() {
        return ($this->email ? $this->email : "");
    }
    
    /**
     * Set the primary email for the given account!
     */
    public function set_primary($email){
        $account_id = Arr::get(Arr::get($this->_db_pending, 0), "args");
        $account_id = Arr::get($account_id, 0) == "account_email.account_id" ? Arr::get($account_id, 2) : 0;
        
        // Demote the old primary, if set.
        $oldPrimary = $this->get_active_primary();
        if($oldPrimary->loaded()){
            $oldPrimary->primary = 0;
            $oldPrimary->save();
        }
        
        // Does this email already exist?
        if($this->helper_pre_get_active()->where("email", "=", strtolower($email))->count_all() > 0){
            // It exists, update it to set primary = 1
            $curEmail = $this->helper_pre_get_active()->where("account_id", "=", $account_id)->where("email", "=", strtolower($email))->find();
            $curEmail->primary = 1;
            $curEmail->verified = gmdate("Y-m-d H:i:s");
            $curEmail->save();
        } else {
            // Store the new one!
            $newEmail = ORM::factory("Account_Email");
            $newEmail->account_id = $account_id;
            $newEmail->email = $email;
            $newEmail->primary = 1;
            $newEmail->verified = gmdate("Y-m-d H:i:s");
            $newEmail->created = gmdate("Y-m-d H:i:s");<commit_msg>Updated a typo in the email confirmation and set_primary function in the Model_Account_Email.<commit_after><?php

defined('SYSPATH') or die('No direct script access.');

class Model_Account_Email extends Model_Master {

    protected $_table_name = 'account_email';
    protected $_db_group = 'mship';
    protected $_primary_key = 'id';
    protected $_table_columns = array(
        'id' => array('data_type' => 'string'),
        'account_id' => array('data_type' => 'bigint'),
        'email' => array('data_type' => 'string'),
        'primary' => array('data_type' => 'boolean', 'is_nullable' => TRUE),
        'verified' => array('data_type' => 'timestamp', 'is_nullable' => TRUE),
        'created' => array('data_type' => 'timestamp', 'is_nullable' => TRUE),
        'deleted' => array('data_type' => 'timestamp', 'is_nullable' => TRUE),
    );
    // fields mentioned here can be accessed like properties, but will not be referenced in write operations
    protected $_ignored_columns = array(
    );
    // Belongs to relationships
    protected $_belongs_to = array(
        'account' => array(
            'model' => 'Account_Main',
            'foreign_key' => 'account_id',
        ),
    );
    // Has man relationships
    protected $_has_many = array();
    
    // Has one relationship
    protected $_has_one = array(
        'sso_email' => array(
            'model' => 'Sso_Email',
            'foreign_key' => 'account_email_id',
        ),
    );

    // Validation rules
    public function rules() {
        return array(
            'email' => array(
                array('not_empty'),
                array('email', array(':value', true)),
                //array(array($this, 'email_check_unique')),
            ),
        );
    }

    // Data filters
    public function filters() {
        return array(
            'email' => array(
                array('trim'),
                array('strtolower'),
            )
        );
    }
    
    /**
     * Check whether the current email has been assigned to an SSO system.
     * 
     * @param string $system The system to check for an assignment for.
     * @param boolean $returnEmail If set to TRUE the email will be returned, instead of true/false.
     * @return boolean TRUE if this system has an email assignment. FALSE otherwise.
     */
    public function assigned_to_sso($system, $returnEmail=false){
        // Let's loop through ALL emails
        foreach($this->where("deleted", "IS", NULL)->find_all() as $email){
            if($email->sso_email->loaded() && $email->sso_email->sso_system == $system){
                if($returnEmail){
                    return $email->email;
                } else {
                    return true;
                }
            }
        }
        return false;
    }
    
    // Check email is unique in the database.
    public function email_check_unique($email) {
        return false;
        return !(bool) ORM::factory("Account_Email")
                        ->where("id", "!=", $this->id)
                        ->where("email", "=", $email)
                        ->where("deleted", "=", NULL)
                        ->count_all() > 0;
    }

    // Gotta love __toString!
    public function __toString() {
        return ($this->email ? $this->email : "");
    }
    
    /**
     * Set the primary email for the given account!
     */
    public function set_primary($email){
        $account_id = Arr::get(Arr::get($this->_db_pending, 0), "args");
        $account_id = Arr::get($account_id, 0) == "account_email.account_id" ? Arr::get($account_id, 2) : 0;
        
        // Demote the old primary, if set.
        $oldPrimary = $this->get_active_primary();
        if($oldPrimary->loaded()){
            $oldPrimary->primary = 0;
            $oldPrimary->save();
        }
        
        // Does this email already exist?
        if($this->helper_pre_get_active()->where("email", "=", strtolower($email))->count_all() > 0){
            // It exists, update it to set primary = 1
            $curEmail = ORM::factory("Account_Email")->helper_pre_get_active()->where("account_id", "=", $account_id)->where("email", "=", strtolower($email))->find();
            $curEmail->primary = 1;
            $curEmail->verified = gmdate("Y-m-d H:i:s");
            $curEmail->save();
        } else {
            // Store the new one!
            $newEmail = ORM::factory("Account_Email");
            $newEmail->account_id = $account_id;
            $newEmail->email = $email;
            $newEmail->primary = 1;
            $newEmail->verified = gmdate("Y-m-d H:i:s");
            $newEmail->created = gmdate("Y-m-d H:i:s");
{'fraction_non_alphanumeric': 0.15046546871617233, 'fraction_numerical': 0.002164970772894566, 'mean_word_length': 2.013372472276582, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 127, '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': '5145808', 'n_tokens_mistral': 2661, 'n_tokens_neox': 2618, 'n_words': 639}