| serg-php |
23.03.2008 12:28 |
Например, еще один дезенденый вариант:
PHP код:
<?php
class SiteItemAdm
{
public $db = NULL;
public $FS_FILE = NULL;
public $TBL_ITEM = NULL;
public $ID = 0;
public $ADM_TPL_PATH = NULL;
public $PAGE_URL = NULL;
public $IDX = "id";
public $O_FS = NULL;
public $ERR_MSG = "";
public $FS_ACTIVE_ONLY = false;
public $_search_by = NULL;
public function SiteItemAdm( )
{
$this->db =& $db;
}
public function &getFSInstance( )
{
if ( !isset( $this->O_FS ) )
{
$this->O_FS =& Factory::create( "FieldsSet", $this->FS_FILE );
if ( $this->FS_ACTIVE_ONLY )
{
$this->O_FS->ITERATOR_FILTER = create_function( "\$o_f", "return \$o_f->ACTIVE;" );
}
}
return $this->O_FS;
}
public function ShowPage( )
{
if ( $_REQUEST['submdel'] && $_REQUEST['chk_id'] )
{
$this->ItemsDelete( );
if ( !$this->ERR_MSG )
{
header( "Location: ".$this->PAGE_URL );
exit( );
}
}
else if ( $_POST['submexport'] )
{
$this->ItemsExport( );
}
$clean['id'] = is_scalar( $_GET['id'] ) && preg_match( "/^\\w+\$/", $_GET['id'] ) ? $_GET['id'] : NULL;
if ( $clean['id'] )
{
$this->ID = $clean['id'];
$this->getFSInstance( );
$this->O_FS->EditFieldsSetup( );
if ( $_POST['go'] )
{
$this->_makeReadOnlyHidden( );
$this->ERR_MSG = $this->ItemValidate( );
if ( !$this->ERR_MSG )
{
$this->_editFormProcess( );
}
}
$this->EditFormShow( );
}
else if ( $_GET['pg'] == "add" )
{
$this->getFSInstance( );
if ( $_POST['go'] )
{
$this->ERR_MSG = $this->ItemValidate( );
if ( !$this->ERR_MSG )
{
$this->AddFormProcess( );
}
}
$this->AddFormShow( );
}
else
{
$this->ListShow( );
}
}
public function ItemsExport( )
{
$o_ex =& new ExportData( );
$o_ex->TBL = $this->TBL_ITEM;
$o_ex->SQL = "SELECT * FROM ".$this->TBL_ITEM." WHERE 1".$this->ListSQLGet( );
$o_ex->Export( );
}
public function ItemsDeleteSQLIds( $var_name = "" )
{
if ( !$var_name )
{
$var_name = "chk_id";
}
$s_id = $_REQUEST[$var_name];
if ( !$s_id )
{
return;
}
if ( !is_array( $s_id ) )
{
$s_id = array(
$s_id
);
}
$s_id = array_map( array(
$this->db,
"esc"
), $s_id );
return implode( ",", $s_id );
}
public function ItemsDelete( )
{
$s_id = $this->ItemsDeleteSQLIds( );
$this->getFSInstance( );
$ql = "FROM ".$this->TBL_ITEM." WHERE ".$this->IDX." IN(".$s_id.")";
$res = $this->db->query( "SELECT * ".$ql );
while ( $v = mysql_fetch_assoc( $res ) )
{
hwdeleterelatedfiles( $v, $this->O_FS->A_UPLOAD_DB );
}
$this->db->query( "DELETE ".$ql );
return $s_id;
}
public function ListShow( )
{
global $sort_by;
global $a_d;
global $nav;
global $q;
global $search_by;
global $per_row;
global $numrecs;
$whitelist = array(
"sort_by" => array( "type" => "regexp", "pattern" => "/^[\\w ,]+\$/" ),
"a_d" => array(
"type" => "option",
"options" => array( "asc", "desc" )
),
"off" => array( "type" => "int" ),
"per_row" => array( "type" => "int" )
);
$clean = esfilter( $_REQUEST, $whitelist );
$sort_by = $clean['sort_by'] ? $clean['sort_by'] : $this->IDX;
$a_d = $clean['a_d'] ? $clean['a_d'] : "desc";
$per_row = $clean['per_row'] ? $clean['per_row'] : 10;
if ( !$clean['off'] || $_SERVER['REQUEST_METHOD'] == "POST" )
{
$clean['off'] = 0;
}
$num = $clean['off'] * $per_row;
$ql = "SELECT /*!40000 SQL_CALC_FOUND_ROWS */ ".$this->ListSQLFieldsGet( )." FROM ".$this->TBL_ITEM." WHERE 1".$this->ListSQLGet( );
if ( preg_match( "/[, ]/", $sort_by ) )
{
$_sort_by = "'".preg_replace( "/\\s*,\\s*/", "','", $sort_by )."'";
$_sort_by = preg_replace( "/\\s+(ASC|DESC)'/i", "' \\1", $_sort_by );
if ( preg_match_all( "/'[\\w]+'/", $_sort_by, $m ) )
{
$cnt = count( $m[0] );
$i = 0;
for ( ; $i < $cnt; ++$i )
{
$db_f = substr( $m[0][$i], 1, -1 );
if ( $this->ColInTable( $db_f ) )
{
$_sort_by = str_replace( $m[0][$i], $db_f, $_sort_by );
}
}
}
$ql .= " ORDER BY ".$_sort_by;
unset( $_sort_by );
}
else if ( $this->ColInTable( $sort_by ) )
{
$ql .= " ORDER BY {$sort_by} {$a_d}";
}
$ql .= " LIMIT {$num},{$per_row}";
$ql = $this->_listSQLAdjust( $ql );
$res = $this->db->query( $ql );
$numrecs = mysql_num_rows( $res );
if ( !$numrecs )
{
}
else
{
$numrecs = hwsqlcalcrows( $ql );
}
if ( $numrecs )
{
$ex_p = $this->ListNavGet( );
$ex_p = array_flip( $ex_p );
foreach ( array_keys( $ex_p ) as $k )
{
if ( isset( $k ) )
{
$ex_p[$k] = $$k;
}
else if ( isset( $_REQUEST[$k] ) && !is_array( $_REQUEST[$k] ) )
{
$ex_p[$k] = $_REQUEST[$k];
}
else
{
$ex_p[$k] = "";
}
}
$nav = getnavigation( $this->PAGE_URL, $per_row, 10, $numrecs, $clean['off'], $ex_p );
unset( $ex_p );
}
$tpl =& new HawkTpl( );
$tpl->InitArray( "row" );
$A_ID = $this->ListSQLResultReview( $res, $tpl );
while ( $v = mysql_fetch_assoc( $res ) )
{
$v['num'] = ++$num;
$this->ListRenderRow( $v );
$a_ph = $A_ID[$v[$this->IDX]];
if ( $a_ph )
{
$v = array_merge( $v, $a_ph );
}
$tpl->AddCell( "row", $v );
}
$search_by = $this->_getSelect( $this->ListSearchFieldsGet( ), $_REQUEST['search_by'] );
$sort_by = $this->_getSelect( $this->ListSortFieldsGet( ), $sort_by );
$a_v = array( "asc" => "{Ascendant}", "desc" => "{Descendant}" );
$a_d = $this->_getSelect( $a_v, $a_d );
$a_v = array( "10" => "10", "15" => "15", "20" => "20", "25" => "25", "30" => "30", "40" => "40", "50" => "50", "75" => "75", "100" => "100" );
$per_row = $this->_getSelect( $a_v, $per_row );
$GLOBALS['GLOBALS']['PAGE_URL'] = $this->PAGE_URL;
if ( $this->ERR_MSG )
{
$GLOBALS['GLOBALS']['err_msg'] = $this->ERR_MSG;
}
$q = htmlspecialchars( $_REQUEST['q'], ENT_QUOTES, $CHARSET );
$tpl->Parse( $this->TplGetPath( "list.htm" ), "\$numrecs,\$q,\$a_d,\$PAGE_URL".$this->ListRenderTpl( ).",\$nav,\$per_row,\$sort_by,\$search_by", 1 );
}
public function ListSearchFieldsGet( )
{
if ( is_array( $this->_search_by ) )
{
return $this->_search_by;
}
$a_v = array( "ID" );
$this->getFSInstance( );
$a_no_search = array( "checkboxes", "checkbox" );
$this->O_FS->rewind( );
while ( $o_f =& $this->O_FS->next( ) )
{
if ( in_array( $o_f->TYPE, $a_no_search ) )
{
continue;
}
$f_db = $o_f->DB_F;
$a_v[$f_db] = $o_f->TITLE;
}
$this->_search_by = $a_v;
return $a_v;
}
public function ListRenderTpl( )
{
return "";
}
public function ListSQLGet( )
{
$search_by = ( boolean )$_REQUEST['search_by'];
$q = trim( ( boolean )$_REQUEST['q'] );
$ql = "";
if ( $q && $search_by )
{
$a_v = $this->ListSearchFieldsGet( );
if ( !isset( $a_v[$search_by] ) )
{
return $ql;
}
unset( $a_v );
$is_exact_match = false;
if ( 2 < strlen( $q ) && $q[0] == "\"" && substr( $q, -1 ) == "\"" )
{
$q = substr( $q, 1, -1 );
$is_exact_match = true;
}
$q = addcslashes( $this->db->quote( $q ), "%_" );
if ( !$is_exact_match )
{
$q = "%{$q}%";
}
$ql .= " AND {$search_by} LIKE '{$q}'";
}
return $ql;
}
public function ListSQLFieldsGet( )
{
return "*";
}
public function _listSQLAdjust( $ql )
{
return $ql;
}
public function ListRenderRow( &$v )
{
}
public function ListNavGet( )
{
return array( "sort_by", "a_d", "per_row", "search_by", "q" );
}
public function ListSortFieldsGet( )
{
return array( "ID" );
}
public function ListSQLResultReview( $res, &$tpl )
{
}
public function ItemValidate( )
{
$this->getFSInstance( );
return $this->O_FS->ValidateData( );
}
public function AddFormShow( $show = true )
{
$a_v = $this->AddReadOnlyFieldsGet( );
if ( $a_v )
{
foreach ( $a_v as $k )
{
$this->O_FS->SetFieldProperty( "TYPE", "hidden", $k );
}
}
$tpl =& new HawkTpl( );
$tpl->InitArray( "row" );
$tpl_f = file_exists( $this->TplGetPath( "add.htm" ) ) ? "add" : "edit";
return $tpl->Parse( $this->TplGetPath( $tpl_f.".htm" ), $this->AddRenderTpl( $tpl ), $show );
}
public function AddFormProcess( )
{
$a_upload = $this->O_FS->A_UPLOAD;
foreach ( $a_upload as $k )
{
uploadisok( $k );
}
$this->db->query( "INSERT INTO ".$this->TBL_ITEM." SET ".substr( $this->O_FS->GetUpdateSQL( "", FALSE ), 1 ).$this->AddSQLAddonGet( ) );
if ( !$this->db->affected_rows( ) )
{
exit( "Error: user creation failed" );
}
$id = $this->db->insert_id( );
if ( !$id && $_POST[$this->IDX] )
{
$id = $this->db->quote( $_POST[$this->IDX] );
}
$this->AddAfterCreate( $id );
header( "Location: ".$this->PAGE_URL );
exit( );
}
public function AddSQLAddonGet( )
{
return "";
}
public function AddReadOnlyFieldsGet( )
{
return array( );
}
public function AddAfterCreate( $id )
{
}
public function AddRenderTpl( &$tpl )
{
return $this->ItemRender( $tpl, 0 );
}
public function EditFormShow( $show = true )
{
$tpl =& new HawkTpl( );
$tpl->InitArray( "row" );
$a_Item = $this->db->one_assoc( "SELECT * FROM ".$this->TBL_ITEM." WHERE {$this->IDX}='{$this->ID}'" );
if ( !$a_Item )
{
header( "Location: ".$this->PAGE_URL );
exit( );
}
$this->_makeReadOnlyHidden( $a_Item );
$this->O_FS->LoadValues( $a_Item );
return $tpl->Parse( $this->TplGetPath( "edit.htm" ), $this->EditRenderTpl( $tpl, $a_Item ), $show );
}
public function _makeReadOnlyHidden( $a_Item = false )
{
$a_v = $this->EditReadOnlyFieldsGet( $a_Item );
if ( $a_v )
{
foreach ( $a_v as $k )
{
$this->O_FS->SetFieldProperty( "TYPE", "hidden", $k );
}
}
}
public function _editFormProcess( )
{
$a_upload = $this->O_FS->A_UPLOAD_DB;
if ( $a_upload )
{
$old_files = $this->db->one_assoc( "SELECT ".implode( ",", $a_upload )." FROM ".$this->TBL_ITEM." WHERE ".$this->IDX."=\"".$this->ID."\"" );
}
$a_skip = array( );
$a_upload = $this->O_FS->A_UPLOAD;
if ( $a_upload )
{
foreach ( $a_upload as $k )
{
$f_db = $this->O_FS->GetDbFByID( $k );
if ( !uploadisok( $k, $old_files[$f_db] ) )
{
$a_skip[] = $k;
}
}
}
$this->db->query( "UPDATE ".$this->TBL_ITEM." SET ".substr( $this->O_FS->GetUpdateSQL( $a_skip, FALSE ), 1 ).$this->_EditSQLAddonGet( )." WHERE {$this->IDX}='{$this->ID}'" );
$this->EditAfterUpdate( );
$this->ERR_MSG = hwlng( "updated_ok" );
}
public function EditRenderTpl( &$tpl, $a_Item )
{
return $this->ItemRender( $tpl );
}
public function _EditSQLAddonGet( )
{
return "";
}
public function EditReadOnlyFieldsGet( $a_Item = false )
{
return array( );
}
public function EditAfterUpdate( )
{
}
public function ItemRender( &$tpl, $w_hidden = true, $auto = true )
{
$this->getFSInstance( );
$z = $a_out = array( );
$this->O_FS->rewind( );
while ( $o_f =& $this->O_FS->next( ) )
{
if ( !$w_hidden && $o_f->TYPE == "hidden" )
{
continue;
}
if ( isset( $_POST[$o_f->ID] ) )
{
$o_f->VALUE = $_POST[$o_f->ID];
}
if ( !$auto )
{
$a_out["ctl_{$o_f->ID}"] = $o_f->GetInputCtrl( );
}
else
{
$z['f_t'] = $o_f->TITLE;
$z['f_v'] = $o_f->GetInputCtrl( );
if ( $o_f->TYPE == "hidden" )
{
$z['f_txt'] = "";
}
else
{
$z['f_txt'] = $o_f->TXT;
}
if ( $o_f->ACTIVE )
{
$z['css'] = $o_f->REQUIRED ? "req" : "none";
}
else
{
$z['css'] = "adm";
}
$tpl->AddCell( "row", $z );
}
}
$a_out['enc_type'] = $this->O_FS->A_UPLOAD ? "ENCTYPE=\"multipart/form-data\"" : "";
$a_out['PAGE_URL'] = $this->PAGE_URL;
$a_out['id'] = $this->ID;
$GLOBALS['GLOBALS']['err_msg'] = $a_out['err_msg'] = $this->ERR_MSG;
return $a_out;
}
public function _getSelect( $a_v, $s_cur = "" )
{
$s = "";
if ( $a_v )
{
foreach ( $a_v as $k => $v )
{
$s .= "<option".( $k == $s_cur ? " selected" : "" )." value=\"".$k."\">".$v;
}
}
return $s;
}
public function ColInTable( $col, $tbl = "" )
{
if ( !$tbl )
{
$tbl = $this->TBL_ITEM;
}
$res = $this->db->query( "SHOW FIELDS FROM ".$tbl );
while ( $v = mysql_fetch_assoc( $res ) )
{
if ( $v['Field'] == $col )
{
return 1;
}
}
return 0;
}
public function TplGetPath( $fn )
{
return $this->ADM_TPL_PATH.$fn;
}
}
?>
|