<?php   

    //include(MODPATH."feed/classes/controller/"."PublishTypes.php");
    require_once("PublishTypes.php");

  class PublishEvent
  {         
      public static function publish(Enum $publishType, $publisher, $message, $userList = null)
      {
          //$currentTime = getdate();
          $customerid_list = null;
          //$ct = func_num_args();
          //for($i = 0; $i < $ct; $i++)
            //var_dump(func_get_arg($i));
          
          switch(get_class($publishType))
          {
            case "PublishCompany":
                $customerid_list = PublishEvent::publisCompany($publisher, $message);
                break;
                
            case "PublishUserList":
                $customerid_list = PublishEvent::publishSelectedUsers($publisher, $message, $userList);
                break;
          }
          
          return $customerid_list;
      }
      
      protected static function publisCompany($publisher, $message)
      {
        
        
        $userTable = ORM::factory("User", $publisher);
        $company = $userTable->company_id;

        
        $userTable = ORM::factory("User")->where("company_id", "=", $company)->with("id")->find_all();
        $customerid_list = array();
        foreach($userTable as $user)
            $customerid_list[] = $user->id;
         
        PublishEvent::writeToDB($publisher, $message, true, $customerid_list);     
        
        return $customerid_list;
      }
      
      // TODO: $userList in bos olup olmadigi kontrol edilmiyor
      protected static function publishSelectedUsers($publisher, $message, $userList)
      {
        if (is_null($userList)) throw new Exception("User list is empty!");
        
        $customerid_list = array();
        
        if (is_array($userList))
        {
            $userTable = ORM::factory("User");
            
            for($i = 0; $i < count($userList); $i++)
            {
                if (is_int($userList[$i]))
                {
                    $customerid_list[] = $userList[$i];
                }
                else if (is_string($userList[$i]))
                {
                    $users = $userTable->where("username", "=", $userList[$i])->with("id")->find_all();
                    
                    foreach($users as $auser)
                    {
                        $customerid_list[] = $auser->id;
                    }
                }
                else
                {
                    throw new Exception("Undefined user type. Must be integer (id) or string (username)");
                }
            }
        }
        else
        {
            throw new Exception("User list must be an array!");
        }
        
        if (in_array($publisher, $customerid_list) == false)
        {
            $customerid_list[] = $publisher;
        }
        
        PublishEvent::writeToDB($publisher, $message, false, $customerid_list);
        
        return $customerid_list;
      }

      protected static function writeToDB($publisher, $message, $to_company, $reader_list)
      {
        $feeds = ORM::factory("Feed");
        //var_dump($feeds);
        $today = date('Y-m-d H:i:s');
        
        $feeds->publisher_id = $publisher;
        $feeds->event_time = $today;
        $feeds->message = $message;
        $feeds->to_company = $to_company;
        //$feeds->readers = $reader_list;
        //$feeds->users = $reader_list;
        $feeds->save();
        
        if ($to_company == false)
        {
            foreach($reader_list as $reader)
            {
                //echo Kohana::debug($reader);
                $users = ORM::factory("User")->find($reader);
                $feeds->add("user",$users);
                /*
                //echo Kohana::debug($users);
                $wfeeds = $users->feeds_written->find_all();
                //echo Kohana::debug($wfeeds);
                $wfeeds = $feeds;
                $users->save();
                */
            }
            
            $feeds->save();
        }
      }
  }
?>
