<?php

class CertificateAuthority extends PhonalisaRecord
{

    public $passphrase;
    public $countryName;
    public $stateName;
    public $cityName;
    public $companyName;
    public $orgUnitName;
    public $commonName;
    public $eMailAddress;

    public static function myAttributes()
    {
        return array(
            'passphrase' => 'passphrase',
            'countryName' => 'countryName',
            'stateName' => 'stateName',
            'cityName' => 'cityName',
            'companyName' => 'companyName',
            'orgUnitName' => 'orgUnitName',
            'commonName' => 'commonName',
            'eMailAddress' => 'eMailAddress'
        );
    }

    /**
     * Returns the static model of the specified AR class.
     * @param string $className active record class name.
     * @return Job the static model class
     */
    public static function model($className = __CLASS__)
    {
        return parent::model($className);
    }

    /**
     * @return array validation rules for model attributes.
     */
    public function rules()
    {
        // NOTE: you should only define rules for those attributes that
        // will receive user inputs.
        return array(
            // The following rule is used by search().
            // Please remove those attributes that should not be searched.
            array('countryName, stateName, cityName, companyName,'
                . 'orgUnitName, commonName, eMailAddress, passphrase', 'safe'),
            array('countryName', 'length', 'max' => 2),
            array('commonName', 'length', 'max' => 64),
            array('eMailAddress', 'length', 'max' => 40),
            array('countryName, stateName, cityName, companyName,'
                . 'orgUnitName, commonName, eMailAddress', 'required'),
            array('passphrase', 'required', 'except' => 'phasephraseSaved'),
            array('commonName', 'ext.PhonalisaValidators.domainName'),
            array('eMailAddress', 'email'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels()
    {
        return array(
            'passphrase' => __('NETWORK_CERTIFICATE_SETTINGS_PASSPHRASE'),
            'countryName' => __('NETWORK_CERTIFICATE_SETTINGS_COUNTRY_NAME'),
            'stateName' => __('NETWORK_CERTIFICATE_SETTINGS_STATE_NAME'),
            'cityName' => __('NETWORK_CERTIFICATE_SETTINGS_CITY_NAME'),
            'companyName' => __('NETWORK_CERTIFICATE_SETTINGS_COMPANY_NAME'),
            'orgUnitName' => __('NETWORK_CERTIFICATE_SETTINGS_ORG_UNIT_NAME'),
            'commonName' => __('NETWORK_CERTIFICATE_SETTINGS_COMMON_NAME'),
            'eMailAddress' => __('NETWORK_CERTIFICATE_SETTINGS_EMAIL_ADDRESS')
        );
    }

    public static function getCommonName()
    {
        $ca = CertificateAuthority::model()->find(array(), "id", 1, 0);
        $commonName = '';
        if ($ca) {
            $commonName = $ca[0]->commonName;
        }
        return $commonName;
    }
}
