<commit_before><?php

namespace ANDS\DOI\Model;

use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
    protected $fillable = [
        'client_name',
        'client_contact_name',
        'ip_address',
        'app_id',
        'client_contact_email',
        'datacite_symbol',
        'shared_secret'
    ];

    public $timestamps = false;

    /**
     * The table of the model
     * @var string
     */
    protected $table = "doi_client";

    /**
     * The primary key of the model,
     * used for DataciteClient::find() method
     *
     * @var string
     */
    protected $primaryKey = "client_id";

    /**
     * Returns all the domain owned by this client
     */

    public function domains()
    {
        return $this->hasMany(
            ClientDomain::class, "client_id", "client_id"
        );
    }
    

    public function addDomains($domains){
        $domArray = explode(",", $domains);
        foreach ($domArray as $d){
            $this->addDomain($d);
        }
    }


    public function addDomain($domain){
        $domain = trim($domain);
        if($domain == '')
            return;
        $dm = ClientDomain::where("client_domain", $domain)
            ->where("client_id", $this->client_id)->first();
        if ($dm != null) {
            return;
        }
        $this->domains()->save(new ClientDomain(["client_domain" => $domain]));
    }


    public function removeClientDomains(){
        ClientDomain::where("client_id", $this->client_id)->delete();
    }
    /**
     * Returns all the prefixes assigned to this client
     */

    public function prefixes()
    {
        return $this->hasMany(
            ClientPrefixes::class, "client_id", "client_id"
        );
    }

    public function getActivePrefix()
    {
        return ClientPrefixes::where("client_id", $this->client_id)->where(["active"=>true])->first();
    }

    public function addClientPrefixes($prefixes){
        $prefixArray = explode(",", $prefixes);
        foreach ($prefixArray as $p){
            $this->addClientPrefix($p);
        }
    }

    public function hasPrefix($prefix_value){
        $clientPrefixes = ClientPrefixes::where("client_id", $this->client_id)->get();
        foreach ($clientPrefixes as $clientPrefix) {
            if($prefix_value ==  $clientPrefix->prefix->prefix_value)
                return true;
        }
        return false;
    }

    /** add prefix to client */

    public function addClientPrefix($prefix_value, $active = true){

        $prefix = null;
        $prefix_value = trim($prefix_value);
        if($prefix_value == '')
            return;

        //set all other prefixes for this client as non active if this prefix is the active one
        if($active){
            ClientPrefixes::where("client_id", $this->client_id)->update(["active"=>false]);
        }

        try {
            // Get the Prefix if exists
            $prefix = Prefix::where("prefix_value", $prefix_value)->first();
            if($prefix) {
                //if this prefix already assigned to this client do nothing)
                $cp = ClientPrefixes::where("prefix_id", $prefix->id)
                    ->where("client_id", $this->client_id)->first();
                if ($cp != null) {
                    ClientPrefixes::where("prefix_id", $prefix->id)
                        ->where("client_id", $this->client_id)->update(["active"=>$active]);
                    return;
                }<commit_msg>don't add null prefix<commit_after><?php

namespace ANDS\DOI\Model;

use Illuminate\Database\Eloquent\Model;

class Client extends Model
{
    protected $fillable = [
        'client_name',
        'client_contact_name',
        'ip_address',
        'app_id',
        'client_contact_email',
        'datacite_symbol',
        'shared_secret'
    ];

    public $timestamps = false;

    /**
     * The table of the model
     * @var string
     */
    protected $table = "doi_client";

    /**
     * The primary key of the model,
     * used for DataciteClient::find() method
     *
     * @var string
     */
    protected $primaryKey = "client_id";

    /**
     * Returns all the domain owned by this client
     */

    public function domains()
    {
        return $this->hasMany(
            ClientDomain::class, "client_id", "client_id"
        );
    }
    

    public function addDomains($domains){
        $domArray = explode(",", $domains);
        foreach ($domArray as $d){
            $this->addDomain($d);
        }
    }


    public function addDomain($domain){
        $domain = trim($domain);
        if($domain == '')
            return;
        $dm = ClientDomain::where("client_domain", $domain)
            ->where("client_id", $this->client_id)->first();
        if ($dm != null) {
            return;
        }
        $this->domains()->save(new ClientDomain(["client_domain" => $domain]));
    }


    public function removeClientDomains(){
        ClientDomain::where("client_id", $this->client_id)->delete();
    }
    /**
     * Returns all the prefixes assigned to this client
     */

    public function prefixes()
    {
        return $this->hasMany(
            ClientPrefixes::class, "client_id", "client_id"
        );
    }

    public function getActivePrefix()
    {
        return ClientPrefixes::where("client_id", $this->client_id)->where(["active"=>true])->first();
    }

    public function addClientPrefixes($prefixes){
        $prefixArray = explode(",", $prefixes);
        foreach ($prefixArray as $p){
            $this->addClientPrefix($p);
        }
    }

    public function hasPrefix($prefix_value){
        $clientPrefixes = ClientPrefixes::where("client_id", $this->client_id)->get();
        foreach ($clientPrefixes as $clientPrefix) {
            if($prefix_value ==  $clientPrefix->prefix->prefix_value)
                return true;
        }
        return false;
    }

    /** add prefix to client */

    public function addClientPrefix($prefix_value, $active = true){

        $prefix = null;
        $prefix_value = trim($prefix_value);
        if($prefix_value == null || $prefix_value == '')
            return;

        //set all other prefixes for this client as non active if this prefix is the active one
        if($active){
            ClientPrefixes::where("client_id", $this->client_id)->update(["active"=>false]);
        }

        try {
            // Get the Prefix if exists
            $prefix = Prefix::where("prefix_value", $prefix_value)->first();
            if($prefix) {
                //if this prefix already assigned to this client do nothing)
                $cp = ClientPrefixes::where("prefix_id", $prefix->id)
                    ->where("client_id", $this->client_id)->first();
                if ($cp != null) {
                    ClientPrefixes::where("prefix_id", $prefix->id)
                        ->where("client_id", $this->client_id)->update(["active"=>$active]);
                    return;
                }
{'fraction_non_alphanumeric': 0.13949507916131793, 'fraction_numerical': 0.0, 'mean_word_length': 2.046046915725456, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 69, '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': '1316567', 'n_tokens_mistral': 1983, 'n_tokens_neox': 1928, 'n_words': 433}