Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

able to add and change images to product #65

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
65 changes: 62 additions & 3 deletions pr/products/save.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
<?session_start();
header( 'Content-Type: text/html; charset=utf-8' );
include '../template/settings.php';
include('../template/connect.php');

if($_SESSION['user_id']==null){
Expand Down Expand Up @@ -65,7 +66,8 @@ function strOrNull($s){
echo json_encode(array('result' => 'Ошибка при загрузке изображения', 'error' => '1'));
die();
}
$fullPath = $_SERVER['DOCUMENT_ROOT'].'/pricer/uploaded/'.$_FILES['image']['name'];
$fullPath = $_SERVER['DOCUMENT_ROOT'].'/'.SITE_FOLDER.'uploaded/'.$_FILES['image']['name'];
//echo '<br>$fullPath: '.$fullPath.'<br>';
if(file_exists($fullPath)){
function on_file_exist($path){
$result = array();
Expand Down Expand Up @@ -115,8 +117,65 @@ function on_file_exist($path){
echo json_encode($result);
exit();
} else {
$stmt = $db->prepare("INSERT products(name, ed_izm_id, in_box, barcode, main_clsf_id, creator) values(?, ?, ?, ?, ?, ?)");
if(!$stmt->execute(array($_REQUEST['product_name'], $_REQUEST['ed_izm'], doKolvo($_REQUEST['in_box']), strOrNull($_REQUEST['barcode']), $_REQUEST['main_clsf_id'], $_SESSION['user_id']))){

if($_FILES['image']['name']){
if ($_FILES['image']['size'] > 1048576){
echo json_encode(array('result' => 'Слишком большой размер файла', 'error' => '1'));
die();
}

$pieces = explode(".", $_FILES['image']['name']);
if(!in_array(mb_strtolower($pieces[count($pieces) - 1]), array('png', 'bmp', 'gif', 'jpg', 'jpeg')) ){
echo json_encode(array('result' => 'Недопустимый тип файла', 'error' => '1'));
die();
}
if($_FILES['image']['error'] != 0){
echo json_encode(array('result' => 'Ошибка при загрузке изображения', 'error' => '1'));
die();
}
$fullPath = $_SERVER['DOCUMENT_ROOT'].'/'.SITE_FOLDER.'uploaded/'.$_FILES['image']['name'];
//echo '<br>$fullPath: '.$fullPath.'<br>';
if(file_exists($fullPath)){
function on_file_exist($path){
$result = array();
$pieces = explode(".", $path);
$ext = array_pop($pieces);
$pieces[0] = implode('.', $pieces);
$pieces[1] = $ext;
$pieces2 = explode("_", $pieces[0]);
$num = array_pop($pieces2);
//echo 'окончание названия существующего файла'.$num.'<br>';
if(ctype_digit($num)){
//echo 'окончание цифровое <br>';
$new_num = (int) $num + 1;
$new_path = implode('_', $pieces2).'_'.$new_num.'.'.$pieces[1];
//echo 'новое название файла: '.$new_path.'<br>';
}else{
$new_path = $pieces[0].'_0.'.$pieces[1];
}
if (file_exists($new_path)) {
//echo 'новое название файла занято <br>';
$result = on_file_exist($new_path);
}else{
//echo 'новое название файла свободно <br>';
$result = $new_path;
}
return $result;
}
$fullPath = on_file_exist($fullPath);
//echo json_encode(array('result' => 'Файл с таким именем уже загружен', 'error' => '1'));
//die();
}
if(move_uploaded_file($_FILES['image']['tmp_name'], $fullPath)){
$pieces = explode("/", $fullPath);
$photoFileName = array_pop($pieces);
}else{
$photoFileName = '';
}
//TODO: Дописать удаление предыдущей картинки
}
$stmt = $db->prepare("INSERT products(name, ed_izm_id, in_box, barcode, main_clsf_id, creator, photo) values(?, ?, ?, ?, ?, ?, ?)");
if(!$stmt->execute(array($_REQUEST['product_name'], $_REQUEST['ed_izm'], doKolvo($_REQUEST['in_box']), strOrNull($_REQUEST['barcode']), $_REQUEST['main_clsf_id'], $_SESSION['user_id'], $photoFileName))){
echo json_encode(array('result' => 'Ошибка добавления товара.', 'error' => '1'));
//echo 'Ошибка добавления товара'; print_r($stmt->errorInfo());
exit();
Expand Down
1 change: 1 addition & 0 deletions pr/template/header.php
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
unset($_SESSION['user_show_login']);
}
header('Content-Type: text/html; charset=utf-8');
include 'settings.php';
include 'connect.php';
function headerOut($curmenu, $treesuf = null){
$tabTitle = 'Ценовичок - '.$curmenu;
Expand Down
5 changes: 5 additions & 0 deletions pr/template/jstree/jstree.php
Original file line number Diff line number Diff line change
Expand Up @@ -125,6 +125,11 @@ function putTree($suf, $path){
$('.fancybox-close').click();
}else{
window.last_tree_id = data.selected.join(':');
<?if($suf=='prod'){?>
$('#dataprod .default').html('Загрузка ...');
<?}else if($suf=='shop'){?>
$('#datashop .default').html('Загрузка ...');
<?}?>
$.get('<?=$path?>tree.php?suf=<?=$suf?>&operation=get_content&id=' + data.selected.join(':'), function (d) {
if(d){
$('#data<?=$suf?> .default').html(d.content).show();
Expand Down
3 changes: 3 additions & 0 deletions pr/template/settings.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
<?
define ('SITE_FOLDER', 'pricer_new/');
?>