<?php

/**
 * This is the model class for table "{{document_storage}}".
 *
 * The followings are the available columns in table '{{document_storage}}':
 * @property integer $id
 * @property integer $category_id
 * @property string $document_month
 * @property string $upload_document_name
 * @property integer $created_by
 * @property integer $created_datetime
 * @property integer $updated_by
 * @property integer $updated_date_time
 */
class DocumentStorage extends CActiveRecord {

    /**
     * @return string the associated database table name
     */
    public function tableName() {
        return '{{document_storage}}';
    }

    /**
     * @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(
            array('category_id, document_year, document_month , upload_document_name', 'required'),
            array('category_id ', 'numerical', 'integerOnly' => true),
            array('document_month', 'length', 'max' => 100),
            array('upload_document_name', 'length', 'max' => 500),
            // The following rule is used by search().
            // @todo Please remove those attributes that should not be searched.
            array('id, category_id, document_month, upload_document_name, created_by, created_datetime, updated_by, updated_date_time', 'safe', 'on' => 'search'),
        );
    }

    /**
     * @return array relational rules.
     */
    public function relations() {
        return array(
            'category' => array(self::BELONGS_TO, 'MasDocumentCategory', 'category_id'),
        );
    }

    /**
     * @return array customized attribute labels (name=>label)
     */
    public function attributeLabels() {
        return array(
            'id' => 'ID',
            'category_id' => 'Category',
            'document_month' => 'Month',
            'document_year' => 'Year',
            'upload_document_name' => 'Upload File',
            'created_by' => 'Created By',
            'created_datetime' => 'Created Datetime',
            'updated_by' => 'Updated By',
            'updated_date_time' => 'Updated Date Time',
        );
    }

    /**
     * Retrieves a list of models based on the current search/filter conditions.
     *
     * Typical usecase:
     * - Initialize the model fields with values from filter form.
     * - Execute this method to get CActiveDataProvider instance which will filter
     * models according to data in model fields.
     * - Pass data provider to CGridView, CListView or any similar widget.
     *
     * @return CActiveDataProvider the data provider that can return the models
     * based on the search/filter conditions.
     */
    public function search() {
        // @todo Please modify the following code to remove attributes that should not be searched.

        $criteria = new CDbCriteria;

        $criteria->compare('id', $this->id);
        $criteria->compare('category_id', $this->category_id);
        $criteria->compare('document_month', $this->document_month, true);
        $criteria->compare('upload_document_name', $this->upload_document_name, true);
        $criteria->compare('created_by', $this->created_by);
        $criteria->compare('created_datetime', $this->created_datetime);
        $criteria->compare('updated_by', $this->updated_by);
        $criteria->compare('updated_date_time', $this->updated_date_time);

        return new CActiveDataProvider($this, array(
            'criteria' => $criteria,
        ));
    }

    /**
     * Returns the static model of the specified AR class.
     * Please note that you should have this exact method in all your CActiveRecord descendants!
     * @param string $className active record class name.
     * @return DocumentStorage the static model class
     */
    public static function model($className = __CLASS__) {
        return parent::model($className);
    }

    public static function getMonthList() {
        $months = [];

        // Start from January of the current year
        $start = new DateTime(date('Y') . '-01-01');
        $end = (clone $start)->modify('+11 months'); // Until December

        while ($start <= $end) {
            $key = (int) $start->format('n');   // 1 to 12 (as integer)
            $value = $start->format('F');      // "January", "February", etc.
            $months[$key] = $value;
            $start->modify('+1 month');
        }

        return $months;
    }

    public static function getYearList($startOffset = -5, $yearsAhead = 10) {
        $years = [];
        $currentYear = (int) date('Y') + $startOffset;

        for ($i = 0; $i <= $yearsAhead; $i++) {
            $year = $currentYear + $i;
            $years[$year] = (string) $year;
        }

        return $years;
    }

    public function getDocumentMonthName() {
        $months = [
            1 => 'January',
            2 => 'February',
            3 => 'March',
            4 => 'April',
            5 => 'May',
            6 => 'June',
            7 => 'July',
            8 => 'August',
            9 => 'September',
            10 => 'October',
            11 => 'November',
            12 => 'December',
        ];

        return isset($months[(int) $this->document_month]) ? $months[(int) $this->document_month] : null;
    }

}
