Skip to content

Commit

Permalink
Add image upload for training, trainer and place
Browse files Browse the repository at this point in the history
  • Loading branch information
TomasVotruba committed Dec 26, 2018
1 parent c1555b1 commit 97d6347
Show file tree
Hide file tree
Showing 16 changed files with 132 additions and 58 deletions.
2 changes: 2 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,5 @@ projects/open-training/public/bundles/
projects/open-training/public/build/*/glyphicons-*

.env

public/uploads/images
1 change: 1 addition & 0 deletions projects/open-training/config/packages/twig.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ twig:

globals:
training_max_references_to_show: '%training_max_references_to_show%'
image_uploads: '%image_uploads%'

# see https://symfony.com/blog/new-in-symfony-2-7-default-date-and-number-format-configuration
date:
Expand Down
6 changes: 3 additions & 3 deletions projects/open-training/config/packages/vich_uploader.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ vich_uploader:
db_driver: 'orm'

mappings:
lecture_images:
uri_prefix: '%training_image_path%'
upload_destination: '%kernel.project_dir%/public%training_image_path%'
image_uploads:
uri_prefix: '%image_uploads%'
upload_destination: '%kernel.project_dir%/public%image_uploads%'
2 changes: 1 addition & 1 deletion projects/open-training/config/services.yaml
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
parameters:
locale: 'cs'
training_max_references_to_show: 3
training_image_path: "/uploads/images/trainings"
image_uploads: "/uploads/images/"

services:
_defaults:
Expand Down
Original file line number Diff line number Diff line change
@@ -1,20 +1,33 @@
parameters:
training_grid_fields:
- 'name'
- { property: 'image', type: 'image', base_path: '%training_image_path%' }
- { property: 'image', type: 'image', base_path: '%image_uploads%' }
- 'perex'
- 'description'
- 'capacity'
- 'price'
- 'place'
- 'trainer'

trainer_grid_fields:
- 'name'
- { property: 'image', type: 'image', base_path: '%image_uploads%' }
- 'email'
- 'phone'
- 'website'
- 'bio'

place_grid_fields:
- 'name'
- { property: 'image', type: 'image', base_path: '%image_uploads%' }
- 'description'
- 'mapUrl'

easy_admin:
entities:
Training:
label: 'Školení'
class: 'OpenTraining\Training\Entity\Training'

list:
fields: "%training_grid_fields%"
show:
Expand All @@ -33,17 +46,35 @@ easy_admin:
TrainingTerm:
label: 'Termíny školení'
class: 'OpenTraining\Training\Entity\TrainingTerm'
form:
exclude_fields: ['registrations', 'slug']

Trainer:
label: 'Školitelé'
class: 'OpenTraining\Training\Entity\Trainer'
list:
fields: '%trainer_grid_fields%'
show:
fields: '%trainer_grid_fields%'
form:
exclude_fields: ['trainings']
fields:
- 'name'
- { property: 'imageFile', type: 'file' }
- 'email'
- 'phone'
- 'website'
- 'bio'
Place:
label: 'Školící místa'
class: 'OpenTraining\Training\Entity\Place'
list:
fields: '%place_grid_fields%'
show:
fields: '%place_grid_fields%'
form:
fields:
- 'name'
- { property: 'imageFile', type: 'file' }
- 'description'
- 'mapUrl'

Reference:
label: 'Zpětné vazby'
Expand Down
5 changes: 5 additions & 0 deletions projects/open-training/packages/Training/src/Entity/Place.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@
namespace OpenTraining\Training\Entity;

use Doctrine\ORM\Mapping as ORM;
use OpenTraining\Entity\UploadableImageTrait;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
* @ORM\Entity
* @Vich\Uploadable
*/
class Place
{
use UploadableImageTrait;

/**
* @ORM\Id()
* @ORM\GeneratedValue()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,17 @@
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\Common\Collections\Collection;
use Doctrine\ORM\Mapping as ORM;
use OpenTraining\Entity\UploadableImageTrait;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
* @ORM\Entity
* @Vich\Uploadable
*/
class Trainer
{
use UploadableImageTrait;

/**
* @ORM\Id()
* @ORM\GeneratedValue()
Expand Down
46 changes: 2 additions & 44 deletions projects/open-training/packages/Training/src/Entity/Training.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@

namespace OpenTraining\Training\Entity;

use DateTime;
use DateTimeInterface;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
use Gedmo\Timestampable\Traits\Timestampable;
use Symfony\Component\HttpFoundation\File\File;
use OpenTraining\Entity\UploadableImageTrait;
use Vich\UploaderBundle\Mapping\Annotation as Vich;

/**
Expand All @@ -17,7 +15,7 @@
*/
class Training
{
use Timestampable;
use UploadableImageTrait;

/**
* @ORM\Id()
Expand All @@ -40,12 +38,6 @@ class Training
*/
private $name;

/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $image;

/**
* @ORM\Column(type="text")
* @var string
Expand Down Expand Up @@ -76,12 +68,6 @@ class Training
*/
private $price;

/**
* @Vich\UploadableField(mapping="training_images", fileNameProperty="image")
* @var File
*/
private $imageFile;

/**
* @ORM\ManyToOne(targetEntity="OpenTraining\Training\Entity\Place")
* @var Place
Expand Down Expand Up @@ -278,32 +264,4 @@ public function getNearestTermSlug(): ?string

return $this->getNearestTerm()->getSlug();
}

public function setImageFile(?File $file = null): void
{
$this->imageFile = $file;

// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($file) {
// if 'updatedAt' is not defined in your entity, use another property
$this->updatedAt = new DateTime('now');
}
}

public function getImageFile(): ?File
{
return $this->imageFile;
}

public function setImage(?string $image): void
{
$this->image = $image;
}

public function getImage(): ?string
{
return $this->image;
}
}
Binary file removed projects/open-training/public/apple-touch-icon.png
Binary file not shown.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
68 changes: 68 additions & 0 deletions projects/open-training/src/Entity/UploadableImageTrait.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
<?php declare(strict_types=1);

namespace OpenTraining\Entity;

use DateTime;
use Doctrine\ORM\Mapping as ORM;
use Symfony\Component\HttpFoundation\File\File;

trait UploadableImageTrait
{
/**
* @ORM\Column(type="string", length=255)
* @var string
*/
private $image;

/**
* @Vich\UploadableField(mapping="image_uploads", fileNameProperty="image")
* @var File
*/
private $imageFile;

/**
* This needs to be changed on file upload, so the image changes.
*
* @ORM\Column(type="datetime", nullable=true)
* @var DateTime
*/
private $imageUploadedAt;

public function getImage(): ?string
{
return $this->image;
}

public function setImage(?string $image): void
{
$this->image = $image;
}

public function getImageFile(): ?File
{
return $this->imageFile;
}

public function setImageFile(?File $file = null): void
{
$this->imageFile = $file;

// VERY IMPORTANT:
// It is required that at least one field changes if you are using Doctrine,
// otherwise the event listeners won't be called and the file is lost
if ($file) {
// if 'updatedAt' is not defined in your entity, use another property
$this->imageUploadedAt = new DateTime('now');
}
}

public function getImageUploadedAt(): ?DateTime
{
return $this->imageUploadedAt;
}

public function setImageUploadedAt(DateTime $imageUploadedAt): void
{
$this->imageUploadedAt = $imageUploadedAt;
}
}
3 changes: 1 addition & 2 deletions projects/open-training/templates/default/homepage.html.twig
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,7 @@
Welcome to lecture

<ul>
<li><a href="{{ url('provision') }}">Provision compute</a></li>
<li><a href="{{ url('trainings') }}">Trainings</a></li>
<li><a href="{{ url('trainings') }}">Vzdělávej se</a></li>
<li><a href="/admin/">Admin</a></li>
<li><a href="/adminer?sqlite=&username=&db=%2Fvar%2Fwww%2Fopen-lectures%2Fvar%2Fdatabase.sqlite">Database</a></li>
</ul>
Expand Down
11 changes: 8 additions & 3 deletions projects/open-training/templates/training/default.twig
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,14 @@
</h2>

<div class="text-center pt-xl-5 pb-xl-5 border-secondary border mb-3 mt-3">
{#<img width="120" src="/assets/images/people/{{ training.getTrainerPhoto() }}" class="img-thumbnail mr-4 rounded-circle pull-right">#}

{#<img src="{{ training.getImage() }}" width="150">#}
{% if training.getImage() %}
<img src="{{ image_uploads }}/{{ training.getImage() }}" width="150">
{% endif %}

{% set trainer = training.getTrainer() %}
{% if trainer.getImage() %}
<img width="120" src="{{ image_uploads }}/{{ trainer.getImage() }}" class="img-thumbnail ml-5 rounded-circle pull-right">
{% endif %}
</div>

{% if training.getPerex() %}
Expand Down

0 comments on commit 97d6347

Please sign in to comment.