Index: trunk/core/admin_templates/tools/system_tools.tpl =================================================================== diff -u -N -r7887 -r7991 --- trunk/core/admin_templates/tools/system_tools.tpl (.../system_tools.tpl) (revision 7887) +++ trunk/core/admin_templates/tools/system_tools.tpl (.../system_tools.tpl) (revision 7991) @@ -37,11 +37,12 @@ "> - : + Table Structure: + table name (prefix optional) OR unit config prefix Index: trunk/core/kernel/application.php =================================================================== diff -u -N -r7855 -r7991 --- trunk/core/kernel/application.php (.../application.php) (revision 7855) +++ trunk/core/kernel/application.php (.../application.php) (revision 7991) @@ -2455,17 +2455,7 @@ */ function TableFound($table_name) { - static $table_found = Array(); - - if (!preg_match('/^'.preg_quote(TABLE_PREFIX, '/').'(.*)/', $table_name)) { - $table_name = TABLE_PREFIX.$table_name; - } - - if (!isset($table_found[$table_name])) { - $table_found[$table_name] = $this->Conn->Query('SHOW TABLES LIKE "'.$table_name.'"'); - } - - return $table_found[$table_name]; + $this->Conn->TableFound($table_name); } } Index: trunk/core/kernel/utility/debugger.php =================================================================== diff -u -N -r7855 -r7991 --- trunk/core/kernel/utility/debugger.php (.../debugger.php) (revision 7855) +++ trunk/core/kernel/utility/debugger.php (.../debugger.php) (revision 7991) @@ -545,7 +545,7 @@ } $string = str_replace( Array('\\', '/') , Array('_no_match_string_', '_n_m_s_'), $string); - $string = highlight_string('', true); + $string = highlight_string('', true); $string = str_replace( Array('_no_match_string_', '_n_m_s_'), Array('\\', '/'), $string); return preg_replace('/<\?(.*)php(.*)\?>/Us', '\\2', $string); } Index: trunk/core/units/images/images_config.php =================================================================== diff -u -N -r7867 -r7991 --- trunk/core/units/images/images_config.php (.../images_config.php) (revision 7867) +++ trunk/core/units/images/images_config.php (.../images_config.php) (revision 7991) @@ -81,7 +81,7 @@ 'ResourceId' => Array('type'=>'int', 'not_null'=>1, 'default' => 0), 'Url' => Array('max_len'=>255, 'default' => '', 'not_null'=>1), 'Name' => Array('max_len'=>255, 'required'=>1, 'not_null'=>1, 'default' => ''), - 'AltName' => Array('max_len'=>255, 'required'=>1, 'not_null'=>1), + 'AltName' => Array('max_len'=>255, 'required'=>1, 'not_null'=>1, 'default' => ''), 'ImageIndex' => Array('type'=>'int', 'default' => 0, 'not_null'=>1), 'LocalImage' => Array('type'=>'int', 'default' => 1, 'not_null'=>1), 'LocalPath' => Array('formatter'=>'kPictureFormatter', 'skip_empty'=>1, 'max_len'=>240, 'default' => '', 'not_null' => 1, 'include_path' => 1, @@ -99,9 +99,9 @@ ), 'Enabled' => Array('formatter'=>'kOptionsFormatter', 'use_phrases' => 1, 'options' => Array ( 1 => 'la_Enabled', 0 => 'la_Disabled' ), 'default' => 0, 'not_null'=>1), 'DefaultImg' => Array('type'=>'int', 'default' => 0, 'not_null'=>1), - 'ThumbUrl' => Array('max_len' => 255), + 'ThumbUrl' => Array('type' => 'string', 'max_len' => 255, 'default' => ''), 'Priority' => Array('type'=>'int', 'default' => 0, 'not_null'=>1), - 'ThumbPath' => Array( 'formatter'=>'kPictureFormatter', 'skip_empty'=>1, 'max_len' => 255, + 'ThumbPath' => Array( 'formatter'=>'kPictureFormatter', 'skip_empty'=>1, 'max_len' => 255, 'default' => '', 'allowed_types' => Array( 0 => 'image/jpeg', 1 => 'image/pjpeg', Index: trunk/core/units/email_messages/email_messages_config.php =================================================================== diff -u -N -r7855 -r7991 --- trunk/core/units/email_messages/email_messages_config.php (.../email_messages_config.php) (revision 7855) +++ trunk/core/units/email_messages/email_messages_config.php (.../email_messages_config.php) (revision 7991) @@ -60,7 +60,7 @@ 'MessageType' => Array('type' => 'string', 'formatter' => 'kOptionsFormatter', 'options' => Array('text'=>'la_Text','html'=>'la_Html'), 'not_null' => '1','default' => 'text'), 'LanguageId' => Array('type' => 'int', 'not_null' => '1', 'default' => '0'), 'EventId' => Array('type' => 'int', 'not_null' => '1', 'default' => '0'), - 'Subject' => Array('type' => 'string'), + 'Subject' => Array('type' => 'string', 'default' => '',), ), 'VirtualFields' => Array( Index: trunk/core/units/admin/admin_events_handler.php =================================================================== diff -u -N -r7887 -r7991 --- trunk/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 7887) +++ trunk/core/units/admin/admin_events_handler.php (.../admin_events_handler.php) (revision 7991) @@ -53,17 +53,29 @@ echo 'error: no table name specified'; return ; } - - if (!preg_match('/^'.preg_quote(TABLE_PREFIX, '/').'(.*)/', $table_name)) { + + if (TABLE_PREFIX && !preg_match('/^'.preg_quote(TABLE_PREFIX, '/').'(.*)/', $table_name)) { + // table name without prefix, then add it $table_name = TABLE_PREFIX.$table_name; } + if (!$this->Conn->TableFound($table_name)) { + // table with prefix doesn't exist, assume that just config prefix passed -> resolve table name from it + $table_name = $this->Application->getUnitOption(substr($table_name, strlen(TABLE_PREFIX)), 'TableName'); + } + $table_info = $this->Conn->Query('DESCRIBE '.$table_name); // 1. prepare config keys + $id_field = ''; $fields = Array(); $float_types = Array ('float', 'double', 'numeric'); foreach ($table_info as $field_info) { + if (preg_match('/l[\d]+_.*/', $field_info['Field'])) { + // don't put multilingual fields in config + continue; + } + $field_options = Array (); // 1. get php field type by mysql field type @@ -76,49 +88,78 @@ $default_value = $field_info['Default']; if (in_array($php_type, $float_types)) { - // this is float number, add formatter + // this is float number if (preg_match('/'.$db_types.'\([\d]+,([\d]+)\)/i', $field_info['Type'], $regs)) { + // size is described in structure -> add formatter $field_options['formatter'] = 'kFormatter'; $field_options['format'] = '%01.'.$regs[1].'f'; $default_value = 0; } + else { + // no size information, just convert to float + $default_value = (float)$default_value; + } } + if (preg_match('/varchar\(([\d]+)\)/i', $field_info['Type'], $regs)) { + $field_options['max_len'] = (int)$regs[1]; + } + if ($field_info['Null'] != 'YES') { $field_options['not_null'] = 1; } if ($field_info['Key'] == 'PRI') { $default_value = 0; - $config['IDField'] = "'".$field_info['Field']."'"; + $id_field = $field_info['Field']; } - $field_options['default'] = $default_value; - - // format config keys - $ret = ''; - foreach ($field_options as $key_name => $key_value) { - if (!preg_match('/^[-\d]+$/', $key_value)) { - // if not only digits are present, then treat as string - $key_value = "'".$key_value."'"; - } - - $ret .= "'".$key_name."' => ".$key_value.', '; + if ($php_type == 'int' && ($field_info['Null'] != 'YES' || is_numeric($default_value))) { + // is integer field AND not null + $field_options['default'] = (int)$default_value; } + else { + $field_options['default'] = $default_value; + } - $fields[ $field_info['Field'] ] = 'Array('.rtrim($ret,',').')'; + $fields[ $field_info['Field'] ] = $this->transformDump($field_options); } - - $config['Fields'] = $fields; - - echo 'Close Window
'; - $ret = print_r($config, true); - $ret = preg_replace( Array('/\[(.*)\]/',"/(.*)\)/"), Array("'\\1'",'\\1),'), $ret); - print_pre($ret, '', true); + $ret = stripslashes(var_export($fields, true)); + $ret = preg_replace("/'(.*?)' => 'Array \((.*?), \)',/", "'\\1' => Array (\\2),", $ret); + $ret = preg_replace("/\n '/", "\n\t'", $ret); + $ret = "'IDField' => '".$id_field."',\n'Fields' => A".substr($ret, 1).','; + + ob_start(); + ?> + + + Table "<?php echo $table_name; ?>" Structure + + + Close Window
+ highlightString($ret); ?> +
Close Window
+ + + status = erSTOP; } + function transformDump($dump) + { + if (is_array($dump)) { + $dump = var_export($dump, true); + } + + $dump = preg_replace("/,\n[ ]*/", ', ', $dump); + $dump = preg_replace("/array \(\n[ ]*/", 'Array (', $dump); // replace array start + $dump = preg_replace("/,\n[ ]*\),/", "),", $dump); // replace array end + + return $dump; + } + /** * Refreshes ThemeFiles & Theme tables by actual content on HDD * Index: trunk/core/kernel/utility/unit_config_reader.php =================================================================== diff -u -N -r7931 -r7991 --- trunk/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 7931) +++ trunk/core/kernel/utility/unit_config_reader.php (.../unit_config_reader.php) (revision 7991) @@ -391,6 +391,13 @@ } else { $options = $config['Fields'][$f_name]; + $default_missing = false; + if (!array_key_exists('default', $options)) { + $debugger->appendHTML("Config Error: Default value for field $f_name in config for prefix ".$config_link." not set !"); + safeDefine('DBG_RAISE_ON_WARNINGS', 1); + $default_missing = true; + } + if ($field['Null'] != 'YES') { // MySQL5 for null returns "NO", but MySQL4 returns "" if ( $f_name != $config['IDField'] && !isset($options['not_null']) && !isset($options['required']) ) { @@ -402,19 +409,16 @@ safeDefine('DBG_RAISE_ON_WARNINGS', 1); } - if (!isset($options['default'])) { - $debugger->appendHTML("Config Error: Default value for field $f_name in config for prefix ".$config_link." not set !"); - safeDefine('DBG_RAISE_ON_WARNINGS', 1); + if (!$default_missing) { + if ($f_name == $config['IDField'] && $options['default'] !== 0) { + $debugger->appendHTML("Config Error: Default value for field IDField $f_name (".var_export($options['default'], true).") in config for prefix ".$config_link." differs from default value from field in database (".var_export($field['Default'], true).") !"); + safeDefine('DBG_RAISE_ON_WARNINGS', 1); + } + else if ($options['default'] != '#NOW#' && $field['Default'] !== (string)$options['default'] && !in_array($options['type'], $float_types)) { + $debugger->appendHTML("Config Error: Default value for field $f_name (".var_export($options['default'], true).") in config for prefix ".$config_link." differs from default value from field in database (".var_export($field['Default'], true).") !"); + safeDefine('DBG_RAISE_ON_WARNINGS', 1); + } } - elseif ($f_name == $config['IDField'] && $options['default'] !== 0) { - $debugger->appendHTML("Config Error: Default value for field IDField $f_name (".var_export($options['default'], true).") in config for prefix ".$config_link." differs from default value from field in database (".var_export($field['Default'], true).") !"); - safeDefine('DBG_RAISE_ON_WARNINGS', 1); - } - else if ($options['default'] != '#NOW#' && $field['Default'] !== (string)$options['default'] && !in_array($options['type'], $float_types)) { - $debugger->appendHTML("Config Error: Default value for field $f_name (".var_export($options['default'], true).") in config for prefix ".$config_link." differs from default value from field in database (".var_export($field['Default'], true).") !"); -// $debugger->dumpVars($field['Default'], $options['default'], 'STRICT'); - safeDefine('DBG_RAISE_ON_WARNINGS', 1); - } // check that all idfields have default values & are not_null!!! } Index: trunk/kernel/units/images/images_config.php =================================================================== diff -u -N -r7867 -r7991 --- trunk/kernel/units/images/images_config.php (.../images_config.php) (revision 7867) +++ trunk/kernel/units/images/images_config.php (.../images_config.php) (revision 7991) @@ -81,7 +81,7 @@ 'ResourceId' => Array('type'=>'int', 'not_null'=>1, 'default' => 0), 'Url' => Array('max_len'=>255, 'default' => '', 'not_null'=>1), 'Name' => Array('max_len'=>255, 'required'=>1, 'not_null'=>1, 'default' => ''), - 'AltName' => Array('max_len'=>255, 'required'=>1, 'not_null'=>1), + 'AltName' => Array('max_len'=>255, 'required'=>1, 'not_null'=>1, 'default' => ''), 'ImageIndex' => Array('type'=>'int', 'default' => 0, 'not_null'=>1), 'LocalImage' => Array('type'=>'int', 'default' => 1, 'not_null'=>1), 'LocalPath' => Array('formatter'=>'kPictureFormatter', 'skip_empty'=>1, 'max_len'=>240, 'default' => '', 'not_null' => 1, 'include_path' => 1, @@ -99,9 +99,9 @@ ), 'Enabled' => Array('formatter'=>'kOptionsFormatter', 'use_phrases' => 1, 'options' => Array ( 1 => 'la_Enabled', 0 => 'la_Disabled' ), 'default' => 0, 'not_null'=>1), 'DefaultImg' => Array('type'=>'int', 'default' => 0, 'not_null'=>1), - 'ThumbUrl' => Array('max_len' => 255), + 'ThumbUrl' => Array('type' => 'string', 'max_len' => 255, 'default' => ''), 'Priority' => Array('type'=>'int', 'default' => 0, 'not_null'=>1), - 'ThumbPath' => Array( 'formatter'=>'kPictureFormatter', 'skip_empty'=>1, 'max_len' => 255, + 'ThumbPath' => Array( 'formatter'=>'kPictureFormatter', 'skip_empty'=>1, 'max_len' => 255, 'default' => '', 'allowed_types' => Array( 0 => 'image/jpeg', 1 => 'image/pjpeg', Index: trunk/core/kernel/db/db_connection.php =================================================================== diff -u -N -r7855 -r7991 --- trunk/core/kernel/db/db_connection.php (.../db_connection.php) (revision 7855) +++ trunk/core/kernel/db/db_connection.php (.../db_connection.php) (revision 7991) @@ -573,5 +573,26 @@ return $this->ChangeQuery($sql); } + + /** + * Allows to detect table's presense in database + * + * @param string $table_name + * @return bool + */ + function TableFound($table_name) + { + static $table_found = Array(); + + if (!preg_match('/^'.preg_quote(TABLE_PREFIX, '/').'(.*)/', $table_name)) { + $table_name = TABLE_PREFIX.$table_name; + } + + if (!isset($table_found[$table_name])) { + $table_found[$table_name] = $this->Query('SHOW TABLES LIKE "'.$table_name.'"'); + } + + return $table_found[$table_name]; + } } ?> \ No newline at end of file