| Current Path : /var/www/html/ebilikparlimen1/ebilik/ks_builtin/ |
| Current File : /var/www/html/ebilikparlimen1/ebilik/ks_builtin/data_delete.php |
<?php
include_once '../library.php';
//check is user authenticated
$isAuth = CUSTOM_User::checkAuthentication ();
if (! $isAuth) {
$red = urlencode ( $_SERVER ['REQUEST_URI'] );
header ( "Location: ../ks_user/login.php?msg=notlogin&red=$red" );
exit ();
}
$id = KS_Filter::inputSanitize( $_GET ['id'] );
$fid = ( int ) $_GET ['fid'];
$lid = ( int ) $_GET ['lid'];
$objForm = new KS_Form ();
$objForm->setId ( $fid );
if (! $objForm->exists ()) {
echo "Error! Specified form ($fid) doesn't exist. Perhaps it has been deleted? Please update your List property.";
exit ();
}
$objForm->select ();
$tablename = $objForm->getTable ();
$arrayTable = explode ( ";", $tablename );
$firstTable = $arrayTable [0];
$counterPri = 0;
//for single table
if ($firstTable) {
$SqlGetField = "SELECT sf_fieldname FROM ks_field WHERE sf_tablename = '$firstTable' LIMIT 1";
$firstField = $ks_db->fetchOne ( $SqlGetField );
if ($firstField) {
$prefix = explode ( "_", $firstField );
$pre = $prefix [0];
}
$newpre = $pre . "_id";
$sqlCountAtt = "SELECT COUNT(sf_fieldname) FROM ks_field WHERE sf_type = 'file' AND sf_tablename = '$firstTable'";
$countAtt = ( int ) $ks_db->fetchOne ( $sqlCountAtt );
for($i = 0; $i < $countAtt; $i ++) {
$sqlAtt = "SELECT sf_fieldname FROM ks_field WHERE sf_type = 'file' AND sf_tablename = '$firstTable' LIMIT 1";
$fieldAtt = $ks_db->fetchOne ( $sqlAtt );
$sqlAttValue = "SELECT $fieldAtt FROM $firstTable WHERE $newpre = '$id' LIMIT 1";
$fieldAttValue = $ks_db->fetchOne ( $sqlAttValue );
$arrIdAtt = explode ( "_", $fieldAttValue );
$getIdAtt = $arrIdAtt [0];
//delete attachment first
$objAttachment = new KS_Attachment ();
$objAttachment->setId ( $getIdAtt );
if ($objAttachment->exists ()) {
$objAttachment->delete ();
}
}
//get primary column
$sqlPrimary = "SHOW KEYS FROM $firstTable WHERE Key_name ='PRIMARY'";
$stmtPrimary = $ks_db->query ( $sqlPrimary );
while ( true == ($rowPrimary = $stmtPrimary->fetch ()) ) {
$primaryCol = $rowPrimary ['Column_name'];
$arrMultiple[] .= $rowPrimary ['Column_name'];
$counterPri++;
$strWhere .= "$primaryCol = '{$_GET[$primaryCol]}' AND ";
}
if ($counterPri > 1){
$sqlDelete = "DELETE FROM $firstTable WHERE $strWhere 1";
}else {
$sqlDelete = "DELETE FROM $firstTable WHERE $newpre = '$id'";
}
$ks_db->query ( $sqlDelete );
}
$objList = new KS_List ();
$objList->setId ( $lid );
if ($objList->exists ()) {
$red = "../list.php?lid=$lid&msg=data_deleted";
} else {
$red = "../home.php";
}
header ( "Location: $red" );