Commit 7461a5ab authored by Florent Delmotte's avatar Florent Delmotte Committed by Cédric Houbart
Browse files

add import category by csv to base_product

parent deb8bf1c
......@@ -41,6 +41,19 @@ class CategoriesService {
return $cats;
}
static function getByName($name) {
$pdo = PDOBuilder::getPDO();
$stmt = $pdo->prepare("SELECT * FROM CATEGORIES WHERE NAME = :name");
$stmt->bindParam(":name", $name, \PDO::PARAM_STR);
if ($stmt->execute()) {
if ($row = $stmt->fetch()) {
$cat = CategoriesService::buildDBCat($row);
return $cat;
}
}
return null;
}
static function get($id) {
$pdo = PDOBuilder::getPDO();
$stmt = $pdo->prepare("SELECT * FROM CATEGORIES WHERE ID = :id");
......
......@@ -28,7 +28,8 @@ if (isset($_POST['delete-cat'])) {
if (\Pasteque\CategoriesService::deleteCat($_POST['delete-cat'])) {
$message = \i18n("Changes saved");
} else {
$error = \i18n("Unable to save changes"). ". Une catégorie non vide ne peut être supprimée";
$error = \i18n("Unable to save changes");
$error .= " " . \i18n("Only empty category can be deleted", PLUGIN_NAME);
}
}
......@@ -38,7 +39,11 @@ $categories = \Pasteque\CategoriesService::getAll();
<?php \Pasteque\tpl_msg_box($message, $error); ?>
<p><a class="btn" href="<?php echo \Pasteque\get_module_url_action(PLUGIN_NAME, 'category_edit'); ?>"><img src="<?php echo \Pasteque\get_template_url(); ?>img/btn_add.png" /><?php \pi18n("Add a category", PLUGIN_NAME); ?></a></p>
<?php \Pasteque\tpl_btn('btn', \Pasteque\get_module_url_action(PLUGIN_NAME, "category_edit"),
\i18n('Add a category', PLUGIN_NAME), 'img/btn_add.png');?>
<?php \Pasteque\tpl_btn('btn', \Pasteque\get_module_url_action(PLUGIN_NAME, "categoriesManagement"),
\i18n('Import categories', PLUGIN_NAME), 'img/btn_add.png');?>
<p><?php \pi18n("%d categories", PLUGIN_NAME, count($categories)); ?></p>
......@@ -60,7 +65,10 @@ $par = !$par;
<td><img class="thumbnail" src="?<?php echo \Pasteque\URL_ACTION_PARAM; ?>=img&w=category&id=<?php echo $category->id; ?>" />
<td><?php echo $category->label; ?></td>
<td class="edition">
<a href="<?php echo \Pasteque\get_module_url_action(PLUGIN_NAME, 'category_edit', array('id' => $category->id)); ?>"><img src="<?php echo \Pasteque\get_template_url(); ?>img/edit.png" alt="<?php \pi18n('Edit'); ?>" title="<?php \pi18n('Edit'); ?>"></a>
<?php \Pasteque\tpl_btn("edition", \Pasteque\get_module_url_action(PLUGIN_NAME,
'category_edit', array("id" => $category->id)), "",
'img/edit.png', \i18n('Edit'), \i18n('Edit'));
?>
<form action="<?php echo \Pasteque\get_current_url(); ?>" method="post"><?php \Pasteque\form_delete("cat", $category->id, \Pasteque\get_template_url() . 'img/delete.png') ?></form>
</td>
</tr>
......
<?php
namespace BaseProducts;
// open csv return null if the file selected had not extension "csv"
// or user not selected file
function init_csv() {
if ($_FILES['csv']['tmp_name'] === NULL) {
return NULL;
}
$ext = strchr($_FILES['csv']['type'], "/");
$ext = strtolower($ext);
if($ext !== "/csv") {
return NULL;
}
$key = array('Designation', 'Parent', 'Ordre');
$csv = new \Pasteque\Csv($_FILES['csv']['tmp_name'], $key);
if (!$csv->open()) {
return $csv;
}
//manage empty field:
$csv->addFilter("Parent", NULL);
$csv->addFilter("Order", NULL);
$csv->addFilter("Designation", NULL);
return $csv;
}
function import_csv($csv) {
$error_mess = array();
$update = 0;
$create = 0;
$error=0;
while ($tab = $csv->readLine()) {
$parentOk = false;
$parent = \Pasteque\CategoriesService::getByName($tab['Parent']);
$image = NULL;
if ($parent) {
$parentOk = true;
$tab['Parent'] = $parent->id;
}
if ($parentOk) {
$cat = new \Pasteque\Category($tab['Parent'], $tab['Designation'],
$image, $tab['Ordre']);
$category_exist = \Pasteque\CategoriesService::getByName($cat->label);
//UPDATE category
if ($category_exist) {
$cat->id = $category_exist->id;
if (\Pasteque\CategoriesService::updateCat($cat)) {
$update++;
} else {
$error++;
$error_mess[] = \i18n("On line %d: Cannot update category: '%s'", PLUGIN_NAME,
$csv->getCurrentLineNumber(), $tab['Designation']);
}
//CREATE category
} else {
$id = \Pasteque\CategoriesService::createCat($cat);
if ($id) {
$create++;
} else {
$error++;
$error_mess[] = \i18n("On line %d: Cannot create category: '%s'", PLUGIN_NAME,
$csv->getCurrentLineNumber(), $tab['Designation']);
}
}
} else {
$error++;
$error_mess[] = \i18n("On line %d: Category parent doesn't exist",
PLUGIN_NAME, $csv->getCurrentLineNumber());
}
}
$message = \i18n("%d line(s) inserted, %d line(s) modified, %d error(s)",
PLUGIN_NAME, $create, $update, $error );
$csv->close();
\Pasteque\tpl_msg_box($message, $error_mess);
}
?>
<?php
if (isset($_FILES['csv'])) {
$dateStr = isset($_POST['date']) ? $_POST['date'] : \i18nDate(time());
$dateStr = \i18nRevDate($dateStr);
$date = \Pasteque\stdstrftime($dateStr);
$csv = init_csv();
if ($csv === NULL) {
\Pasteque\tpl_msg_box(NULL, \i18n("Selected file empty or bad format", PLUGIN_NAME));
} else if (!$csv->isOpen()) {
$err = array();
foreach ($csv->getErrors() as $mess) {
$err[] = \i18n($mess);
}
\Pasteque\tpl_msg_box(NULL, $err);
} else {
import_csv($csv, $date);
}
}
?>
<h1><?php \pi18n("Import category from csv", PLUGIN_NAME); ?></h1>
<form method="post" action="<?php echo \Pasteque\get_module_url_action(PLUGIN_NAME, 'categoriesManagement');?>" enctype="multipart/form-data">
<?php \pi18n("File", PLUGIN_NAME) ?>: <input type="file" name="csv">
<input type="submit" name="envoyer" value=<?php \pi18n("send", PLUGIN_NAME)?>>
</form>
......@@ -17,6 +17,7 @@
"Categories": "Categories",
"Edit categories": "Edit categories",
"Import categories": "Importer des catégories",
"Add a category": "Add a category",
"%d categories": "%d categories",
"No category found": "No category found",
......@@ -27,5 +28,17 @@
"Edit tax": "Edit tax",
"Rates": "Rates",
"New tax rate": "New rate",
"Unable to delete tax. Tax cannot be deleted when in use.": "Unable to delete tax. Tax cannot be deleted when in use."
"Unable to delete tax. Tax cannot be deleted when in use.": "Unable to delete tax. Tax cannot be deleted when in use.",
"File": "Fichier",
"send": "send",
"Only empty category can be deleted": "Only empty category can be deleted",
"Import category": "Import category",
"Import category from csv": "Import category from csv",
"On line %d: Cannot create category: '%s'": "On line %d: Cannot create category: '%s'",
"On line %d: Cannot update category: '%s'": "On line %d: Cannot update category: '%s'",
"On line %d: Category parent doesn't exist": "On line %d: Category parent doesn't exist",
"%d line(s) inserted, %d line(s) modified, %d error(s)": "%d line(s) inserted, %d line(s) modified, %d error(s)",
"Selected file empty or bad format": "Selected file empty or bad format"
}
......@@ -27,5 +27,19 @@
"Edit tax": "Édition de la taxe",
"Rates": "Taux",
"New tax rate": "Nouveau taux",
"Unable to delete tax. Tax cannot be deleted when in use.": "Impossible de supprimer la taxe. Une taxe ne peut pas être supprimée lorsqu'elle est utilisée."
"Unable to delete tax. Tax cannot be deleted when in use.": "Impossible de supprimer la taxe. Une taxe ne peut pas être supprimée lorsqu'elle est utilisée.",
"send": "envoyer",
"File": "Fichier",
"Only empty category can be deleted": "Une catégorie non vide ne peut être supprimée",
"Import categories": "Importer des catégories",
"Import category from csv": "Importer des categories à partir d'un csv",
"On line %d: Cannot update category: '%s'": "À la ligne %d: Impossible de mettre à jour la category: '%s'",
"On line %d: Cannot create product: '%s'": "À la ligne %d: Impossible de créer le produit: '%s'",
"On line %d: Category parent doesn't exist": "À la ligne %d: La categorie parent n'existe pas",
"Cannot open file": "Impossible d'ouvrir le fichier",
"%d line(s) inserted, %d line(s) modified, %d error(s)": "%d ligne(s) inserée(s), %d ligne(s) modifiée(s), %d erreur(s)",
"Selected file empty or bad format": "Aucun fichier sélectionné ou mauvais format"
}
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment