<?php
/**
 * IMPORTANT NOTE
 * The code of this system is closed and a private proprietary of Flex Solutions.
 * A written authorization/license from Flex Solutions is required before using or modifying this code.
 * Email: sales@flexsolution.biz, Phone: +962795342585, Website: http://www.flexsolutions.biz
 */
 

abstract class LsmsActiveRecord extends CActiveRecord
    {

    function yiiparam( $name, $default = null )
    {
        if (isset(Yii::app()->params[$name]))
            return Yii::app()->params[$name];
        else
            return $default;
    }

    /**
     * Prepares create_time, create_user_id, update_time and update_user_id attributes before performing validation.
     */
    protected function beforeValidate()
    {

        if ($this->isNewRecord )
        {
            //  set the create date, last updated date and the user doing the creating
            $this->created_date = $this->last_edited_date = new CDbExpression('NOW()');
            if (Yii::app()->user->isGuest)
            {
                $this->created_by = $this->last_edited_by = 'System';
            }
            else
            {
                $this->created_by = $this->last_edited_by = Yii::app()->user->id;
            }
        }
        else
        {
            ////not a new record, so just set the last updated time and last updated user id
            $this->last_edited_date = new CDbExpression('NOW()');
            if (Yii::app()->user->isGuest)
            {
                $this->last_edited_by = 'System';
            }
            else
            {
                $this->last_edited_by = Yii::app()->user->id;
            }
        }

        return parent::beforeValidate();
    }

    /*
     * return array contains label popover value
     */

    protected function attributeLabelsPopover()
    {
        return array();
    }

    /**
     * @return array scopes.
     */
    public function scopes()
    {
        return array(
            'active' => array(
                'condition' => 'status=\'Active\'',
            ),
        );
    }

    // this function will return the code prefix of a given table (this information will be taken from the setting table)
    public static function getCodePrefix( $tableName )
    {
        // note, each code_prefix in the setting table is written as tableName_code_prefix
        // get the settings as array
        $settings = Setting::getSettingsArray();

        // get the value of the setting table_name_code_prefix
        $table_code_prefix = $settings['non_ldap_persons_number_prefix'];

        return $table_code_prefix;
    }

    // this function will return the max code in a given table
    public static function getMaxCode( $tableName )
    {
        return Yii::app()->db->createCommand()
                ->select('max(number) as max_code')
                ->from($tableName)
                ->where('alias= ""')
                ->queryScalar();
    }

    // this function will generate and return a code for the given table
    public static function generateCode( $tableName, $num_of_digits )
    {
        // get the code prefix
        $code_prefix = LsmsActiveRecord::getCodePrefix($tableName);

        // get the max code
        $max_code = LsmsActiveRecord::getMaxCode($tableName);

        // check if the $max_code was null (i.e. there is no record in the database)
        $max_code = ($max_code == null) ? 0 : $max_code;

        // omit the prefix and convert it to normal integer, if there is no record in the database, no change will occur, i.e. it will remain zero (in other word, no prefix will be available to omit)
        $max_code_without_prefix = (int) str_replace($code_prefix, "", $max_code);

        // calculate the generated number (i.e. generated code without the prefix)
        $generated_code_without_prefix = $max_code_without_prefix + 1;

        // pad the number to the specified digits (e.g. if $num_of_digits=6, and the code was (34) then the padded code is 000034)
        $padded_generated_code_without_prefix = str_pad($generated_code_without_prefix, $num_of_digits, "0", STR_PAD_LEFT);

        // form the generated code by append the prefix again
        $generated_code = $code_prefix . $padded_generated_code_without_prefix;

        // return the generated code
        return $generated_code;
    }

    }
