Beispiel #1
0
function table_edit($tablename, $home = "", $action = "", $id = "", $masterfield = "", $mastervalue = "", $order = "", $actionstring_or_function = "")
{
    if (!$home) {
        $home = self_q();
    }
    if (!$action) {
        $action = arg(0);
    }
    if (!$id) {
        $id = arg(1);
    }
    global $table_edit_props;
    $actionstring = $actionstring_or_function;
    ////////////// PREPARE
    $table_long_alias = $tablename;
    global $tables;
    if (isset($tables[$tablename]['weight'])) {
        weight_fix($tablename);
    }
    $table_long_alias = table_long_alias($tablename);
    /////////////////////////////////////
    $master_cond = "";
    if ($masterfield) {
        if (strtolower($mastervalue) == 'null') {
            $master_cond = " AND {$masterfield} is null";
        } else {
            $master_cond = " AND {$masterfield}='{$mastervalue}' ";
        }
    }
    global $tables;
    if (!isset($tables[$tablename])) {
        die("error, table_edit - tables[{$tablename}] not set");
    }
    if (isset($tables[$tablename]['weight'])) {
        if ($order) {
            $order .= ",";
        }
        $order .= " {$tablename}.weight ";
    }
    if ($order) {
        $order = " ORDER BY {$order} ";
    }
    //this is reaction on drag and drop reorder
    if ($action == "move") {
        $d = $_REQUEST['delta'];
        if ($d > 0) {
            for ($i = 0; $i < $d; $i++) {
                table_edit($tablename, "return!", "down", $id, $masterfield, $mastervalue);
            }
        }
        if ($d < 0) {
            $d = -$d;
            for ($i = 0; $i < $d; $i++) {
                table_edit($tablename, "return!", "up", $id, $masterfield, $mastervalue);
            }
        }
        die("");
    }
    if ($action == "up") {
        $weight = db_result(db_query("SELECT weight FROM {$tablename} WHERE id=%d {$master_cond}", $id));
        $prevweight = db_result(db_query("SELECT max(weight) FROM {$tablename} WHERE weight<%f {$master_cond}", $weight));
        $previd = db_result(db_query("SELECT id FROM {$tablename} WHERE weight=%f {$master_cond}", $prevweight));
        db_query("UPDATE {$tablename} SET weight=%f WHERE id=%d {$master_cond}", $prevweight, $id);
        db_query("UPDATE {$tablename} SET weight=%f WHERE id=%d {$master_cond}", $weight, $previd);
        if ($home == 'return!') {
            return;
        }
        redir($home);
    }
    if ($action == "down") {
        $weight = db_result(db_query("SELECT weight FROM {$tablename} WHERE id=%d {$master_cond}", $id));
        $prevweight = db_result(db_query("SELECT min(weight) FROM {$tablename} WHERE weight>%f {$master_cond}", $weight));
        if ($prevweight) {
            $previd = db_result(db_query("SELECT id FROM {$tablename} WHERE weight=%f {$master_cond}", $prevweight));
            db_query("UPDATE {$tablename} SET weight=%f WHERE id=%d {$master_cond}", $prevweight, $id);
            db_query("UPDATE {$tablename} SET weight=%f WHERE id=%d {$master_cond}", $weight, $previd);
        }
        if ($home == 'return!') {
            return;
        }
        redir($home);
    }
    if ($action == "del") {
        db_query("DELETE FROM {$tablename} WHERE id=%d {$master_cond}", $id);
    }
    if ($action == "edit") {
        if (form_post("edit")) {
            $sets = "";
            foreach ($tables[$tablename]['fields'] as $value) {
                if ($sets) {
                    $sets .= ", ";
                }
                if (str_end($value, "_check")) {
                    if (form_post($value)) {
                        $sets .= "{$value}=1";
                    } else {
                        $sets .= "{$value}=0";
                    }
                } else {
                    if (str_end($value, "_time")) {
                        //hms mdy
                        $f = str_start($value, "_time");
                        $ts = mktime(form_post($f . "_hour"), form_post($f . "_min"), form_post($f . "_sec"), form_post($f . "_month"), form_post($f . "_day"), form_post($f . "_year"));
                        $sets .= "{$value}={$ts}";
                    } else {
                        $p = form_post($value);
                        $p = SlashSymbolsFix($p);
                        if (mysql || pdo_sqlite) {
                            $p = mysql_real_escape_string($p);
                        }
                        if (sqlite2) {
                            $p = sqlite_escape_string($p);
                        }
                        if (sqlite3) {
                            $p = $GLOBALS['dbhandle']->escapeString($p);
                        }
                        if ($p == "null") {
                            $sets .= "{$value}=null";
                        } else {
                            $sets .= "{$value} = '" . $p . "' ";
                        }
                    }
                }
            }
            $s = "UPDATE {$tablename} SET {$sets} WHERE id={$id} {$master_cond}";
            db_query($s);
            $callback = "table_" . $tablename . "_edit";
            if (function_exists($callback)) {
                $callback($id);
            }
            redir($home);
        }
        page_header("Edit {$table_long_alias}");
        if (isset($tables[$tablename]['fields'])) {
            $r = db_object_get($tablename, $id);
            form_start();
            table_edit_form_generate($tablename, $r);
            form_submit("{~Save changes}", "edit");
            form_end();
            return form();
        } else {
            return "";
        }
    }
    if ($action == "add" && $table_edit_props->add_records) {
        if (form_post("add")) {
            //fixme: unsecure, sql injection
            $fields = "";
            $values = "";
            if (isset($tables[$tablename]['fields'])) {
                foreach ($tables[$tablename]['fields'] as $field) {
                    if ($fields) {
                        $fields .= ", ";
                    }
                    $fields .= $field;
                    if ($values) {
                        $values .= ", ";
                    }
                    if (str_end($field, "_check")) {
                        if (form_post($field)) {
                            $values .= "1";
                        } else {
                            $values .= "0";
                        }
                    } else {
                        if (str_end($field, "_time")) {
                            //hms mdy
                            $f = str_start($field, "_time");
                            $ts = mktime(form_post($f . "_hour"), form_post($f . "_min"), form_post($f . "_sec"), form_post($f . "_month"), form_post($f . "_day"), form_post($f . "_year"));
                            $values .= "{$ts}";
                        } else {
                            $p = form_post($field);
                            $p = SlashSymbolsFix($p);
                            if (mysql || pdo_sqlite) {
                                $p = mysql_real_escape_string($p);
                            } else {
                                if (sqlite2) {
                                    $p = sqlite_escape_string($p);
                                } else {
                                    if (sqlite3) {
                                        $p = $GLOBALS['dbhandle']->escapeString($p);
                                    }
                                }
                            }
                            if ($p == 'null') {
                                $values .= "null";
                            } else {
                                $values .= "'" . $p . "'";
                            }
                        }
                    }
                }
            }
            if ($masterfield) {
                if ($fields) {
                    $fields .= ", ";
                }
                $fields .= " {$masterfield}";
                if (strtolower($mastervalue) == 'null') {
                    $values .= ", null ";
                } else {
                    $values .= ", '{$mastervalue}'";
                }
            }
            if (isset($tables[$tablename]['weight'])) {
                if ($fields) {
                    $fields .= ", ";
                }
                $fields .= " weight";
                $values .= ", " . (db_result(db_query("SELECT max(id) FROM {$tablename}")) + 1);
            }
            if ($values && $values[0] == ',') {
                $values = substr($values, 1, strlen($values));
            }
            db_query("INSERT INTO {$tablename} (id, {$fields}) VALUES (null, {$values})");
            $id = db_last_id();
            $callback = "table_" . $tablename . "_edit";
            if (function_exists($callback)) {
                $callback($id);
            }
            if ($table_edit_props->add_redir) {
                redir($home);
            }
        }
        page_header("Add {$table_long_alias}");
        form_start("", "post", " name=add_form ");
        table_edit_form_generate($tablename);
        if ($table_edit_props->add_record_html) {
            global $form;
            $form .= "<tr><td><td>" . $table_edit_props->add_record_html;
        }
        if ($table_edit_props->add_record_button_show) {
            form_submit("{~Add record}", "add");
        } else {
            form_hidden("add", "1");
        }
        form_end();
        return form();
    }
    if (strlen($GLOBALS['pageheader'] == 0)) {
        if (!str_end($table_long_alias, "s")) {
            page_header("{$table_long_alias}" . "s List");
        } else {
            page_header($table_long_alias);
        }
    }
    $ff = array();
    $ff = @$tables[$tablename]['fields'];
    $fields = "";
    $joins = "{$tablename}";
    $titles = array();
    if ($ff) {
        foreach ($ff as $f) {
            if ($fields) {
                $fields .= ", ";
            }
            $type = substr($f, strlen($f) - 3, 3);
            if ($type == "_id") {
                $cap = substr($f, 0, strlen($f) - 3);
                $table = $cap . "s";
                if (!isset($tables[$table])) {
                    $table = str_prefix($tablename) . $table;
                }
                $titlefield = "";
                foreach ($tables[$table]['fields'] as $v) {
                    $titlefield = $v;
                    break;
                }
                $fields .= " {$table}.{$titlefield} as {$cap} ";
                $joins .= " LEFT JOIN {$table} ON {$tablename}.{$f} = {$table}.id ";
                $titles[] = $cap;
            } else {
                $fields .= "{$tablename}.{$f}";
                $titles[] = $f;
            }
        }
    }
    $where = "";
    if ($masterfield) {
        if (strtolower($mastervalue) == 'null') {
            $where = " WHERE {$masterfield} is null ";
        } else {
            $where = " WHERE {$masterfield}='{$mastervalue}' ";
        }
    }
    if ($fields) {
        $fields_s = ", {$fields}";
    } else {
        $fields_s = "";
    }
    $q = "SELECT {$tablename}.id as id {$fields_s} FROM {$joins} {$where} {$order}";
    $act = "";
    if ($table_edit_props->edit_record_show) {
        $edit_image = "edit.png";
        if ($table_edit_props->use_rename_icon_for_edit) {
            $edit_image = "rename.png";
        }
        $act .= "<a href=?q={$home}/edit/[id]><img src=images/bios/{$edit_image} border=0></a>";
    }
    if ($table_edit_props->del_record_show) {
        $act .= "<a href=?q={$home}/del/[id]><img onclick=\"return confirm('{~Are you sure?}');\"src=images/bios/del.png border=0></a>";
    }
    //up down arrows
    /*    if(isset($tables[$tablename]['weight'])) {
            $act = " <a href=?q=$home/up/[id]><img src=images/up.png></a> <a href=?q=$home/down/[id]><img src=images/down.png></a> ".$act;
        }*/
    $rr = db_query($q);
    $s = "";
    $rr = db_fetch_objects($rr);
    if (count($rr) == 0) {
        $s .= "{~no records}<br>";
    } else {
        if ($table_edit_props->action_string_left) {
            table_start(count($ff) + 2);
            if ($table_edit_props->col_title_show) {
                table_add("", " class=table_edit_header ");
            }
        } else {
            table_start(count($ff) + 1);
        }
        ///HEADERS
        if ($table_edit_props->col_title_show) {
            foreach ($titles as $v) {
                if (str_end($v, "_check")) {
                    $v = str_start($v, "_check");
                } else {
                    if (str_end($v, "_text")) {
                        $v = str_start($v, "_text");
                    }
                }
                table_add("{~{$v}}", " class=table_edit_header ");
            }
            table_add("", " class=table_edit_header ");
        }
        foreach ($rr as $r) {
            $r = (array) $r;
            ////// table add id attribute to tr
            global $table_row_attributes;
            $table_row_attributes = " id=" . $r['id'] . " ";
            ///////////////////////////////////////
            $acts_left = "";
            if ($table_edit_props->action_string_left) {
                $acts_left = $table_edit_props->action_string_left;
                $acts_left = str_replace("[id]", $r['id'], $acts_left);
                table_add($acts_left);
            }
            foreach ($r as $key => $value) {
                if (str_end($key, "_check")) {
                    if ($value == 1) {
                        table_add("<INPUT TYPE=CHECKBOX READONLY readonly='readonly' checked onclick='javascript:return false'>");
                    } else {
                        table_add("<INPUT TYPE=CHECKBOX READONLY readonly='readonly' onclick='javascript:return false'>");
                    }
                } else {
                    if (str_end($key, "_time")) {
                        table_add(@date("Y/m/d H:i:s", $value));
                    } else {
                        if ($key != 'id') {
                            table_add($value, " class=table_edit_cell ");
                        }
                    }
                }
            }
            $useract = "";
            if (function_exists($actionstring)) {
                $useract = $actionstring($r['id'], $r);
            } else {
                $useract = $actionstring;
            }
            $acts = str_replace("[id]", $r['id'], $act . " " . $useract);
            table_add($acts);
        }
        $s = "";
        $s .= table_flush(" class=table_edit ");
        /// table drag reorder
        if (isset($tables[$tablename]['weight']) && $tables[$tablename]['weight']) {
            $s .= table_edit_drag_code($home);
        }
        /////
    }
    if ($table_edit_props->new_record_show && $table_edit_props->add_records) {
        $html = "<img src=images/bios/add.png border=0>&nbsp;{~Add a new record}";
        if ($table_edit_props->new_record_html) {
            $html = $table_edit_props->new_record_html;
        }
        $s .= "<br><a href=?q={$home}/add>{$html}</a>";
    }
    return $s;
}
Beispiel #2
0
function page_report($daysago = 0)
{
    $midnight = @mktime(0, 0, 0, date('n'), @date('j') - $daysago);
    $rr = db_fetch_objects(db_query("SELECT * FROM log WHERE time_time>{$midnight}"));
    table_start(6);
    $state = 0;
    $total = 0;
    foreach ($rr as $r) {
        $next = db_result(db_query("SELECT time_time FROM log WHERE id>{$r->id} ORDER BY id LIMIT 1"));
        if (!$next) {
            $next = time();
        }
        $span = round(($next - $r->time_time) / (60 * 60), 2);
        if ($r->active_check) {
            table_add("<input class=active_check type=checkbox>");
        } else {
            table_add("");
        }
        table_add(@date("H:i:s", $r->time_time));
        table_add($r->memo);
        table_add($span, " class=span ");
        $mins = 60 * $span;
        table_add("hour ({$mins} min)");
        $h = $span * 200;
        if ($r->active_check) {
            $bg = "#0f0";
        } else {
            $bg = "#ddd";
        }
        table_add("<div style='height:{$h}px;background:{$bg};'>&nbsp;</div>");
        if ($r->active_check) {
            $total += $span;
        }
    }
    $o = table();
    $total_min = 60 * $total;
    $o .= "total: {$total} ({$total_min} min)<br>";
    $o .= "checked total: <span id=checked_total></span>";
    return $o;
}