<?php

class Reg{

	/*
		The find static method selects categories
		from the database and returns them as
		an array of Category  objects.
	*/
    
	public static function find($arr = array()){
            
		global $db;

		//if (empty($arr)){
                $sql = 'SELECT count(*) FROM company WHERE name = "'.
                        $arr['cname']. '"';
                $st = $db->prepare($sql);
		$st->execute();
                $test1=$st->fetchAll();
                
                //print_r($sql);
                $sql = 'SELECT count(uname) FROM user WHERE uname = "'.
                        $arr['uname'] . '" or email = "' . $arr['email'] . '"';
                //print_r($sql);
                $st = $db->prepare($sql);
		$st->execute();
                $test2=$st->fetchAll();
                $res=0;
                if ($test1[0]['count(*)']>0){
                    $res=1;
                }
                if ($test2[0]['count(uname)']>0){
                    $res=$res+4;
                }
                //print_r($test1);
//print_r($test2);
//print_r($res);
		return $res;
	}
        public static function save($arr = array()){
            
		global $db;
                $sql = 'Insert into company (id,name)VALUES (NULL,"'.$arr['cname'] .'")';
                $st = $db->prepare($sql);
		$st->execute();
                //print_r($sql);
                $sql = 'SELECT id from company where name = "'.$arr['cname'] .'"';
                //print_r($sql);
                $st = $db->prepare($sql);
		$st->execute();
                $result = $st->fetchAll();
                //print_r($result);
		//if (empty($arr)){
                $sql = 'Insert into user (id,fname,lname,uname,pwd,company,access,active,email) 
                    VALUES (NULL,"'.$arr['fname'] .'","'.
                        $arr['lname'] .'","'.$arr['uname'] .'","'. md5($arr['pwd']) .'","'.
                        $result[0]['id'] .'",1,1,"'. $arr['email'] .'")';
                $st = $db->prepare($sql);
                //print_r($sql);
		$st->execute();

		// Returns an array of Category objects:
		return 17;
	}
}
?>
