<?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 SubscribersSchemaModel extends Model {
    public function getSchema($params = array()) {
        extract($params);
        if (!empty($primaryID))
            $conditionArry = array(
                '#subscID' => $primaryID
            );
        $conditionArry = isset($conditionArry) ? $conditionArry : '';
        $schema        = array(
            'section' => 'subscribers',
            'name' => 'subscribers',
            'pattern_id' => 'subscribers',
            'key' => array(
                'subscID'
            ),
            'order' => 0,
            'table' => 'subscribers',
            'alt_table' => '',
            'condition' => $conditionArry,
            'references' => '',
            'export_fields' => array(
                'subscID' => array(
                    'db_field' => 'subscID',
                    'alt_key' => true
                ),
                'userID' => array(
                    'db_field' => 'userID',
                    'validateIt' => array(
                        'users',
                        'userID',
                        " userStatus='Active'"
                    )
                ),
                'subscEmailID' => array(
                    'db_field' => 'subscEmailID',
                    'pre_insert' => array(
                        'fn_put_checkExists',
                        '#this',
                        'subscribers',
                        'subscEmailID',
                        " subscStatus!='Deleted'",
                        'subscID',
                        '$subscID'
                    ),
                    'validate' => array(
                        'required',
                        'email'
                    ),
                    'error' => array(
                        'Subscriber emailID is missing',
                        'Invalid EmailID'
                    )
                ),
                'contactPhone' => array(
                    'db_field' => 'contactPhone'
                ),
                'remoteAddress' => array(
                    'db_field' => 'remoteAddress'
                ),
                'subscStatus' => array(
                    'db_field' => 'subscStatus',
                    'validate' => array(
                        'required'
                    ),
                    'error' => array(
                        'Subscriber Status is required'
                    )
                ),
                'subscribedDate' => array(
                    'db_field' => 'subscribedDate',
                    'validate' => array(
                        'required'
                    ),
                    'error' => array(
                        'Subscribed Date is required'
                    )
                ),
                'unsubscrDate' => array(
                    'db_field' => 'unsubscrDate'
                ),
                'languageID' => array(
                    'db_field' => 'languageID',
                    'validateIt' => array(
                        'languages',
                        'languageID',
                        " languageStatus='Active'"
                    )
                )
            )
        );
        $get           = isset($get) ? $get : '';
        $schema        = $this->setFilter($get, $conditionArry, $schema);
        return $schema;
    }
    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']        = isset($conditionArry) ? $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;
    }
}
