/modules/gazpme/phpMyEdit.class.php
PHP | 3396 lines | 2863 code | 135 blank | 398 comment | 894 complexity | 5d7b1b1a3d78f1a7efa1e32b1f1b23e9 MD5 | raw file
Possible License(s): LGPL-2.1, LGPL-3.0, GPL-2.0
Large files files are truncated, but you can click here to view the full file
- <?php
-
- /*
- * phpMyEdit - instant MySQL table editor and code generator
- *
- * phpMyEdit.class.php - main table editor class definition file
- * ____________________________________________________________
- *
- * Copyright (c) 1999-2002 John McCreesh <jpmcc@users.sourceforge.net>
- * Copyright (c) 2001-2002 Jim Kraai <jkraai@users.sourceforge.net>
- * Versions 5.0 and higher developed by Ondrej Jombik <nepto@php.net>
- * Copyright (c) 2002-2006 Platon Group, http://platon.sk/
- * All rights reserved.
- *
- * See README file for more information about this software.
- * See COPYING file for license information.
- *
- * Download the latest version from
- * http://platon.sk/projects/phpMyEdit/
- */
-
- /* $Platon: phpMyEdit/phpMyEdit.class.php,v 1.204 2007-09-16 12:57:07 nepto Exp $ */
-
- /* This is a generic table editing program. The table and fields to be
- edited are defined in the calling program.
-
- This program works in three passes.
- * Pass 1 (the last part of the program) displays the selected SQL
- table in a scrolling table on the screen. Radio buttons are used to
- select a record for editing or deletion. If the user chooses Add,
- Change, Copy, View or Delete buttons.
- * Pass 2 starts, displaying the selected record. If the user chooses
- the Save button from this screen.
- * Pass 3 processes the update and the display returns to the
- original table view (Pass 1).
- */
-
- class phpMyEdit_timer /* {{{ */
- {
- var $startTime;
- var $started;
-
- function phpMyEdit_timer($start = true)
- {
- $this->started = false;
- if ($start) {
- $this->start();
- }
- }
-
- function start()
- {
- $startMtime = explode(' ', microtime());
- $this->startTime = (double) $startMtime[0] + (double) $startMtime[1];
- $this->started = true;
- }
-
- function end($iterations = 1)
- {
- // get the time, check whether the timer was started later
- $endMtime = explode(' ', microtime());
- if ($this->started) {
- $endTime = (double)($endMtime[0])+(double)($endMtime[1]);
- $dur = $endTime - $this->startTime;
- $avg = 1000 * $dur / $iterations;
- $avg = round(1000 * $avg) / 1000;
- return $avg;
- } else {
- return 'phpMyEdit_timer ERROR: timer not started';
- }
- }
- } /* }}} */
-
- if (! function_exists('array_search')) { /* {{{ */
- function array_search($needle, $haystack)
- {
- foreach ($haystack as $key => $value) {
- if ($needle == $value)
- return $key;
- }
- return false;
- }
- } /* }}} */
-
- if (! function_exists('realpath')) { /* {{{ */
- function realpath($path)
- {
- return $path;
- }
- } /* }}} */
-
- class phpMyEdit
- {
- // Class variables {{{
-
- // Database handling
- var $hn; // hostname
- var $un; // user name
- var $pw; // password
- var $tb; // table
- var $db; // database
- var $dbp; // database with point and delimiters
- var $dbh; // database handle
- var $close_dbh; // if database handle should be closed
-
- // Record manipulation
- var $key; // name of field which is the unique key
- var $key_num; // number of field which is the unique key
- var $key_type; // type of key field (int/real/string/date etc.)
- var $key_delim; // character used for key value quoting
- var $rec; // number of record selected for editing
- var $inc; // number of records to display
- var $fm; // first record to display
- var $fl; // is the filter row displayed (boolean)
- var $fds; // sql field names
- var $fdn; // sql field names => $k
- var $num_fds; // number of fields
- var $options; // options for users: ACDFVPI
- var $fdd; // field definitions
- var $qfn; // value of all filters used during the last pass
- var $sfn; // sort field number (- = descending sort order)
- var $cur_tab; // current selected tab
-
- // Operation
- var $navop; // navigation buttons/operations
- var $sw; // filter display/hide/clear button
- var $operation; // operation to do: Add, Change, Delete
- var $saveadd;
- var $moreadd;
- var $canceladd;
- var $savechange;
- var $morechange;
- var $cancelchange;
- var $savecopy;
- var $cancelcopy;
- var $savedelete;
- var $canceldelete;
- var $cancelview;
-
- // Additional features
- var $labels; // multilingual labels
- var $cgi; // CGI variable features array
- var $js; // JS configuration array
- var $dhtml; // DHTML configuration array
- var $url; // URL array
- var $message; // informational message to print
- var $notify; // change notification e-mail adresses
- var $logtable; // name of optional logtable
- var $navigation; // navigation style
- var $tabs; // TAB names
- var $timer = null; // phpMyEdit_timer object
- var $sd; var $ed; // sql start and end delimiters '`' in case of MySQL
-
- // Predefined variables
- var $comp_ops = array('<'=>'<','<='=>'<=','='=>'=','>='=>'>=','>'=>'>');
- var $sql_aggrs = array(
- 'sum' => 'Total',
- 'avg' => 'Average',
- 'min' => 'Minimum',
- 'max' => 'Maximum',
- 'count' => 'Count');
- var $page_types = array(
- 'L' => 'list',
- 'F' => 'filter',
- 'A' => 'add',
- 'V' => 'view',
- 'C' => 'change',
- 'P' => 'copy',
- 'D' => 'delete'
- );
- var $default_buttons = array(
- 'L' => array('<<','<','add','view','change','copy','delete','>','>>','goto','goto_combo'),
- 'F' => array('<<','<','add','view','change','copy','delete','>','>>','goto','goto_combo'),
- 'A' => array('save','more','cancel'),
- 'C' => array('save','more','cancel'),
- 'P' => array('save', 'cancel'),
- 'D' => array('save','cancel'),
- 'V' => array('change','cancel')
- );
- // }}}
-
- /*
- * column specific functions
- */
-
- function col_has_sql($k) { return isset($this->fdd[$k]['sql']); }
- function col_has_sqlw($k) { return isset($this->fdd[$k]['sqlw']) && !$this->virtual($k); }
- function col_has_values($k) { return isset($this->fdd[$k]['values']) || isset($this->fdd[$k]['values2']); }
- function col_has_php($k) { return isset($this->fdd[$k]['php']); }
- function col_has_URL($k) { return isset($this->fdd[$k]['URL'])
- || isset($this->fdd[$k]['URLprefix']) || isset($this->fdd[$k]['URLpostfix']); }
- function col_has_multiple($k)
- { return $this->col_has_multiple_select($k) || $this->col_has_checkboxes($k); }
- function col_has_multiple_select($k)
- { return $this->fdd[$k]['select'] == 'M' && ! $this->fdd[$k]['values']['table']; }
- function col_has_checkboxes($k)
- { return $this->fdd[$k]['select'] == 'C' && ! $this->fdd[$k]['values']['table']; }
- function col_has_radio_buttons($k)
- { return $this->fdd[$k]['select'] == 'O' && ! $this->fdd[$k]['values']['table']; }
- function col_has_datemask($k)
- { return isset($this->fdd[$k]['datemask']) || isset($this->fdd[$k]['strftimemask']); }
-
- /*
- * functions for indicating whether navigation style is enabled
- */
-
- function nav_buttons() { return stristr($this->navigation, 'B'); }
- function nav_text_links() { return stristr($this->navigation, 'T'); }
- function nav_graphic_links() { return stristr($this->navigation, 'G'); }
- function nav_up() { return (stristr($this->navigation, 'U') && !($this->buttons[$this->page_type]['up'] === false)); }
- function nav_down() { return (stristr($this->navigation, 'D') && !($this->buttons[$this->page_type]['down'] === false)); }
-
- /*
- * functions for indicating whether operations are enabled
- */
-
- function add_enabled() { return stristr($this->options, 'A'); }
- function change_enabled() { return stristr($this->options, 'C'); }
- function delete_enabled() { return stristr($this->options, 'D'); }
- function filter_enabled() { return stristr($this->options, 'F'); }
- function view_enabled() { return stristr($this->options, 'V'); }
- function copy_enabled() { return stristr($this->options, 'P') && $this->add_enabled(); }
- function tabs_enabled() { return $this->display['tabs'] && count($this->tabs) > 0; }
- function hidden($k) { return stristr($this->fdd[$k]['input'],'H'); }
- function password($k) { return stristr($this->fdd[$k]['input'],'W'); }
- function readonly($k) { return stristr($this->fdd[$k]['input'],'R') || $this->virtual($k); }
- function virtual($k) { return stristr($this->fdd[$k]['input'],'V') && $this->col_has_sql($k); }
-
- function add_operation() { return $this->operation == $this->labels['Add'] && $this->add_enabled(); }
- function change_operation() { return $this->operation == $this->labels['Change'] && $this->change_enabled(); }
- function copy_operation() { return $this->operation == $this->labels['Copy'] && $this->copy_enabled(); }
- function delete_operation() { return $this->operation == $this->labels['Delete'] && $this->delete_enabled(); }
- function view_operation() { return $this->operation == $this->labels['View'] && $this->view_enabled(); }
- function filter_operation() { return $this->fl && $this->filter_enabled() && $this->list_operation(); }
- function list_operation() { /* covers also filtering page */ return ! $this->change_operation()
- && ! $this->add_operation() && ! $this->copy_operation()
- && ! $this->delete_operation() && ! $this->view_operation(); }
- function next_operation() { return ($this->navop == $this->labels['Next']) || ($this->navop == '>'); }
- function prev_operation() { return ($this->navop == $this->labels['Prev']) || ($this->navop == '<'); }
- function first_operation() { return ($this->navop == $this->labels['First']) || ($this->navop == '<<'); }
- function last_operation() { return ($this->navop == $this->labels['Last']) || ($this->navop == '>>'); }
- function clear_operation() { return $this->sw == $this->labels['Clear']; }
-
- function add_canceled() { return $this->canceladd == $this->labels['Cancel']; }
- function view_canceled() { return $this->cancelview == $this->labels['Cancel']; }
- function change_canceled() { return $this->cancelchange == $this->labels['Cancel']; }
- function copy_canceled() { return $this->cancelcopy == $this->labels['Cancel']; }
- function delete_canceled() { return $this->canceldelete == $this->labels['Cancel']; }
-
- function is_values2($k, $val = 'X') /* {{{ */
- {
- return $val === null ||
- (isset($this->fdd[$k]['values2']) && !isset($this->fdd[$k]['values']['table']));
- } /* }}} */
-
- function processed($k) /* {{{ */
- {
- if ($this->virtual($k)) {
- return false;
- }
- $options = @$this->fdd[$k]['options'];
- if (! isset($options)) {
- return true;
- }
- return
- ($this->saveadd == $this->labels['Save'] && stristr($options, 'A')) ||
- ($this->moreadd == $this->labels['More'] && stristr($options, 'A')) ||
- ($this->savechange == $this->labels['Save'] && stristr($options, 'C')) ||
- ($this->morechange == $this->labels['Apply'] && stristr($options, 'C')) ||
- ($this->savecopy == $this->labels['Save'] && stristr($options, 'P')) ||
- ($this->savedelete == $this->labels['Save'] && stristr($options, 'D'));
- } /* }}} */
-
- function displayed($k) /* {{{ */
- {
- if (is_numeric($k)) {
- $k = $this->fds[$k];
- }
- $options = @$this->fdd[$k]['options'];
- if (! isset($options)) {
- return true;
- }
- return
- ($this->add_operation() && stristr($options, 'A')) ||
- ($this->view_operation() && stristr($options, 'V')) ||
- ($this->change_operation() && stristr($options, 'C')) ||
- ($this->copy_operation() && stristr($options, 'P')) ||
- ($this->delete_operation() && stristr($options, 'D')) ||
- ($this->filter_operation() && stristr($options, 'F')) ||
- ($this->list_operation() && stristr($options, 'L'));
- } /* }}} */
-
- function debug_var($name, $val) /* {{{ */
- {
- if (is_array($val) || is_object($val)) {
- echo "<pre>$name\n";
- ob_start();
- //print_r($val);
- var_dump($val);
- $content = ob_get_contents();
- ob_end_clean();
- echo htmlspecialchars($content);
- echo "</pre>\n";
- } else {
- echo 'debug_var()::<i>',htmlspecialchars($name),'</i>';
- echo '::<b>',htmlspecialchars($val),'</b>::',"<br />\n";
- }
- } /* }}} */
-
- /*
- * sql functions
- */
- function sql_connect() /* {{{ */
- {
- $this->dbh = @ini_get('allow_persistent')
- ? @mysql_pconnect($this->hn, $this->un, $this->pw)
- : @mysql_connect($this->hn, $this->un, $this->pw);
- } /* }}} */
-
-
- function sql_disconnect() /* {{{ */
- {
- if ($this->close_dbh) {
- @mysql_close($this->dbh);
- $this->dbh = null;
- }
- } /* }}} */
-
- function sql_fetch(&$res, $type = 'a') /* {{{ */
- {
- if($type == 'n') $type = MYSQL_NUM;
- else $type = MYSQL_ASSOC;
- return @mysql_fetch_array($res, $type);
- } /* }}} */
-
- function sql_free_result(&$res) /* {{{ */
- {
- return @mysql_free_result($res);
- } /* }}} */
-
- function sql_affected_rows(&$dbh) /* {{{ */
- {
- return @mysql_affected_rows($dbh);
- } /* }}} */
-
- function sql_field_len(&$res,$field) /* {{{ */
- {
- return @mysql_field_len($res, $field);
- } /* }}} */
-
- function sql_insert_id() /* {{{ */
- {
- return mysql_insert_id($this->dbh);
- } /* }}} */
-
- function sql_limit($start, $more) /* {{{ */
- {
- return ' LIMIT '.$start.', '.$more.' ';
- } /* }}} */
-
- function sql_delimiter() /* {{{ */
- {
- $this->sd = '`'; $this->ed='`';
- return $this->sd;
- } /* }}} */
-
-
- function myquery($qry, $line = 0, $debug = 0) /* {{{ */
- {
- global $debug_query;
- if ($debug_query || $debug) {
- $line = intval($line);
- echo '<h4>MySQL query at line ',$line,'</h4>',htmlspecialchars($qry),'<hr size="1" />',"\n";
- }
- if (isset($this->db)) {
- $ret = @mysql_db_query($this->db, $qry, $this->dbh);
- } else {
- $ret = @mysql_query($qry, $this->dbh);
- }
- if (! $ret) {
- echo '<h4>MySQL error ',mysql_errno($this->dbh),'</h4>';
- echo htmlspecialchars(mysql_error($this->dbh)),'<hr size="1" />',"\n";
- }
- return $ret;
- } /* }}} */
-
- /* end of sql functions */
-
- function make_language_labels($language) /* {{{ */
- {
- // just try the first language and variant
- // this isn't content-negotiation rfc compliant
- $language = strtoupper($language);
-
- // try the full language w/ variant
- $file = $this->dir['lang'].'PME.lang.'.$language.'.inc';
- while (! file_exists($file)) {
- $pos = strrpos($language, '-');
- if ($pos === false) {
- $file = $this->dir['lang'].'PME.lang.EN.inc';
- break;
- }
- $language = substr($language, 0, $pos);
- $file = $this->dir['lang'].'PME.lang.'.$language.'.inc';
- }
- $ret = @include($file);
- if (! is_array($ret)) {
- return $ret;
- }
- $small = array(
- 'Search' => 'v',
- 'Hide' => '^',
- 'Clear' => 'X',
- 'Query' => htmlspecialchars('>'));
- if ((!$this->nav_text_links() && !$this->nav_graphic_links())
- || !isset($ret['Search']) || !isset($ret['Query'])
- || !isset($ret['Hide']) || !isset($ret['Clear'])) {
- foreach ($small as $key => $val) {
- $ret[$key] = $val;
- }
- }
- return $ret;
- } /* }}} */
-
- function set_values($field_num, $prepend = null, $append = null, $strict = false) /* {{{ */
- {
- return (array) $prepend + (array) $this->fdd[$field_num]['values2']
- + (isset($this->fdd[$field_num]['values']['table']) || $strict
- ? $this->set_values_from_table($field_num, $strict)
- : array())
- + (array) $append;
- } /* }}} */
-
- function set_values_from_table($field_num, $strict = false) /* {{{ */
- {
- $db = &$this->fdd[$field_num]['values']['db'];
- $table = $this->sd.$this->fdd[$field_num]['values']['table'].$this->ed;
- $key = &$this->fdd[$field_num]['values']['column'];
- $desc = &$this->fdd[$field_num]['values']['description'];
- $dbp = isset($db) ? $this->sd.$db.$this->ed.'.' : $this->dbp;
- $qparts['type'] = 'select';
- if ($table != $this->sd.$this->ed) {
- $qparts['select'] = 'DISTINCT '.$table.'.'.$this->sd.$key.$this->ed;
- if ($desc && is_array($desc) && is_array($desc['columns'])) {
- $qparts['select'] .= ',CONCAT('; // )
- $num_cols = sizeof($desc['columns']);
- if (isset($desc['divs'][-1])) {
- $qparts['select'] .= '"'.addslashes($desc['divs'][-1]).'",';
- }
- foreach ($desc['columns'] as $key => $val) {
- if ($val) {
- $qparts['select'] .= 'IFNULL(CAST('.$this->sd.$val.$this->ed.' AS CHAR),"")';
- if ($desc['divs'][$key]) {
- $qparts['select'] .= ',"'.addslashes($desc['divs'][$key]).'"';
- }
- $qparts['select'] .= ',';
- }
- }
- $qparts['select']{strlen($qparts['select']) - 1} = ')';
- $qparts['select'] .= ' AS '.$this->sd.'PMEalias'.$field_num.$this->ed;
- $qparts['orderby'] = $this->sd.'PMEalias'.$field_num.$this->ed;
- } else if ($desc && is_array($desc)) {
- // TODO
- } else if ($desc) {
- $qparts['select'] .= ','.$table.'.'.$this->sd.$desc.$this->ed;
- $qparts['orderby'] = $this->sd.$desc.$this->ed;
- } else if ($key) {
- $qparts['orderby'] = $this->sd.$key.$this->ed;
- }
- $qparts['from'] = $dbp.$table;
- $ar = array(
- 'table' => $table,
- 'column' => $column,
- 'description' => $desc);
- $qparts['where'] = $this->substituteVars($this->fdd[$field_num]['values']['filters'], $ar);
- if ($this->fdd[$field_num]['values']['orderby']) {
- $qparts['orderby'] = $this->substituteVars($this->fdd[$field_num]['values']['orderby'], $ar);
- }
- } else { /* simple value extraction */
- $key = &$this->fds[$field_num];
- $this->virtual($field_num) && $key = $this->fqn($field_num);
- $qparts['select'] = 'DISTINCT '.$this->sd.$key.$this->ed.' AS PMEkey';
- $qparts['orderby'] = 'PMEkey';
- $qparts['from'] = $this->dbp.$this->sd.$this->tb.$this->ed;
- }
- $values = array();
- $res = $this->myquery($this->get_SQL_query($qparts), __LINE__);
- while ($row = $this->sql_fetch($res, 'n')) {
- $values[$row[0]] = $desc ? $row[1] : $row[0];
- }
- return $values;
- } /* }}} */
-
- function fqn($field, $dont_desc = false, $dont_cols = false) /* {{{ */
- {
- is_numeric($field) || $field = array_search($field, $this->fds);
- // if read SQL expression exists use it
- if ($this->col_has_sql($field) && !$this->col_has_values($field))
- return $this->fdd[$field]['sql'];
- // on copy/change always use simple key retrieving
- if ($this->add_operation()
- || $this->copy_operation()
- || $this->change_operation()) {
- $ret = $this->sd.'PMEtable0'.$this->ed.'.'.$this->sd.$this->fds[$field].$this->ed;
- } else {
- if ($this->fdd[$this->fds[$field]]['values']['description'] && ! $dont_desc) {
- $desc = &$this->fdd[$this->fds[$field]]['values']['description'];
- if (is_array($desc) && is_array($desc['columns'])) {
- $ret = 'CONCAT('; // )
- $num_cols = sizeof($desc['columns']);
- if (isset($desc['divs'][-1])) {
- $ret .= '"'.addslashes($desc['divs'][-1]).'",';
- }
- foreach ($desc['columns'] as $key => $val) {
- if ($val) {
- $ret .= 'IFNULL(CAST('.$this->sd.'PMEjoin'.$field.$this->ed.'.'.$this->sd.$val.$this->ed.' AS CHAR),"")';
- if ($desc['divs'][$key]) {
- $ret .= ',"'.addslashes($desc['divs'][$key]).'"';
- }
- $ret .= ',';
- }
- }
- $ret{strlen($ret) - 1} = ')';
- } else if (is_array($desc)) {
- // TODO
- } else {
- $ret = $this->sd.'PMEjoin'.$field.$this->ed.'.'.$this->sd.$this->fdd[$this->fds[$field]]['values']['description'].$this->ed;
- }
- // TODO: remove me
- } elseif (0 && $this->fdd[$this->fds[$field]]['values']['column'] && ! $dont_cols) {
- $ret = $this->sd.'PMEjoin'.$field.$this->ed.'.'.$this->fdd[$this->fds[$field]]['values']['column'];
- } else {
- $ret = $this->sd.'PMEtable0'.$this->ed.'.'.$this->sd.$this->fds[$field].$this->ed;
- }
- // TODO: not neccessary, remove me!
- if (is_array($this->fdd[$this->fds[$field]]['values2'])) {
- }
- }
- return $ret;
- } /* }}} */
-
- function get_SQL_main_list_query($qparts) /* {{{ */
- {
- return $this->get_SQL_query($qparts);
- } /* }}} */
-
-
-
- function get_SQL_query($parts) /* {{{ */
- {
- foreach ($parts as $k => $v) {
- $parts[$k] = trim($parts[$k]);
- }
- switch ($parts['type']) {
- case 'select':
- $ret = 'SELECT ';
- if ($parts['DISTINCT'])
- $ret .= 'DISTINCT ';
- $ret .= $parts['select'];
- $ret .= ' FROM '.$parts['from'];
- if ($parts['where'] != '')
- $ret .= ' WHERE '.$parts['where'];
- if ($parts['groupby'] != '')
- $ret .= ' GROUP BY '.$parts['groupby'];
- if ($parts['having'] != '')
- $ret .= ' HAVING '.$parts['having'];
- if ($parts['orderby'] != '')
- $ret .= ' ORDER BY '.$parts['orderby'];
- if ($parts['limit'] != '')
- $ret .= ' '.$parts['limit'];
- if ($parts['procedure'] != '')
- $ret .= ' PROCEDURE '.$parts['procedure'];
- break;
- case 'update':
- $ret = 'UPDATE '.$parts['table'];
- $ret .= ' SET '.$parts['fields'];
- if ($parts['where'] != '')
- $ret .= ' WHERE '.$parts['where'];
- break;
- case 'insert':
- $ret = 'INSERT INTO '.$parts['table'];
- $ret .= ' VALUES '.$parts['values'];
- break;
- case 'delete':
- $ret = 'DELETE FROM '.$parts['table'];
- if ($parts['where'] != '')
- $ret .= ' WHERE '.$parts['where'];
- break;
- default:
- die('unknown query type');
- break;
- }
- return $ret;
- } /* }}} */
-
- function get_SQL_column_list() /* {{{ */
- {
- $fields = array();
- for ($k = 0; $k < $this->num_fds; $k++) {
- if (! $this->displayed[$k] && $k != $this->key_num) {
- continue;
- }
- $fields[] = $this->fqn($k).' AS '.$this->sd.'qf'.$k.$this->ed; // no delimiters here, or maybe some yes
- if ($this->col_has_values($k)) {
- if($this->col_has_sql($k)) $fields[] = $this->fdd[$k]['sql'].' AS '.$this->sd.'qf'.$k.'_idx'.$this->ed;
- else $fields[] = $this->fqn($k, true, true).' AS '.$this->sd.'qf'.$k.'_idx'.$this->ed;
- }
- if ($this->col_has_datemask($k)) {
- $fields[] = 'UNIX_TIMESTAMP('.$this->fqn($k).') AS '.$this->sd.'qf'.$k.'_timestamp'.$this->ed;
- }
- }
- return join(',', $fields);
- } /* }}} */
-
- function get_SQL_join_clause() /* {{{ */
- {
- $main_table = $this->sd.'PMEtable0'.$this->ed;
- $join_clause = $this->sd.$this->tb.$this->ed." AS $main_table";
- for ($k = 0, $numfds = sizeof($this->fds); $k < $numfds; $k++) {
- $main_column = $this->fds[$k];
- if($this->fdd[$main_column]['values']['db']) {
- $dbp = $this->sd.$this->fdd[$main_column]['values']['db'].$this->ed.'.';
- } else {
- //$dbp = $this->dbp;
- }
- $table = $this->sd.$this->fdd[$main_column]['values']['table'].$this->ed;
- $join_column = $this->sd.$this->fdd[$main_column]['values']['column'].$this->ed;
- $join_desc = $this->sd.$this->fdd[$main_column]['values']['description'].$this->ed;
- if ($join_desc != $this->sd.$this->ed && $join_column != $this->sd.$this->ed) {
- $join_table = $this->sd.'PMEjoin'.$k.$this->ed;
- $ar = array(
- 'main_table' => $main_table,
- 'main_column' => $this->sd.$main_column.$this->ed,
- 'join_table' => $join_table,
- 'join_column' => $join_column,
- 'join_description' => $join_desc);
- $join_clause .= " LEFT OUTER JOIN $dbp".$table." AS $join_table ON (";
- $join_clause .= isset($this->fdd[$main_column]['values']['join'])
- ? $this->substituteVars($this->fdd[$main_column]['values']['join'], $ar)
- : "$join_table.$join_column = $main_table.".$this->sd.$main_column.$this->ed;
- $join_clause .= ')';
- }
- }
- return $join_clause;
- } /* }}} */
-
- function get_SQL_where_from_query_opts($qp = null, $text = 0) /* {{{ */
- {
- if ($qp == null) {
- $qp = $this->query_opts;
- }
- $where = array();
- foreach ($qp as $field => $ov) {
- if (is_numeric($field)) {
- $tmp_where = array();
- foreach ($ov as $field2 => $ov2) {
- $tmp_where[] = sprintf('%s %s %s', $field2, $ov2['oper'], $ov2['value']);
- }
- $where[] = '('.join(' OR ', $tmp_where).')';
- } else {
- if (is_array($ov['value'])) {
- $tmp_ov_val = '';
- foreach ($ov['value'] as $ov_val) {
- strlen($tmp_ov_val) > 0 && $tmp_ov_val .= ' OR ';
- $tmp_ov_val .= sprintf('FIND_IN_SET("%s",%s)', $ov_val, $field);
- }
- $where[] = "($tmp_ov_val)";
- } else {
- $where[] = sprintf('%s %s %s', $field, $ov['oper'], $ov['value']);
- }
- }
- }
- // Add any coder specified filters
- if (! $text && $this->filters) {
- $where[] = '('.$this->filters.')';
- }
- if (count($where) > 0) {
- if ($text) {
- return str_replace('%', '*', join(' AND ',$where));
- } else {
- return join(' AND ',$where);
- }
- }
- return ''; /* empty string */
- } /* }}} */
-
- function gather_query_opts() /* {{{ */
- {
- $this->query_opts = array();
- $this->prev_qfn = $this->qfn;
- $this->qfn = '';
- if ($this->clear_operation()) {
- return;
- }
- // gathers query options into an array, $this->query_opts
- $qo = array();
- for ($k = 0; $k < $this->num_fds; $k++) {
- $l = 'qf'.$k;
- $lc = 'qf'.$k.'_comp';
- $li = 'qf'.$k.'_id';
- $m = $this->get_sys_cgi_var($l);
- $mc = $this->get_sys_cgi_var($lc);
- $mi = $this->get_sys_cgi_var($li);
- if (! isset($m) && ! isset($mi)) {
- continue;
- }
- if (is_array($m) || is_array($mi)) {
- if (is_array($mi)) {
- $m = $mi;
- $l = $li;
- }
- if (in_array('*', $m)) {
- continue;
- }
- if ($this->col_has_values($k) && $this->col_has_multiple($k)) {
- foreach (array_keys($m) as $key) {
- $m[$key] = addslashes($m[$key]);
- }
- $qo[$this->fqn($k)] = array('value' => $m);
- } else {
- $qf_op = '';
- foreach (array_keys($m) as $key) {
- if ($qf_op == '') {
- $qf_op = 'IN';
- $qf_val = '"'.addslashes($m[$key]).'"';
- $afilter = ' IN ("'.addslashes($m[$key]).'"'; // )
- } else {
- $afilter = $afilter.',"'.addslashes($m[$key]).'"';
- $qf_val .= ',"'.addslashes($m[$key]).'"';
- }
- $this->qfn .= '&'.$this->cgi['prefix']['sys'].$l.'['.rawurlencode($key).']='.rawurlencode($m[$key]);
- }
- $afilter = $afilter.')';
- // XXX: $dont_desc and $dont_cols hack
- $dont_desc = isset($this->fdd[$k]['values']['description']);
- $dont_cols = isset($this->fdd[$k]['values']['column']);
- $qo[$this->fqn($k, $dont_desc, $dont_cols)] =
- array('oper' => $qf_op, 'value' => "($qf_val)"); // )
- }
- } else if (isset($mi)) {
- if ($mi == '*') {
- continue;
- }
- if ($this->fdd[$k]['select'] != 'M' && $this->fdd[$k]['select'] != 'D' && $mi == '') {
- continue;
- }
- $afilter = addslashes($mi);
- $qo[$this->fqn($k, true, true)] = array('oper' => '=', 'value' => "'$afilter'");
- $this->qfn .= '&'.$this->cgi['prefix']['sys'].$li.'='.rawurlencode($mi);
- } else if (isset($m)) {
- if ($m == '*') {
- continue;
- }
- if ($this->fdd[$k]['select'] != 'M' && $this->fdd[$k]['select'] != 'D' && $m == '') {
- continue;
- }
- $afilter = addslashes($m);
- if ($this->fdd[$k]['select'] == 'N') {
- $mc = in_array($mc, $this->comp_ops) ? $mc : '=';
- $qo[$this->fqn($k)] = array('oper' => $mc, 'value' => "'$afilter'");
- $this->qfn .= '&'.$this->cgi['prefix']['sys'].$l .'='.rawurlencode($m);
- $this->qfn .= '&'.$this->cgi['prefix']['sys'].$lc.'='.rawurlencode($mc);
- } else {
- $afilter = '%'.str_replace('*', '%', $afilter).'%';
- $ids = array();
- $ar = array();
- $ar[$this->fqn($k)] = array('oper' => 'LIKE', 'value' => "'$afilter'");
- if (is_array($this->fdd[$k]['values2'])) {
- foreach ($this->fdd[$k]['values2'] as $key => $val) {
- if (strlen($m) > 0 && stristr($val, $m)) {
- $ids[] = '"'.addslashes($key).'"';
- }
- }
- if (count($ids) > 0) {
- $ar[$this->fqn($k, true, true)]
- = array('oper' => 'IN', 'value' => '('.join(',', $ids).')');
- }
- }
- $qo[] = $ar;
- $this->qfn .= '&'.$this->cgi['prefix']['sys'].$l.'='.rawurlencode($m);
- }
- }
- }
- $this->query_opts = $qo;
- } /* }}} */
-
- /*
- * Create JavaScripts
- */
-
- function form_begin() /* {{{ */
- {
- $page_name = htmlspecialchars($this->page_name);
- if ($this->add_operation() || $this->change_operation() || $this->copy_operation()
- || $this->view_operation() || $this->delete_operation()) {
- $field_to_tab = array();
- for ($tab = $k = $this->cur_tab = 0; $k < $this->num_fds; $k++) {
- if (isset($this->fdd[$k]['tab'])) {
- if ($tab == 0 && $k > 0) {
- $this->tabs[0] = 'PMEtab0';
- $this->cur_tab = 1;
- $tab++;
- }
- if (is_array($this->fdd[$k]['tab'])) {
- $this->tabs[$tab] = @$this->fdd[$k]['tab']['name'];
- $this->fdd[$k]['tab']['default'] && $this->cur_tab = $tab;
- } else {
- $this->tabs[$tab] = @$this->fdd[$k]['tab'];
- }
- $tab++;
- }
- $field_to_tab[$k] = max(0, $tab - 1);
- }
- if (preg_match('/^'.$this->dhtml['prefix'].'tab(\d+)$/', $this->get_sys_cgi_var('cur_tab'), $parts)) {
- $this->cur_tab = $parts[1];
- }
- if ($this->tabs_enabled()) {
- // initial TAB styles
- echo '<style type="text/css" media="screen">',"\n";
- for ($i = 0; $i < count($this->tabs); $i++) {
- echo ' #'.$this->dhtml['prefix'].'tab',$i,' { display: ';
- echo (($i == $this->cur_tab || $this->tabs[$i] == 'PMEtab0' ) ? 'block' : 'none') ,'; }',"\n";
- }
- echo '</style>',"\n";
- // TAB javascripts
- echo '<script type="text/javascript"><!--',"\n\n";
- $css_class_name1 = $this->getCSSclass('tab', $position);
- $css_class_name2 = $this->getCSSclass('tab-selected', $position);
- echo 'var '.$this->js['prefix'].'cur_tab = "'.$this->dhtml['prefix'].'tab',$this->cur_tab,'";
-
- function '.$this->js['prefix'].'show_tab(tab_name)
- {';
- if ($this->nav_up()) {
- echo '
- document.getElementById('.$this->js['prefix'].'cur_tab+"_up_label").className = "',$css_class_name1,'";
- document.getElementById('.$this->js['prefix'].'cur_tab+"_up_link").className = "',$css_class_name1,'";
- document.getElementById(tab_name+"_up_label").className = "',$css_class_name2,'";
- document.getElementById(tab_name+"_up_link").className = "',$css_class_name2,'";';
- }
- if ($this->nav_down()) {
- echo '
- document.getElementById('.$this->js['prefix'].'cur_tab+"_down_label").className = "',$css_class_name1,'";
- document.getElementById('.$this->js['prefix'].'cur_tab+"_down_link").className = "',$css_class_name1,'";
- document.getElementById(tab_name+"_down_label").className = "',$css_class_name2,'";
- document.getElementById(tab_name+"_down_link").className = "',$css_class_name2,'";';
- }
- echo '
- document.getElementById('.$this->js['prefix'].'cur_tab).style.display = "none";
- document.getElementById(tab_name).style.display = "block";
- '.$this->js['prefix'].'cur_tab = tab_name;
- document.'.$this->cgi['prefix']['sys'].'form.'.$this->cgi['prefix']['sys'].'cur_tab.value = tab_name;
- }',"\n\n";
- echo '// --></script>', "\n";
- }
- }
-
- if ($this->add_operation() || $this->change_operation() || $this->copy_operation()) {
- $first_required = true;
- for ($k = 0; $k < $this->num_fds; $k++) {
- if ($this->displayed[$k] && ! $this->readonly($k) && ! $this->hidden($k)
- && ($this->fdd[$k]['js']['required'] || isset($this->fdd[$k]['js']['regexp']))) {
- if ($first_required) {
- $first_required = false;
- echo '<script type="text/javascript"><!--',"\n";
- echo '
- function '.$this->js['prefix'].'trim(str)
- {
- while (str.substring(0, 1) == " "
- || str.substring(0, 1) == "\\n"
- || str.substring(0, 1) == "\\r")
- {
- str = str.substring(1, str.length);
- }
- while (str.substring(str.length - 1, str.length) == " "
- || str.substring(str.length - 1, str.length) == "\\n"
- || str.substring(str.length - 1, str.length) == "\\r")
- {
- str = str.substring(0, str.length - 1);
- }
- return str;
- }
-
- function '.$this->js['prefix'].'form_control(theForm)
- {',"\n";
- }
- if ($this->col_has_values($k)) {
- $condition = 'theForm.'.$this->cgi['prefix']['data'].$this->fds[$k].'.selectedIndex == -1';
- $multiple = $this->col_has_multiple_select($k);
- } else {
- $condition = '';
- $multiple = false;
- if ($this->fdd[$k]['js']['required']) {
- $condition = $this->js['prefix'].'trim(theForm.'.$this->cgi['prefix']['data'].$this->fds[$k].'.value) == ""';
- }
- if (isset($this->fdd[$k]['js']['regexp'])) {
- $condition .= (strlen($condition) > 0 ? ' || ' : '');
- $condition .= sprintf('!(%s.test('.$this->js['prefix'].'trim(theForm.%s.value)))',
- $this->fdd[$k]['js']['regexp'], $this->cgi['prefix']['data'].$this->fds[$k]);
- }
- }
-
- /* Multiple selects have their name like ''name[]''.
- It is not possible to work with them directly, because
- theForm.name[].something will result into JavaScript
- syntax error. Following search algorithm is provided
- as a workaround for this.
- */
- if ($multiple) {
- echo '
- multiple_select = null;
- for (i = 0; i < theForm.length; i++) {
- if (theForm.elements[i].name == "',$this->cgi['prefix']['data'].$this->fds[$k],'[]") {
- multiple_select = theForm.elements[i];
- break;
- }
- }
- if (multiple_select != null && multiple_select.selectedIndex == -1) {';
- } else {
- echo '
- if (',$condition,') {';
- }
- echo '
- alert("';
- if (isset($this->fdd[$k]['js']['hint'])) {
- echo htmlspecialchars($this->fdd[$k]['js']['hint']);
- } else {
- echo $this->labels['Please enter'],' ',$this->fdd[$k]['name'],'.';
- }
- echo '");';
- if ($this->tabs_enabled() && $field_to_tab[$k] >= $this->cur_tab) {
- echo '
- '.$this->js['prefix'].'show_tab("'.$this->dhtml['prefix'].'tab',$field_to_tab[$k],'");';
- }
- echo '
- theForm.',$this->cgi['prefix']['data'].$this->fds[$k],'.focus();
- return false;
- }',"\n";
- }
- }
- if (! $first_required) {
- echo '
- return true;
- }',"\n\n";
- echo '// --></script>', "\n";
- }
- }
-
- if ($this->filter_operation()) {
- echo '<script type="text/javascript"><!--',"\n";
- echo '
- function '.$this->js['prefix'].'filter_handler(theForm, theEvent)
- {
- var pressed_key = null;
- if (theEvent.which) {
- pressed_key = theEvent.which;
- } else {
- pressed_key = theEvent.keyCode;
- }
- if (pressed_key == 13) { // enter pressed
- theForm.submit();
- return false;
- }
- return true;
- }',"\n\n";
- echo '// --></script>', "\n";
- }
-
- if ($this->display['form']) {
- echo '<form class="',$this->getCSSclass('form'),'" method="post"';
- echo ' action="',$page_name,'" name="'.$this->cgi['prefix']['sys'].'form">',"\n";
- }
- return true;
- } /* }}} */
-
- function form_end() /* {{{ */
- {
- if ($this->display['form']) {
- echo '</form>',"\n";
- }
- } /* }}} */
-
- function display_tab_labels($position) /* {{{ */
- {
- if (! is_array($this->tabs)) {
- return false;
- }
- echo '<table summary="labels" class="',$this->getCSSclass('tab', $position),'">',"\n";
- echo '<tr class="',$this->getCSSclass('tab', $position),'">',"\n";
- for ($i = ($this->tabs[0] == 'PMEtab0' ? 1 : 0); $i < count($this->tabs); $i++) {
- $css_class_name = $this->getCSSclass($i != $this->cur_tab ? 'tab' : 'tab-selected', $position);
- echo '<td class="',$css_class_name,'" id="'.$this->dhtml['prefix'].'tab',$i,'_',$position,'_label">';
- echo '<a class="',$css_class_name,'" id="'.$this->dhtml['prefix'].'tab',$i,'_',$position,'_link';
- echo '" href="javascript:'.$this->js['prefix'].'show_tab(\''.$this->dhtml['prefix'].'tab',$i,'\')">';
- echo $this->tabs[$i],'</a></td>',"\n";
- }
- echo '<td class="',$this->getCSSclass('tab-end', $position),'"> </td>',"\n";
- echo '</tr>',"\n";
- echo '</table>',"\n";
- } /* }}} */
-
- /*
- * Display functions
- */
-
- function display_add_record() /* {{{ */
- {
- for ($tab = 0, $k = 0; $k < $this->num_fds; $k++) {
- if (isset($this->fdd[$k]['tab']) && $this->tabs_enabled() && $k > 0) {
- $tab++;
- echo '</table>',"\n";
- echo '</div>',"\n";
- echo '<div id="'.$this->dhtml['prefix'].'tab',$tab,'">',"\n";
- echo '<table class="',$this->getCSSclass('main'),'" summary="',$this->tb,'">',"\n";
- }
- if (! $this->displayed[$k]) {
- continue;
- }
- if ($this->hidden($k)) {
- echo $this->htmlHiddenData($this->fds[$k], $this->fdd[$k]['default']);
- continue;
- }
- $css_postfix = @$this->fdd[$k]['css']['postfix'];
- $css_class_name = $this->getCSSclass('input', null, 'next', $css_postfix);
- $escape = isset($this->fdd[$k]['escape']) ? $this->fdd[$k]['escape'] : true;
- echo '<tr class="',$this->getCSSclass('row', null, true, $css_postfix),'">',"\n";
- echo '<td class="',$this->getCSSclass('key', null, true, $css_postfix),'">';
- echo $this->fdd[$k]['name'],'</td>',"\n";
- echo '<td class="',$this->getCSSclass('value', null, true, $css_postfix),'"';
- echo $this->getColAttributes($k),">\n";
- if ($this->col_has_values($k)) {
- $vals = $this->set_values($k);
- $selected = @$this->fdd[$k]['default'];
- $multiple = $this->col_has_multiple($k);
- $readonly = $this->readonly($k);
- $strip_tags = true;
- //$escape = true;
- if ($this->col_has_checkboxes($k) || $this->col_has_radio_buttons($k)) {
- echo $this->htmlRadioCheck($this->cgi['prefix']['data'].$this->fds[$k],
- $css_class_name, $vals, $selected, $multiple, $readonly,
- $strip_tags, $escape);
- } else {
- echo $this->htmlSelect($this->cgi['prefix']['data'].$this->fds[$k],
- $css_class_name, $vals, $selected, $multiple, $readonly,
- $strip_tags, $escape);
- }
- } elseif (isset ($this->fdd[$k]['textarea'])) {
- echo '<textarea class="',$css_class_name,'" name="',$this->cgi['prefix']['data'].$this->fds[$k],'"';
- echo ($this->readonly($k) ? ' disabled' : '');
- if (intval($this->fdd[$k]['textarea']['rows']) > 0) {
- echo ' rows="',$this->fdd[$k]['textarea']['rows'],'"';
- }
- if (intval($this->fdd[$k]['textarea']['cols']) > 0) {
- echo ' cols="',$this->fdd[$k]['textarea']['cols'],'"';
- }
- if (isset($this->fdd[$k]['textarea']['wrap'])) {
- echo ' wrap="',$this->fdd[$k]['textarea']['wrap'],'"';
- } else {
- echo ' wrap="virtual"';
- }
- echo '>';
- if($escape) echo htmlspecialchars($this->fdd[$k]['default']);
- else echo $this->fdd[$k]['default'];
- echo '</textarea>',"\n";
- } elseif ($this->col_has_php($k)) {
- echo include($this->fdd[$k]['php']);
- } else {
- // Simple edit box required
- $len_props = '';
- $maxlen = intval($this->fdd[$k]['maxlen']);
- $size = isset($this->fdd[$k]['size']) ? $this->fdd[$k]['size'] : min($maxlen, 60);
- if ($size > 0) {
- $len_props .= ' size="'.$size.'"';
- }
- if ($maxlen > 0) {
- $len_props .= ' maxlength="'.$maxlen.'"';
- }
- echo '<input class="',$css_class_name,'" ';
- echo ($this->password($k) ? 'type="password"' : 'type="text"');
- echo ($this->readonly($k) ? ' disabled' : '');
- echo ' name="',$this->cgi['prefix']['data'].$this->fds[$k],'"';
- echo $len_props,' value="';
- if($escape) echo htmlspecialchars($this->fdd[$k]['default']);
- else echo $this->fdd[$k]['default'];
- echo '" />';
- }
- echo '</td>',"\n";
- if ($this->guidance) {
- $css_class_name = $this->getCSSclass('help', null, true, $css_postfix);
- $cell_value = $this->fdd[$k]['help'] ? $this->fdd[$k]['help'] : ' ';
- echo '<td class="',$css_class_name,'">',$cell_value,'</td>',"\n";
- }
- echo '</tr>',"\n";
- }
- } /* }}} */
-
- function display_copy_change_delete_record() /* {{{ */
- {
- /*
- * For delete or change: SQL SELECT to retrieve the selected record
- */
-
- $qparts['type'] = 'select';
- $qparts['select'] = $this->get_SQL_column_list();
- $qparts['from'] = $this->get_SQL_join_clause();
- $qparts['where'] = '('.$this->fqn($this->key).'='
- .$this->key_delim.$this->rec.$this->key_delim.')';
-
- $res = $this->myquery($this->get_SQL_query($qparts),__LINE__);
- if (! ($row = $this->sql_fetch($res))) {
- return false;
- }
- for ($tab = 0, $k = 0; $k < $this->num_fds; $k++) {
- if (isset($this->fdd[$k]['tab']) && $this->tabs_enabled() && $k > 0) {
- $tab++;
- echo '</table>',"\n";
- echo '</div>',"\n";
- echo '<div id="'.$this->dhtml['prefix'].'tab',$tab,'">',"\n";
- echo '<table class="',$this->getCSSclass('main'),'" summary="',$this->tb,'">',"\n";
- }
- if (! $this->displayed[$k]) {
- continue;
- }
- if ($this->copy_operation() || $this->change_operation()) {
- if ($this->hidden($k)) {
- if ($k != $this->key_num) {
- echo $this->htmlHiddenData($this->fds[$k], $row["qf$k"]);
- }
- continue;
- }
- $css_po…
Large files files are truncated, but you can click here to view the full file