function import_csv($csv)
{
    $error_mess = array();
    $update = 0;
    $create = 0;
    $error = 0;
    while ($tab = $csv->readLine()) {
        $parentOk = false;
        if ($tab['Parent'] !== NULL) {
            $parent = \Pasteque\CategoriesService::getByName($tab['Parent']);
            $image = NULL;
            if ($parent) {
                $parentOk = true;
                $tab['Parent'] = $parent->id;
            }
        } else {
            // Category isn't subCategory
            $parentOk = true;
        }
        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);
}
示例#2
0
        if ($_POST['clearImage']) {
            $img = NULL;
        } else {
            $img = "";
        }
    }
    $parent_id = NULL;
    if ($_POST['parentId'] !== "") {
        $parent_id = $_POST['parentId'];
    }
    $dispOrder = 0;
    if ($_POST['dispOrder'] !== "") {
        $dispOrder = intval($_POST['dispOrder']);
    }
    $cat = \Pasteque\Category::__build($_POST['id'], $_POST['reference'], $parent_id, $_POST['label'], $img !== null, $dispOrder);
    if (\Pasteque\CategoriesService::updateCat($cat, $img)) {
        $message = \i18n("Changes saved");
    } else {
        $error = \i18n("Unable to save changes");
    }
} else {
    if (isset($_POST['label'])) {
        if ($_FILES['image']['tmp_name'] !== "") {
            $img = file_get_contents($_FILES['image']['tmp_name']);
        } else {
            $img = NULL;
        }
        $parent_id = NULL;
        if ($_POST['parentId'] !== "") {
            $parent_id = $_POST['parentId'];
        }