Finalized images action and loop
This commit is contained in:
@@ -1,4 +1,9 @@
|
||||
<?php
|
||||
use Thelia\Model\ProductImage;
|
||||
use Propel\Runtime\Exception\PropelException;
|
||||
use Thelia\Model\CategoryImage;
|
||||
use Thelia\Model\FolderImage;
|
||||
use Thelia\Model\ContentImage;
|
||||
require __DIR__ . '/../core/bootstrap.php';
|
||||
|
||||
$thelia = new Thelia\Core\Thelia("dev", true);
|
||||
@@ -10,8 +15,37 @@ $con->beginTransaction();
|
||||
|
||||
$currency = \Thelia\Model\CurrencyQuery::create()->filterByCode('EUR')->findOne();
|
||||
|
||||
function generate_image($image, $position, $typeobj, $id) {
|
||||
|
||||
global $faker;
|
||||
|
||||
$image
|
||||
->setTitle($faker->text(20))
|
||||
->setDescription($faker->text(250))
|
||||
->setChapo($faker->text(40))
|
||||
->setPostscriptum($faker->text(40))
|
||||
->setPosition($position)
|
||||
->setFile(sprintf("sample-image-%s.png", $id))
|
||||
->save()
|
||||
;
|
||||
|
||||
// Generate images
|
||||
$image_data = file_get_contents("http://placehold.it/320x200&text=Image+for+$typeobj+ID+".$id);
|
||||
$image_file = sprintf("%s/../local/media/images/%s/sample-image-%s.png", __DIR__, $typeobj, $id);
|
||||
|
||||
if (! is_dir(dirname($image_file))) mkdir(dirname($image_file), 0777, true);
|
||||
|
||||
if ($fh = fopen($image_file, "w")) {
|
||||
fwrite($fh, $image_data);
|
||||
fclose($fh);
|
||||
}
|
||||
}
|
||||
|
||||
try {
|
||||
|
||||
$stmt = $con->prepare("SET foreign_key_checks = 0");
|
||||
$stmt->execute();
|
||||
|
||||
$category = Thelia\Model\CategoryQuery::create()
|
||||
->find();
|
||||
$category->delete();
|
||||
@@ -50,6 +84,9 @@ try {
|
||||
->find();
|
||||
$content->delete();
|
||||
|
||||
$stmt = $con->prepare("SET foreign_key_checks = 1");
|
||||
$stmt->execute();
|
||||
|
||||
//first category
|
||||
$sweet = new Thelia\Model\Category();
|
||||
$sweet->setParent(0);
|
||||
@@ -60,6 +97,10 @@ try {
|
||||
|
||||
$sweet->save();
|
||||
|
||||
$image = new CategoryImage();
|
||||
$image->setCategoryId($sweet->getId());
|
||||
generate_image($image, 1, 'category', $sweet->getId());
|
||||
|
||||
//second category
|
||||
$jeans = new Thelia\Model\Category();
|
||||
$jeans->setParent(0);
|
||||
@@ -70,6 +111,10 @@ try {
|
||||
|
||||
$jeans->save();
|
||||
|
||||
$image = new CategoryImage();
|
||||
$image->setCategoryId($jeans->getId());
|
||||
generate_image($image, 2, 'category', $jeans->getId());
|
||||
|
||||
//third category
|
||||
$other = new Thelia\Model\Category();
|
||||
$other->setParent($jeans->getId());
|
||||
@@ -80,6 +125,10 @@ try {
|
||||
|
||||
$other->save();
|
||||
|
||||
$image = new CategoryImage();
|
||||
$image->setCategoryId($other->getId());
|
||||
generate_image($image, 3, 'category', $other->getId());
|
||||
|
||||
for ($i=1; $i <= 5; $i++) {
|
||||
$product = new \Thelia\Model\Product();
|
||||
$product->addCategory($sweet);
|
||||
@@ -102,8 +151,12 @@ try {
|
||||
$productPrice->setProductSaleElements($stock);
|
||||
$productPrice->setCurrency($currency);
|
||||
$productPrice->setPrice($faker->randomFloat(2, 20, 2500));
|
||||
|
||||
$productPrice->save();
|
||||
|
||||
$image = new ProductImage();
|
||||
$image->setProductId($product->getId());
|
||||
generate_image($image, $i, 'product', $product->getId());
|
||||
}
|
||||
|
||||
for ($i=1; $i <= 5; $i++) {
|
||||
@@ -130,6 +183,10 @@ try {
|
||||
$productPrice->setPrice($faker->randomFloat(2, 20, 2500));
|
||||
$productPrice->save();
|
||||
|
||||
$image = new ProductImage();
|
||||
$image->setProductId($product->getId());
|
||||
generate_image($image, $i, 'product', $product->getId());
|
||||
|
||||
}
|
||||
|
||||
//folders and contents
|
||||
@@ -143,6 +200,10 @@ try {
|
||||
|
||||
$folder->save();
|
||||
|
||||
$image = new FolderImage();
|
||||
$image->setFolderId($folder->getId());
|
||||
generate_image($image, $i, 'folder', $folder->getId());
|
||||
|
||||
for($j=0; $j<rand(0, 4); $j++) {
|
||||
$subfolder = new Thelia\Model\Folder();
|
||||
$subfolder->setParent($folder->getId());
|
||||
@@ -153,6 +214,10 @@ try {
|
||||
|
||||
$subfolder->save();
|
||||
|
||||
$image = new FolderImage();
|
||||
$image->setFolderId($subfolder->getId());
|
||||
generate_image($image, $j, 'folder', $subfolder->getId());
|
||||
|
||||
for($k=0; $k<rand(1, 5); $k++) {
|
||||
$content = new Thelia\Model\Content();
|
||||
$content->addFolder($subfolder);
|
||||
@@ -162,6 +227,11 @@ try {
|
||||
$content->setDescription($faker->text(255));
|
||||
|
||||
$content->save();
|
||||
|
||||
$image = new ContentImage();
|
||||
$image->setContentId($content->getId());
|
||||
generate_image($image, $k, 'content', $content->getId());
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -188,8 +258,13 @@ try {
|
||||
}
|
||||
|
||||
$con->commit();
|
||||
} catch (Exception $e) {
|
||||
echo "error : ".$e->getMessage()."\n";
|
||||
}
|
||||
catch (PropelException $pe) {
|
||||
echo "Propel error: ".$pe->getMessage()."\n".$pe->getTraceAsString();
|
||||
$con->rollBack();
|
||||
}
|
||||
catch (Exception $e) {
|
||||
echo "error occured : ".$e->getMessage()."\n".$e->getTraceAsString();
|
||||
$con->rollBack();
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user