Пример #1
0
function _asset_get_rows()
{
    $t =& $GLOBALS["t"];
    $tname = $t["title"];
    $tview = $t["view"];
    $mode = $t["views"][$tview]["SCHEMA_MODE"];
    $vars = array("handler" => $t["handler"], "sqlvarsnoquote" => $t["sqlvarsnoquote"], "default_sql" => $t["default_sql"], "custom_name" => $t["custom_name"]);
    $rows = db_select($tname, $t["fields_query"], $t["sqlwhere"], $t["sqlorder"], $t["sqllimit"], $t["sqlvars"], $vars);
    if (!empty($GLOBALS["current_view"]["ENABLE_CALENDAR"]) and is_array($rows) and count($rows) > 0) {
        $rows = date::build_views_sql($rows);
    }
    $i = 0;
    $total_row = array();
    $has_total_row = false;
    if ($mode == "edit" and is_array($rows) and !empty($t["sqlvars"]["item"]) and count($rows) < count($t["sqlvars"]["item"])) {
        sys_warning("{t}Item(s) not found or access denied.{/t}");
    }
    if (is_array($rows) and count($rows) > 1 and empty($_REQUEST["preview"]) and empty($_REQUEST["iframe"])) {
        $total_row = array("_bgstyle" => "", "_fgstyle" => "", "issum" => 1);
        $first_row = array_slice($rows, 0, 1);
        foreach ($first_row[0] as $field => $value) {
            if (isset($t["fields"][$field]["SUM"]) or isset($t["fields"][$field]["AVG"])) {
                if (empty($_REQUEST["iframe"]) or isset($t["fields"][$field]["AVG"])) {
                    $has_total_row = true;
                }
                $total_row[$field] = array("data" => array(0), "filter" => array(0));
            } else {
                if (isset($t["fields"][$field])) {
                    $total_row[$field] = array("data" => array(""), "filter" => array(""));
                }
            }
        }
    }
    if (is_array($rows)) {
        foreach ($rows as $row) {
            if (empty($row["id"])) {
                $row["id"] = $i++;
            }
            while (isset($t["data"][$row["id"]])) {
                $row["id"] .= " ";
            }
            $row["_id"] = $row["id"];
            $row["_folder"] = isset($row["folder"]) ? $row["folder"] : "";
            $row["_table"] = $tname;
            $row["_bgstyle"] = "";
            $row["_fgstyle"] = "";
            if (!isset($t["fields"]["id"])) {
                unset($row["id"]);
            }
            if (isset($t["views"][$tview]["CHANGESEEN"]) and empty($row["seen"])) {
                $vars = $t["sqlvars"];
                $vars["id"] = $row["_id"];
                db_update($tname, array("seen" => 1), array("id=@id@"), $vars, array("handler" => $t["handler"]));
            }
            foreach ($row as $field => $value) {
                if (isset($t["fields"][$field])) {
                    $value = $row[$field];
                    $row[$field] = array();
                    $row["_bgstyle"] = "";
                    $row["_fgstyle"] = "";
                    if ($has_total_row and is_numeric($value)) {
                        if (isset($t["fields"][$field]["SUM"])) {
                            $total_row[$field]["filter"][0] += $value;
                        } else {
                            if (isset($t["fields"][$field]["AVG"])) {
                                $total_row[$field]["filter"][0] += round($value / count($rows), 4);
                            }
                        }
                    }
                    if ($value != "") {
                        switch ($t["fields"][$field]["SIMPLE_TYPE"]) {
                            case "dateselect":
                            case "select":
                                $value = explode("|", trim($value, "|"));
                                break;
                            case "files":
                                $value = explode("|", trim($value, "|"));
                                foreach ($value as $key => $file) {
                                    $locked = file_exists($file . ".lck");
                                    $row[$field]["locked"][$key] = $locked;
                                    if ($GLOBALS["sel_folder"]["rights"]["write"] and $locked) {
                                        $row[$field]["can_unlock"][$key] = sys_can_unlock($file, $_SESSION["username"]);
                                    } else {
                                        if ($GLOBALS["sel_folder"]["rights"]["write"]) {
                                            $row[$field]["can_lock"][$key] = sys_can_lock($file);
                                        }
                                    }
                                }
                                break;
                            case "multitext":
                                $value = _asset_explode($value, ", ");
                                break;
                            case "text":
                                if (!empty($t["fields"][$field]["SEPARATOR"])) {
                                    $value = _asset_explode($value, $t["fields"][$field]["SEPARATOR"]);
                                }
                                break;
                        }
                    }
                    if (!is_array($value)) {
                        $value = array($value);
                    }
                    $row[$field]["data"] = $value;
                    $row[$field]["filter"] = $value;
                }
            }
            $t["data"][$row["_id"]] = $row;
        }
    }
    $t["datasets"] = count($t["data"]);
    if ($mode == "" and $has_total_row) {
        $t["data"][] = $total_row;
    }
    if ($mode != "" and $mode != "static") {
        if ($mode == "edit") {
            _asset_lock_rows();
        }
        if ($mode != "new") {
            _asset_restore_rows($mode == "edit_as_new");
        }
    } else {
        _asset_filter_rows();
    }
}