Esempio n. 1
0
<?php

include './config.php';
if (isset($_POST['submit'])) {
    $first_name = $_POST['first_name'];
    $last_name = $_POST['last_name'];
    $father_name = $_POST['father_name'];
    $date_of_birth = $_POST['$date_of_birth'];
    $gender = $_POST['gender'];
    $username = $_POST['username'];
    $password = $_POST['password'];
    $sql = "SELECT * FROM `students` WHERE `username`='" . DB::quote($username) . "'";
    if (DB::getTotal($sql)) {
        die("User name  <b>{$username}</b> already in use");
    }
    $sql_query = "insert into `students` SET\n                `first_name` ='" . DB::quote($first_name) . "',\n                `last_name`='" . DB::quote($last_name) . "',\n                `father_name`='" . DB::quote($father_name) . "',\n                `date_of_birth`='" . DB::quote($date_of_birth) . "',\n                `gender`='" . DB::quote($gender) . "',\n                `username`='" . DB::quote($username) . "',\n                `password`='" . DB::quote($password) . "'";
    $result = DB::query($sql);
    Auth::login($username, $password);
    Header('Location: course.php');
    exit;
} else {
    die("Hacking attempt");
}
Esempio n. 2
0
    function display($page = 0)
    {
        global $CFG;
        $filters = self::getFilterResults();
        $page = $page > 0 ? $page : $_SESSION['page' . $this->i];
        $_SESSION['page' . $this->i] = $page;
        $page = !($page > 0) || $_REQUEST['submit'] || $this->order_asc_changed || $this->order_by_changed ? 1 : $page;
        $fields = DB::getTableFields($this->table);
        $total_rows = DB::get($this->table, $this->fields, $page, $this->rows_per_page, $this->order_by, $this->order_asc, 1, $filters, $this->inset_id, $this->inset_id_field, false, false, false, false, false, false, false, $this->sql_filter, $this->group_by, $this->no_group_by);
        $data = DB::get($this->table, $this->fields, $page, $this->rows_per_page, $this->order_by, $this->order_asc, 0, $filters, $this->inset_id, $this->inset_id_field, false, false, false, false, false, false, false, $this->sql_filter, $this->group_by, $this->no_group_by);
        $HTML = "";
        if ($CFG->backstage_mode && User::permission(0, 0, $this->link_url, false, $this->is_tab) > 1 && $this->inset_id === false) {
            $HTML .= '
			<form id="grid_form_' . $this->table . $this->i . '" name="grid_form_' . $this->table . $this->i . '" action="' . $CFG->self . '" method="POST">
				<input type="hidden" name="current_url" value="' . $CFG->url . '" />
				<input type="hidden" name="action" value="" />
				<input type="hidden" name="return_to_self" value="1" />';
            if (is_array($this->fields)) {
                foreach ($this->fields as $properties) {
                    if ($properties['aggregate_function']) {
                        $i_name = $properties['name'];
                        switch ($properties['aggregate_function']) {
                            case 'grand_total':
                                $grand_total[$i_name] = 0;
                                break;
                            case 'page_total':
                                $page_total[$i_name] = 0;
                                break;
                            case 'grand_avg':
                                $grand_avg[$i_name] = array();
                                break;
                            case 'page_avg':
                                $page_avg[$i_name] = array();
                                break;
                            case 'both_total':
                                $page_total[$i_name] = 0;
                                $grand_total[$i_name] = 0;
                                break;
                            case 'both_avg':
                                $page_avg[$i_name] = array();
                                $grand_avg[$i_name] = array();
                                break;
                        }
                    }
                }
            }
            if (is_array($this->fields)) {
                foreach ($this->fields as $properties) {
                    if ($properties['cumulative_function']) {
                        $i_name = $properties['name'];
                        if ($properties['cumulative_function'] == 'sum') {
                            $page_total[$i_name] = 0;
                            if ($grand_total) {
                                $grand_total[$i_name] = 0;
                            }
                        } elseif ($properties['cumulative_function'] == 'avg') {
                            $page_avg[$i_name] = array();
                            if ($grand_avg) {
                                $grand_avg[$i_name] = array();
                            }
                        }
                    }
                }
            }
        }
        if ($this->mode == 'list') {
            $HTML .= "<ul class=\"grid_list\">";
            if (is_array($data)) {
                $j = 0;
                foreach ($data as $row) {
                    $HTML .= "<li><ul>";
                    if (is_array($this->fields)) {
                        foreach ($this->fields as $name => $properties) {
                            $key = $name;
                            if (strstr($name, 'lll')) {
                                $name_parts = explode('lll', $name);
                                $name = $name_parts[0];
                            }
                            if ($this->inset_id > 0 && $name == $this->inset_id_field) {
                                continue;
                            }
                            $value = $row[$key];
                            $link_id = $row[$name . '_id'] ? $row[$name . '_id'] : $value;
                            $class = $properties['class'] ? "class=\"{$properties['class']}\"" : '';
                            if ($CFG->pm_editor) {
                                $method_name = Form::peLabel($properties['method_id'], 'field');
                            }
                            $HTML .= "<li {$class}>" . $method_name . "";
                            if (!empty($properties['is_media'])) {
                                reset($CFG->image_sizes);
                                $m_values = explode('|||', $value);
                                $m_size = !empty($properties['media_size']) ? $properties['media_size'] : key($CFG->image_sizes);
                                $m_limit = !empty($properties['media_amount']) ? $properties['media_amount'] : 1;
                                $HTML .= Gallery::multiple($properties['subtable'], $row['id'], $properties['name'], $properties['media_size'], 0, false, $properties['media_amount'], false, false, true);
                                $HTML .= '<div class="clear"></div>';
                            } else {
                                if ($fields[$name]['Type'] == 'datetime' || @in_array($name, $foreign_dates)) {
                                    $value = date($CFG->default_date_format, strtotime($value));
                                } elseif ($fields[$name]['Type'] == "enum('Y','N')") {
                                    $value = $value == 'Y' ? '<div class="y_icon"></div>' : '<div class="n_icon"></div>';
                                }
                                if ($value['filter']) {
                                    $order_asc = $this->order_asc ? false : true;
                                    if ($this->order_by == $name) {
                                        $dir_img = $this->order_asc ? $CFG->up : $CFG->down;
                                    } else {
                                        $dir_img = false;
                                    }
                                    $HTML .= '<b>' . Link::url($this->link_url, $properties['header_caption'] . $dir_img, "filter{$this->i}={$name}&order_by{$this->i}={$this->order_by}&order_asc{$this->i}={$order_asc}&is_tab={$this->is_tab}", $this->filter_results, false, 'content') . ':</b> ';
                                } else {
                                    $HTML .= $this->show_list_captions ? '<b>' . $properties['header_caption'] . ':</b> ' : '';
                                }
                                if (empty($properties['link_url'])) {
                                    $HTML .= str_ireplace('|||', ' ', $value);
                                } else {
                                    $action = $CFG->backstage_mode ? '&action=record' : '';
                                    $value = str_replace('|||', ' ', $value);
                                    if (!empty($value)) {
                                        $HTML .= Link::url($properties['link_url'], $value, "id={$link_id}&is_tab={$properties['link_is_tab']}{$action}", false, false, $this->target_elem_id);
                                    }
                                }
                                if (is_array($page_total)) {
                                    if (array_key_exists($name, $page_total)) {
                                        $page_total[$name] += $value;
                                    }
                                }
                                if (is_array($page_avg)) {
                                    if (array_key_exists($name, $page_avg)) {
                                        $page_avg[$name][] = $value;
                                    }
                                }
                            }
                            $HTML .= "</li>";
                        }
                    }
                    if ($this->show_buttons) {
                        $HTML .= '<li><nobr>' . ($CFG->backstage_mode ? "<span><label for=\"{$row['id']}\">Select:</label><input id=\"{$row['id']}\" type=\"checkbox\" value=\"{$row['id']}\" class=\"grid_select checkbox_input\"/></span>" : '');
                        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) > 0) {
                            $HTML .= Link::url($this->link_url, false, 'id=' . $row['id'] . '&action=record&is_tab=' . $this->is_tab, false, false, $this->target_elem_id, 'view', false, false, false, false, $CFG->view_hover_caption) . ' ';
                        }
                        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) > 1) {
                            $HTML .= Link::url($this->link_url, false, 'id=' . $row['id'] . '&action=form&is_tab=' . $this->is_tab, false, false, $this->target_elem_id, 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' ';
                        }
                        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) > 1) {
                            $HTML .= '<a href="#" class="delete" title="' . $CFG->delete_hover_caption . '" onclick="gridDelete(' . $row['id'] . ',\'' . $this->table . '\',this)"></a></li>';
                        }
                    }
                    $HTML .= '</nobr></li></ul>';
                    $j++;
                }
            } else {
                $HTML .= '<li>' . $CFG->grid_no_results . '</li>';
            }
            $HTML .= "</ul>";
        } elseif ($this->mode == 'graph' || $this->mode == 'graph_line' || $this->mode == 'graph_pie') {
            $name_column = $this->graph_name_column;
            $y_axis = $this->graph_value_column;
            $x_axis = $this->graph_x_axis;
            if (is_array($this->fields)) {
                foreach ($this->fields as $name => $properties) {
                    if (strstr($fields[$name]['Type'], 'varchar') || !$properties['is_op'] && !empty($properties['subtable'])) {
                        if (!$name_column) {
                            $name_column = $name;
                        }
                        $this->name_columns[$name] = $properties['header_caption'];
                    } elseif (strstr($fields[$name]['Type'], 'date')) {
                        if (!$x_axis) {
                            $x_axis = $name;
                        }
                        $this->x_columns[$name] = $properties['header_caption'];
                    } elseif (($properties['is_op'] || strstr($fields[$name]['Type'], 'int') || strstr($fields[$name]['Type'], 'double')) && $name != 'id' && empty($properties['subtable'])) {
                        if (!$y_axis) {
                            $y_axis = $name;
                        }
                        $this->value_columns[$name] = $properties['header_caption'];
                    }
                }
            }
            if ($data) {
                foreach ($data as $row) {
                    $x_values[] = strtotime($row[$x_axis]);
                    $y_values[] = $row[$y_axis];
                }
                $days = (max($x_values) - min($x_values)) / 86400;
                $max_x = max($x_values);
                $min_x = min($x_values);
                $timestamp = $min_x;
                if ($days <= 30) {
                    $time_unit = 'days';
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp += 86400;
                    }
                } elseif ($days > 30 && $days <= 183) {
                    $time_unit = 'weeks';
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp += 86400 * 7;
                    }
                } elseif ($days > 183 && $days <= 910) {
                    $time_unit = 'months';
                    $timestamp = strtotime(date('n/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('n/1/Y', strtotime($p_name . ' + 1 month')));
                    }
                } elseif ($days > 910 && $days <= 1820) {
                    $time_unit = 'months';
                    $timestamp = strtotime(date('n/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('n/1/Y', strtotime($p_name . ' + 2 months')));
                    }
                } elseif ($days > 1820 && $days <= 3640) {
                    $time_unit = 'months';
                    $timestamp = strtotime(date('n/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('n/1/Y', strtotime($p_name . ' + 4 months')));
                    }
                } elseif ($days > 3640 && $days <= 7280) {
                    $time_unit = 'months';
                    $timestamp = strtotime(date('n/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('n/1/Y', strtotime($p_name . ' + 6 months')));
                    }
                } elseif ($days > 7280 && $days <= 14560) {
                    $time_unit = 'months';
                    $timestamp = strtotime(date('n/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('n/1/Y', strtotime($p_name . ' + 8 months')));
                    }
                } elseif ($days > 14560 && $days <= 29120) {
                    $time_unit = 'months';
                    $timestamp = strtotime(date('n/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('n/1/Y', strtotime($p_name . ' + 10 months')));
                    }
                } elseif ($days > 29120 && $days <= 58240) {
                    $time_unit = 'years';
                    $timestamp = strtotime(date('1/1/Y', $min_x));
                    while ($timestamp <= $max_x) {
                        $time_units[] = $timestamp;
                        $timestamp = strtotime(date('1/1/Y', strtotime($p_name . ' + 1 year')));
                    }
                }
                $reps = 0;
                foreach ($data as $row) {
                    if (is_array($this->fields)) {
                        $name_value = $this->graph_combine != 'Y' ? $row[$name_column] : 'All';
                        $c_units = count($time_units);
                        $x_val = strtotime($row[$x_axis]);
                        for ($i = 0; $i < $c_units; $i++) {
                            if ($x_val >= $time_units[$i] && ($x_val < $time_units[$i + 1] || !$time_units[$i + 1])) {
                                $x_current = $time_units[$i];
                                break;
                            }
                        }
                        $key = $x_current;
                        if ($x_prev != $x_current) {
                            $y_current = false;
                            $reps = 0;
                        }
                        if ($this->fields[$y_axis]['is_op'] && !$this->fields[$y_axis]['run_in_sql']) {
                            $y_current = self::doOperation($y_axis, $this->fields[$y_axis], $row, $name_value);
                        } else {
                            $y_current = $y_current + $row[$y_axis];
                        }
                        $x_prev = $x_current;
                        if ($this->mode != 'graph_pie') {
                            if (!$graph_data[$name_value][$key]) {
                                $graph_data[$name_value][$key] = $y_current;
                            } else {
                                if ($this->fields[$y_axis]['cumulative_function'] == 'avg') {
                                    $graph_data[$name_value][$key] = ($graph_data[$name_value][$key] + $y_current) / $reps;
                                } else {
                                    $graph_data[$name_value][$key] += $y_current;
                                }
                            }
                        } else {
                            if (!$graph_data[1][$name_value]) {
                                $graph_data[1][$name_value] = $y_current;
                            } else {
                                if ($this->fields[$y_axis]['cumulative_function'] == 'avg') {
                                    $graph_data[1][$name_value] = ($graph_data[1][$name_value] + $y_current) / $reps;
                                } else {
                                    $graph_data[1][$name_value] += $y_current;
                                }
                            }
                        }
                        $titles[$name_value] = $name_value;
                        $reps++;
                    }
                }
                if ($this->mode != 'graph_pie') {
                    foreach ($graph_data as $name_value => $val) {
                        $last_value = 0;
                        foreach ($time_units as $unit) {
                            $key = $unit;
                            if (!array_key_exists($key, $graph_data[$name_value])) {
                                if (!empty($this->fields[$y_axis]['cumulative_function'])) {
                                    $graph_data[$name_value][$key] = $last_value;
                                } else {
                                    $graph_data[$name_value][$key] = 0;
                                }
                            } else {
                                $last_value = $graph_data[$name_value][$key];
                            }
                        }
                    }
                    foreach ($graph_data as $name_value => $val) {
                        ksort($graph_data[$name_value]);
                        $last_value = 0;
                        foreach ($graph_data[$name_value] as $key => $val) {
                            if ($time_unit == 'days' || $time_unit == 'weeks') {
                                $key1 = date('M j', $key);
                            } elseif ($time_unit == 'months') {
                                $key1 = date('M', $key);
                            } elseif ($time_unit == 'years') {
                                $key1 = date('Y', $key);
                            }
                            $graph_data1[$name_value][$key1] = $val;
                        }
                    }
                } else {
                    $graph_data1 = $graph_data;
                }
            }
            $HTML .= '<img class="graph" src="includes/graph.php?graph_data=' . urlencode(serialize($graph_data1)) . '&titles=' . urlencode(serialize($titles)) . '&mode=' . $this->mode . '">';
        } else {
            $HTML .= "<table><tr class=\"grid_header\">";
            if ($CFG->backstage_mode && !$this->links_out && $this->show_buttons && $CFG->is_ctrl_panel != 'Y') {
                $HTML .= "<th><label for=\"grid_select{$this->i}\"/><input id=\"grid_select{$this->i}\" type=\"checkbox\" class=\"grid_select checkbox_input\" onclick=\"gridSelectAll(this)\"/></th>";
            }
            if (is_array($this->fields)) {
                foreach ($this->fields as $name => $value) {
                    $key = $name;
                    if (strstr($name, 'lll')) {
                        $name_parts = explode('lll', $name);
                        $name = $name_parts[0];
                    }
                    if ($this->inset_id > 0) {
                        if ($value['name'] == $this->inset_id_field) {
                            continue;
                        }
                        if (strstr($this->inset_id_field, '.')) {
                            $inset_field_parts = explode('.', $this->inset_id_field);
                            if ($value['subtable'] == $inset_field_parts[0] && (in_array($inset_field_parts[1], $value['subtable_fields']) || $inset_field_parts[1] == 'id')) {
                                continue;
                            }
                        }
                    }
                    if ($value['is_op'] && !$value['run_in_sql']) {
                        if ($CFG->pm_editor) {
                            $method_name = Form::peLabel($value['method_id'], 'aggregate');
                        }
                        $HTML .= "<th>" . $value['header_caption'] . $method_name . '</th>';
                        continue;
                    } elseif ($value['is_form']) {
                        if ($CFG->pm_editor) {
                            $method_name = Form::peLabel($value['method_id'], 'inlineForm');
                        }
                        if ($value['header_caption']) {
                            $HTML .= "<th class=\"multiple_input\">" . $value['header_caption'] . $method_name . '</th>';
                        }
                        continue;
                    }
                    if ($CFG->pm_editor) {
                        $method_name = !$value['run_in_sql'] ? Form::peLabel($value['method_id'], 'field') : Form::peLabel($value['method_id'], 'aggregate');
                    }
                    if ($value['filter']) {
                        $order_asc = $this->order_asc ? false : true;
                        if ($this->order_by == $name) {
                            $dir_img = $this->order_asc ? $CFG->up : $CFG->down;
                        } else {
                            $dir_img = false;
                        }
                        $filter_results = $_REQUEST['form_filters' . $this->i];
                        $HTML .= "<th>" . Link::url($CFG->url, $value['header_caption'] . $dir_img, false, array('filter' . $this->i => $name, 'order_by' . $this->i => $this->order_by, 'order_asc' . $this->i => $order_asc, 'is_tab' => $this->is_tab, 'inset_id' => $this->inset_id, 'inset_id_field' => $this->inset_id_field, 'inset_i' => $this->inset_i, 'form_filters' . $this->i => $filter_results, 'search_fields' . $this->i => $_REQUEST['search_fields' . $this->i]), false, false, $this->inset_i > 0 ? 'inset_area_' . $this->inset_i : 'content') . $method_name . "</th>";
                    } else {
                        $HTML .= "<th>" . $value['header_caption'] . $method_name . '</th>';
                    }
                }
            }
            $HTML .= $this->show_buttons ? "<th>&nbsp;</th>" : '';
            $HTML .= '</tr>';
            if (is_array($data)) {
                $alt = false;
                foreach ($data as $row) {
                    $alt = $alt ? false : 'alt';
                    if ($this->alert_condition1) {
                        $condition = String::doFormulaReplacements($this->alert_condition1, $row, 1);
                        $alert_class1 = eval("if ({$condition}) { return 1;} else { return 0;}") ? 'alert1' : '';
                    }
                    if ($this->alert_condition2) {
                        $condition = String::doFormulaReplacements($this->alert_condition2, $row, 1);
                        $alert_class2 = eval("if ({$condition}) { return 1;} else { return 0;}") ? 'alert2' : '';
                    }
                    $HTML .= '<tr class="' . $alt . ' ' . $alert_class1 . ' ' . $alert_class2 . '">';
                    if (!is_array($this->fields)) {
                        continue;
                    }
                    if ($CFG->backstage_mode && !$this->links_out && $this->show_buttons && $CFG->is_ctrl_panel != 'Y') {
                        $HTML .= "<td><label for=\"checkbox{$row['id']}\"/><input id=\"checkbox{$row['id']}\" type=\"checkbox\" value=\"{$row['id']}\" class=\"grid_select checkbox_input\"/></td>";
                    }
                    foreach ($this->fields as $name => $properties) {
                        $key = $name;
                        if (strstr($name, 'lll')) {
                            $name_parts = explode('lll', $name);
                            $name = $name_parts[0];
                        }
                        if ($this->inset_id > 0) {
                            if ($properties['name'] == $this->inset_id_field) {
                                continue;
                            }
                            if (strstr($this->inset_id_field, '.')) {
                                $inset_field_parts = explode('.', $this->inset_id_field);
                                if ($properties['subtable'] == $inset_field_parts[0] && (in_array($inset_field_parts[1], $properties['subtable_fields']) || $inset_field_parts[1] == 'id')) {
                                    continue;
                                }
                            }
                        }
                        $value = $row[$key];
                        $link_id = $row[$name . '_id'] ? $row[$name . '_id'] : $value;
                        $class = $properties['class'] ? "class=\"{$properties['class']}\"" : '';
                        $HTML .= "<td {$class}>";
                        if (!empty($properties['is_media'])) {
                            reset($CFG->image_sizes);
                            $m_values = explode('|||', $value);
                            $m_size = !empty($properties['media_size']) ? $properties['media_size'] : key($CFG->image_sizes);
                            $m_limit = !empty($properties['media_amount']) ? $properties['media_amount'] : 1;
                            $HTML .= Gallery::multiple($properties['subtable'], $row['id'], $properties['name'], $properties['media_size'], 0, false, $properties['media_amount'], false, false, true);
                        } elseif ($properties['is_op'] && !$properties['run_in_sql']) {
                            $value1 = number_format(self::doOperation($key, $properties, $row), 2);
                            if (is_array($page_total)) {
                                if (array_key_exists($name, $page_total)) {
                                    $page_total[$key] += $value1;
                                }
                            }
                            if (is_array($page_avg)) {
                                if (array_key_exists($name, $page_avg)) {
                                    $page_avg[$key][] = $value1;
                                }
                            }
                            $HTML .= $value1;
                        } elseif ($properties['is_form']) {
                            if (!$properties['header_caption']) {
                                continue;
                            }
                            $HTML .= '<div>';
                            if (!$ref) {
                                $ref = new ReflectionClass('Form');
                                if (is_array($properties['inputs_array'])) {
                                    foreach ($properties['inputs_array'] as $method => $args) {
                                        $method_parts = explode('|', $method);
                                        $method1 = $method_parts[0];
                                        $params = $ref->getMethod($method1)->getParameters();
                                        if (is_array($params)) {
                                            $i = 0;
                                            foreach ($params as $param) {
                                                $param_name = $param->getName();
                                                if ($param_name == 'value') {
                                                    $i_methods[$method]['value'] = $i;
                                                } elseif ($param_name == 'static') {
                                                    $i_methods[$method]['static'] = $i;
                                                } elseif ($param_name == 'j') {
                                                    $i_methods[$method]['j'] = $i;
                                                } elseif ($param_name == 'checked') {
                                                    $i_methods[$method]['checked'] = $i;
                                                } elseif ($param_name == 'grid_input') {
                                                    $i_methods[$method]['grid_input'] = $i;
                                                } elseif ($param_name == 'is_current_timestamp') {
                                                    $i_methods[$method]['is_current_timestamp'] = $i;
                                                }
                                                $i++;
                                            }
                                        }
                                    }
                                }
                            }
                            if (!empty($properties['insert_new_record_when'])) {
                                $properties['insert_new_record_when'] = String::replaceConditionals('(' . $properties['insert_new_record_when'] . ')', $row, $properties['f_id_field']);
                                $result = eval("if ({$properties['insert_new_record_when']}) { return 0;} else { return 1;}");
                            }
                            $i_table = !empty($properties['table']) ? $properties['table'] : $this->table;
                            $i_f_id = $properties['f_id'] ? $row[str_replace('[', '', str_replace(']', '', $properties['f_id']))] : $row['id'];
                            if (!$result) {
                                $i_row = DB::getRecord($i_table, 0, $row['id'], 1, $properties['f_id_field'], $properties['order_by'], $properties['order_asc']);
                            } else {
                                $i_row = false;
                            }
                            $HTML .= '
							<input type="hidden" name="iform_table' . $this->i . '[' . $row['id'] . ']" value="' . $i_table . '" />
							<input type="hidden" name="iform_id' . $this->i . '[' . $row['id'] . ']" value="' . $i_row['id'] . '" />';
                            if ($i_row) {
                                $HTML .= '<input type="hidden" name="iform_action' . $this->i . '[' . $row['id'] . ']" value="edit" />';
                            } else {
                                $HTML .= '<input type="hidden" name="iform_action' . $this->i . '[' . $row['id'] . ']" value="new" />';
                            }
                            if (is_array($properties['inputs_array'])) {
                                foreach ($properties['inputs_array'] as $method => $args) {
                                    $method_parts = explode('|', $method);
                                    $method1 = $method_parts[0];
                                    $CFG->method_id = $args['pm_method_id'];
                                    unset($args['pm_method_id']);
                                    unset($args['show_total']);
                                    $args1 = $args;
                                    $args = array_values($args);
                                    $i_value = $i_methods[$method]['value'];
                                    $i_checked = $i_methods[$method]['checked'];
                                    $i_static = $i_methods[$method]['static'];
                                    $i_j = $i_methods[$method]['j'];
                                    $i_is_current_timestamp = $i_methods[$method]['is_current_timestamp'];
                                    $input_name = $args[0];
                                    $args[$i_static] = 1;
                                    $args[$i_j] = $input_name;
                                    $args[0] = $row['id'];
                                    if ($method1 == 'textInput') {
                                        $args[13] = '';
                                        ksort($args);
                                    }
                                    if ($method1 == 'hiddenInput') {
                                        $args[8] = '';
                                        $args[$i_is_current_timestamp] = $args1['is_current_timestamp'];
                                        ksort($args);
                                    }
                                    if ($args1['show_total']) {
                                        $totals[$input_name][] = $row[$input_name];
                                    }
                                    if ($method1 == 'checkBox') {
                                        $args[$i_checked] = $i_row[$input_name];
                                    } else {
                                        $args[$i_value] = $i_row[$input_name];
                                    }
                                    if (!$class_instance) {
                                        $CFG->form_output_started = true;
                                        $class_instance = $ref->newInstanceArgs(array('grid_form_' . $this->table . $this->i));
                                    }
                                    $method_instance = $ref->getMethod($method1);
                                    $HTML .= '<div class="col" id="' . $properties['method_id'] . '">' . $method_instance->invokeArgs($class_instance, $args) . '</div>';
                                }
                            }
                            if ($class_instance) {
                                $CFG->o_method_suppress = true;
                                $method_instance = $ref->getMethod('hiddenInput');
                                $HTML .= $method_instance->invokeArgs($class_instance, array($row['id'], 0, $i_f_id, false, false, false, 1, $properties['f_id_field']));
                                $CFG->o_method_suppress = false;
                            }
                            $HTML .= '<div class="clear"></div>';
                            $HTML .= '</div>';
                        } else {
                            $value = self::detectData($key, $value, $fields, $foreign_dates);
                            if (!empty($properties['link_url'])) {
                                $action = $CFG->backstage_mode ? '&action=record' : '';
                                $value = str_replace('|||', ' ', $value);
                                if (!empty($value)) {
                                    $HTML .= Link::url($properties['link_url'], $value, "id={$link_id}&is_tab={$properties['link_is_tab']}{$action}", false, false, 'content');
                                }
                            } else {
                                $HTML .= str_ireplace('|||', ' ', $value);
                            }
                            if (is_array($page_total)) {
                                if (array_key_exists($name, $page_total)) {
                                    $page_total[$key] += $value;
                                }
                            }
                            if (is_array($page_avg)) {
                                if (array_key_exists($name, $page_avg)) {
                                    $page_avg[$key][] = $value;
                                }
                            }
                        }
                        $HTML .= "</td>";
                    }
                    if ($this->show_buttons) {
                        $HTML .= '<td><nobr>';
                        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) > 0) {
                            $HTML .= Link::url($this->link_url, false, 'id=' . $row['id'] . '&action=record&is_tab=' . $this->is_tab, false, false, $this->target_elem_id, 'view', false, false, false, false, $CFG->view_hover_caption) . ' ';
                        }
                        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) > 1) {
                            $HTML .= Link::url($this->link_url, false, 'id=' . $row['id'] . '&action=form&is_tab=' . $this->is_tab, false, false, $this->target_elem_id, 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' ';
                        }
                        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) > 1 && !$this->links_out) {
                            $HTML .= '<a href="#" title="' . $CFG->delete_hover_caption . '" onclick="gridDelete(' . $row['id'] . ',\'' . $this->table . '\',this)" class="delete"></a></nobr></td>';
                        }
                    }
                    $HTML .= '</tr>';
                }
            } else {
                $HTML .= '<tr><td colspan="' . (count($this->fields) + 2) . '">' . $CFG->grid_no_results . '</td></tr>';
            }
            if ($page_total || $page_avg) {
                $HTML .= '<tr>';
                if ($this->show_buttons) {
                    $HTML .= '<td></td>';
                }
                foreach ($this->fields as $name => $properties) {
                    if ($page_total[$name]) {
                        $subtotal = $page_total[$name];
                        $subtotal = stristr($fields[$name]['Type'], 'double') ? number_format($subtotal, 2) : $subtotal;
                        $subtotals[$name] = $subtotal;
                    } else {
                        $subtotal = false;
                    }
                    if ($page_avg[$name]) {
                        $subavg = array_sum($page_avg[$name]) / count($page_avg[$name]);
                        $subavg = stristr($fields[$name]['Type'], 'double') ? number_format($subavg, 2) : $subavg;
                        $subavgs[$name] = $subavg;
                    } else {
                        $subavg = false;
                    }
                }
                $subtotals = $subtotals ? $subtotals : array();
                $subavgs = $subavgs ? $subavgs : array();
                $subs = array_merge($subtotals, $subavgs);
                foreach ($this->fields as $name => $properties) {
                    $sub = $subs[$name] ? $subs[$name] : false;
                    $HTML .= '<td class="subtotal">' . (is_numeric($sub) ? number_format($sub, 2) : $sub) . '</td>';
                }
                if ($this->show_buttons) {
                    $HTML .= '<td class="subtotal"><em>' . ($page_total ? $CFG->subtotal_label : '') . ($page_total && $page_avg ? '/' : '') . ($page_avg ? $CFG->subavg_label : '') . '</em></td>';
                }
                $HTML .= '</tr>';
            }
            if ($grand_total || $grand_avg) {
                $HTML .= '<tr>';
                if ($this->show_buttons) {
                    $HTML .= '<td class="total"></td>';
                }
                foreach ($this->fields as $name => $properties) {
                    if ($properties['is_op'] && !$properties['run_in_sql']) {
                        continue;
                    }
                    if (is_array($grand_total)) {
                        if (array_key_exists($name, $grand_total)) {
                            $total = array_key_exists($name, $grand_total) ? number_format(DB::getTotal($properties, $this->table), 2) : false;
                            $totals[$name] = $total;
                        }
                    }
                    if (is_array($grand_avg)) {
                        if (array_key_exists($name, $grand_avg)) {
                            //$avg = (array_key_exists($name,$grand_avg)) ? number_format(DB::get($this->table,array($this->table.'.'.$name),$page,$this->rows_per_page,$this->order_by,$this->order_asc,0,$this->filter_results,$this->inset_id,$this->inset_id_field,0,1),2) : false;
                            $avg = array_key_exists($name, $grand_avg) ? number_format(DB::getAverage($properties, $this->table), 2) : false;
                            $totals[$name] = $avg;
                        }
                    }
                }
                foreach ($this->fields as $name => $properties) {
                    if ($properties['is_op'] && !$properties['run_in_sql']) {
                        /*
                        $formula = $properties['formula'];
                        foreach ($totals as $o_name => $o_value) {
                        	$formula = str_replace($o_name,str_replace(',','',$o_value),$formula);
                        }
                        $total = eval("return $formula ;");
                        */
                        $total = false;
                    } else {
                        if ($totals[$name]) {
                            $total = $totals[$name];
                        } else {
                            $total = false;
                        }
                    }
                    $HTML .= '<td class="total">' . (is_numeric($total) ? number_format($total, 2) : $total) . '</td>';
                }
                if ($this->show_buttons) {
                    $HTML .= '<td class="total"><em>' . ($grand_total ? $CFG->total_label : '') . ($grand_total && $grand_avg ? '/' : '') . ($grand_avg ? $CFG->avg_label : '') . '</em></td>';
                }
                $HTML .= '</tr>';
            }
            $HTML .= '</table>';
        }
        $pagination = Grid::pagination($page, $total_rows);
        if ($this->grid_label) {
            if ($CFG->pm_editor) {
                $method_name = Form::peLabel($this->grid_label['method_id'], 'gridLabel');
            }
            $grid_label = $this->grid_label['text'] . ' ' . $method_name;
        } else {
            $grid_label = Ops::getPageTitle();
        }
        Grid::show_filters();
        Grid::show_errors();
        $amount = $total_rows > 0 ? '(' . $total_rows . ')' : false;
        if (!($this->inset_id > 0 || $CFG->is_form_inset)) {
            echo '
			<div class="area full_box" id="grid_' . $this->i . '">
				<h2>' . $grid_label . ' ' . $amount . '</h2>
				<div class="box_bar"></div>
				<div class="box_tl"></div>
				<div class="box_tr"></div>
				<div class="box_bl"></div>
				<div class="box_br"></div>
				<div class="t_shadow"></div>
				<div class="r_shadow"></div>
				<div class="b_shadow"></div>
				<div class="l_shadow"></div>
				<div class="box_b"></div>
				<div class="grid_buttons">
				' . $pagination . '';
            if (is_array($this->modes)) {
                if (count($this->modes) > 1) {
                    echo '<div class="modes">';
                    foreach ($this->modes as $mode => $enabled) {
                        $class1 = $mode == $this->mode ? 'active_view' : false;
                        if ($mode == 'table') {
                            $class = 'switch_table';
                            $caption = $CFG->switch_to_table;
                        } elseif ($mode == 'list') {
                            $class = 'switch_list';
                            $label = $CFG->switch_to_list;
                        } elseif ($mode == 'graph') {
                            $class = 'switch_graph';
                            $label = $CFG->switch_to_graph;
                        } elseif ($mode == 'graph_line') {
                            $class = 'switch_graph_line';
                            $label = $CFG->switch_to_graph_line;
                        } elseif ($mode == 'graph_pie') {
                            $class = 'switch_graph_pie';
                            $label = $CFG->switch_to_graph_pie;
                        }
                        echo Link::url($CFG->url, false, false, array('page' . $this->i => $page, 'p_bypass' . $this->i => 1, 'mode' . $this->i => $mode), true, 'content', $class . ' ' . $class1, false, false, false, false, $label) . ' ';
                    }
                    echo '</div>';
                }
            }
            if (!$this->links_out && $this->show_buttons && $CFG->is_ctrl_panel != 'Y') {
                echo '<div class="button before"></div>';
                if (is_array($this->fields)) {
                    foreach ($this->fields as $properties) {
                        if ($properties['is_form']) {
                            $i_name = !$properties['button_submit_all'] ? $properties['name'] : 'all';
                            echo '<a href="#" onclick="gridSubmitForm(\'' . $this->table . $this->i . '\')" class="button"><div class="save"></div>' . $properties['save_button_caption'] . '</a>';
                            if ($properties['button_submit_all']) {
                                break;
                            }
                        }
                    }
                }
                if (array_key_exists('is_active', $fields)) {
                    echo '<a class="button" href="#" onclick="gridSetActive(\'' . $this->table . '\',1)">' . $CFG->grid_activate_button . '</a>';
                    echo '<a class="button" href="#" onclick="gridSetActive(\'' . $this->table . '\')">' . $CFG->grid_deactivate_button . '</a>';
                }
                echo Link::url($this->link_url, '<div class="add_new"></div>' . $CFG->add_new_caption, '&action=form&is_tab=' . $this->is_tab, false, false, $this->target_elem_id, 'button') . '<a class="button last" href="#" onclick="gridDeleteSelection(\'' . $this->table . '\')"><div class="delete"></div> ' . $CFG->delete_button_label . '</a>';
                echo '<div class="button after"></div>';
            }
            echo '
				</div>
				<div class="contain">';
        }
        $HTML .= '
		<script type="text/javascript">
			$(document).ready(function() {
				$("#grid_' . $this->i . '").find("th").mouseover(function() {
					gridHighlightTH(this);
				});
				$("#grid_' . $this->i . '").find("th").mouseout(function() {
					gridUnHighlightTH(this);
				});
				$("#grid_' . $this->i . '").find("td").mouseover(function() {
					gridHighlightTD(this);
				});
				$("#grid_' . $this->i . '").find("td").mouseout(function() {
					gridUnHighlightTD(this);
				});
			';
        if (User::permission(0, 0, $this->link_url, false, $this->is_tab) < 1) {
            $HTML .= '
				$("input").attr("disabled","disabled");
				$("select").attr("disabled","disabled");
			';
        }
        $HTML .= '
			});
		</script>';
        echo $HTML;
        if ($this->rows_per_page > 30) {
            echo $pagination;
        }
        if ($CFG->backstage_mode && User::permission(0, 0, $this->link_url, false, $this->is_tab) > 1 && $this->inset_id === false) {
            echo "</form>";
        }
        if (!($this->inset_id > 0 || $CFG->is_form_inset)) {
            echo '</div></div>';
        }
    }