<?
class admin_dialog_sendmail extends admin_dialog {

var $params = array("target", "mode", "id", "page");

    function makeData() {
        parent::makeData();

        $this->users = $this->db->get_list("SELECT DISTINCT
                                      email
                                    FROM "._DB_TABLE_PREFIX."users
                                    WHERE email<>''
                                    ORDER BY email ASC
                                    ");

        // get log
        $this->messages = $this->db->get_list("SELECT *
                                    FROM "._DB_TABLE_PREFIX."sendmail
                                    ORDER BY sendtime DESC
                                    ");
        foreach($this->messages as $k=>$mess)
        {
            $this->messages[$k]["sendtime"] = date("H:i:s, F j, Y", $this->messages[$k]["sendtime"]);
        }


        switch($this->mode) {

            case "view":
                if (isset($this->id))
                {
                  $res = $this->db->get_list("SELECT *
                                    FROM "._DB_TABLE_PREFIX."sendmail
                                    WHERE id='".$this->id."'
                                    ");
                  $res[0]["sendtime"] = date("H:i:s, F j, Y", $res[0]["sendtime"]);
                  $this->row = $res[0];
                  $this->row['users'] = unserialize($this->row['users']);
                }
                break;

            default:
                break;

        }
    }

    function action_send_message() {
          if(isset($_REQUEST["ids"]) && is_array($_REQUEST["ids"])) {
              $s_ids = serialize($_REQUEST["ids"]);
              //todo: !!!!!
              foreach ($_REQUEST["ids"] as $email) {
                  $subject = $_REQUEST["subject"];
                  $body    = $_REQUEST["message"];
                  @mail($email, $subject, $body, "From: ".$_SESSION["email"]);
              }
              //$this->status = "ok";
              $this->db->insert(_DB_TABLE_PREFIX."sendmail",
                                array("sendtime" => time(),
                                      "subj"     => $_REQUEST["subject"],
                                      "message"  => $_REQUEST["message"],
                                      "users"    => $s_ids));
          }
          //$this->status = "failed";
    }

}
?>