<?php require_once 'header.php'; ?>
<div id="container_background">
<div id="container_content"><?php
$db = db_connect();
$sql = 'SELECT * FROM modules WHERE modules_id = :modules_id';
$mod = $db->prepare($sql);
$mod->bindValue(':modules_id', $_GET['m'], PDO::PARAM_STR);
$mod->execute();
if(!$module = $mod->fetch(PDO::FETCH_ASSOC)){
?><p class="info err">Moduł nie istnieje!</p><?php
}
if($_POST['buy_x']){
$extImg = array('image/gif', 'image/jpeg', 'image/png', 'image/jpg'); $insertColumn = '';
$insertValue = '';
if($module['clogo']){
if($_FILES['logo']['error'] != 4){
if(!in_array($_FILES['logo']['type'], $extImg)){ $errors['extImg'] = 'Niepoprawny format zdjęcia!';
}
if($_FILES['logo']['size'] > 2097152){
$errors['size'] = 'Rozmiar obrazka nie może przekraczać 2Mb!';
}
}else{
$errors['err'] = 'Logo nie zostało wysłane';
}
}
fixFilesArray($_FILES['photo']);
$i = 1;
foreach ($_FILES['photo'] as $position => $file) {
$errors['extImg'][$i] = 'Niepoprawny format zdjęcia!';
}
if($file['size'] > 2097152){
$errors['size'][$i] = 'Rozmiar obrazka nie może przekraczać 2Mb!';
}
$i = $i+1;
}
if(!preg_match("/^[a-z0-9]{3,16}$/", $_POST['site_address'])){ $errors['site_address'] = 'Adres może zawierać tylko litery i cyfry';
}else{
$sql = 'SELECT idcompany FROM company WHERE c_site_address = :c_site_address';
$name = $db->prepare($sql);
$name->bindValue(':c_site_address', $_POST['site_address'], PDO::PARAM_STR);
$name->execute();
if($testName = $name->fetch(PDO::FETCH_ASSOC)){
$errors['nameExists'] = 'Nazwa strony jest już zajęta';
}else{
$insertColumn .= 'c_site_address,';
$insertValue .= ':c_site_address,';
}
}
if($module['cname']){
if(empty($_POST['name'])){ $errors['name'] = 'Pole nazwa firmy musi być wypełnione!';
}else{
$insertColumn .= ' c_name,';
$insertValue .= ' :c_name,';
}
}
if($module['ctrade']){
if(empty($_POST['trade'])){ $errors['trade'] = 'Pole branża musi być wypełnione!';
}else{
$insertColumn .= ' c_trade,';
$insertValue .= ' :c_trade,';
}
}
if($module['cdescription']){
if(empty($_POST['description'])){ $errors['description'] = 'Pole opis musi być wypełnione!';
}else{
$insertColumn .= ' c_description,';
$insertValue .= ' :c_description,';
}
}
if($module['cactivity']){
if(empty($_POST['activity'])){ $errors['activity'] = 'Pole opis działalności musi być wypełnione!';
}else{
$insertColumn .= ' c_activity,';
$insertValue .= ' :c_activity,';
}
}
if($module['chobby']){
if(empty($_POST['hobby'])){ $errors['hobby'] = 'Pole hobby musi być wypełnione!';
}else{
$insertColumn .= ' c_hobby,';
$insertValue .= ' :c_hobby,';
}
}
if($module['cmail']){
if (!filter_var($_POST['email'], FILTER_VALIDATE_EMAIL)){
$errors['email'] = 'Pole email musi być wypełnione!';
}else{
$insertColumn .= ' c_email,';
$insertValue .= ' :c_email,';
}
}
if($module['caddress']){
if (!filter_var($_POST['phone'], FILTER_VALIDATE_INT)){
$errors['phone'] = 'Pole phone musi być wypełnione!';
}else{
$insertColumn .= ' c_phonenumber,';
$insertValue .= ' :c_phonenumber,';
}
if(empty($_POST['street'])){ $errors['street'] = 'Pole ulica musi być wypełnione!';
}else{
$insertColumn .= ' c_street,';
$insertValue .= ' :c_street,';
}
if(empty($_POST['city'])){ $errors['city'] = 'Pole miasto musi być wypełnione!';
}else{
$insertColumn .= ' c_city,';
$insertValue .= ' :c_city,';
}
if ( !preg_match('/^[0-9]{2}-?[0-9]{3}$/', $_POST['postcode'])){ $errors['postcode'] = 'Nieprawidłowy kod pocztowy';
}else{
$insertColumn .= ' c_postcode,';
$insertValue .= ' :c_postcode,';
}
}
mkdir('images/company/'.$_POST['site_address'], 0777
); $logo = substr($_SERVER['REQUEST_TIME'], 7
,3
).clearText
($file['name']); if(move_uploaded_file($_FILES['logo']['tmp_name'], 'images/company/'.$_POST['site_address'].'/'.$logo)){ $uploadedImg[] = 1;
$insertColumn .= ' c_logo,';
$insertValue .= ' :c_logo,';
}else{
$uploadedImg[] = 0;
}
foreach ($_FILES['photo'] as $position => $file){
$imgName = substr($_SERVER['REQUEST_TIME'], 7
,3
).clearText
($file['name']); if(move_uploaded_file($file['tmp_name'], 'images/company/'.$_POST['site_address'].'/'.$imgName)){ $uploadedImg[] = 1;
$imgNames[] = $imgName;
}else{
$uploadedImg[] = 0;
}
}
$insertColumn .= 'subscription, c_view, c_registration_date';
$insertValue .= ':subscription, :view, :registration_date';
$db = db_connect();
$sql = 'INSERT INTO company('.$insertColumn.')
VALUES('.$insertValue.')';
$stmt = $db->prepare($sql);
$stmt->bindValue(':c_site_address', $_POST['site_address'], PDO::PARAM_STR);
$stmt->bindValue(':subscription', $_POST['subscription'], PDO::PARAM_INT);
$stmt->bindValue(':view', 0, PDO::PARAM_INT);
$stmt->bindValue(':registration_date', date("Y-m-d"), PDO
::PARAM_STR); if($module['cname']){
$stmt->bindValue(':c_name', $_POST['name'], PDO::PARAM_STR);
}
if($module['ctrade']){
$stmt->bindValue(':c_trade', $_POST['trade'], PDO::PARAM_STR);
}
if($module['clogo']){
$stmt->bindValue(':c_logo', $logo, PDO::PARAM_STR);
}
if($module['cdescription']){
$stmt->bindValue(':c_description', $_POST['description'], PDO::PARAM_STR);
}
if($module['cactivity']){
$stmt->bindValue(':c_activity', $_POST['activity'], PDO::PARAM_STR);
}
if($module['chobby']){
$stmt->bindValue(':c_hobby', $_POST['hobby'], PDO::PARAM_STR);
}
if($module['cmail']){
$stmt->bindValue(':c_email', $_POST['email'], PDO::PARAM_STR);
}
if($module['caddress']){
$stmt->bindValue(':c_phonenumber', $_POST['phone'], PDO::PARAM_INT);
$stmt->bindValue(':c_street', $_POST['street'], PDO::PARAM_STR);
$stmt->bindValue(':c_city', $_POST['city'], PDO::PARAM_STR);
$stmt->bindValue(':c_postcode', $_POST['postcode'], PDO::PARAM_STR);
}
try {
$db->beginTransaction();
$stmt->execute();
$id = $db->lastInsertId();
$sql = 'INSERT INTO company_images(idcompany, images)
VALUES(:idcompany, :images)';
$insImg = $db->prepare($sql);
foreach($imgNames as $img){
$insImg->bindValue(':idcompany', $id, PDO::PARAM_INT);
$insImg->bindValue(':images', $img, PDO::PARAM_STR);
$insImg->execute();
}
$db->commit();
}catch(PDOExecption $e){
$db->rollback();
rrmdir('../images/comapny/'.$_POST['site_address']);
print "Error!: " . $e->getMessage() . "</br>"; }
}
}else{
foreach ($errors as $err){
?><p class="info err">
<?php print $err;?></p>
<?php }
}
}else{
header('Location: card_list.php'); }?>
</div>
</div>
</body>
</html>