<?
class admin_dialog_htmlwidgets extends admin_dialog {

var $params = array("target", "mode", "page", "id");
var $iconsDir = 'var/hwicons/';


    function makeData() {
        parent::makeData();
        $this->widget = func_new("htmlwidget", $this->id);
        
        if($this->mode == "edit") {
            $this->form_fields = $this->widget->row;
        }
    }


    function processIcon() {
        $res = "";
        if(isset($_REQUEST[_FILE]['icon'])) {
            $p = pathinfo($_REQUEST[_FILE]['icon']['name']);
            $newName = time().".".$p["extension"];
            if(copy($_REQUEST[_FILE]['icon']['tmp_name'], $this->iconsDir.$newName)) {
                $res = $newName;
            }
        }
        return $res;
    }


    function action_update() {
        $item = func_new("htmlwidget", $this->id);
        $item->row["title"] = $_REQUEST["title"];
        $item->row["source"] = $_REQUEST["source"];

        $icon = $this->processIcon();
        if($icon != "") {
            if($item->row["icon"] != $icon) {
                @unlink($this->iconsDir.$item->row["icon"]);
            }
            $item->row["icon"] = $icon;
        }
        $item->update();
    }


    function action_add() {
        $item = func_new("htmlwidget");
        $item->row["title"] = $_REQUEST["title"];
        $item->row["source"] = $_REQUEST["source"];
        $item->row["icon"] = $this->processIcon();
        $item->create();
    }


    function action_delete() {
        if(isset($_REQUEST["ids"]) && is_array($_REQUEST["ids"])) {
            foreach($_REQUEST["ids"] as $k => $v) {
                $w = func_new("htmlwidget", $k);
                @unlink($this->iconsDir.$w->row["icon"]);
                $w->delete();
            }
        }
    }

}
?>