Added more content to demo : feature, folders, content

This commit is contained in:
Julien Chanséaume
2014-07-23 17:16:16 +02:00
committed by Julien Chanseaume
parent 1e8e26cb1d
commit f679e8e9cb
6 changed files with 266 additions and 70 deletions

View File

@@ -20,6 +20,8 @@
/* along with this program. If not, see <http://www.gnu.org/licenses/>. */
/* */
/*************************************************************************************/
use Thelia\Model\ProductAssociatedContent;
if (php_sapi_name() != 'cli') {
throw new \Exception('this script can only be launched with cli sapi');
}
@@ -43,9 +45,15 @@ try {
$stmt = $con->prepare("SET foreign_key_checks = 1");
$stmt->execute();
$categories = createCategories($con);
$material = createMaterials($con);
$color = createColors($con);
$brands = createBrands($con);
$brands = createBrands($faker, $con);
$folders = createFolders($faker, $con);
$contents = createContents($faker, $folders, $con);
$categories = createCategories($faker, $con);
echo "creating templates\n";
$template = new \Thelia\Model\Template();
@@ -63,16 +71,15 @@ try {
->setAttribute($color)
->save($con);
/*
$ft = new Thelia\Model\FeatureTemplate();
$ft
->setTemplate($template)
->setFeature($brand)
->setFeature($material)
->save($con);
*/
echo "end creating templates\n";
createProduct($faker, $categories, $brands, $template, $color, null, $con);
createProduct($faker, $categories, $brands, $contents, $template, $color, $material, $con);
$con->commit();
} catch (Exception $e) {
@@ -80,7 +87,7 @@ try {
$con->rollBack();
}
function createProduct($faker, $categories, $brands, $template, $attribute, $feature, $con)
function createProduct($faker, $categories, $brands, $contents, $template, $attribute, $feature, $con)
{
echo "start creating products\n";
$fileSystem = new \Symfony\Component\Filesystem\Filesystem();
@@ -90,15 +97,16 @@ function createProduct($faker, $categories, $brands, $template, $attribute, $fea
$row++;
if($row == 1) continue;
$product = new \Thelia\Model\Product();
$productCategories = explode(';', $data[13]);
$product
->setRef($data[0])
->setVisible(1)
->setTaxRuleId(1)
->setTemplate($template)
;
foreach ($productCategories as $productCategory) {
$productCategories = explode(';', $data[15]);
foreach ($productCategories as $productCategory) {
$productCategory = trim($productCategory);
if (array_key_exists($productCategory, $categories)) {
$product->addCategory($categories[$productCategory]);
@@ -184,39 +192,60 @@ function createProduct($faker, $categories, $brands, $template, $attribute, $fea
$productSaleElements = $product->getProductSaleElementss()->getFirst();
$productSaleElements->setIsDefault(1)->save($con);
/*
$brand = $data[11];
$featurAv = \Thelia\Model\FeatureAvI18nQuery::create()
->filterByLocale('en_US')
->filterByTitle($brand)
->findOne($con);
// associated content
$associatedContents = explode(";", $data[14]);
foreach ($associatedContents as $associatedContent) {
$content = new ProductAssociatedContent();
if ( ! array_key_exists($associatedContent, $contents)){
continue;
}
$featureProduct = new Thelia\Model\FeatureProduct();
$featureProduct->setProduct($product)
->setFeatureId($feature->getId())
->setFeatureAvId($featurAv->getId())
->save($con)
;
*/
$content
->setProduct($product)
->setContent($contents[$associatedContent])
->save($con)
;
}
// feature
$features = explode(";", $data[13]);
foreach ($features as $aFeature) {
$featurAv = \Thelia\Model\FeatureAvI18nQuery::create()
->filterByLocale('en_US')
->filterByTitle($aFeature)
->findOne($con);
$featureProduct = new Thelia\Model\FeatureProduct();
$featureProduct->setProduct($product)
->setFeatureId($feature->getId())
->setFeatureAvId($featurAv->getId())
->save($con)
;
}
}
}
echo "end creating products\n";
}
/*
function createBrand($con)
function createMaterials($con)
{
echo "start creating brands feature\n";
if (($handle = fopen(THELIA_ROOT . '/setup/import/brand.csv', "r")) !== FALSE) {
echo "start creating materials feature\n";
$feature = null;
$features = array();
if (($handle = fopen(THELIA_ROOT . '/setup/import/materials.csv', "r")) !== FALSE) {
$row=0;
$feature = new \Thelia\Model\Feature();
$feature
->setPosition(1)
->setLocale('fr_FR')
->setTitle('Marque')
->setTitle('Matière')
->setLocale('en_US')
->setTitle('Brand');
->setTitle('Material');
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$row++;
$featureAv = new \Thelia\Model\FeatureAv();
@@ -225,21 +254,23 @@ function createBrand($con)
->setLocale('fr_FR')
->setTitle($data[0])
->setLocale('en_US')
->setTitle($data[0]);
$feature->addFeatureAv($featureAv);
->setTitle($data[1]);
//$featureAv->setFeature($feature);
$feature->addFeatureAv($featureAv);
}
$feature->save($con);
fclose($handle);
}
echo "brands feature created successfully\n";
echo "materials feature created successfully\n";
return $feature;
}
*/
function createBrands($con)
function createBrands($faker, $con)
{
echo "start creating brands\n";
@@ -259,8 +290,12 @@ function createBrands($con)
->setPosition($row-1)
->setLocale('fr_FR')
->setTitle(trim($data[0]))
->setChapo($faker->text(20))
->setDescription($faker->text(100))
->setLocale('en_US')
->setTitle(trim($data[0]))
->setChapo($faker->text(20))
->setDescription($faker->text(100))
->save($con);
$brands[trim($data[0])] = $brand;
@@ -295,7 +330,7 @@ function createBrands($con)
}
function createCategories($con)
function createCategories($faker, $con)
{
echo "start creating categories\n";
$categories = array();
@@ -311,8 +346,12 @@ function createCategories($con)
->setParent(0)
->setLocale('fr_FR')
->setTitle(trim($data[0]))
->setChapo($faker->text(20))
->setDescription($faker->text(100))
->setLocale('en_US')
->setTitle(trim($data[1]))
->setChapo($faker->text(20))
->setDescription($faker->text(100))
->save($con);
$categories[trim($data[1])] = $category;
}
@@ -323,6 +362,116 @@ function createCategories($con)
return $categories;
}
function createFolders($faker, $con)
{
echo "start creating folders\n";
$fileSystem = new \Symfony\Component\Filesystem\Filesystem();
$folders = array();
if (($handle = fopen(THELIA_ROOT . '/setup/import/folders.csv', "r")) !== FALSE) {
$row=0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$row++;
if ($row == 1) continue;
$folder = new \Thelia\Model\Folder();
$folder
->setVisible(1)
->setPosition($row-1)
->setLocale('fr_FR')
->setTitle(trim($data[0]))
->setChapo(trim($data[2]) || $faker->text(20))
->setDescription(trim($data[4]) || $faker->text(100))
->setLocale('en_US')
->setTitle(trim($data[1]))
->setChapo(trim($data[3]) || $faker->text(20))
->setDescription(trim($data[5]) || $faker->text(100))
->save($con);
$folders[trim($data[1])] = $folder;
$images = explode(';', $data[6]);
foreach ($images as $image) {
$image = trim($image);
if(empty($image)) continue;
$folderImage = new \Thelia\Model\FolderImage();
$folderImage
->setFolderId($folder->getId())
->setFile($image)
->save($con);
$fileSystem->copy(THELIA_ROOT . 'setup/import/images/'.$image, THELIA_ROOT . 'local/media/images/folder/'.$image, true);
}
}
fclose($handle);
}
echo "Folders created successfully\n";
return $folders;
}
function createContents($faker, $folders, $con)
{
echo "start creating contents\n";
$fileSystem = new \Symfony\Component\Filesystem\Filesystem();
$contents = array();
if (($handle = fopen(THELIA_ROOT . '/setup/import/contents.csv', "r")) !== FALSE) {
$row=0;
while (($data = fgetcsv($handle, 1000, ";")) !== FALSE) {
$row++;
if ($row == 1) continue;
$content = new \Thelia\Model\Content();
$content
->setVisible(1)
->setPosition($row-1)
->setLocale('fr_FR')
->setTitle(trim($data[0]))
->setChapo(trim($data[2]))
->setDescription(trim($data[4]))
->setLocale('en_US')
->setTitle(trim($data[1]))
->setChapo(trim($data[3]))
->setDescription(trim($data[5]));
// folder
$contentFolders = explode(';', $data[7]);
foreach ($contentFolders as $contentFolder) {
$contentFolder = trim($contentFolder);
if (array_key_exists($contentFolder, $folders)) {
$content->addFolder($folders[$contentFolder]);
}
}
$content->save($con);
$images = explode(';', $data[6]);
foreach ($images as $image) {
$image = trim($image);
if(empty($image)) continue;
$contentImage = new \Thelia\Model\ContentImage();
$contentImage
->setContentId($content->getId())
->setFile($image)
->save($con);
$fileSystem->copy(THELIA_ROOT . 'setup/import/images/'.$image, THELIA_ROOT . 'local/media/images/content/'.$image, true);
}
$contents[trim($data[1])] = $content;
}
fclose($handle);
}
echo "Contents created successfully\n";
return $contents;
}
function createColors($con)
{
echo "start creating colors attributes\n";
@@ -408,6 +557,15 @@ function clearTables($con)
->find($con);
$attributeAv->delete($con);
$brand = Thelia\Model\BrandQuery::create()
->find($con);
$brand->delete($con);
$brand = Thelia\Model\BrandI18nQuery::create()
->find($con);
$brand->delete($con);
$category = Thelia\Model\CategoryQuery::create()
->find($con);
$category->delete($con);
@@ -424,6 +582,22 @@ function clearTables($con)
->find($con);
$product->delete($con);
$folder = Thelia\Model\FolderQuery::create()
->find($con);
$folder->delete($con);
$folder = Thelia\Model\FolderI18nQuery::create()
->find($con);
$folder->delete($con);
$content = Thelia\Model\ContentQuery::create()
->find($con);
$content->delete($con);
$content = Thelia\Model\ContentI18nQuery::create()
->find($con);
$content->delete($con);
$accessory = Thelia\Model\AccessoryQuery::create()
->find($con);
$accessory->delete($con);