<?php

/*******************************************************************************
 * test url: www.archline.hu/maintenance/al.php?action=selectprogram&npid=b559bb8b-46f0-46ed-8cd5-8403369a1745&pwd=9600018350430116&isid=C9EB
 ******************************************************************************/

class SelectProgram
{
    function __construct()
    {
        if (!class_exists('crm_hardlock')) require_once("common_cadline_libraries/crm_hardlock.class.php");
    }

    public static function getXML($pwd = '', $npid = '', $isid = '', $namirialNonProfit = false, $BIMLadderNonProfit = false, $userid = '', $commercial = false)
    {
        $multiple = false;
        //$resultXml = '<'.'?'.'xml version="1.0"'.'?'.'><SelectProgramResult>';
        if ($pwd == '' || $npid == '' || $isid == '') return (void);

        crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`computers` WHERE `computer_id`='" . $npid . "' LIMIT 1;");
        $res2 = crm_database::getInstance()->fetchNext();
        if (empty($res2))
            $compid = crm_hardlock::AddOrUpdateComputer($npid, '');
        else
            $compid = $res2['id'];

        $password = '';
        $actcode = '';
        if ($commercial) {
            //ba figyelem, teszteletlen ag!!!
            if (substr($pwd, 6, 1) == 'L') {
                $actualPrograms = crm_hardlock::GetAllPrograms($nUserID, 2, $verid);
            } else {
                $actualPrograms = crm_hardlock::GetAllPrograms($nUserID, 1, $verid);
            }

            if (count($actualPrograms) > 1) {
                $password = $actualPrograms[0]['prHlNum'];
                $actcode = $actualPrograms[0]['prActCode'];
                //$resultXml .= '<Password>'.$actualPrograms[0]['prHlNum'].'</Password><DayCode>'.$actualPrograms[0]['prActCode'].'</DayCode>';
            } else if (count($actualPrograms) == 1) {
                $password = $actualPrograms[0]['prHlNum'];
                $actcode = $actualPrograms[0]['prActCode'];
                //$resultXml .= '<Password>'.$actualPrograms[0]['prHlNum'].'</Password><DayCode>'.$actualPrograms[0]['prActCode'].'</DayCode>';
            }
        } else {
            if (!$namirialNonProfit && !$BIMLadderNonProfit)
                $queryString = "SELECT * FROM `" . CRMADATBAZIS . "`.`nonprofit` WHERE `computer_id`='" . $compid . "' AND `isid`='" . $isid . "' AND partnerID is null LIMIT 1;";
            else if ($namirialNonProfit)
                $queryString = "SELECT * FROM `" . CRMADATBAZIS . "`.`nonprofit` WHERE `computer_id`='" . $compid . "' AND `isid`='" . $isid . "' AND partnerID = '2nt' LIMIT 1;";
            else if ($BIMLadderNonProfit)
                $queryString = "SELECT * FROM `" . CRMADATBAZIS . "`.`nonprofit` WHERE `computer_id`='" . $compid . "' AND `isid`='" . $isid . "' AND partnerID = 'kbl' LIMIT 1;";
            crm_database::getInstance()->query($queryString);
            $res =  crm_database::getInstance()->fetchNext();

            $multiple   = true;
            if (count($res)) {
                crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`p_versions` WHERE `verPassword8and9`='" . $res['verid'] . "' LIMIT 1;");
                $ver = crm_database::getInstance()->fetchNext();
                crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`h_programs` WHERE `prHlNum`='" . $res['hlID'] . "' AND `prVerID`='" . $ver['verID'] . "' LIMIT 1;");
                $res = crm_database::getInstance()->fetchNext();
                $password = $res['prPass'];
                $actcode = $res['prActCode'];
            }
        }
        header('Content-Type: text/xml; charset=utf-8');
        header('Content-Disposition: inline; filename=response.xml');
        echo '<' . '?' . 'xml version="1.0"' . '?' . '><SelectProgramResult><Password>' . $password . '</Password><DayCode>' . $actcode . '</DayCode><MultiplePrograms>false</MultiplePrograms></SelectProgramResult>';
    }

    public static function checkNonProfitExists($npid = '', $isid = '')
    {
        if ($npid == '' || $isid == '') return (void);

        crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`computers` WHERE `computer_id`='" . $npid . "' LIMIT 1;");
        $res2   = crm_database::getInstance()->fetchNext();
        $compid = $res2['id'];

        crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`nonprofit` WHERE `computer_id`='" . $compid . "' AND `isid`='" . $isid . "' LIMIT 1;");
        $res = crm_database::getInstance()->fetchNext();

        if (!empty($res)) {
            $user = $res['nUserID'];
            crm_database::getInstance()->query("SELECT * FROM `" . CRMADATBAZIS . "`.`users` WHERE `nUserID`=" . $user . " LIMIT 1;");
            $res = crm_database::getInstance()->fetchNext();

            header('Content-Type: text/xml; charset=utf-8');
            header('Content-Disposition: inline; filename=response.xml');
            echo '<' . '?' . 'xml version="1.0"' . '?' . '><NonProfitDatabaseResult><Name>' . $res['strName'] . '</Name><Email>' . $res['strEMail'] . '</Email><Telephone>' . $res['strTel'] . '</Telephone><Company>' . $res['strCompany'] . '</Company></NonProfitDatabaseResult>';
        }
    }
}
