public static function get($table, $fields, $page = 0, $per_page = 0, $order_by = false, $order_asc = false, $count = false, $filter_results = false, $f_id = false, $f_id_field = false, $get_total = false, $get_average = false, $s_date = false, $e_date = false, $s_date_field = false, $e_date_field = false, $calendar_mode = false, $filters = false, $group_by = false, $no_group_by = false) { global $CFG; if (!self::tableExists($table)) { $this->errors[$table] = $CFG->grid_no_table_error; self::show_errors(); return false; } $page = !$page || $page < 1 ? 1 : (int) $page; $table_fields = self::getTableFields($table); $subtables = self::getSubtables($table); $subtables = !$subtables ? array() : $subtables; $start_row = $per_page * ($page - 1); $remote_i = 0; if (!$get_total || !$get_average) { $db_fields[] = "{$table}.id AS id"; } if (!$table_fields) { $this->errors[$table] = $CFG->grid_no_table_error; self::show_errors(); return false; } if (is_array($filter_results)) { foreach ($filter_results as $f_result => $f_value) { if ($_REQUEST['subtables'][$f_result]['subtable']) { $fields[$f_result] = array('subtable' => $_REQUEST['subtables'][$f_result]['subtable'], 'subtable_fields' => $_REQUEST['subtables'][$f_result]['subtable_fields'], 'f_id_field' => $_REQUEST['subtables'][$f_result]['f_id_field'], 'filter_result' => 1, 'f_value' => $f_value['results']); unset($filter_results[$f_result]); } } } if (is_array($fields)) { foreach ($fields as $name => $field) { if ($field['is_op'] && !$field['run_in_sql']) { continue; } $key = $name; if (strstr($name, 'lll')) { $name_parts = explode('lll', $name); $name = $name_parts[0]; } $is_field = array_key_exists($name, $table_fields) || is_array($field['subtable_fields']) && $CFG->in_file_manager; $has_subtable = in_array($field['subtable'], $subtables); $has_table = self::tableExists($field['subtable']); $subtable = $field['subtable']; $s_fields = array(); if ($field['aggregate_function'] == 'page_total' || $field['aggregate_function'] == 'both_total') { $page_total[$key] = 0; } if ($field['aggregate_function'] == 'page_avg' || $field['aggregate_function'] == 'both_avg') { $page_avg[$key] = array(); } if ($field['aggregate_function'] == 'grand_total' || $field['aggregate_function'] == 'both_total') { $grand_total[$key] = true; } if ($field['aggregate_function'] == 'grand_avg' || $field['aggregate_function'] == 'both_avg') { $grand_avg[$key] = true; } if ($field['run_in_sql']) { $formula1 = "{$field['formula']} AS `{$name}`"; /* $db_fields[] = "{$field['formula']} AS $name"; $formulas[] = $field['formula']; */ if ($field['join_path']) { if (!strstr($field['join_path'], ',')) { $foreign_tables[$subtable] = $field['join_path']; if ($field['filter_result']) { $filter_results[$subtable] = $field['f_value']; } } else { $join_path = explode(',', $field['join_path']); if (is_array($join_path)) { $join_table = false; if (strstr($field['order_by'], '.')) { $otparts = explode('.', $field['order_by']); $foreign_order_table = $otparts[0]; } else { $foreign_order_table = false; $otparts = false; } $i1 = 1; $c1 = count($join_path); foreach ($join_path as $join_field) { $join_field_parts = explode('.', $join_field); $join_table = $join_field_parts[0]; $j_field = $join_field_parts[1]; $remote_tables[$remote_i][$join_table][] = $j_field; if ($i1 == $c1) { $remote_tables[$remote_i][$join_table]['join_condition'] = $field['join_condition']; } $i1++; } if ($field['filter_result']) { $join_table1 = $join_table . $remote_i . '.' . $j_field; $filter_results[$join_table1] = $field['f_value']; } $formula1 = str_replace($join_table, $join_table . $remote_i, $formula1); } } } else { $formulas[] = $formula1; } $db_fields[] = $formula1; continue; } if (!$is_field && !$has_subtable && !$has_subtable && empty($field['is_media']) && empty($field['f_id_field'])) { //$this->errors[$key] = $CFG->grid_no_field_error; unset($fields[$k]); continue; } elseif ($is_field && !$has_subtable && !$has_table) { if (is_array($field['subtable_fields'])) { foreach ($field['subtable_fields'] as $s_field) { $f_subtable = $field['subtable'] ? '_' . $field['subtable'] : ''; $s_fields[] = "{$table}{$f_subtable}.{$s_field}"; } $db_fields[] = "CONCAT_WS('{$concat_char}'," . implode(',', $s_fields) . ") AS `{$name}`"; } else { $db_fields[] = "{$table}.{$name} AS `{$name}`"; } } elseif ($has_subtable || $has_table) { $subtable_fields = !is_array($field['subtable_fields']) && !empty($field['subtable_fields']) ? array($field['subtable_fields']) : $field['subtable_fields']; $concat_char = !empty($field['subtable_fields_concat']) ? $field['subtable_fields_concat'] : ' '; $concat_char = !$subtable_fields ? '|||' : $concat_char; if ($is_field && !$has_subtable && $has_table) { $subtable_fields = !$subtable_fields ? self::getTableFields($field['subtable'], true) : $subtable_fields; $fields[$key]['subtable_fields'] = $subtable_fields; if (@array_key_exists($subtable, $join_tables)) { //if something gets messed up, it's probably this $alias = 'a' . rand(1, 99); $j_table = $subtable . $alias; } else { $j_table = $subtable; } $int_fields = self::getFieldsLikeType($field['subtable'], 'int'); $int_exists = array(); $dou_fields = self::getFieldsLikeType($field['subtable'], 'double'); $dou_exists = array(); foreach ($subtable_fields as $s_field) { $s_fields[] = "{$j_table}.{$s_field}"; $int_exists[] = @array_key_exists($s_field, $int_fields); $dou_exists[] = @array_key_exists($s_field, $dou_fields); } if (count($subtable_fields) == array_sum($int_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL)"; } elseif (count($subtable_fields) == array_sum($dou_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL(10,2))"; } else { $cast1 = false; $cast2 = false; } $db_fields[] = "{$cast1}CONCAT_WS('{$concat_char}'," . implode(',', $s_fields) . "){$cast2} AS `{$key}`"; $db_fields[] = "{$j_table}.id AS {$key}_id"; $j_field = $field['f_id_field'] ? array($name, $field['f_id_field']) : $name; $f_field = $field['f_id_field'] ? $field['f_id_field'] : 'id'; if (@array_key_exists($subtable, $join_tables)) { $join_tables[$subtable . '|' . $alias] = $j_field; if ($field['filter_result']) { $filter_results[$subtable . '|' . $alias . '.' . $f_field] = $field['f_value']; } } else { $join_tables[$subtable] = $j_field; if ($field['filter_result']) { $filter_results[$subtable . '.' . $f_field] = $field['f_value']; } } } elseif (!$is_field && $has_subtable && $has_table) { $subtable_fields = !$subtable_fields ? self::getTableFields($field['subtable'], true) : $subtable_fields; $fields[$key]['subtable_fields'] = $subtable_fields; $int_fields = self::getFieldsLikeType($field['subtable'], 'int'); $int_exists = array(); $dou_fields = self::getFieldsLikeType($field['subtable'], 'double'); $dou_exists = array(); foreach ($subtable_fields as $s_field) { $s_fields[] = "{$field['subtable']}.{$s_field}"; $int_exists[] = @array_key_exists($s_field, $int_fields); $dou_exists[] = @array_key_exists($s_field, $dou_fields); } if (count($subtable_fields) == array_sum($int_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL)"; } elseif (count($subtable_fields) == array_sum($dou_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL(10,2))"; } else { $cast1 = false; $cast2 = false; } $db_fields[] = "{$cast1}GROUP_CONCAT(DISTINCT CONCAT_WS('{$concat_char}'," . implode(',', $s_fields) . ") SEPARATOR ', '){$cast2} AS `{$name}`"; $db_fields[] = "{$field['subtable']}.id AS {$name}_id"; $double_join_tables[$subtable] = $name; } elseif (!$is_field && $has_subtable && !$has_table) { if (empty($field['is_media'])) { $subtable_fields = !$subtable_fields ? self::getTableFields("{$table}_{$field['subtable']}", true) : $subtable_fields; $fields[$key]['subtable_fields'] = $subtable_fields; $int_fields = self::getFieldsLikeType($field['subtable'], 'int'); $int_exists = array(); $dou_fields = self::getFieldsLikeType($field['subtable'], 'double'); $dou_exists = array(); foreach ($subtable_fields as $s_field) { $s_fields[] = "{$table}_{$field['subtable']}.{$s_field}"; $int_exists[] = array_key_exists($s_field, $int_fields); $dou_exists[] = array_key_exists($s_field, $dou_fields); } if (count($subtable_fields) == array_sum($int_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL)"; } elseif (count($subtable_fields) == array_sum($dou_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL(10,2))"; } else { $cast1 = false; $cast2 = false; } $db_fields[] = "{$cast1}GROUP_CONCAT(DISTINCT CONCAT_WS('{$concat_char}'," . implode(',', $s_fields) . ")){$cast2} AS `{$name}`"; $db_fields[] = "{$table}_{$field['subtable']}.id AS {$name}_id"; $join_subtables[$subtable] = 'id'; } else { $db_fields[] = "(CONCAT_WS('|||','{$table}','{$subtable}','{$name}','{$field['thumb_amount']}','{$field['media_amount']}','{$field['media_size']}')) AS `{$name}`"; } } elseif (!$is_field && !$has_subtable && $has_table) { if (empty($field['is_media'])) { $subtable_fields = !$subtable_fields ? self::getTableFields($field['subtable'], true) : $subtable_fields; $fields[$key]['subtable_fields'] = $subtable_fields; $f_where[$name] = $field['subtable']; $remote_i++; $int_fields = self::getFieldsLikeType($field['subtable'], 'int'); $int_exists = array(); $dou_fields = self::getFieldsLikeType($field['subtable'], 'double'); $dou_exists = array(); foreach ($subtable_fields as $s_field) { $s_fields[] = "{$field['subtable']}{$remote_i}.{$s_field}"; $int_exists[] = @array_key_exists($s_field, $int_fields); $dou_exists[] = @array_key_exists($s_field, $dou_fields); } if (count($subtable_fields) == array_sum($int_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL)"; } elseif (count($subtable_fields) == array_sum($dou_exists)) { $cast1 = "CAST("; $cast2 = " AS DECIMAL(10,2))"; } else { $cast1 = false; $cast2 = false; } if (!$field['filter_result']) { $db_fields[] = "{$cast1}CONCAT_WS('{$concat_char}'," . implode(',', $s_fields) . "){$cast2} AS `{$name}`"; $db_fields[] = "{$field['subtable']}{$remote_i}.id AS {$name}_id"; } if (!strstr($field['f_id_field'], ',')) { $foreign_tables[$subtable] = $field['f_id_field']; $foreign_order[$subtable]['order_by'] = $field['order_by']; $foreign_order[$subtable]['order_asc'] = $field['order_asc']; $foreign_order[$subtable]['limit_is_curdate'] = $field['limit_is_curdate']; if ($field['filter_result']) { $filter_results[$subtable] = $field['f_value']; } } else { $join_path = explode(',', $field['f_id_field']); if (is_array($join_path)) { $join_table = false; if (strstr($field['order_by'], '.')) { $otparts = explode('.', $field['order_by']); $foreign_order_table = $otparts[0]; } else { $foreign_order_table = false; $otparts = false; } foreach ($join_path as $join_field) { $join_field_parts = explode('.', $join_field); $join_table = $join_field_parts[0]; $j_field = $join_field_parts[1]; $remote_tables[$remote_i][$join_table][] = $j_field; } if ($field['filter_result']) { $join_table1 = $join_table . $remote_i . '.' . $j_field; $filter_results[$join_table1] = $field['f_value']; } if ($join_table && $field['order_by']) { $join_table = $foreign_order_table ? $foreign_order_table : $join_table; $foreign_order[$join_table]['order_by'] = is_array($otparts) ? $otparts[1] : $field['order_by']; $foreign_order[$join_table]['order_asc'] = $field['order_asc']; $foreign_order[$join_table]['limit_is_curdate'] = $field['limit_is_curdate']; $foreign_order[$join_table]['remote_i'] = $remote_i; } } } $field_properties = self::getTableFields($field['subtable']); foreach ($field_properties as $k => $v) { if ($v['Type'] == 'datetime') { $foreign_dates[$k] = $k; } } } } } } } if (strstr($f_id_field, ',')) { $remote_i++; $join_path = explode(',', $f_id_field); if (is_array($join_path)) { $c = count($join_path) - 1; $i = 0; foreach ($join_path as $join_field) { if ($i == $c) { break; } $join_field_parts = explode('.', $join_field); $join_table = $join_field_parts[0]; $j_field = $join_field_parts[1]; $remote_tables[$remote_i][$join_table][] = $j_field; $f_id_table_i = $remote_i; $i++; } } } if (is_array($filter_results['cat_selects'])) { foreach ($filter_results['cat_selects'] as $c_table => $ids) { if (!@array_key_exists($c_table, $double_join_tables)) { $double_join_tables[$c_table] = ''; } } } if (!$db_fields) { return false; } $order_asc = $order_asc ? 'ASC' : 'DESC'; if ($count) { $sql = "SELECT COUNT(DISTINCT {$table}.id) AS total FROM {$table} "; } elseif ($get_total) { $sql = "SELECT SUM(" . implode(',', $db_fields) . ") AS grand_total FROM {$table} "; } elseif ($get_average) { $sql = "SELECT AVG(" . implode(',', $db_fields) . ") AS grand_total FROM {$table} "; } else { $sql = "SELECT " . implode(',', $db_fields) . " FROM {$table} "; } if (is_array($join_tables)) { foreach ($join_tables as $j_table => $j_field) { if (strstr($j_table, '|')) { $j_parts = explode('|', $j_table); $j_table = $j_parts[0]; $j_alias = $j_parts[0] . $j_parts[1]; $j_as = $j_alias; } else { $j_alias = $j_table; $j_as = false; } if (is_array($j_field)) { $j_field1 = $j_field[0]; $j_field2 = $j_field[1]; if (strstr($j_field2, ',')) { $j_parts = explode(',', $j_field2); if (strstr($j_parts[1], '.')) { $j_parts1 = explode('.', $j_parts[1]); $j_field2 = $j_parts1[1]; } else { $j_field2 = $j_parts[1]; } } } else { $j_field1 = $j_field; $j_field2 = 'id'; } $sql .= " LEFT JOIN {$j_table} {$j_as} ON ({$table}.{$j_field1} = {$j_alias}.{$j_field2}) "; if ($j_table != $j_alias) { $joined_tables[$j_table] = $j_alias; } } } if (is_array($join_subtables)) { foreach ($join_subtables as $j_table => $j_field) { $sql .= " LEFT JOIN {$table}_{$j_table} ON ({$table}.id = {$table}_{$j_table}.f_id) "; } } if (is_array($double_join_tables)) { foreach ($double_join_tables as $d_table => $d_field) { $sql .= " LEFT JOIN {$table}_{$d_table} ON ({$table}.id = {$table}_{$d_table}.f_id) "; $sql .= " LEFT JOIN {$d_table} ON ({$table}_{$d_table}.c_id = {$d_table}.id) "; } } if (is_array($foreign_tables)) { foreach ($foreign_tables as $j_table => $j_field) { $o_field = $foreign_order[$j_table]['order_by']; $o_asc = !empty($foreign_order[$j_table]['order_asc']) ? '>' : '<'; $sql .= " LEFT JOIN {$j_table} ON ({$table}.id = {$j_table}.{$j_field}) "; $sql .= " LEFT JOIN {$j_table} {$j_table}xx ON ({$j_table}.{$j_field} = {$j_table}xx.{$j_field} AND {$j_table}.{$o_field} {$o_asc} {$j_table}xx.{$o_field}) "; } } if (is_array($remote_tables)) { $c = count($remote_tables); foreach ($remote_tables as $remote_i => $join_sequence) { $i = 0; foreach ($join_sequence as $r_table => $r_field) { $r_table = str_replace('||', '', $r_table); // removed [[ && !@array_key_exists($r_table,$join_tables) ]] if (!@array_key_exists($r_table, $foreign_tables) && !@array_key_exists($r_table, $join_subtables) && !@array_key_exists($r_table, $double_join_tables)) { if ($r_table != $table) { $o_field = $foreign_order[$r_table]['order_by']; $j_field = $prev_field == 'id' ? $r_field[0] : 'id'; $j_field = $r_table == $prev_table ? $prev_field : $r_field[0]; $remote_i1 = $i > 0 ? $remote_i : ''; $join_condition = !empty($r_field['join_condition']) ? 'AND ' . str_replace($r_table, $r_table . $remote_i, $r_field['join_condition']) : ''; if (empty($o_field)) { $sql .= " LEFT JOIN {$r_table} {$r_table}{$remote_i} ON ({$prev_table}{$remote_i1}.{$prev_field} = {$r_table}{$remote_i}.{$j_field} {$join_condition}) "; } else { $o_asc = !empty($foreign_order[$j_table]['order_asc']) ? '>' : '<'; $sql .= " LEFT JOIN {$r_table} {$r_table}{$remote_i} ON ({$prev_table}{$remote_i1}.{$prev_field} = {$r_table}{$remote_i}.{$j_field}) "; $sql .= " LEFT JOIN {$r_table} {$r_table}{$remote_i}xx ON ({$prev_table}{$remote_i1}.{$prev_field} = {$r_table}{$remote_i}xx.{$j_field} AND {$r_table}{$remote_i}.{$o_field} {$o_asc} {$r_table}{$remote_i}xx.{$o_field}) "; } $joined_tables[$r_table] = $r_table . $remote_i; if ($formulas) { foreach ($formulas as $formula) { if (strstr($formula, $r_table)) { $formula1 = str_replace($r_table, $r_table . $remote_i, $formula); $sql = str_replace($formula, $formula1, $sql); } } } $i++; } } $prev_table = $r_table; $prev_field = count($r_field) > 1 ? $r_field[1] : $r_field[0]; } } } $sql .= " WHERE 1 "; if (is_array($foreign_order)) { foreach ($foreign_order as $f_table => $properties) { $remote_i = $properties['remote_i'] > 0 ? $properties['remote_i'] : ''; $sql .= " AND {$f_table}{$remote_i}xx.{$properties['order_by']} IS NULL"; } } if (is_array($filter_results)) { if (!empty($filter_results['first_letter']['results'])) { $r_subtable = !empty($filter_results['first_letter_subtable']) ? $filter_results['first_letter_subtable'] : $table; $sql .= " AND {$r_subtable}.{$filter_results['first_letter_field']} LIKE '{$filter_results['first_letter']}%' "; } foreach ($filter_results as $r_name => $r_properties) { $r_value = $r_properties['results']; if (!is_array($r_value) && strlen($r_value) == 0) { continue; } $w_table = @array_key_exists($r_name, $f_where) ? $f_where[$r_name] : $table; $r_name_orig = $r_name; $r_name = self::replaceTables($r_name, $joined_tables); if ($r_name == 'cat_selects') { $having = ''; foreach ($r_value as $c_table => $cats) { if (is_array($cats)) { $having .= " AND SUM(IF("; foreach ($cats as $cat) { $having .= " {$c_table}.id = {$cat} OR"; } $having = substr($having, 0, -2); $having .= ',1,0)) > 0 '; } } } elseif ($r_name == 'first_letter' || $r_name == 'first_letter_field' || $r_name == 'first_letter_subtable' || $r_name == 'per_page') { continue; } elseif (stristr($r_name, 'search')) { $r_name_parts = explode('|', $r_name); $r_i = $r_name_parts[1]; if (is_array($_REQUEST['search_fields' . $CFG->control_pass_id])) { $sql .= " AND ( "; foreach ($_REQUEST['search_fields' . $CFG->control_pass_id] as $s_field => $s_subtable) { $s_field_parts = explode('|', $s_field); if ($s_field_parts[1] != $r_i) { continue; } $s_field = $s_field_parts[0]; $s_subtable = !empty($s_subtable) ? $s_subtable : $table; if (!is_numeric($s_field)) { $sql .= " {$s_subtable}.{$s_field} LIKE '%{$r_value}%' OR"; } } $sql = substr($sql, 0, -2); $sql .= " ) "; } else { $sql .= " AND {$w_table}.{$r_name} = '{$r_value}' "; } } elseif (@array_key_exists($r_name, $_REQUEST['datefields'])) { $is_b = stristr($r_name, 'bbb'); $r_name = str_replace('bbb', '', $r_name); if ($is_b) { $sql .= " AND {$w_table}.{$r_name} <= '" . date('Y-m-d', strtotime($r_value)) . "' "; } else { $sql .= " AND {$w_table}.{$r_name} >= '" . date('Y-m-d', strtotime($r_value)) . "' "; } } elseif (@in_array($r_name, $_REQUEST['month_fields'])) { $r_name = str_replace('_month', '', $r_name); $sql .= " AND MONTH({$w_table}.{$r_name}) = '" . $r_value . "' "; } elseif (@in_array($r_name, $_REQUEST['year_fields'])) { $r_name = str_replace('_year', '', $r_name); $sql .= " AND YEAR({$w_table}.{$r_name}) = '" . $r_value . "' "; } elseif (@array_key_exists($r_name, $_REQUEST['tokenizers'])) { $value1 = is_array(unserialize($r_value)) ? unserialize($r_value) : $r_value; if (is_array($value1)) { $tokenizer_values = $value1; } elseif (strlen($value1) > 0) { $tokenizer_values = String::unFaux($value1); } if (is_array($tokenizer_values)) { $sql .= " AND ("; foreach ($tokenizer_values as $r_name1 => $r_value1) { $sql .= " {$w_table}.{$r_name} = {$r_value1} OR"; } $sql = substr($sql, 0, -2); $sql .= ") "; } } else { $r_name_parts = explode(',', $r_name); $r_name_orig_parts = explode(',', $r_name_orig); $r_name_sql = false; $equals = $r_properties['not_equals'] ? '!=' : '='; $sql .= ' AND ('; foreach ($r_name_parts as $k1 => $r_name_part) { if (strstr($r_name_part, '.')) { $r_orig_name = explode('.', $r_name_orig_parts[$k1]); if (!empty($join_tables[$r_orig_name[0]])) { $r_name_sql[] = " {$r_name_orig_parts[$k1]} {$equals} '{$r_value}' "; } $r_name_sql[] = " {$r_name_part} {$equals} '{$r_value}' "; } else { $r_name_sql[] = " {$table}.{$r_name_part} {$equals} '{$r_value}' "; } } $sql .= implode('OR', $r_name_sql); $sql .= ' ) '; } } } if (($f_id > 0 || $f_id === 0 || $f_id === '0') && $f_id_field) { if (strstr($f_id_field, ',')) { $parts = explode(',', $f_id_field); $c = count($parts) - 1; $r_parts = explode('.', $parts[$c]); $j_table = $joined_tables[$r_parts[0]] ? $joined_tables[$r_parts[0]] : $r_parts[0]; $r_table = $j_table . '.' . $r_parts[1]; $sql .= " AND {$r_table} = {$f_id} "; } elseif (strstr($f_id_field, '.')) { $r_parts = explode('.', $f_id_field); $j_table = $joined_tables[$r_parts[0]] ? $joined_tables[$r_parts[0]] : $r_parts[0]; $sql .= " AND {$j_table}.{$r_parts[1]} = {$f_id} "; } else { $sql .= " AND {$table}.{$f_id_field} = {$f_id} "; } } if (!$calendar_mode) { if ($s_date && $s_date_field) { $sql .= " AND {$table}.{$s_date_field} >= '" . date('Y-m-d', $s_date) . "' "; } if ($e_date && $e_date_field) { $sql .= " AND {$table}.{$e_date_field} <= '" . date('Y-m-d', $e_date) . "' "; } } else { if ($s_date && $s_date_field && ($e_date && $e_date_field)) { $sql .= " AND (DATE({$table}.{$s_date_field}) <= '" . date('Y-m-d', $e_date) . "' AND (DATE({$table}.{$e_date_field}) >= '" . date('Y-m-d', $s_date) . "' OR DATE({$table}.{$e_date_field}) < '1980-01-01')) "; } else { $sql .= " AND (DATE({$table}.{$s_date_field}) >= '" . date('Y-m-d', $s_date) . "' AND DATE({$table}.{$s_date_field}) <= '" . date('Y-m-d', $e_date) . "') "; } } if ($filters) { foreach ($filters as $filter) { if ($filter) { $filter = String::doFormulaReplacements($filter); $filter = self::replaceTables($filter, $joined_tables); $sql .= " AND {$filter} "; } } } if (!$count && !$get_average && !$get_total || $having) { if (!$no_group_by) { $group_field = $group_by ? $group_by : "{$table}.id"; $sql .= " GROUP BY {$group_field} "; } } if ($having) { $sql .= " HAVING 1 " . $having; } if (!$count && !$get_average && !$get_total) { if ($order_by) { $order_by = !strstr('.', $order_by) && !$fields[$order_by]['is_op'] ? $table . '.' . $order_by : '`' . $order_by . '`'; $sql .= " ORDER BY {$order_by} {$order_asc} "; } else { $sql .= " ORDER BY {$table}.id {$order_string} "; } if ($start_row > 0 || $per_page > 0) { $sql .= " LIMIT {$start_row},{$per_page} "; } } //echo $sql.'<br><br>'; $result = db_query_array($sql); if ($count && $having) { $result[0]['total'] = count($result); } if ($count) { return $result[0]['total']; } elseif ($get_total || $get_average) { return $result[0]['grand_total']; } else { return $result; } }
function createRecord($table, $insert_array, $trigger_field = false, $trigger_value = false, $day = false, $month = false, $year = false, $send_condition = false, $any_modification = false, $register_changes = false, $on_new_record_only = false, $store_row = false, $if_not_exists = false, $run_in_cron = false) { global $CFG; if ($CFG->backstage_mode) { $HTML = ''; if ($CFG->pm_editor) { if (!$this->edit_record) { $this->HTML[] = "[create_record]" . self::peLabel($CFG->method_id, 'createRecord'); } else { $this->HTML[] = "[edit_record]" . self::peLabel($CFG->method_id, 'editRecord'); } } /* $HTML .= ' <input type="hidden" name="trigger_field1[]" value="'.$trigger_field.'" /> <input type="hidden" name="trigger_value1[]" value="'.$trigger_value.'" /> <input type="hidden" name="create_record_table[]" value="'.$table.'" /> '; $this->HTML[] = $HTML; */ } if ($run_in_cron && !$CFG->in_cron) { return false; } elseif ($run_in_cron && $CFG->in_cron) { $modified = true; } //used to have this ($CFG->ignore_request == $table) return false. Don't remember why. if ((is_array($this->errors) || !($this->save_called || $CFG->save_called) || !$_REQUEST[$this->name] || $_REQUEST['bypass_create_record']) && !$CFG->in_cron) { return false; } if (!$on_new_record_only && $this->record_created && !$trigger_field || $on_new_record_only && !$this->record_created) { return false; } if ($send_condition) { $send_condition = String::doFormulaReplacements($send_condition, $this->info, 1); if (!eval("if ({$send_condition}) { return 1;} else { return 0;}")) { return false; } } if ($register_changes) { $changes = '<div class="show_details"><a onclick="showDetails(this);return false;" href="#">' . $CFG->comments_show_details . '</a><a onclick="hideDetails(this);return false;" style="display:none;" href="#">' . $CFG->comments_hide_details . '</a></div><div class="details" style="display:none;">'; } if ($this->info && $register_changes) { foreach ($this->info as $name => $value) { $grid_input_modified = false; if (@in_array($name, $_REQUEST['grid_inputs'])) { if (is_array($this->info[$name])) { if (is_array($this->old_info[$name])) { foreach ($this->old_info[$name] as $id => $row) { foreach ($row as $k => $v) { $key = $row['id']; if (!empty($v) && $v != 'N' && $k != 'id' && $k != 'f_id') { $compare[$key][$k] = $v; } } } if ($compare) { ksort($compare); $compare = array_values($compare); } } $i = 0; foreach ($this->info[$name] as $id => $row) { foreach ($row as $k => $v) { if (!empty($v)) { $filtered[$i] = $row; } } $i++; } if ($filtered && $compare) { $i = 0; foreach ($filtered as $array) { if (is_array($array)) { foreach ($array as $k => $v) { if ($v != $compare[$i][$k] && (!empty($v) && !empty($compare[$i][$k]))) { $grid_input_modified = true; } } } $i++; } if (!$grid_input_modified) { if (count($filtered) != count($compare)) { $grid_input_modified = true; } } } elseif ($compare && !$filtered || $filtered && !$compare) { $grid_input_modified = true; } } } if ($name == 'cat_selects') { if (is_array($this->info[$name])) { @asort($this->info[$name]); @asort($this->old_info[$name]); $this->info[$name] = @array_values($this->info[$name]); $this->old_info[$name] = @array_values($this->old_info[$name]); foreach ($this->info[$name] as $key => $arr) { @asort($arr); $this->info[$name][$key] = @array_values($arr); } if (is_array($this->old_info[$name])) { foreach ($this->old_info[$name] as $key => $arr) { @asort($arr); $this->old_info[$name][$key] = @array_values($arr); } } if ($this->info[$name] != $this->old_info[$name]) { $modified = true; $changes .= '<b>' . $name . '</b> ' . $CFG->comments_set_to . ' ' . (is_array($value) ? print_r($value, true) : $value) . '<br/>'; } } } elseif (strip_tags($this->info[$name]) != strip_tags($this->old_info[$name]) && !@in_array($name, $_REQUEST['grid_inputs']) || $grid_input_modified) { $modified = true; $changes .= '<b>' . $name . '</b> ' . $CFG->comments_set_to . ' ' . (is_array($value) ? print_r($value, true) : $value) . '<br/>'; } $bypass = false; $compare = false; $filtered = false; } } if ($register_changes) { $changes .= '</div>'; } if ($on_new_record_only && $this->record_created) { $modified = true; } if ($this->info[$trigger_field] != $this->old_info[$trigger_field] || $modified) { if ($this->info[$trigger_field] == $trigger_value || !$trigger_value && $trigger_field || $modified) { if (!is_array($insert_array) && stristr($insert_array, 'array:')) { $insert_array = str_ireplace('array:', '', $insert_array); $ia1 = explode(',', $insert_array); if (is_array($ia1)) { foreach ($ia1 as $v) { if (strstr($v, '=>')) { $ia2 = explode('=>', $v); $ia3[$ia2[0]] = $ia2[1]; $last_key = $ia2[0]; } else { $ia3[$last_key] .= ',' . $v; } } } unset($insert_array); $insert_array = $ia3; } if (is_array($insert_array)) { foreach ($insert_array as $new_field => $old_field) { if ($old_field == 'curdate') { $insert_values[$new_field] = date('Y-m-d 00:00:00'); } elseif ($old_field == 'curtime') { $insert_values[$new_field] = date('Y-m-d H:i:s', time() + Settings::mysqlTimeDiff() * 3600); } elseif ($old_field == 'user_id') { $insert_values[$new_field] = User::$info['id']; } elseif ($old_field == 'record_id') { $insert_values[$new_field] = $this->record_id; } elseif (strstr($old_field, '(') && strstr($old_field, ')')) { if ($this->record_created) { $this->old_info['id'] = $this->record_id; } $formula = String::doFormulaReplacements($old_field, $this->old_info, 1); $insert_values[$new_field] = eval("return ({$formula});"); } elseif ($old_field == 'current_url') { $insert_values[$new_field] = $CFG->url; } elseif (!is_array($old_field)) { if (array_key_exists($old_field, $this->info)) { $insert_values[$new_field] = $this->info[$old_field]; } else { $insert_values[$new_field] = $old_field; } } else { $insert_values[$new_field] = DB::getForeignValue(implode(',', $old_field), $this->info['id']); } } if ($register_changes) { $insert_values['comments'] = $changes; } if ($store_row && $table == 'comments') { if ($this->edit_record && $table != $this->table) { $row = DB::getRecord($table, $this->edit_record_field_id, 0, 1); $insert_values['f_table_row'] = serialize($row); } else { $insert_values['f_table_row'] = serialize($this->info); } } $CFG->ignore_request = $table; $CFG->bypass_unserialize = true; $this->edit_record_id_field = $this->edit_record_id_field ? $this->edit_record_id_field : $this->record_id; if (!$this->edit_record) { if ($if_not_exists) { $insert_values1 = $insert_values; if ($k = array_search($this->record_id, $insert_values1)) { unset($insert_values1[$k]); } if (DB::recordExists($table, $insert_values1)) { return false; } } $this->create_record[] = array('table' => $table, 'insert_values' => $insert_values); //echo 'Insert:'; //print_ar($insert_values); } else { $this->create_record[] = array('edit' => 1, 'table' => $table, 'insert_values' => $insert_values, 'id' => $this->edit_record_field_id); //echo 'Update:'; //print_ar($insert_values); } } } } }
function startRestricted($groups = false, $only_admin = false, $users = false, $user_id_equals_field = false, $group_id_equals_field = false, $condition = false, $exclude_groups = false, $exclude_admin = false, $exclude_users = false) { global $CFG; if ($CFG->pm_editor) { $method_name = Form::peLabel($CFG->method_id, 'startRestricted'); } if ($CFG->pm_editor) { $this->HTML[] = "\n\t\t\t{$legend} [start_restricted] {$method_name}"; } else { if ($only_admin) { if ($condition) { $condition = String::doFormulaReplacements($condition, $this->row); $restricted = $restricted ? $restricted : @eval("if ({$condition}) { return 0;} else { return 1;}"); } } else { if (is_array($users)) { $restricted = !array_key_exists(User::$info['id'], $users); } if (is_array($groups)) { $restricted = !$restricted ? !array_key_exists(User::$info['f_id'], $groups) : $restricted; } if (is_array($exclude_users)) { $restricted = !$restricted ? array_key_exists(User::$info['id'], $exclude_users) : $restricted; } if (is_array($exclude_groups)) { $restricted = !$restricted ? array_key_exists(User::$info['f_id'], $exclude_groups) : $restricted; } if ($exclude_admin) { $restricted = !$restricted ? User::$info['is_admin'] == 'Y' : $restricted; } if ($user_id_equals_field) { if (strstr($user_id_equals_field, ',')) { $parts = explode(',', $user_id_equals_field); $parts1 = explode('.', $parts[0]); $first_table = $parts1[0]; $first_field = $parts1[1]; $c = count($parts) - 1; $parts2 = explode('.', $parts[$c]); $last_table = $parts2[0]; $last_field = $parts2[1]; $row = DB::getFields($last_table, $this->row[$first_field], array($last_field), $user_id_equals_field, false, false, false, false, 1); $restricted = !$restricted ? $restricted : User::$info['id'] != $row[$last_field]; } else { $restricted = !$restricted ? $restricted : User::$info['id'] != $this->row[$user_id_equals_field]; } } if ($group_id_equals_field) { if (strstr($group_id_equals_field, ',')) { $parts = explode(',', $group_id_equals_field); $parts1 = explode('.', $parts[0]); $first_table = $parts1[0]; $first_field = $parts1[1]; $c = count($parts) - 1; $parts2 = explode('.', $parts[$c]); $last_table = $parts2[0]; $last_field = $parts2[1]; $row = DB::getFields($last_table, $this->row[$first_field], array($last_field), $group_id_equals_field, false, false, false, false, 1); $restricted = !$restricted ? $restricted : User::$info['f_id'] != $row[$last_field]; } else { $restricted = !$restricted ? $restricted : User::$info['f_id'] != $this->row[$group_id_equals_field]; } } if ($condition) { $condition = String::doFormulaReplacements($condition, $this->row); $restricted = $restricted ? $restricted : eval("if ({$condition}) { return 0;} else { return 1;}"); } } $restricted = User::$info['is_admin'] == 'Y' && !$exclude_admin ? false : $restricted; if ($restricted) { $this->HTML[] = "<restricted>"; } } }
private function show($comments) { global $CFG; if ($comments) { echo '<ul>'; foreach ($comments as $comment) { $elapsed = time() + Settings::mysqlTimeDiff() * 3600 - strtotime($comment['date']); if ($elapsed < 60) { $time_ago = $CFG->comments_less_than_minute; } elseif ($elapsed > 60 && $elapsed < 60 * 60) { $minutes = floor($elapsed / 60); $time_ago = str_ireplace('[field]', $minutes, $CFG->comments_minutes_ago); } elseif ($elapsed > 60 * 60 && $elapsed < 60 * 60 * 24) { $hours = floor($elapsed / 60 / 60); $time_ago = str_ireplace('[field]', $hours, $CFG->comments_hours_ago); } elseif ($elapsed > 60 * 60 * 24 && $elapsed < 60 * 60 * 24 * 30.4) { $days = floor($elapsed / 60 / 60 / 24); $time_ago = str_ireplace('[field]', $days, $CFG->comments_days_ago); } else { $months = floor($elapsed / 60 / 60 / 24 / 30.4); $time_ago = str_ireplace('[field]', $months, $CFG->comments_months_ago); } if ($comment['user_id'] > 0) { $user = DB::getRecord($this->user_table, $comment['user_id'], false, true); $name = !empty($comment['website']) ? Link::url($comment['website'], $user['user']) : $user['user']; } else { $name = !empty($comment['website']) ? Link::url($comment['website'], $comment['name']) : $comment['name']; } $short = $this->short_version ? '_short' : ''; $icon = $comment['type'] ? eval('return $CFG->comment_type_' . $comment['type'] . ';') : $CFG->comment_type_1; $action = $comment['type'] ? eval('return $CFG->comments_action_' . $comment['type'] . $short . ';') : $CFG->comments_wrote_label; $action = String::doFormulaReplacements($action, unserialize($comment['f_table_row']), 1, 1); echo ' <li id="comment_' . $comment['id'] . '" class="level_' . $comment['type'] . '"> <div class="c_head">'; if ($this->fields) { foreach ($this->fields as $f_name => $field) { $CFG->o_method_id = $field['method_id']; $CFG->o_method_name = 'field'; $record = new Record($field['table'], $comment['record_id']); echo '<div class="added_field">' . $record->field($field['name'], $field['caption'], $field['subtable'], $field['subtable_fields'], $field['link_url'], $field['concat_char'], true, $field['f_id_field'], $field['order_by'], $field['order_asc'], $comment['record_id'], $field['link_is_tab'], $field['limit_is_curdate'], false, $field['link_id_field']) . '</div>'; } } echo ' ' . $icon . ' ' . $name . ' (' . $time_ago . ') ' . $action . ' </div>'; if (!$this->short_version) { echo ' <div class="c_comment"> ' . (strlen($comment['comments']) != strlen(strip_tags($comment['comments'])) ? $comment['comments'] : nl2br($comment['comments'])) . ' </div>'; } echo ' ' . ($comment['type'] <= 1 && !$this->short_version ? '<div class="c_reply"><a href="#" onclick="showReplyBox(' . $comment['id'] . ',' . $this->i . ');return false;">' . $CFG->comments_reply_label . '</a></div>' : '') . ' <div class="c_form"></div> </li>'; if (is_array($comment['children'])) { Comments::show($comment['children']); } } echo '<div style="clear:both;height:0;"></div></ul>'; } }
public static function parseVariables($variables, $row, $record_id = 0, $url = false, $update_variable_values = false) { global $CFG; $reserved_keywords = array('current_url', 'action', 'bypass', 'is_tab'); if (is_array($variables)) { foreach ($variables as $k => $v) { $is_formula = strstr($v, '(') && strstr($v, ')'); $k1 = $url ? "{$url}[{$k}]" : $k; $v1 = $is_formula ? $v : str_replace('[', '', str_replace(']', '', $v)); if (strstr($v1, '(') && strstr($v1, ')')) { $formula = String::doFormulaReplacements($v1, $row, 1); $v1 = eval("return ({$formula});"); } if (in_array($k, $reserved_keywords)) { $variables1[$k] = $v; } elseif ($k == 'record_id') { $variables1[$k1] = $is_formula ? $v1 : $record_id; } elseif ($k == 'id') { $variables1[$k] = $is_formula ? $v1 : $record_id; if ($update_variable_values) { $variables1['record_id'] = $is_formula ? $v1 : $record_id; } } elseif ($v1 == 'id') { $variables1[$k1] = $is_formula ? $v1 : $record_id; } elseif ($v1 == 'curdate') { $variables1[$k1] = date('Y-m-d 00:00:00'); } elseif ($v1 == 'curtime') { $variables1[$k1] = date('Y-m-d H:i:s', time() + Settings::mysqlTimeDiff() * 3600); } elseif ($k == 'user_id') { $variables1[$k1] = User::$info['id']; } elseif (strstr($v1, 'count')) { $p = explode('(', $v1); $v1 = str_replace(')', '', $p[1]); $variables1[$k1] = DB::countRows($v1); } elseif (strstr($v1, '.')) { $parts = explode('.', $v1); $sql = "SELECT {$parts[1]} FROM {$parts[0]} WHERE f_id = {$record_id}"; $result = db_query_array($sql); if ($result) { $m1 = $parts[1]; $variables1[$k1] = $result[0][$m1]; } } elseif (strstr($v1, ',')) { $variables1[$k1] = DB::getForeignValue(implode(',', $v1), $row); } else { self::replaceSystemVars($v1); if (strstr($v1, '++')) { $v1 = $row[str_replace('++', '', $v1)] + 1; } elseif (strstr($v1, '--')) { $v1 = $row[str_replace('--', '', $v1)] - 1; } else { $v1 = array_key_exists($v1, $row) ? $row[$v1] : $v; } $variables1[$k1] = $v1; } } } if ($record_id > 0 && $update_variable_values) { $variables1['record_id'] = $record_id; } return $variables1; }
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> </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>'; } }
function display() { global $CFG; if (!is_array($this->tables)) { return false; } self::getData(); self::getFolders(); if ($this->mode == 'icons') { if (is_array($this->folders['path'])) { $this->folders['path'] = array_reverse($this->folders['path']); } $this->folders['path'][] = $this->folders['this']; foreach ($this->folders['path'] as $folder) { $path .= '/' . $folder['folder_name']; } } else { $path = '/' . $CFG->filter_submit_text; } if (!$this->bypass) { self::show_filters(); $HTML .= ' <div class="' . $this->class . '"> <table> <tr> <td class="bar" colspan="2"> <a class="m_back" title="' . $CFG->back . '" onclick="file_manager.last()"></a> <a class="m_forward" title="' . $CFG->forward . '" onclick="file_manager.next()"></a> <a class="m_up" title="' . $CFG->up_directory . '" onclick="file_manager.openFolder(' . $this->folders['up']['id'] . ')"></a> <a class="m_add_folder" title="' . $CFG->add_directory . '" onclick="file_manager.addFolder()"></a>'; if ($this->tables) { foreach ($this->tables as $table => $properties) { $table1 = substr($table, -1) == 's' ? ucfirst(substr($table, 0, -1)) : ucfirst($table); $HTML .= '<a class="m_add_file" title="' . $CFG->add_new_caption . ' ' . $table1 . '" onclick="file_manager.addFile(\'' . $table . '\')"></a>'; } } $HTML .= ' <div id="fm_path" class="fm_path">' . $path . '</div>'; if (User::$info['is_admin'] == 'Y' || !$this->only_admin && key($this->download_group) == User::$info['f_id']) { $HTML .= ' <div class="fm_download"><a href="#" onclick="file_manager.downloadResults();return false;">' . ($this->mode == 'icons' ? $CFG->download_all : $CFG->download_results) . '</a><iframe class="fm_download_iframe" src="" name="download"></iframe></div>'; } $HTML .= ' <div class="clear"></div> </td> </tr> <tr> <td class="tree">'; if (is_array($this->folders['path'])) { $last_p_id = $this->folders['path'][0]['id']; } $has_sub = self::hasSubfolders($id) ? 'this' : 'false'; $no_triangle = $has_sub == 'false' ? 'not' : ''; $down = $last_p_id > 0 && $id == $last_p_id ? 1 : false; $HTML .= ' <div class="folder_container"> <input type="hidden" id="id" value="0" /> <div id="triangle" class="triangle' . $down . ' ' . $no_triangle . '" onclick="file_manager.showSubfolders(0,' . $has_sub . ')"></div> <div class="folder home" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(0,' . $has_sub . ')"></div> <div class="desc" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(0,' . $has_sub . ')">/</div> <div class="clear"></div> </div>'; if (is_array($this->folders['top_level'])) { foreach ($this->folders['top_level'] as $id => $row) { $has_sub = self::hasSubfolders($id) ? 'this' : 'false'; $no_triangle = $has_sub == 'false' ? 'not' : ''; $down = $last_p_id > 0 && $id == $last_p_id ? 1 : false; $HTML .= ' <div class="folder_container"> <input type="hidden" id="id" value="' . $row['id'] . '" /> <div id="triangle" class="triangle' . $down . ' ' . $no_triangle . '" onclick="file_manager.showSubfolders(' . $row['id'] . ',' . $has_sub . ')"></div> <div class="folder" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $row['id'] . ',' . $has_sub . ')"></div> <div class="desc" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $row['id'] . ',' . $has_sub . ')">' . $row['folder_name'] . '</div> <div class="clear"></div>'; if ($last_p_id > 0 && $id == $last_p_id) { $i = 0; foreach ($this->folders['path'] as $key => $sub) { if (!($key > 0)) { continue; } $has_sub = self::hasSubfolders($id) ? 'this' : 'false'; $no_triangle = $has_sub == 'false' ? 'not' : ''; $down = $sub['id'] == $this->folders['this']['id'] ? false : 1; $HTML .= ' <div class="folder_container indent"> <input type="hidden" id="id" value="' . $sub['id'] . '" /> <div id="triangle" class="triangle' . $down . ' ' . $no_triangle . '" onclick="file_manager.showSubfolders(' . $sub['id'] . ',' . $has_sub . ')"></div> <div class="folder" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $sub['id'] . ',' . $has_sub . ')"></div> <div class="desc" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $sub['id'] . ',' . $has_sub . ')">' . $sub['folder_name'] . '</div> <div class="clear"></div>'; $i++; } for ($j = 0; $j < $i; $j++) { $HTML .= '</div>'; } } $HTML .= '</div>'; } } $HTML .= ' </td> <td class="navigator" id="navigator">'; } if ($this->data_download) { foreach ($this->data_download as $d_table => $d_ids) { $HTML .= '<input type="hidden" class="download_ids" id="download_' . $d_table . '" value="' . @implode('|', $d_ids) . '" />'; } } $HTML .= ' <input type="hidden" id="folder_table" value="' . $this->folder_table . '" /> <input type="hidden" id="current_id" value="' . $this->current_folder_id . '" /> <input type="hidden" id="current_url" value="' . $CFG->url . '" /> <input type="hidden" id="is_tab" value="' . $CFG->is_tab . '" />'; if ($this->mode == 'icons') { if ($this->tables) { foreach ($this->tables as $table => $properties) { $HTML .= '<input type="hidden" id="folder_link" value="&action=form&p_id=' . $this->current_folder_id . '" />'; $HTML .= '<input type="hidden" id="' . $table . '_link" value="current_url=' . $properties['url'] . '&action=form&is_tab=' . $properties['link_is_tab'] . '&' . $properties['url'] . '[' . $properties['folder_field'] . ']=' . $this->current_folder_id . '" />'; $HTML .= '<input type="hidden" id="' . $table . '_target" value="' . $properties['target_elem_id'] . '" />'; } } if (!($this->current_folder_id > 0) && $this->folders['top_level']) { foreach ($this->folders['top_level'] as $folder) { $HTML .= ' <div class="folder_container"> <div class="ops">'; if (User::permission(0, 0, $this->folder_table) > 0) { $HTML .= Link::url($this->folder_table, false, 'id=' . $folder['id'] . '&action=record&is_tab=' . $folder['link_is_tab'], false, false, 'edit_box', 'view', false, false, false, false, $CFG->view_hover_caption) . ' '; } if (User::permission(0, 0, $this->folder_table) > 1) { $HTML .= Link::url($this->folder_table, false, 'id=' . $folder['id'] . '&action=form&is_tab=' . $folder['link_is_tab'], false, false, 'edit_box', 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' '; } if (User::permission(0, 0, $this->folder_table) > 1) { $HTML .= '<a href="#" title="' . $CFG->delete_hover_caption . '" onclick="file_manager.deleteThis(' . $folder['id'] . ',\'' . $this->folder_table . '\',this)" class="delete"></a>'; } $HTML .= ' </div> <div class="folder" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $folder['id'] . ')"></div> <div class="desc" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $folder['id'] . ')">' . $folder['folder_name'] . '</div> <input type="hidden" id="id" value="' . $folder['id'] . '" /> </div>'; } } elseif ($this->folders['children']) { foreach ($this->folders['children'] as $folder) { $HTML .= ' <div class="folder_container"> <div class="ops">'; if (User::permission(0, 0, $this->folder_table) > 0) { $HTML .= Link::url($this->folder_table, false, 'id=' . $folder['id'] . '&action=record', false, false, 'edit_box', 'view', false, false, false, false, $CFG->view_hover_caption) . ' '; } if (User::permission(0, 0, $this->folder_table) > 1) { $HTML .= Link::url($this->folder_table, false, 'id=' . $folder['id'] . '&action=form', false, false, 'edit_box', 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' '; } if (User::permission(0, 0, $this->folder_table) > 1) { $HTML .= '<a href="#" title="' . $CFG->delete_hover_caption . '" onclick="file_manager.deleteThis(' . $folder['id'] . ',\'' . $this->folder_table . '\',this)" class="delete"></a>'; } $HTML .= ' </div> <div class="folder" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $folder['id'] . ')"></div> <div class="desc" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $folder['id'] . ')">' . $folder['folder_name'] . '</div> <input type="hidden" id="id" value="' . $folder['id'] . '" /> </div>'; } } if ($this->data) { foreach ($this->data as $file) { if ($file['alert_condition1']) { $file_row = DB::getRecord($file['table'], $file['id']); $condition = String::doFormulaReplacements($file['alert_condition1'], $file_row, 1); $alert_class1 = eval("if ({$condition}) { return 1;} else { return 0;}") ? 'alert1' : ''; } if ($file['alert_condition2']) { $file_row = is_array($file_row) ? $file_row : DB::getRecord($file['table'], $file['id']); $condition = String::doFormulaReplacements($file['alert_condition2'], $file_row, 1); $alert_class2 = eval("if ({$condition}) { return 1;} else { return 0;}") ? 'alert2' : ''; } $HTML .= ' <div class="file_container ' . $alert_class1 . ' ' . $alert_class2 . '"> <div class="ops">'; if (User::permission(0, 0, $file['url']) > 0) { $HTML .= Link::url($file['url'], false, 'id=' . $file['id'] . '&action=record&is_tab=' . $file['link_is_tab'], false, false, $file['target_elem_id'], 'view', false, false, false, false, $CFG->view_hover_caption) . ' '; } if (User::permission(0, 0, $file['url']) > 1) { $HTML .= Link::url($file['url'], false, 'id=' . $file['id'] . '&action=form&is_tab=' . $file['link_is_tab'], false, false, $file['target_elem_id'], 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' '; } if (User::permission(0, 0, $file['url']) > 1) { $HTML .= '<a href="#" title="' . $CFG->delete_hover_caption . '" onclick="file_manager.deleteThis(' . $file['id'] . ',\'' . $file['table'] . '\',this)" class="delete"></a>'; } $is_tab = $file['link_is_tab'] ? $file['link_is_tab'] : 'false'; $HTML .= ' </div> <div class="file" onclick="file_manager.select(this,event);" ondblclick="file_manager.showFile(\'' . $file['url'] . '\',' . $file['id'] . ',' . $is_tab . ',\'' . $file['target_elem_id'] . '\');"></div> <div class="desc" onclick="file_manager.select(this,event);" ondblclick="file_manager.showFile(\'' . $file['url'] . '\',' . $file['id'] . ',' . $is_tab . ',\'' . $file['target_elem_id'] . '\');">' . $file['file_name'] . '</div> <input type="hidden" id="id" value="' . $file['id'] . '" /> <input type="hidden" id="table" value="' . $file['table'] . '" /> <input type="hidden" id="folder_field" value="' . $file['folder_field'] . '" /> </div>'; } } } elseif ($this->mode == 'search') { if ($this->data) { foreach ($this->data as $row) { $k = $row['file_name']; $all_data[$k . '_fi'] = $row; } } if ($this->folders1) { foreach ($this->folders1 as $row) { $k = $row['folder_name']; $all_data[$k . '_fo'] = $row; } } if ($all_data) { ksort($all_data); $HTML .= '<div class="search">'; foreach ($all_data as $row) { if (array_key_exists('file_name', $row)) { if ($row['alert_condition1']) { $file_row = DB::getRecord($row['table'], $row['id']); $condition = String::doFormulaReplacements($row['alert_condition1'], $file_row, 1); $alert_class1 = eval("if ({$condition}) { return 1;} else { return 0;}") ? 'alert1' : ''; } if ($row['alert_condition2']) { $file_row = is_array($file_row) ? $file_row : DB::getRecord($row['table'], $row['id']); $condition = String::doFormulaReplacements($row['alert_condition2'], $file_row, 1); $alert_class2 = eval("if ({$condition}) { return 1;} else { return 0;}") ? 'alert2' : ''; } $is_tab = $row['link_is_tab'] ? $row['link_is_tab'] : 'false'; $HTML .= '<div class="search_container ' . $alert_class1 . ' ' . $alert_class2 . '" onclick="file_manager.select(this,event);" ondblclick="file_manager.showFile(\'' . $row['url'] . '\',' . $row['id'] . ',' . $is_tab . ',\'' . $row['target_elem_id'] . '\');">'; $HTML .= ' <div class="file"></div> <div class="desc">' . $row['file_name'] . '</div> <input type="hidden" id="id" value="' . $row['id'] . '" /> <input type="hidden" id="table" value="' . $row['table'] . '" /> <input type="hidden" id="folder_field" value="' . $row['folder_field'] . '" /> <div class="ops1">'; if (User::permission(0, 0, $row['url']) > 0) { $HTML .= Link::url($row['url'], false, 'id=' . $row['id'] . '&action=record&is_tab=' . $row['link_is_tab'], false, false, $row['target_elem_id'], 'view', false, false, false, false, $CFG->view_hover_caption) . ' '; } if (User::permission(0, 0, $row['url']) > 1) { $HTML .= Link::url($row['url'], false, 'id=' . $row['id'] . '&action=form&is_tab=' . $row['link_is_tab'], false, false, $row['target_elem_id'], 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' '; } if (User::permission(0, 0, $row['url']) > 1) { $HTML .= '<a href="#" title="' . $CFG->delete_hover_caption . '" onclick="file_manager.deleteThis(' . $row['id'] . ',\'' . $row['table'] . '\',this)" class="delete"></a>'; } $HTML .= "</div></div>"; } else { $HTML .= '<div class="search_container" onclick="file_manager.select(this,event);" ondblclick="file_manager.openFolder(' . $row['id'] . ')">'; $HTML .= ' <div class="folder"></div> <div class="desc">' . $row['folder_name'] . '</div> <input type="hidden" id="id" value="' . $row['id'] . '" /> <div class="ops1">'; if (User::permission(0, 0, $this->folder_table) > 0) { $HTML .= Link::url($this->folder_table, false, 'id=' . $row['id'] . '&action=record', false, false, 'edit_box', 'view', false, false, false, false, $CFG->view_hover_caption) . ' '; } if (User::permission(0, 0, $this->folder_table) > 1) { $HTML .= Link::url($this->folder_table, false, 'id=' . $row['id'] . '&action=form', false, false, 'edit_box', 'edit', false, false, false, false, $CFG->edit_hover_caption) . ' '; } if (User::permission(0, 0, $this->folder_table) > 1) { $HTML .= '<a href="#" title="' . $CFG->delete_hover_caption . '" onclick="file_manager.deleteThis(' . $row['id'] . ',\'' . $this->folder_table . '\',this)" class="button"></a>'; } $HTML .= ' </div>'; $HTML .= '</div>'; } } $HTML .= '</div>'; } } if (!$CFG->pm_editor) { $HTML .= ' <script language="text/javascript"> ' . (!$this->n_or_b ? 'file_manager.addHistory(' . $this->current_folder_id . ');' : '') . ' file_manager.setPath("' . $path . '"); $("#navigator").click(function() { file_manager.unselect(); }); $(".tree").click(function() { file_manager.unselect(); }); file_manager.startSelectable(); </script> '; } if (!$this->bypass) { $HTML .= ' <div class="clear"></div> </td> </tr> </table> </div>'; } echo $HTML; }