<?php
/***************************************************************************
 *																		*
 * Copyright (c) 2005 ISPG Technologies (I) Pvt Ltd. All rights reserved.	*
 *																		  *
 * This  is  commercial  software,  only  users  who have purchased a valid *
 * license  and  accept  to the terms of the  License Agreement can install *
 * and use this program.													*
 *																		  *
 ****************************************************************************
 * PLEASE READ THE FULL TEXT  OF THE SOFTWARE  LICENSE   AGREEMENT  IN  THE *
 * "copyright.txt" FILE PROVIDED WITH THIS DISTRIBUTION PACKAGE.			*
 ****************************************************************************/ //
//$Id: SchemaModel.php, v 1.1 2012/12/24 20:52:11
//
class AttributeSchemaModel extends Model {
    public function getSchema($params = array()) {
        extract($params);
        if (!empty($primaryID)) {
            $conditionArry = array(
                'attributeID' => $primaryID
            );
        }
       
        $conditionArry = isset($conditionArry) ? $conditionArry : '';
        $schema        = array(
            'section' => 'attributes',
            'name' => 'attributes',
            'pattern_id' => 'attributes',
            'key' => array(
                'attributeID'
            ),
            'order' => 0,
            'table' => 'attributes',
            'alt_table' => '',
            'condition' => $conditionArry,
            'references' => array(
                'attribute_details' => array(
                    'reference_fields' => array(
                        'attributeID' => '#key',
                        'languageID' => '1'
                    ),
                    'join_type' => 'LEFT'
                )
            ),
            'export_fields' => array(
                'attributeID' => array(
                    'db_field' => 'attributeID',
                    'alt_key' => true
                ),
                'attributeKey' => array(
                    'db_field' => 'attributeKey',
                    'validate' => array(
                        'required'
                    ),
                    'error' => array(
                        'attributeKey is required'
                    )
                ),
                'attrInputType' => array(
                    'db_field' => 'attrInputType',
                    'validate' => array(
                        'required'
                    ),
                    'error' => array(
                        'attrInputType is required'
                    )
                ),
                'attrName' => array(
                    'table' => 'attribute_details',
                    'db_field' => 'attrName',
                    'process_get' => array(
                        'fn_html_entity_decode',
                        '#this'
                    ),
                    'validate' => array(
                        'required'
                    ),
                    'error' => array(
                        'attrName is required'
                    )
                )
            )
        );
        $get           = isset($get) ? $get : '';
        $conditionArry = isset($conditionArry) ? $conditionArry : '';
        $schema        = $this->setFilter($get, $conditionArry, $schema);
        return $schema;
    }
    function fn_put_checkKeyExist($data) {
        if ($this->ObjDatabase->checkExist('users', 'userEmail', $data, " userStatus!='Deleted' ") == 'YES') {
            echo 'Email Already Exist';
            exit;
        }
    }
    function setFilter($get, $conditionArry, $schema) {
        if (is_array($get)) {
            extract($get);
        }
        if (!empty($filter)) {
            foreach ($filter as $filtr) {
                if ($filtr['eq'] != '') {
                    $conditionArry[$filtr['key']]['eq'] = $filtr['eq'];
                } else if ($filtr['neq'] != '') {
                    $conditionArry[$filtr['key']]['neq'] = $filtr['neq'];
                } else if ($filtr['lt'] != '') {
                    $conditionArry[$filtr['key']]['lt'] = $filtr['lt'];
                } else if ($filtr['gt'] != '') {
                    $conditionArry[$filtr['key']]['gt'] = $filtr['gt'];
                } else if ($filtr['lteq'] != '') {
                    $conditionArry[$filtr['key']]['lteq'] = $filtr['lteq'];
                } else if ($filtr['gteq'] != '') {
                    $conditionArry[$filtr['key']]['gteq'] = $filtr['gteq'];
                } else if ($filtr['in'] != '') {
                    $conditionArry[$filtr['key']]['in'] = $filtr['in'];
                } else if ($filtr['nin'] != '') {
                    $conditionArry[$filtr['key']]['nin'] = $filtr['nin'];
                } else if ($filtr['llike'] != '') {
                    $conditionArry[$filtr['key']]['llike'] = $filtr['llike'];
                } else if ($filtr['rlike'] != '') {
                    $conditionArry[$filtr['key']]['rlike'] = $filtr['rlike'];
                } else if ($filtr['like'] != '') {
                    $conditionArry[$filtr['key']]['like'] = $filtr['like'];
                }
            }
        }
        $schema['condition']        = $conditionArry;
        $schema['resultPerRequest'] = isset($resultPerRequest) ? $resultPerRequest : '';
        if (isset($page) && $page != '' && $page >= 0) {
            $schema['page'] = $page;
            $schema['from'] = $page * $resultPerRequest;
            $schema['to']   = $resultPerRequest;
        } else if (isset($from) && $from != '' && $to != '') {
            $schema['from'] = $from;
            $schema['to']   = $to;
        } else if (isset($limit) && $limit != '' && $limit >= 0) {
            $schema['limit'] = $limit;
        }
        if (isset($order) && $order != '') {
            $schema['order'] = $order;
            if ($dir != '') {
                $schema['dir'] = $dir;
            } else {
                $schema['dir'] = 'ASC';
            }
        }
        return $schema;
    }
}
	