<?php

class locale_Astrology Extends locale_matchMaking {

    protected static $USERID = "602531"; //
    protected static $API_KEY = "d8c14ac1bfc3449025931bbfdaf96048";
////    protected static $USERID = "602631"; // navin@ninehertzindia.com
////    protected static $API_KEY = "7a5736bd7b5864f21fd90d27a8cd800f";// navin@ninehertzindia.com
//    protected static $USERID = "602717"; // navin@ninehertzindia.com
//    protected static $API_KEY = "531364d481fdf3203bddbc7c646967d2";// anant@theninehertz.com
    protected static $baseUrl = "http://json.astrologyapi.com/v1/";
    protected static $data = array();
    protected  static $lang='en';
    
    function __construct($name, $date, $month, $year, $hour, $minute, $latitude, $longitude, $timezone) {
       
        require_once 'VedicRishiClient.php';
    }

    function makeHoroData($rs) {
        return array(
            'day' => $rs['date'],
            'month' => $rs['month'],
            'year' => $rs['year'],
            'hour' => $rs['hour'],
            'min' => $rs['minute'],
            'lat' => $rs['latitude'],
            'lon' => $rs['longitude'],
            'tzone' => $rs['timezone'],
            'name' => $rs['fname'] . " " . $rs['lname']
        );
    }

    function getCurlReponse($userId, $apiKey, $resource, array $data, $language = "en") {
        $apiEndPoint = "http://json.astrologyapi.com/v1";

        $serviceUrl = $apiEndPoint . '/' . $resource . '/';

        $authData = $userId . ":" . $apiKey;

        $ch = curl_init();
        curl_setopt($ch, CURLOPT_URL, $serviceUrl);
        curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);

        curl_setopt($ch, CURLOPT_TIMEOUT, 20);
        curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);

        $header[] = 'Authorization: Basic ' . base64_encode($authData);
        /* Setting the Language of Response */
        if ($language != NULL) {
            array_push($header, 'Accept-Language: ' . $language);
        }

        curl_setopt($ch, CURLOPT_HTTPHEADER, $header);
        curl_setopt($ch, CURLOPT_POST, 1);
        curl_setopt($ch, CURLOPT_POSTFIELDS, http_build_query($data));

        $response = curl_exec($ch);


        if (curl_error($ch)) {
            $msg = curl_error($ch);
            $arr = array("status" => false, "msg" => $msg);
            curl_close($ch);
            echo json_encode($arr);
            exit;
            echo 'Request Error:' . curl_error($ch);
            exit;
        }

        $respJson = json_decode($response);

        if (array_key_exists("status", $respJson)) {
            if (!$respJson->status) {
                $arr = array("status" => false, "msg" => $respJson->msg);
                echo json_encode($arr);
                exit;
            }
        } else {
            return $response;
        }
    }

    function setDefaultChart($userid, $device_token, $deviceType) {
        global $db;
        $status = false;
        if (is_object($rs))
            $rs = get_object_vars($rs);

        if ($userid > 0)
            $cond = " and userid=" . $userid;
        else
            $cond = " and device_token='" . $device_token . "' and device_type='" . $deviceType . "'";

        $sqlChk = "select id from user_kundali where 1" . $cond;
        $resultChk = $db->query($sqlChk);
        if ($resultChk->size() > 0) {
            return 0;
        } else
            return 1;
    }
    
    function updateKundli($kundaliID,$rs,$data,$rawData){
        global $db;
        $userid = $rs['userid'];
        $device_token = $rs['device_token'];
        $deviceType = $rs['device_type'];
        $name = $rs['fname'] . " " . $rs['lname'];
        $birthDate = date("Y-m-d", strtotime($rs['year'] . "-" . $rs['month'] . "-" . $rs['date']));
        $birthTime = $rs['hour'] . ":" . $rs['minute'];
        $location = $rs['location'];
        $lat = $rs['latitude'];
        $lng = $rs['longitude'];
        $timeZone = $rs['timezone'];
        $gender = strtoupper($rs['gender']);
        //$kundaliID = intval($rs['kundali_id']);
        $isDefault = 0;
       
        
//        $data = json_decode($data);
           $kundali = $data->data;
        if (intval($userid) > 0 or $device_token != '') 
           
            $userid = (intval($userid) > 0) ? $userid : -1;
              $sql = "update user_kundali set " .
                " userid ='" . $userid . "'," .
                "dtdate='" . nowDate() . "'," .
                "name='" . $name . "'," .
                "gender='" . $gender . "'," .
                "birth_date='" . ($birthDate) . "'," .
                "birth_time='" . $birthTime . "'," .
                "location='" . $location . "'," .
                "lat='" . $lat . "'," .
                "lng='" . $lng . "'," .
                "timezone='" . $timeZone . "'," .
                "device_token='" . $device_token . "'," .
                "device_type='" . $deviceType . "'," .
                "data='" . json_encode($kundali) . "',raw_data='".$rawData."' where id=".$kundaliID;
            
        
                $result = $db->query($sql);
                return $kundaliID;
                //$kundaliID = $result->insertID();
    }

    function saveKundali($rs, $data,$rawData) {
        global $db;
       
        $userid = $rs['userid'];
        $device_token = $rs['device_token'];
        $deviceType = $rs['device_type'];
        $name = $rs['fname'] . " " . $rs['lname'];
        $birthDate = date("Y-m-d", strtotime($rs['year'] . "-" . $rs['month'] . "-" . $rs['date']));
        $birthTime = $rs['hour'] . ":" . $rs['minute'];
        $location = $rs['location'];
        $lat = $rs['latitude'];
        $lng = $rs['longitude'];
        $timeZone = $rs['timezone'];
        $gender = strtoupper($rs['gender']);
        $kundaliID = intval($rs['kundali_id']);
        $isDefault = 0;
        $data = json_decode($data);
       
        if ($data->status == true) {
            $kundali =  $data->data;

            if (intval($userid) > 0 or $device_token != '') {
                $userid = (intval($userid) > 0) ? $userid : -1;

                if ($kundaliID>0){
                    $kundaliID    =   self::updateKundli($kundaliID,$rs,$data,$rawData);
                }else{
                
                    if ($userid > 0)
                        $cond = " and userid=" . $userid;
                    else
                        $cond = " and device_token='" . $device_token . "' and device_type='" . $deviceType . "'";

                    $sqlChk = "select * from user_kundali where birth_date='" . $birthDate . "'" .
                            " and birth_time='" . $birthTime . "' and lat='" . $lat . "' and lng='" . $lng . "'" . $cond;

                    $resultChk = $db->query($sqlChk);
                    if ($resultChk->size() <= 0) {
                        $isDefault = self::setDefaultChart($userid, $device_token, $deviceType);

                         $sql = "insert into user_kundali(userid,dtdate,name,gender,birth_date,birth_time,location,lat,lng,timezone," .
                                "device_token,device_type,is_default,data,raw_data)values(" .
                                "" . $userid . "," .
                                "'" . nowDate() . "'," .
                                "'" . $name . "'," .
                                "'" . $gender . "'," .
                                "'" . ($birthDate) . "'," .
                                "'" . $birthTime . "'," .
                                "'" . $location . "'," .
                                "'" . $lat . "'," .
                                "'" . $lng . "'," .
                                "'" . $timeZone . "'," .
                                "'" . $device_token . "'," .
                                "'" . $deviceType . "'," .
                                "'" . $isDefault . "'," .
                                "'" . json_encode($kundali) . "',".
                                "'" . ($rawData) . "')";
                                
                        $result = $db->query($sql);
                        $kundaliID = $result->insertID();
                    } else {
                        $rs = $resultChk->fetch();
                        $kundaliID = $rs['id'];
                    }
                }
            }
        }
        return $kundaliID;
    }
    

    function getPlanetNo($name) {
        global $db;
        $planetNo = "-1";
        $sql = "select planet_no from planets where UPPER(name)='" . strtoupper($name) . "'";
        $result = $db->query($sql);
        if ($result->size() > 0) {
            $rs = $result->fetch();
            $planetNo = $rs['planet_no'];
        }
        return $planetNo;
    }

    function chartData($data) {
        global $lang;
        self::$lang =   $lang;
        $name = $data['year'] . $data['month'] . $data['date'] . "-" . $data['hour'] . $data['minute'];
        $fileName = "../uploads/" . $name . ".json";
        $data1 = array();

        if (file_exists($fileName)) {

            $resp = file_get_contents($fileName);
            $jsonData = ($resp);
            $natel = 1;
        } else {

            $natel = self::getBasicChart($data);
            $d9 = self::getHoroD9($data);
			

            $data1['data']['natel'] = $natel;
            $data1['data']['d9'] = $d9;
            $jsonData = json_encode($data1);
            if (!empty($natel)) {
//                                $f  = fopen($fileName, "w");
//                                fwrite($f,($jsonData));
//                                fclose($f);
            }
        }
         
		
        if (!empty($natel)) {
            $resp = self::calcChart($jsonData);
        }
       
        if ($data['save'] or $data['save'] == "1"){
            $kundaliID = self::saveKundali($data,$resp, $jsonData);
			
		}

        $resp = json_decode($resp);
        $resp->data->kundli_id = $kundaliID;

//        print_r($resp);
//        exit;

        return ($resp);
    }

    function getLagna($data) {
        global $db;
        $lagnaData = array();
        $yogkarkaPlante = "";

        foreach ($data as $val) {
            if (strtoupper($val->name) == "ASCENDANT") {
                $lagnaData = $val;
            }
        }
        $sql = "select * from lagna L inner join lang_planets_name P ON planet_no=L.yoga_karka where lagna_sign=" .
                "(select sign_no from signs where UPPER(name)='" . strtoupper($lagnaData->sign) . "') and lang_type='".self::$lang."'";
        
        $result = $db->query($sql);
        if ($result->size() > 0) {
            $rs = $result->fetch();
            $yogkarkaPlante = (!is_null($rs['yoga_karka'])) ? $rs['name'] : "";
        }
        $lagnaData->yogkarka_planet = strtoupper($yogkarkaPlante);
        $nakshtraArr    =   self::getNakshatraName($lagnaData->nakshatra);
        $lagnaData->nakshatra_name = $nakshtraArr['short_code'];
        $lagnaData->nakshatra = $nakshtraArr['name'];

        return $lagnaData;
    }

    function getNakshatraShortName($nameEn){
        global $db;
        
        $name   =   "";
         $sql    =   "select short_name from nakshatra_list where name = '$nameEn'";
        $result =   $db->query($sql);
        if ($result->size()>0){
            $rs =   $result->fetch();
            $name=  $rs['short_name'];
        }
        return $name;
    }
    /*
     * get karak list
     */

    function getKarakList() {
        global $db;
        $sql = "select * from lang_Jaimini_Sutras where lang_type='".self::$lang."'";
        $result = $db->query($sql);
        $return = [];
        while ($row = $result->fetch()) {
            $return[] = $row['name'];
        }
        return $return;
    }
    function getKarakListEn() {
        global $db;
        $sql = "select * from Jaimini_Sutras ";
        $result = $db->query($sql);
        $return = [];
        while ($row = $result->fetch()) {
            $return[] = $row['name'];
        }
        return $return;
    }

    function getKarka($chart,$lng="") {
        $degre = [];
        foreach ($chart as $val) {
            $name = $val->name;
            if (strtoupper($name) == "RAHU" or strtoupper($name) == "KETU")
                continue;
            else
                $degre[$name] = $val->normDegree;
        }
        arsort($degre);
        $return = [];
        $karkaListEn = self::getKarakListEn();
        $karkaList = self::getKarakList();
        $i = 0;
        
        foreach ($degre as $key => $val) {
            if ($key == "Ascendant")
                continue;
            $return['otlg'][$key] = isset($karkaList[$i]) ? $karkaList[$i] : '';
            $return['en'][$key] = isset($karkaListEn[$i]) ? $karkaListEn[$i] : '';
            
            $i++;
        }
        
        
        return $return;
    }

    /*
     * get Aspects List
     */

    function getAspectsHouse() {
        global $db;
        $sql = "select * from aspects ";
        $result = $db->query($sql);
        $return = [];
        while ($row = $result->fetch()) {
            $return[$row['planet_name']] = explode(',', $row['aspacts']);
        }
        return $return;
    }

    function getAspects($chart) {
        // get Aspacts house list
        $pAspects = self::getAspectsHouse();
        $planetHouseAspects = [];
        $planetHouse = [];
        foreach ($chart as $key => $val) {
            if ($val->name == "Ascendant")
                continue;
            $house = $val->house;
            $planetHouse[$house][] = $val->name;
            $planetHouseAspects[$val->name]['house'] = $house;
            $aspectLists = isset($pAspects[$val->name]) ? $pAspects[$val->name] : [];
            // set aspects houses
            foreach ($aspectLists as $al) {
                $houes_no = $al + $house - 1;
                if ($houes_no > 12)
                    $houes_no = $houes_no - 12;
                $planetHouseAspects[$val->name]['aspect_house'][] = $houes_no;
            }
        }
//        echo "<pre>";
//        print_r($planetHouseAspects);
//        die;
        //aspect planet
        $aspect_planet = [];
        foreach ($planetHouseAspects as $planet => $vals) {
            foreach ($vals['aspect_house'] as $ho) {
                if (count($planetHouse[$ho]) > 0) {
                    foreach ($planetHouse[$ho] as $asph) {
                        $planetHouseAspects[$planet]['aspect_planet'][] = $asph;
                        $aspect_planet[$planet][] = $asph;
                    }
                }
            }
        }

//        aspect planet reverse
        foreach ($aspect_planet as $key => $pls) {
            foreach ($pls as $pl) {
                $planetHouseAspects[$pl]['aspect_planet_reverse'][] = $key;
            }
        }
//        echo "<pre>";
//        print_r($planetHouseAspects);die;

        $houes_planet_asc = [];

        foreach ($planetHouseAspects as $key => $val) {
            if (isset($val['aspect_house'])) {
                foreach ($val['aspect_house'] as $h) {
                    $houes_planet_asc[$h][] = $key;
                }
            }

//            $houes_planet_asc[$val['house']] = array_unique($houes_planet_asc[$val['house']]);
//            $aspect_planet = isset($val['aspect_planet'])?$val['aspect_planet']:[];
//            $aspect_planet_reverse = isset($val['aspect_planet_reverse'])?$val['aspect_planet_reverse']:[];
//            if(count($aspect_planet_reverse) > 0){
//                if(isset($houes_planet_asc[$val['house']])){
//                    $houes_planet_asc[$val['house']] = array_merge($houes_planet_asc[$val['house']], $aspect_planet_reverse);
//                }else{
//                    $houes_planet_asc[$val['house']] = $aspect_planet_reverse;
//                } 
//                $houes_planet_asc[$val['house']] = array_unique($houes_planet_asc[$val['house']]);
//            }
//            $aspect_planet_marge = array_merge($aspect_planet, $aspect_planet_reverse);
//            $aspect_planet_marge = array_unique($aspect_planet_marge);
//            $planetHouseAspects[$key]['aspect_planet_merge'] =  $aspect_planet_marge;
        }
        
//        echo "<pre>";
//        print_r($planetHouseAspects);
//        print_r($houes_planet_asc);
//        die;
        foreach ($planetHouseAspects as $key => $val) {
            if(isset($houes_planet_asc[$val['house']])){
                $planetHouseAspects[$key]['aspect_planet'] = $houes_planet_asc[$val['house']];
            }else{
                $planetHouseAspects[$key]['aspect_planet'] = [];
            }
        }
       // echo "<pre>";
        //print_r($houes_planet_asc);
       // print_r($planetHouseAspects);
        //exit;
        return [$planetHouseAspects, $houes_planet_asc];
    }

    function calcChart($resp,$lang="en") {
        global $db;
        $signs = array();
        $nakshatra = "";
        if ($lang!='en')
            self::$lang=$lang;
        $resp = json_decode($resp);

        $isVargottam = 0;
       
        $lagna = self::getLagna($resp->data->natel);
        $signs['lagna'] = $lagna;

        list($aspects_sign, $aspects_house) = self::getAspects($resp->data->natel);

       // print_r($aspects_sign);die;

        $houseDetail = self::getHouseData($aspects_house);
        $signs['house_detail'] = $houseDetail;

        $karka = self::getKarka($resp->data->natel);
        
       


        foreach ($resp->data->natel as $val) {

            $signName = strtoupper($val->sign);
            if ($val->name == "Ascendant")
                break;
            $sql = "select * from signs where UPPER(name)='" . $signName . "'";
            $result = $db->query($sql);
            $rs = $result->fetch();
            $val->planet_no = self::getPlanetNo($val->name);
            $val->display_house_no = $rs['sign_no'];
            $val->house = $val->house . "";

            if (isset($aspects_sign[$val->name]) && $aspects_sign[$val->name]['aspect_planet']) {
                $val->aspect_planet = $aspects_sign[$val->name]['aspect_planet'];
            } else {
                $val->aspect_planet = [];
            }

//            $found_key = array_search($val->house,array_column($houseDetail, 'house_no'));
//            $val->house_detail      =   $houseDetail[$found_key];

            $val->is_nakshatra = (strtoupper($val->name) == "MOON") ? 1 : 0;
            $val->is_vargottam = self::checkVargottam($resp->data->d9, $signName, $val->name);
            $val->is_combust = ($val->is_planet_set) ? 1 : 0;
            $val->is_yogkarka = ($lagna->yogkarka_planet == strtoupper($val->name)) ? 1 : 0;
            $val->is_exaltation_sign = 0;
            $val->is_debilitation_sign = 0;

            $val->is_mooltrikona_sign = 0;
            $val->significations = "";
            // no 10
            self::getSignS($val);

            $val->is_own_house_planet = self::getOwnHousePlanet($val->display_house_no, $val->planet_no);

            $val->shadbala = "71";
            $val->strength = "371";
            $nakshtraArr    =   self::getNakshatraName($val->nakshatra);
            $val->nakshatra_name = $nakshtraArr['short_code'];
            $val->nakshatra_en = self::getNakshatraShortName(strtolower($val->nakshatra));
            $val->nakshatra = $nakshtraArr['name'];
            
            $val->karak_name = isset($karka['otlg'][$val->name]) ? $karka['otlg'][$val->name] : '';
            $val->karak_name_en = isset($karka['en'][$val->name]) ? $karka['en'][$val->name] : '';

            $signs['chart'][] = $val;
        }

        //print_r($signs);die;

        $arr = array("status" => true, "msg" => "Chart Data Generated.", "data" => $signs);
        return json_encode($arr);
    }

    function planetOwnsHouse($planetNo) {
        global $db;
    }

    function getHouseData($aspects_house = []) {
        global $db;
        $houseData = array();
         $sql = "select house_no,H.significations,category,plannet as significator_planet,P.name as planet_name " .
                " from lang_house H INNER join planets P on P.planet_no=H.plannet where lang_type='".self::$lang."' order by house_no";
        //$sql    =   "select * from house where house_no=".$houseNo;
        $result = $db->query($sql);
        if ($result->size() > 0) {
            while ($rs = $result->fetch()) {
                if (isset($aspects_house[$rs['house_no']])) {
                    $rs['house_aspect'] = $aspects_house[$rs['house_no']];
                } else {
                    $rs['house_aspect'] = [];
                }
                $houseData[] = $rs;
            }
        }
//        print_r($houseData);
//        exit;
        return $houseData;
    }

    function getNakshatraName($name) {
        global $db;
        $nakshtraArr=array();
//        $name = strtolower($name);
//        $nameArr = explode(" ", $name);
//        if (count($nameArr) > 1) {
//            $nm1 = substr($nameArr[0], 0, 2);
//            $nm2 = substr($nameArr[1], 0, 2);
//            $nakshtra = $nm1 . "-" . $nm2;
//        } else {
//            $nm1 = substr($nameArr[0], 0, 4);
//            $nakshtra = $nm1;
//        }
//        return strtoupper($nakshtra);
            $sql = "select * from lang_nakshatras_name where nakshatra_id=(select id from nakshatra_list where name = '$name') and lang_type='".self::$lang."'";
        $result = $db->query($sql);
        $planet = [];
        while ($rs = $result->fetch()) {
            $nakshtraArr['short_code']=$rs['short_code'];
            $nakshtraArr['name']=$rs['name'];
        }
        return $nakshtraArr;
    }

    function getSignS(&$val) {
        global $db;
        $sql = "select * from planets where planet_no = " . $val->planet_no;

        $result = $db->query($sql);
        $planet = [];
        while ($rs = $result->fetch()) {
            $planet = $rs;
        }

        if (count($planet) > 0) {
            if ($planet['exaltation_sign'] == $val->display_house_no) {
                $val->is_exaltation_sign = 1;
            }
            if ($planet['debilitation_sign'] == $val->display_house_no)
                $val->is_debilitation_sign = 1;

            if ($planet['mooltrikona_sign'] == $val->display_house_no)
                $val->is_mooltrikona_sign = 1;
            $val->significations = $planet['significations'];
            $sql = "select * from lang_planets where planet_no = " . $val->planet_no." and lang_type='".self::$lang."'";
            $result1 = $db->query($sql);
            if ($result1->size()>0){
                $rs1    =   $result1->fetch();
                $val->significations = $rs1['significations'];
            }
        }
    }

    function getBasicChart($data) {
        $data = self::makeHoroData($data);

        $resourceName = 'planets';
        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);

        return json_decode($resp);
    }

    function checkVargottam($d9Data, $sign, $planet) {
        $isVargottam = 0;
        foreach ($d9Data as $val) {

            if (strtoupper($sign) == strtoupper($val->sign_name)) {
                foreach ($val->planet as $planetVal)
                    if (strtoupper($planetVal) == strtoupper($planet)) {
                        $isVargottam = 1;
                        break 1;
                    }
            }
        }
        return $isVargottam;
    }

    function checkYogakaraka($lagna, $planet) {
        global $db;
        $sql = "select * from lagna where lagna_sign=" . $lagna;
        foreach ($d9Data as $val) {

            if (strtoupper($sign) == strtoupper($val->sign_name)) {
                foreach ($val->planet as $planetVal)
                    if (strtoupper($planetVal) == strtoupper($planet)) {
                        $isVargottam = 1;
                        break 1;
                    }
            }
        }
        return $isVargottam;
    }

    function getHoroD9($data) {
        $data = self::makeHoroData($data);

        $resourceName = 'horo_chart/D9';
        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);

        return json_decode($resp);
    }

//    function divisionalChart($rs){
//        global $db;
//        $signs      =   array();
//        $nakshatra  =   "";
//        $resp       =   self::getDivisionalChart($rs);
//        if (!empty($resp)){
//            $lagna                  =   self::getLagna($resp->data->natel);
//            $signs['lagna']         = $lagna;
//            //$resp->nakshatra_name = self::getNakshatraName($resp->nakshatra);
//      
//            foreach ($resp->data->natel as $val){
//                $signName=strtoupper($val->sign);
//                if ($val->name=="Ascendant")
//                    break;
//                $sql    ="select * from signs where UPPER(name)='".$signName."'";
//                $result =   $db->query($sql);
//                $rs =   $result->fetch();
//                $val->planet_no         =   self::getPlanetNo($val->name);
//                $val->display_house_no  =   $rs['sign_no'];
//                $val->house             =   $val->house."";
//                $val->is_nakshatra      =   (strtoupper($val->name)=="MOON")?1:0;
//                $val->is_vargottam      =   self::checkVargottam($resp->data->d9,$signName,$val->name);
//                $val->is_combust        =   ($val->is_planet_set)?1:0;
//                $val->is_yogkarka       =   ($lagna->yogkarka_planet==strtoupper($val->name))?1:0;            
//                $val->is_exaltation_sign = 0;
//                $val->is_debilitation_sign = 0;
//
//                $val->is_mooltrikona_sign = 0;  
//                $val->significations = "";
//                // no 10
//                self::getSignS($val);
//
//                $val->is_own_house_planet       = self::getOwnHousePlanet($val->display_house_no, $val->planet_no);   
//
//                $val->shadbala  = "71";
//                $val->strength  = "371";
//    //            $val->nakshatra  = "KRIT";
//                $val->nakshatra_name     = self::getNakshatraName($val->nakshatra);
//
//                $signs['chart'][]    =   $val;
//            }
//        }
//        
////        print_r($signs);die;
//    
//            $arr    =   array("status"=>"true","msg"=>"Chart Data Generated.","data"=>$signs);
//            return json_encode($arr);
//    
//    }
//    
//    function getDivisionalChart($rs){
//        $data = self::makeHoroData($rs);
//        $chartType  =   $rs['chart_type'];
//       /*
//        chalit : For Chalit Chart,
//SUN : For Sun Chart,
//MOON : For Moon Chart,
//D1 : For Brith Chart,
//D2 : For Hora Chart,
//D3 : For Dreshkan Chart,
//D4 : For Chathurthamasha Chart,
//D5 : For Panchmansha Chart,
//D7 : For Saptamansha Chart,
//D8 : For Ashtamansha Chart,
//D9 : For Navamansha Chart,
//D10 : For Dashamansha Chart,
//D12 : For Dwadashamsha chart,
//D16 : For Shodashamsha Chart,
//D20 : For Vishamansha Chart,
//D24 : For Chaturvimshamsha Chart,
//D27 : For Bhamsha Chart,
//D30 : For Trishamansha Chart,
//D40 : For Khavedamsha Chart,
//D45 : For Akshvedansha Chart,
//D60 : For Shashtymsha Chart,
//chalit : For Chalit Chart
//        */
//        $resourceName = 'horo_chart/'.$chartType;
//        $resp   =   "";
//        $resp=self::getCurlReponse(self::$USERID,self::$API_KEY,$resourceName,$data);
//       
//        return json_decode($resp);
//    }
//    
    function getPlanets() {
        global $db;
        $sql = "select planet_no,name from planets order by planet_no";
        $result = $db->query($sql);
        while ($rs = $result->fetch()) {
            $data[] = $rs;
        }
        //$arr    =   array("status"=>"true","msg"=>"Chart Data Generated.","data"=>$data);
        echo json_encode($data);
    }

    function getOwnHousePlanet($houes, $planet) {
        global $db;
        $sql = "select lordship from signs where sign_no = $houes";
        $result = $db->query($sql);
        while ($rs = $result->fetch()) {
            if ($planet == $rs['lordship'])
                return 1;
        }
        return 0;
    }

    function getPartnerReport($rs) {
        $data = self::makeHoroData($rs);
//       $data    =   array("you_date"=>'07',"you_month"=> "08","you_year"=>"2018","you_gender"=>"male",
//                        "match_date"=>"10","match_month"=>"05","match_year"=>"1979","match_gender"=>"female","match_name"=>"Chhaya");

        $resourceName = 'general_ascendant_report';
        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data, "hi");

        return json_decode($resp);
    }

    function dailyPrediction($rs) {
        $data = self::makeHoroData($rs);

        $resourceName = 'planet_nature';
        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);

        return json_decode($resp);
    }

    function Yoga($rs) {

        $data = array(
            'day' => $rs['date'],
            'month' => $rs['month'],
            'year' => $rs['year'],
            'hour' => $rs['hour'],
            'min' => $rs['minute'],
            'lat' => $rs['latitude'],
            'lon' => $rs['longitude'],
            'tzone' => $rs['timezone'],
            'varshaphal_year' => $rs['varshaphal_year'],
            'name' => $rs['fname'] . " " . $rs['lname']
        );


        $resourceName = 'varshaphal_yoga';
        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);

        return json_decode($resp);
    }

    function getDashaDescription($sign) {
        global $db;
        $sql = "select * from lang_dasha_description where UPPER(sign_name) = '" . strtoupper($sign). "' and lang_type='".self::$lang."'";
        
        $result = $db->query($sql);
        $rs = $result->fetch();
        return $rs;
    }

    function getDashaKarka($charts, $dasha_desc) {
//        print_r($dasha_desc);die;
        $planets = [];
        foreach ($charts as $chart) {
            $karka = [];
            if ($chart->isRetro == "true") {
                $karka[] = RETROGRADE;
            }
            if ($chart->is_vargottam) {
                $karka[] = VARGOTTAM;
            }
            if ($chart->is_combust) {
                $karka[] = COMBUST;
            }
            if ($chart->is_yogkarka) {
                $karka[] = YOGAKARAKA;
            }

            if ($chart->is_exaltation_sign) {
                $karka[] = EXALTED;
            }
            if ($chart->is_debilitation_sign) {
                $karka[] = DEBILITATED;
            }
            if ($chart->is_mooltrikona_sign) {
                $karka[] = MOOLTRIKONA;
            }
            if ($chart->is_own_house_planet) {
                $karka[] = OWNHOUSE;
            }
            $karka_atr = "";

            if (count($karka) > 0)
                $karka_atr = "(" . implode(',', $karka) . ")";

            $planets[$chart->name]['desc'] = $karka_atr . " " . $dasha_desc[strtolower($chart->name)];
            $planets[$chart->name]['tag'] = $dasha_desc[strtolower($chart->name) . "_tag"];
        }
        return $planets;
    }

    function dashaDateFormat($dt) {
        $dtf = explode("  ", $dt);
        $sqlDate = sqlDate($dtf[0]);

        return $sqlDate; //." ". date("H:i:00",strtotime($dtf[1]));
    }

    function checkDashaSaved($rs) {
        global $db;
        $dashaID = "";

        $device_token = $rs['device_token'];
        $device_type = $rs['device_type'];
        $name = $rs['fname'] . " " . $rs['lname'];
        $birthDate = date("Y-m-d", strtotime($rs['year'] . "-" . $rs['month'] . "-" . $rs['date']));
        $birthTime = $rs['hour'] . ":" . $rs['minute'];
        $location = $rs['location'];
        $lat = $rs['latitude'];
        $lng = $rs['longitude'];
        $timeZone = $rs['timezone'];

        $userid = (empty($rs['userid'])) ? $rs['userid'] : -1;

        $sql = "select * from user_dasha_master where birth_date='" . $birthDate . "'" .
                " and birth_time='" . $birthTime . "' and lat='" . $lat . "' and lng='" . $lng . "'";
        $result = $db->query($sql);
        if ($result->size() <= 0) {
            $sql = "insert into user_dasha_master(userid,dtdate,birth_date,birth_time,location,lat,lng,timezone," .
                    "device_token,device_type)values(" .
                    "" . $userid . "," .
                    "'" . nowDate() . "'," .
                    "'" . ($birthDate) . "'," .
                    "'" . $birthTime . "'," .
                    "'" . $location . "'," .
                    "'" . $lat . "'," .
                    "'" . $lng . "'," .
                    "'" . $timeZone . "'," .
                    "'" . $device_token . "'," .
                    "'" . $device_type . "')";

            $resultInsert = $db->query($sql);
            $dashaID = $resultInsert->insertID();
        } else {
            $rs = $result->fetch();
            $dashaID = $rs['id'];
        }
        return $dashaID;
    }

    function saveDasha($kundaliID, $dashaData) {
        global $db;
        $sql = "";
        //$kundaliID    =   self::checkDashaSaved($rs);
        if ($kundaliID <= 0)
            return;

        $sql = "select * from user_dasha where kundli_id=" . $kundaliID . " and dasha_type='MAHADASHA'";
        $result = $db->query($sql);
        $savedDashaCount = $result->size();

        foreach ($dashaData['vimshotri'] as $dasha) {
            $sql = "select * from user_dasha where kundli_id=" . $kundaliID . " and dasha_type='MAHADASHA'";
            $result = $db->query($sql);
            if ($savedDashaCount <= 0) {
                $sql = "insert into user_dasha(kundli_id,dasha_type,planet,`start`,`end`,description,tag)VALUES(" .
                        "" . $kundaliID . "," .
                        "'MAHADASHA'," .
                        "'" . $dasha->planet . "'," .
                        "'" . self::dashaDateFormat($dasha->start) . "'," .
                        "'" . self::dashaDateFormat($dasha->end) . "'," .
                        "'" . ($dasha->dasha_description) . "'," .
                        "'" . ($dasha->dasha_tag) . "')";

                $result = $db->query($sql);
                $parentID = $result->insertID();
            }
            if (!empty($dasha->antardasha)) {
                self::saveAntardasha($dasha->antardasha, $dasha->planet, $kundaliID);
            }
        }
		$db->query("update user_kundali set dasha_captured=1 where id=".$kundaliID);
    }

    function saveAntardasha($antardashaData, $mahadashaPlanet, $kundaliID = "", $rs = "") {
        global $db;
        $sql = "";
        if ($kundaliID <= 0)
            return;
//        if (empty($dashaID))
//            $dashaID    =   self::checkDashaSaved($rs);

        foreach ($antardashaData as $dasha) {

            $sql = "select * from user_dasha where kundli_id=" . $kundaliID . " and dasha_type='ANTARDASHA' and planet='" . $dasha->planet . "' and mahadasha='" . $mahadashaPlanet . "'";
            $result = $db->query($sql);
            if ($result->size() <= 0) {
                $sql = "insert into user_dasha(kundli_id,mahadasha,dasha_type,planet,`start`,`end`,description,tag)VALUES(" .
                        "" . $kundaliID . "," .
                        "'" . $mahadashaPlanet . "'," .
                        "'ANTARDASHA'," .
                        "'" . $dasha->planet . "'," .
                        "'" . self::dashaDateFormat($dasha->start) . "'," .
                        "'" . self::dashaDateFormat($dasha->end) . "'," .
                        "'" . ($dasha->dasha_description) . "'," .
                        "'" . ($dasha->dasha_tag) . "')";
                $result = $db->query($sql);
                $parentID = $result->insertID();
            }

            if (!empty($dasha->pratyantardasha)) {
                self::savePratyantardasha($dasha->pratyantardasha, $mahadashaPlanet, $dasha->planet, $kundaliID, $rs);
            }
        }
    }

    function savePratyantardasha($pratyantarData, $mahadashaPlanet, $antardashaPlanet, $kundaliID = "", $rs = "") {
        global $db;
        $sql = "";

        if ($kundaliID <= 0)
            return;

        foreach ($pratyantarData as $dasha) {
            $sql = "select * from user_dasha where kundli_id=" . $kundaliID . " and dasha_type='PRATYANTAR' and planet='" . $dasha->planet . "' and mahadasha='" . $mahadashaPlanet . "' and antardasha='" . $antardashaPlanet . "'";

            $result = $db->query($sql);
            if ($result->size() <= 0) {
                $sql = "insert into user_dasha(kundli_id,mahadasha,antardasha,dasha_type,planet,`start`,`end`,description,tag)VALUES(" .
                        "" . $kundaliID . "," .
                        "'" . $mahadashaPlanet . "'," .
                        "'" . $antardashaPlanet . "'," .
                        "'PRATYANTAR'," .
                        "'" . $dasha->planet . "'," .
                        "'" . self::dashaDateFormat($dasha->start) . "'," .
                        "'" . self::dashaDateFormat($dasha->end) . "'," .
                        "'" . ($dasha->dasha_description) . "'," .
                        "'" . ($dasha->dasha_tag) . "')";

                $result = $db->query($sql);
            }
            if (!empty($dasha->sookshmadasha)) {
                self::saveSookshamadasha($dasha->sookshmadasha, $mahadashaPlanet, $antardashaPlanet, $dasha->planet, $kundaliID, $rs);
            }
        }
    }

    function saveSookshamadasha($sookshmadashaData, $mahadashaPlanet, $antardashaPlanet, $pratyantardashaPlanet, $kundaliID = "", $rs = "") {
        global $db;
        $sql = "";

        if ($kundaliID <= 0)
            return;

        foreach ($sookshmadashaData as $dasha) {
//            $sql=   "select * from user_dasha where dasha_id=".$dashaID." and dasha_type='SOOKSHMA' and planet='".$dasha->planet."'";
//            $result =   $db->query($sql);
//            if ($result->size()<=0){
            $sql = "insert into user_dasha(kundli_id,mahadasha,antardasha,pratyantar,dasha_type,planet,`start`,`end`,description,tag)VALUES(" .
                    "" . $kundaliID . "," .
                    "'" . $mahadashaPlanet . "'," .
                    "'" . $antardashaPlanet . "'," .
                    "'" . $pratyantardashaPlanet . "'," .
                    "'SOOKSHMA'," .
                    "'" . $dasha->planet . "'," .
                    "'" . self::dashaDateFormat($dasha->start) . "'," .
                    "'" . self::dashaDateFormat($dasha->end) . "'," .
                    "'" . ($dasha->dasha_description) . "'," .
                    "'" . ($dasha->dasha_tag) . "')";
            $result = $db->query($sql);
            //}
        }
    }

    function Dasha($rs) {
        ErrorLog::log("REQUEST URL", $rs);
        $kundaliID = $rs['kundali_id'];
        if ($kundaliID > 0)
            $rs = UserClass::parseKundaliDetail($kundaliID);

        $data = array(
            'day' => $rs['date'],
            'month' => $rs['month'],
            'year' => $rs['year'],
            'hour' => $rs['hour'],
            'min' => $rs['minute'],
            'lat' => $rs['latitude'],
            'lon' => $rs['longitude'],
            'tzone' => $rs['timezone'],
            'name' => $rs['name']
        );


        
        $resourceName = 'current_vdasha_all';
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);
//        print_r($resp);
//        exit;
        // get Planets
        $resp_planets = self::chartData($rs);
        $lagan = $resp_planets->data->lagna;
        $dasha_desc = self::getDashaDescription($lagan->sign);
       
        //planetName
        $planet_karka = self::getDashaKarka($resp_planets->data->chart, $dasha_desc);

//        $fileName   =   "../uploads/anagh-dasha-detail.json";
//        $resp       =   file_get_contents($fileName);
//        print_r($resp);
//        exit;

        $resp = json_decode($resp);
        $dasha = array();
        foreach ($resp->major->dasha_period as $vimshotri) {
            $planet = $vimshotri->planet;
            
            $antar = self::getAntardasha($resp, $planet, $planet_karka);
            if (!empty($antar)) {
                $vimshotri->antardasha = $antar;
            }
            $vimshotri->dasha_description = self::planetName($planet) . " " . $planet_karka[$planet]['desc'];
            $vimshotri->dasha_tag = $planet_karka[$planet]['tag'];
            $dasha['vimshotri'][] = $vimshotri;
        }
        
        //self::saveDasha($kundaliID, $dasha);
        $arr = array("status" => true, "msg" => "Dasha Generated.", "data" => $dasha);
        return ($arr);
    }

    function getAntardasha($data, $planet_p, $planet_karka) {
        $antardashaArr = array();
        $antartdashaPlanet = $data->minor->planet->major;

        if (strtoupper($antartdashaPlanet) == strtoupper($planet_p)) {

            foreach ($data->minor->dasha_period as $antar) {
                $planet = $antar->planet;
                //$antar->dasha_description = $planet_p."-".$planet." ".$planet_karka[$antar->planet]['desc'];
                $antar->dasha_description = self::planetName($planet) . " " . $planet_karka[$antar->planet]['desc'];
                $antar->dasha_tag = $planet_karka[$antar->planet]['tag'];
                $pratyantar = self::getPratyantardasha($data, $planet, $planet_karka, $planet_p);
                if (!empty($pratyantar)) {
                    $antar->pratyantardasha = $pratyantar;
                }
                $antardashaArr[] = $antar;
            }
        }
        return $antardashaArr;
    }

    function getPratyantardasha($data, $planet, $planet_karka, $planet_p) {
        $pratyantardashaArr = array();
        $pratyantardashaPlanet = $data->sub_minor->planet->minor;
        if (strtoupper($pratyantardashaPlanet) == strtoupper($planet)) {
            foreach ($data->sub_minor->dasha_period as $pratya) {
                $planet = $pratya->planet;

                //$pratya->dasha_description = $planet_p."-".$planet."-".$pratya->planet." ".$planet_karka[$pratya->planet]['desc'];
                $pratya->dasha_description = self::planetName($pratya->planet) . " " . $planet_karka[$pratya->planet]['desc'];
                $pratya->dasha_tag = $planet_karka[$pratya->planet]['tag'];
                $sookshma = self::getSookshmadasha($data, $planet, $planet_karka, $planet_p);
                if (!empty($sookshma)) {
                    $pratya->sookshmadasha = $sookshma;
                }
                $pratyantardashaArr[] = $pratya;
            }
            return $pratyantardashaArr;
        }
    }

    function getSookshmadasha($data, $planet, $planet_karka, $planet_p) {
        $sookshmadashaArr = array();
        $sookshmadashaPlanet = $data->sub_sub_minor->planet->minor;

        if (strtoupper($sookshmadashaPlanet) == strtoupper($planet)) {
            foreach ($data->sub_sub_minor->dasha_period as $sookshma) {
                //$pratya->dasha_description = $planet_p."-".$planet."-".$pratya->planet." ".$planet_karka[$pratya->planet]['desc'];
                $sookshma->dasha_description = self::planetName($sookshma->planet) . " " . $planet_karka[$sookshma->planet]['desc'];
                $sookshma->dasha_tag = $planet_karka[$sookshma->planet]['tag'];
                $sookshmadashaArr[] = $sookshma;
            }
            return $sookshmadashaArr;
        }
    }

    function getAntardashaByMajor($rs) {
        $status = "false";
        $kundaliID = $rs['kundali_id'];
        $majorPlanet = $rs['major_planet_name'];
        if ($kundaliID > 0)
            $rs = UserClass::parseKundaliDetail($kundaliID);

        $data = array(
            'day' => $rs['date'],
            'month' => $rs['month'],
            'year' => $rs['year'],
            'hour' => $rs['hour'],
            'min' => $rs['minute'],
            'lat' => $rs['latitude'],
            'lon' => $rs['longitude'],
            'tzone' => $rs['timezone'],
            'name' => $rs['fname'] . " " . $rs1['lname']
        );


        $userid = "-1";

        $resourceName = 'sub_vdasha/' . ucfirst($majorPlanet);

        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);
        $status = "true";

        $resp = json_decode($resp);
        // get Planets
        $resp_planets = self::chartData($rs);
        $lagan = $resp_planets->data->lagna;
        $dasha_desc = self::getDashaDescription($lagan->sign);
        $planet_karka = self::getDashaKarka($resp_planets->data->chart, $dasha_desc);


        foreach ($resp as $key => $val) {
            //$resp[$key]->dasha_description = $majorPlanet." ".$val->planet." ".$planet_karka[$val->planet]['desc'];
            $resp[$key]->dasha_description = self::planetName($val->planet) . " " . $planet_karka[$val->planet]['desc'];
            $resp[$key]->dasha_tag = $planet_karka[$val->planet]['tag'];
        }



       // self::saveAntardasha($resp, $majorPlanet, $kundaliID, $rs);
        $arr = array("status" => true, "msg" => "Antardasha Generated.", "data" => $resp,
            "major_planet_name" => $majorPlanet
        );
        return ($arr);
    }

    function getPratyantarByMinor($rs) {
        $kundaliID = $rs['kundali_id'];
        $majorPlanet = $rs['major_planet_name'];
        $minorPlanet = $rs['minor_planet_name'];

        if ($kundaliID > 0)
            $rs = UserClass::parseKundaliDetail($kundaliID);
        $status = "false";

        $data = array(
            'day' => $rs['date'],
            'month' => $rs['month'],
            'year' => $rs['year'],
            'hour' => $rs['hour'],
            'min' => $rs['minute'],
            'lat' => $rs['latitude'],
            'lon' => $rs['longitude'],
            'tzone' => $rs['timezone'],
            'name' => $rs['fname'] . " " . $rs['lname']
        );
        $userid = "-1";

        $resourceName = 'sub_sub_vdasha/' . ucfirst($majorPlanet) . "/" . ucfirst($minorPlanet);



        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);
        $status = "true";

        $resp = json_decode($resp);

        // get Planets

        $resp_planets = self::chartData($rs);
        $lagan = $resp_planets->data->lagna;
        $dasha_desc = self::getDashaDescription($lagan->sign);
        $planet_karka = self::getDashaKarka($resp_planets->data->chart, $dasha_desc);

        foreach ($resp as $key => $val) {
            //$resp[$key]->dasha_description = $majorPlanet." ".$minorPlanet." ".$val->planet." ".$planet_karka[$val->planet]['desc'];
            $resp[$key]->dasha_description = self::planetName($val->planet) . " " . $planet_karka[$val->planet]['desc'];
            $resp[$key]->dasha_tag = $planet_karka[$val->planet]['tag'];
            $rsA = array_merge($rs, array("minor_minor_planet_name" => $val->planet,
                "major_planet_name" => $majorPlanet,
                "minor_planet_name" => $minorPlanet,
                "kundli_id" => $kundaliID));
            $arr = self::getSookshmadashaByMinor($rsA);
        }

       // self::savePratyantardasha($resp, $majorPlanet, $minorPlanet, $kundaliID, $rs);

        $arr = array("status" => true, "msg" => "Pratyantar Dasha Generated.", "data" => $resp,
            "major_planet_name" => $majorPlanet,
            "minor_planet_name" => $minorPlanet,
        );
        return ($arr);
    }

    function getSookshmadashaByMinor($rs) {
        $status = "false";

        $kundaliID = $rs['kundli_id'];
        $majorPlanet = $rs['major_planet_name'];
        $minorPlanet = $rs['minor_planet_name'];
        $minorMinorPlanet = $rs['minor_minor_planet_name'];

        if ($kundaliID > 0)
            $rs = UserClass::parseKundaliDetail($kundaliID);

        $data = array(
            'day' => $rs['date'],
            'month' => $rs['month'],
            'year' => $rs['year'],
            'hour' => $rs['hour'],
            'min' => $rs['minute'],
            'lat' => $rs['latitude'],
            'lon' => $rs['longitude'],
            'tzone' => $rs['timezone'],
            'name' => $rs['fname'] . " " . $rs['lname']
        );

        $userid = "-1";


        $resourceName = 'sub_sub_sub_vdasha/' . ucfirst($majorPlanet) . "/" . ucfirst($minorPlanet) . "/" . $minorMinorPlanet;

        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);
        $status = "true";

        $resp = json_decode($resp);

        // get Planets
        $resp_planets = self::chartData($rs);
        $lagan = $resp_planets->data->lagna;
        $dasha_desc = self::getDashaDescription($lagan->sign);
        $planet_karka = self::getDashaKarka($resp_planets->data->chart, $dasha_desc);

        foreach ($resp as $key => $val) {
            //$resp[$key]->dasha_description = $majorPlanet." ".$minorPlanet." ".$val->planet." ".$planet_karka[$val->planet]['desc'];
            $resp[$key]->dasha_description = self::planetName($val->planet) . " " . $planet_karka[$val->planet]['desc'];
            $resp[$key]->dasha_tag = $planet_karka[$val->planet]['tag'];
        }

        //self::saveSookshamadasha($resp, $majorPlanet, $minorPlanet, $minorMinorPlanet, $kundaliID, $rs);

        $arr = array("status" => "true", "msg" => "Sookshma Dasha Generated.", "data" => $resp,
            "major_planet_name" => $majorPlanet,
            "minor_planet_name" => $minorPlanet,
            "minor_minor_planet_name" => $minorMinorPlanet,
        );
        return ($arr);
    }

    function getMatchMaking($rs) {
        $data = self::makeMatchMakingData($rs);

        $resourceName = 'match_making_detailed_report';
        $resp = "";
        $resp = self::getCurlReponse(self::$USERID, self::$API_KEY, $resourceName, $data);
        //$fileName   =   "../uploads/matchmaking.json";
        //$resp  = file_get_contents($fileName);
        // print_r($resp);
        $resp = json_decode($resp);

        $totalPoint = intval($resp->ashtakoota->total->received_points);
        if ($totalPoint <= 17) {
            $text = "Ashtakoota Milan result is Incompatible. Although marriage is not advisable, Please avail Tvam custom compatibility analysis to know the complete compatibility assessment.";
        } elseif ($totalPoint >= 18 and $totalPoint <= 23) {
            $text = "Ashtakoota Milan result is Average. Marriage is advisable but married life will be mixed,Please avail Tvam custom compatibility analysis to know the complete picture.";
        } elseif ($totalPoint >= 24 and $totalPoint <= 31) {
            $text = "Ashtakoota Milan result is Very Good. Marriage is recommended and will be highly fulfilling. Please avail Tvam custom compatibility analysis to know the complete compatibility assessment.";
        } else {
            $text = "Ashtakoota Milan result is Excellent. Marriage is highly recommended and will be exceptional. Please avail Tvam custom compatibility analysis to know the complete compatibility assessment.";
        }
        $resp->conclusion = $text;
        $resp = json_encode($resp);
        if ($rs['save']==1){
            self::saveMatchMaking($rs, $resp);
        }
        $resp = json_decode($resp);
        $arr = array("status" => true, "msg" => "Match Making Generated.", "data" => $resp);
        return ($arr);
    }
    
    function planetName($planetName){
        global $db;
        $name   =   "";
        $sql    =   "select name from lang_planets_name where planet_no =(select planet_no from planets where UPPER(name)='".strtoupper($planetName)."') and lang_type='".self::$lang."'";
        $result =   $db->query($sql);
        if ($result->size()>0){
            $rs =   $result->fetch();
            $name   =   $rs['name'];
        }
        return $name;
    }
    
    function getNakshatrasDetail($param) {
        global $db;
        global $lang;
        self::$lang =   $lang;
        $nakshatras_name = $param->nakshatras_name;
        $status = false;
        $msg = "Please enter nakshatras name";
        $data = "";
//        echo $nakshatras_name;
        if (isset($nakshatras_name) && !empty($nakshatras_name)) {
            $sql = " SELECT description,(select you_tube_link from nakshatras where short_name='".($nakshatras_name) ."' ) as you_tube_link FROM lang_nakshatras WHERE nakshatra_id =(select id from nakshatras where  short_name = '" . trim($nakshatras_name) . "') and lang_type='".self::$lang."'";
         

            $result = $db->query($sql);
            if ($result->size() > 0) {

                $rs = $result->fetch();
                $status = true;
                $data = $rs;
                $msg = "Success";
            }
        }
        $arr = array("status" => $status, "msg" => $msg, "data" => $data);
        return $arr;
    }

    /*
     * Get Ascendents name to detail
     */

    function getAscendentsDetail($param) {
        global $db;
        global $lang;
        self::$lang =   $lang;
        $sign_id = $param->sign_id;
        $status = false;
        $msg = "Please enter sign id";
        $data = "";
//        echo $nakshatras_name;
        if (isset($sign_id) && !empty($sign_id)) {
            $sql = " SELECT description,(select you_tube_link from ascendents where signs_id=".($sign_id) ." ) as you_tube_link FROM lang_ascendents WHERE ascendent_id in(select id from ascendents where signs_id = '" . trim($sign_id) . "') and lang_type='".self::$lang."'";

            $result = $db->query($sql);
            if ($result->size() > 0) {
                $rs = $result->fetch();
                $status = true;
                $data = $rs;
                $msg = "Success";
            }
        }
        $arr = array("status" => $status, "msg" => $msg, "data" => $data);
        return $arr;
    }

}
