<?
class admin_dialog_websearch extends admin_dialog {

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


    function makeData() {
        parent::makeData();
        $this->websearch = func_new("websearch", $this->id);

        if($this->mode == "edit") {
            $this->form_fields = $this->websearch->row;
        }
    }


    function processLogo() {
        $logoFile = '';
        if(isset($_REQUEST[_FILE]['logo'])) {
            $p = pathinfo($_REQUEST[_FILE]['logo']['name']);
            $newName = "websearch_".time().".".$p["extension"];
            if(copy($_REQUEST[_FILE]['logo']['tmp_name'], $this->itemsDir.$newName)) {
                $logoFile = $newName;
            }
        }
        return $logoFile;
    }


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

        if($logo = $this->processLogo()) {
            if($item->row["logo"] != $logo) {
                @unlink($this->itemsDir.$item->row["logo"]);
            }
            $item->row["logo"] = $logo;
        }
        $item->update();
    }


    function action_add() {
        $item = func_new("websearch");
        $item->row["title"] = $_REQUEST["title"];
        $item->row["url"] = $_REQUEST["url"];
        $item->row["logo"] = $this->processLogo();
        $item->create();
    }


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

}
?>