From df90aa0483bc9dcbcedfb648f21dc56111166331 Mon Sep 17 00:00:00 2001 From: angeluss Date: Tue, 17 Oct 2017 16:50:27 +0300 Subject: [PATCH 01/21] add-media. CRUD and filestack uploader --- .gitignore | 4 + .../Controller/Backend/MediaController.php | 113 ++++++++ src/Event/EventBundle/Entity/Media.php | 269 ++++++++++++++++++ .../Entity/Repository/MediaRepository.php | 11 + src/Event/EventBundle/Form/Type/MediaType.php | 48 ++++ src/Event/EventBundle/Menu/Builder.php | 1 + .../Resources/config/routing/backend.yml | 19 ++ .../views/Backend/Media/index.html.twig | 94 ++++++ .../views/Backend/Media/manage.html.twig | 56 ++++ .../Resources/views/Backend/layout.html.twig | 1 + web/uploads/.gitkeep | 0 11 files changed, 616 insertions(+) create mode 100644 src/Event/EventBundle/Controller/Backend/MediaController.php create mode 100644 src/Event/EventBundle/Entity/Media.php create mode 100644 src/Event/EventBundle/Entity/Repository/MediaRepository.php create mode 100644 src/Event/EventBundle/Form/Type/MediaType.php create mode 100644 src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig create mode 100644 src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig create mode 100644 web/uploads/.gitkeep diff --git a/.gitignore b/.gitignore index 51dccc8..cb125d2 100644 --- a/.gitignore +++ b/.gitignore @@ -15,3 +15,7 @@ app/config/parameters.yml composer.phar behat.yml +.idea +web/uploads/* +!web/uploads/.gitkeep +!web/uploads/media/.gitkeep \ No newline at end of file diff --git a/src/Event/EventBundle/Controller/Backend/MediaController.php b/src/Event/EventBundle/Controller/Backend/MediaController.php new file mode 100644 index 0000000..143c5bb --- /dev/null +++ b/src/Event/EventBundle/Controller/Backend/MediaController.php @@ -0,0 +1,113 @@ +get('kernel')->getRootDir() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . + 'web' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR; + } + + public function getUploadUrl(){ + return '/uploads/media/'; + } + + public function indexAction() + { + return $this->render('EventEventBundle:Backend/Media:index.html.twig', array( + 'media' => $this->getRepository('EventEventBundle:Media')->findAll(), + 'uploadPath' => $this->getUploadPath(), + 'uploadUrl' => $this->getUploadUrl() + )); + } + + public function manageAction(Request $request, $id = null) + { + $oldFileName = ''; + if ($id === null) { + $entity = new Media(); + } else { + $entity = $this->findOr404('EventEventBundle:Media', $id); + $oldFileName = $entity->getFilename(); + } + + $form = $this->createForm(MediaType::class, $entity); + + if ($request->getMethod() === 'POST') { + $form->handleRequest($request); + + if ($form->isValid()) { + if(is_null($entity->getId())){ + $entity->setCreatedDate(new \DateTime()); + } + + if($oldFileName !== $entity->getFilename()) { + $url = $request->request->get('file_url'); + $path = $this->getUploadPath(); + $entity->setFilename( time() . '_' . $entity->getFilename()); + $file = $this->getFile($url); + file_put_contents($path . $entity->getFilename(), $file); + chmod($path . $entity->getFilename(), 0777); + if(is_file($path . $oldFileName)) { + unlink($path . $oldFileName); + } + } + + $entity->setUpdatedDate(new \DateTime()); + $this->getManager()->persist($entity); + $this->getManager()->flush(); + + $successFlashText = sprintf('Media %s updated.', $entity->getTitle()); + if (!$id) { + $successFlashText = sprintf('Media %s added.', $entity->getTitle()); + } + $this->setSuccessFlash($successFlashText); + + return $this->redirectToRoute('backend_media'); + } + } + + return $this->render('EventEventBundle:Backend/Media:manage.html.twig', [ + 'media' => $entity, + 'form' => $form->createView(), +// 'configLocales' => $this->container->getParameter('media.locales'), + ]); + } + + protected function getFile($url){ + $ch = curl_init(); + curl_setopt($ch, CURLOPT_URL, $url); + curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); + $file = curl_exec($ch); + curl_close($ch); + return $file; + } + + public function deleteAction($id) + { + $this->isGrantedAdmin(); + + $entity = $this->findOr404('EventEventBundle:Media', $id); + $file = $this->getUploadPath() . $entity->getFilename(); + $this->getManager()->remove($entity); + $this->getManager()->flush(); + if(is_file($file)) { + unlink($file); + } + + $this->setSuccessFlash('Media deleted.'); + + return $this->redirectToRoute('backend_media'); + } + + +} diff --git a/src/Event/EventBundle/Entity/Media.php b/src/Event/EventBundle/Entity/Media.php new file mode 100644 index 0000000..5a81c52 --- /dev/null +++ b/src/Event/EventBundle/Entity/Media.php @@ -0,0 +1,269 @@ +filename; + } + + public function __construct() + { + + } + + /** + * Get id + * + * @return integer + */ + public function getId() + { + return $this->id; + } + + /** + * Set title + * + * @param string $title + * @return Media + */ + public function setTitle($title) + { + $this->title = $title; + + return $this; + } + + /** + * Get title + * + * @return string + */ + public function getTitle() + { + return $this->title; + } + + /** + * Set filename + * + * @param string $filename + * @return Media + */ + public function setFilename($filename) + { + $this->filename = $filename; + + return $this; + } + + /** + * Get filename + * + * @return string + */ + public function getFilename() + { + return $this->filename; + } + + /** + * Set description + * + * @param string $description + * @return Media + */ + public function setDescription($description) + { + $this->description = $description; + + return $this; + } + + /** + * Get description + * + * @return string + */ + public function getDescription() + { + return $this->description; + } + + /** + * Set copyrightInfo + * + * @param string $copyrightInfo + * @return Media + */ + public function setCopyrightInfo($copyrightInfo) + { + $this->copyrightInfo = $copyrightInfo; + + return $this; + } + + /** + * Get copyrightInfo + * + * @return string + */ + public function getCopyrightInfo() + { + return $this->copyrightInfo; + } + + /** + * Set mediaCredits + * + * @param string $mediaCredits + * @return Media + */ + public function setMediaCredits($mediaCredits) + { + $this->mediaCredits = $mediaCredits; + + return $this; + } + + /** + * Get mediaCredits + * + * @return string + */ + public function getMediaCredits() + { + return $this->mediaCredits; + } + + /** + * Set createdDate + * + * @param \DateTime $createdDate + * @return Media + */ + public function setCreatedDate($createdDate) + { + if (!$createdDate instanceOf \DateTime) { + $createdDate = \DateTime::createFromFormat('m/d/Y', $createdDate); + } + + $this->createdDate = $createdDate; + + return $this; + } + + /** + * Get createdDate + * + * @return \DateTime + */ + public function getCreatedDate() + { + return $this->createdDate; + } + + /** + * Set updatedDate + * + * @param \DateTime $updatedDate + * @return Media + */ + public function setUpdatedDate($updatedDate) + { + if (!$updatedDate instanceOf \DateTime) { + $updatedDate = \DateTime::createFromFormat('m/d/Y', $updatedDate); + } + + $this->updatedDate = $updatedDate; + + return $this; + } + + /** + * Get updatedDate + * + * @return \DateTime + */ + public function getUpdatedDate() + { + return $this->updatedDate; + } + + public function __toString() + { + return $this->title; + } +} diff --git a/src/Event/EventBundle/Entity/Repository/MediaRepository.php b/src/Event/EventBundle/Entity/Repository/MediaRepository.php new file mode 100644 index 0000000..fbd17d1 --- /dev/null +++ b/src/Event/EventBundle/Entity/Repository/MediaRepository.php @@ -0,0 +1,11 @@ +add('title', TextType::class) + ->add('filename', HiddenType::class, ['required' => false]) + ->add('description', TextareaType::class, [ + 'label' => 'Media Description', + 'attr' => array('class' => 'input-xxlarge', 'rows' => 10), + 'required' => false + ]) + ->add('copyrightInfo', TextareaType::class, [ + 'label' => 'Copyright Info', + 'attr' => array('class' => 'input-xxlarge', 'rows' => 10), + 'required' => false + ]) + ->add('mediaCredits', TextareaType::class, [ + 'label' => 'Media Credits', + 'attr' => array('class' => 'input-xxlarge', 'rows' => 10), + 'required' => false + ]) + ; + } + + public function configureOptions(OptionsResolver $resolver) + { + $resolver->setDefaults([ + 'data_class' => 'Event\EventBundle\Entity\Media' + ]); + } + + public function getBlockPrefix() + { + return 'media'; + } +} diff --git a/src/Event/EventBundle/Menu/Builder.php b/src/Event/EventBundle/Menu/Builder.php index d714cf4..ad41e31 100644 --- a/src/Event/EventBundle/Menu/Builder.php +++ b/src/Event/EventBundle/Menu/Builder.php @@ -34,6 +34,7 @@ public function sideBar(FactoryInterface $factory, array $options) $event->addChild('Sponsors', array('route' => 'backend_sponsor')); $event->addChild('Organizers', array('route' => 'backend_organizer')); $event->addChild('Calls For Paper', array('route' => 'backend_call_for_paper')); + $event->addChild('Media', array('route' => 'backend_media')); return $menu; } diff --git a/src/Event/EventBundle/Resources/config/routing/backend.yml b/src/Event/EventBundle/Resources/config/routing/backend.yml index ed61cb5..4568271 100644 --- a/src/Event/EventBundle/Resources/config/routing/backend.yml +++ b/src/Event/EventBundle/Resources/config/routing/backend.yml @@ -169,3 +169,22 @@ backend_call_for_paper_delete: id: \d+ options: expose: true + +# Media +backend_media: + path: /media + defaults: { _controller: EventEventBundle:Backend/Media:index } + +backend_media_add: + path: /media/add + defaults: { _controller: EventEventBundle:Backend/Media:manage } + +backend_media_edit: + path: /media/edit/{id} + defaults: { _controller: EventEventBundle:Backend/Media:manage, id: null } + requirements: { id: \d+ } + +backend_media_delete: + path: /media/delete/{id} + defaults: { _controller: EventEventBundle:Backend/Media:delete } + requirements: { id: \d+ } \ No newline at end of file diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig new file mode 100644 index 0000000..e572946 --- /dev/null +++ b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig @@ -0,0 +1,94 @@ +{% extends 'EventEventBundle:Backend:layout.html.twig' %} + +{% block content %} +
+ + + + + + + + + + + + + + + + {% for file in media %} + + + + + + + + + + {% else %} + + + + {% endfor %} + +
{{ 'ID'|trans }}{{ 'Thumbnail'|trans }}{{ 'Title'|trans }}{{ 'Description'|trans }}{{ 'Created'|trans }}{{ 'Updated'|trans }}{{ 'Actions'|trans }}
{{ file.id }} + no-img + + + {{ file.title }} + + {{ file.description }}{{ file.createdDate|date }}{{ file.updatedDate|date }} + +
No media found.
+
+{% endblock %} + +{% block javascripts %} + {{ parent() }} + + +{% endblock %} diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig new file mode 100644 index 0000000..93400af --- /dev/null +++ b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig @@ -0,0 +1,56 @@ +{% extends 'EventEventBundle:Backend:layout.html.twig' %} + +{% block content %} +
+ + + {{ form_start(form, {'method': 'POST', 'attr': {'id': 'media'}, 'action': path('backend_media_edit', {'id': media.id})}) }} +
+
+ {{ form_row(form.title) }} + {{ form_row(form.filename) }} +
+ + +
+ {{ form_row(form.description) }} + {{ form_row(form.copyrightInfo) }} + {{ form_row(form.mediaCredits) }} + +
+ + {{ form_rest(form) }} + + +
+ {{ form_end(form) }} + +
+{% endblock %} + +{% block javascripts %} + {{ parent() }} + + + +{% endblock %} \ No newline at end of file diff --git a/src/Event/EventBundle/Resources/views/Backend/layout.html.twig b/src/Event/EventBundle/Resources/views/Backend/layout.html.twig index 87407e2..e4888b1 100644 --- a/src/Event/EventBundle/Resources/views/Backend/layout.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/layout.html.twig @@ -39,6 +39,7 @@ {% for message in app.session.flashbag.get('success') %}
{{ message|trans }} + ×
{% endfor %} {% for message in app.session.flashbag.get('error') %} diff --git a/web/uploads/.gitkeep b/web/uploads/.gitkeep new file mode 100644 index 0000000..e69de29 From d3b39256c2ee0a92230e87f82b9f57c12e37909d Mon Sep 17 00:00:00 2001 From: angeluss Date: Tue, 17 Oct 2017 17:10:39 +0300 Subject: [PATCH 02/21] add back link and styles --- src/Event/EventBundle/Form/Type/MediaType.php | 6 +++--- src/Event/EventBundle/Resources/public/css/style.css | 10 ++++++++++ .../Resources/views/Backend/Media/manage.html.twig | 5 +++-- 3 files changed, 16 insertions(+), 5 deletions(-) diff --git a/src/Event/EventBundle/Form/Type/MediaType.php b/src/Event/EventBundle/Form/Type/MediaType.php index 2f7fa4e..97d72c8 100644 --- a/src/Event/EventBundle/Form/Type/MediaType.php +++ b/src/Event/EventBundle/Form/Type/MediaType.php @@ -18,17 +18,17 @@ public function buildForm(FormBuilderInterface $builder, array $options) ->add('filename', HiddenType::class, ['required' => false]) ->add('description', TextareaType::class, [ 'label' => 'Media Description', - 'attr' => array('class' => 'input-xxlarge', 'rows' => 10), + 'attr' => array('class' => 'input-xxlarge', 'rows' => 3), 'required' => false ]) ->add('copyrightInfo', TextareaType::class, [ 'label' => 'Copyright Info', - 'attr' => array('class' => 'input-xxlarge', 'rows' => 10), + 'attr' => array('class' => 'input-xxlarge', 'rows' => 3), 'required' => false ]) ->add('mediaCredits', TextareaType::class, [ 'label' => 'Media Credits', - 'attr' => array('class' => 'input-xxlarge', 'rows' => 10), + 'attr' => array('class' => 'input-xxlarge', 'rows' => 3), 'required' => false ]) ; diff --git a/src/Event/EventBundle/Resources/public/css/style.css b/src/Event/EventBundle/Resources/public/css/style.css index 982da99..23ec902 100644 --- a/src/Event/EventBundle/Resources/public/css/style.css +++ b/src/Event/EventBundle/Resources/public/css/style.css @@ -128,3 +128,13 @@ form input { .events-list { text-align: right; } + +/* backend media uploader */ +.uploader-container { + margin: 10px 0; +} + +#media_title { + width: 530px; +} +/* backend media uploader */ \ No newline at end of file diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig index 93400af..f93e972 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig @@ -4,6 +4,7 @@
{{ form_start(form, {'method': 'POST', 'attr': {'id': 'media'}, 'action': path('backend_media_edit', {'id': media.id})}) }} @@ -11,8 +12,8 @@
{{ form_row(form.title) }} {{ form_row(form.filename) }} -
- +
+
{{ form_row(form.description) }} From c5276ff52086e5817b1ccb355b393a22e5b4514b Mon Sep 17 00:00:00 2001 From: angeluss Date: Tue, 17 Oct 2017 17:11:57 +0300 Subject: [PATCH 03/21] remove desc column --- .../EventBundle/Resources/views/Backend/Media/index.html.twig | 2 -- 1 file changed, 2 deletions(-) diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig index e572946..d131d46 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig @@ -13,7 +13,6 @@ {{ 'ID'|trans }} {{ 'Thumbnail'|trans }} {{ 'Title'|trans }} - {{ 'Description'|trans }} {{ 'Created'|trans }} {{ 'Updated'|trans }} {{ 'Actions'|trans }} @@ -31,7 +30,6 @@ {{ file.title }} - {{ file.description }} {{ file.createdDate|date }} {{ file.updatedDate|date }} From ab7a33d2bfa8118b12dc317f8a6ba6eb87da94c8 Mon Sep 17 00:00:00 2001 From: angeluss Date: Tue, 17 Oct 2017 17:18:59 +0300 Subject: [PATCH 04/21] change index styles --- .../EventBundle/Resources/views/Backend/Media/index.html.twig | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig index d131d46..5c78462 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig @@ -12,7 +12,7 @@ {{ 'ID'|trans }} {{ 'Thumbnail'|trans }} - {{ 'Title'|trans }} + {{ 'Title'|trans }} {{ 'Created'|trans }} {{ 'Updated'|trans }} {{ 'Actions'|trans }} @@ -81,7 +81,6 @@ null, null, null, - null, { "bSearchable": false, "bSortable": false } ], "aaSorting": [[0, 'desc']] From a549fcdbcb6b6e6a89442362b213439724a3b0b6 Mon Sep 17 00:00:00 2001 From: angeluss Date: Wed, 18 Oct 2017 19:02:39 +0300 Subject: [PATCH 05/21] CS changes --- .../Controller/Backend/MediaController.php | 42 ++++++++----------- src/Event/EventBundle/Entity/Media.php | 9 ++-- .../views/Backend/Media/index.html.twig | 20 ++++----- .../views/Backend/Media/manage.html.twig | 5 +-- 4 files changed, 33 insertions(+), 43 deletions(-) diff --git a/src/Event/EventBundle/Controller/Backend/MediaController.php b/src/Event/EventBundle/Controller/Backend/MediaController.php index 143c5bb..a996028 100644 --- a/src/Event/EventBundle/Controller/Backend/MediaController.php +++ b/src/Event/EventBundle/Controller/Backend/MediaController.php @@ -13,12 +13,11 @@ class MediaController extends Controller public function getUploadPath() { - return $this->get('kernel')->getRootDir() . DIRECTORY_SEPARATOR . '..' . DIRECTORY_SEPARATOR . - 'web' . DIRECTORY_SEPARATOR . 'uploads' . DIRECTORY_SEPARATOR . 'media' . DIRECTORY_SEPARATOR; + return $this->container->getParameter('media.uploadPath'); } public function getUploadUrl(){ - return '/uploads/media/'; + return $this->container->getParameter('media.uploadUrl'); } public function indexAction() @@ -34,41 +33,38 @@ public function manageAction(Request $request, $id = null) { $oldFileName = ''; if ($id === null) { - $entity = new Media(); + $media = new Media(); } else { - $entity = $this->findOr404('EventEventBundle:Media', $id); - $oldFileName = $entity->getFilename(); + $media = $this->findOr404('EventEventBundle:Media', $id); + $oldFileName = $media->getFilename(); } - $form = $this->createForm(MediaType::class, $entity); + $form = $this->createForm(MediaType::class, $media); if ($request->getMethod() === 'POST') { $form->handleRequest($request); if ($form->isValid()) { - if(is_null($entity->getId())){ - $entity->setCreatedDate(new \DateTime()); - } - if($oldFileName !== $entity->getFilename()) { + + if ($oldFileName !== $media->getFilename()) { $url = $request->request->get('file_url'); $path = $this->getUploadPath(); - $entity->setFilename( time() . '_' . $entity->getFilename()); + $media->setFilename(time() . '_' . $media->getFilename()); $file = $this->getFile($url); - file_put_contents($path . $entity->getFilename(), $file); - chmod($path . $entity->getFilename(), 0777); - if(is_file($path . $oldFileName)) { + file_put_contents($path . $media->getFilename(), $file); + chmod($path . $media->getFilename(), 0777); + if (is_file($path . $oldFileName)) { unlink($path . $oldFileName); } } - $entity->setUpdatedDate(new \DateTime()); - $this->getManager()->persist($entity); + $this->getManager()->persist($media); $this->getManager()->flush(); - $successFlashText = sprintf('Media %s updated.', $entity->getTitle()); + $successFlashText = sprintf('Media %s updated.', $media->getTitle()); if (!$id) { - $successFlashText = sprintf('Media %s added.', $entity->getTitle()); + $successFlashText = sprintf('Media %s added.', $media->getTitle()); } $this->setSuccessFlash($successFlashText); @@ -77,9 +73,8 @@ public function manageAction(Request $request, $id = null) } return $this->render('EventEventBundle:Backend/Media:manage.html.twig', [ - 'media' => $entity, + 'media' => $media, 'form' => $form->createView(), -// 'configLocales' => $this->container->getParameter('media.locales'), ]); } @@ -89,6 +84,7 @@ protected function getFile($url){ curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); $file = curl_exec($ch); curl_close($ch); + return $file; } @@ -100,7 +96,7 @@ public function deleteAction($id) $file = $this->getUploadPath() . $entity->getFilename(); $this->getManager()->remove($entity); $this->getManager()->flush(); - if(is_file($file)) { + if (is_file($file)) { unlink($file); } @@ -108,6 +104,4 @@ public function deleteAction($id) return $this->redirectToRoute('backend_media'); } - - } diff --git a/src/Event/EventBundle/Entity/Media.php b/src/Event/EventBundle/Entity/Media.php index 5a81c52..dedf441 100644 --- a/src/Event/EventBundle/Entity/Media.php +++ b/src/Event/EventBundle/Entity/Media.php @@ -73,14 +73,13 @@ class Media * @ORM\Column(name="updated_date", type="datetime") */ private $updatedDate; - - public function getUploadImg(){ - return '/uploads/media/' . $this->filename; - } public function __construct() { - + if (is_null($this->getId())){ + $this->setCreatedDate(new \DateTime()); + } + $this->setUpdatedDate(new \DateTime()); } /** diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig index 5c78462..814cf01 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig @@ -23,7 +23,7 @@ {{ file.id }} - no-img + no-img @@ -69,21 +69,21 @@ if ( 0 < elementsCount) { $('#media-table').dataTable({ - "iDisplayLength": 50, - "sDom": "<'row'<'span7'l><'span2'f>r>t<'row'<'span3'i><'span6'p>>", - "sPaginationType": "bootstrap", - "oLanguage": { - "sLengthMenu": "_MENU_ records per page" + 'iDisplayLength': 50, + 'sDom': "<'row'<'span7'l><'span2'f>r>t<'row'<'span3'i><'span6'p>>", + 'sPaginationType': 'bootstrap', + 'oLanguage': { + 'sLengthMenu': '_MENU_ records per page' }, - "aoColumns": [ + 'aoColumns': [ null, - { "bSearchable": false, "bSortable": false }, + { 'bSearchable': false, 'bSortable': false }, null, null, null, - { "bSearchable": false, "bSortable": false } + { 'bSearchable': false, 'bSortable': false } ], - "aaSorting": [[0, 'desc']] + 'aaSorting': [[0, 'desc']] }); } }); diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig index f93e972..c1fd9b3 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig @@ -39,15 +39,12 @@ var client = filestack.init('A3w1aZwZmQBibC09rAcZnz'); function showPicker() { client.pick({ - accept: 'image/*', + accept: 'image/*' }).then(function(result) { var file = result.filesUploaded[0]; -// var file = JSON.stringify(result.filesUploaded); - console.log(file); $('#media_filename').val(file.filename); $('#file_url').val(file.url); - console.log(file.filename); if($('#media_title').val() === ''){ $('#media_title').val(file.filename); } From 8a2f92808d41703e22c8d7ac73394d7af161536e Mon Sep 17 00:00:00 2001 From: angeluss Date: Thu, 19 Oct 2017 15:50:52 +0300 Subject: [PATCH 06/21] fixes --- .../Controller/Backend/MediaController.php | 5 ++- src/Event/EventBundle/Entity/Media.php | 44 +++++++++---------- .../Resources/config/routing/backend.yml | 2 +- .../views/Backend/Media/index.html.twig | 4 +- .../views/Backend/Media/manage.html.twig | 2 +- 5 files changed, 29 insertions(+), 28 deletions(-) diff --git a/src/Event/EventBundle/Controller/Backend/MediaController.php b/src/Event/EventBundle/Controller/Backend/MediaController.php index a996028..db851a4 100644 --- a/src/Event/EventBundle/Controller/Backend/MediaController.php +++ b/src/Event/EventBundle/Controller/Backend/MediaController.php @@ -45,7 +45,7 @@ public function manageAction(Request $request, $id = null) $form->handleRequest($request); if ($form->isValid()) { - + $media->setUpdated(new \DateTime()); if ($oldFileName !== $media->getFilename()) { $url = $request->request->get('file_url'); @@ -78,7 +78,8 @@ public function manageAction(Request $request, $id = null) ]); } - protected function getFile($url){ + protected function getFile($url) + { $ch = curl_init(); curl_setopt($ch, CURLOPT_URL, $url); curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); diff --git a/src/Event/EventBundle/Entity/Media.php b/src/Event/EventBundle/Entity/Media.php index dedf441..a508544 100644 --- a/src/Event/EventBundle/Entity/Media.php +++ b/src/Event/EventBundle/Entity/Media.php @@ -65,21 +65,21 @@ class Media * * @ORM\Column(name="created_date", type="datetime") */ - private $createdDate; + private $created; /** * @var \DateTime * * @ORM\Column(name="updated_date", type="datetime") */ - private $updatedDate; + private $updated; public function __construct() { if (is_null($this->getId())){ - $this->setCreatedDate(new \DateTime()); + $this->setCreated(new \DateTime()); } - $this->setUpdatedDate(new \DateTime()); + $this->setUpdated(new \DateTime()); } /** @@ -208,57 +208,57 @@ public function getMediaCredits() } /** - * Set createdDate + * Set created * - * @param \DateTime $createdDate + * @param \DateTime $created * @return Media */ - public function setCreatedDate($createdDate) + public function setCreated($created) { - if (!$createdDate instanceOf \DateTime) { - $createdDate = \DateTime::createFromFormat('m/d/Y', $createdDate); + if (!$created instanceOf \DateTime) { + $created = \DateTime::createFromFormat('m/d/Y', $created); } - $this->createdDate = $createdDate; + $this->created = $created; return $this; } /** - * Get createdDate + * Get created * * @return \DateTime */ - public function getCreatedDate() + public function getCreated() { - return $this->createdDate; + return $this->created; } /** - * Set updatedDate + * Set updated * - * @param \DateTime $updatedDate + * @param \DateTime $updated * @return Media */ - public function setUpdatedDate($updatedDate) + public function setUpdated($updated) { - if (!$updatedDate instanceOf \DateTime) { - $updatedDate = \DateTime::createFromFormat('m/d/Y', $updatedDate); + if (!$updated instanceOf \DateTime) { + $updated = \DateTime::createFromFormat('m/d/Y', $updated); } - $this->updatedDate = $updatedDate; + $this->updated = $updated; return $this; } /** - * Get updatedDate + * Get updated * * @return \DateTime */ - public function getUpdatedDate() + public function getUpdated() { - return $this->updatedDate; + return $this->updated; } public function __toString() diff --git a/src/Event/EventBundle/Resources/config/routing/backend.yml b/src/Event/EventBundle/Resources/config/routing/backend.yml index 4568271..04bdec7 100644 --- a/src/Event/EventBundle/Resources/config/routing/backend.yml +++ b/src/Event/EventBundle/Resources/config/routing/backend.yml @@ -187,4 +187,4 @@ backend_media_edit: backend_media_delete: path: /media/delete/{id} defaults: { _controller: EventEventBundle:Backend/Media:delete } - requirements: { id: \d+ } \ No newline at end of file + requirements: { id: \d+ } diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig index 814cf01..c09f9f1 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/index.html.twig @@ -30,8 +30,8 @@ {{ file.title }} - {{ file.createdDate|date }} - {{ file.updatedDate|date }} + {{ file.created|date }} + {{ file.updated|date }}
diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig index c1fd9b3..fefe6cb 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig @@ -51,4 +51,4 @@ }); } -{% endblock %} \ No newline at end of file +{% endblock %} From cacf24dbad01fa50f3be87f155cdb996da1abdac Mon Sep 17 00:00:00 2001 From: angeluss Date: Thu, 19 Oct 2017 16:58:17 +0300 Subject: [PATCH 07/21] add guzzle client --- .../EventBundle/Controller/Backend/MediaController.php | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/src/Event/EventBundle/Controller/Backend/MediaController.php b/src/Event/EventBundle/Controller/Backend/MediaController.php index db851a4..8c6879a 100644 --- a/src/Event/EventBundle/Controller/Backend/MediaController.php +++ b/src/Event/EventBundle/Controller/Backend/MediaController.php @@ -2,6 +2,7 @@ namespace Event\EventBundle\Controller\Backend; +use Guzzle\Http\Client; use Symfony\Component\Config\Definition\Exception\Exception; use Symfony\Component\HttpFoundation\Request; use Event\EventBundle\Controller\Controller; @@ -80,11 +81,8 @@ public function manageAction(Request $request, $id = null) protected function getFile($url) { - $ch = curl_init(); - curl_setopt($ch, CURLOPT_URL, $url); - curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); - $file = curl_exec($ch); - curl_close($ch); + $client = new Client(); + $file = $client->createRequest('GET', $url)->send()->getBody(true); return $file; } From a0b73047e0d21c55e22738966abd0f8ba7b62fc7 Mon Sep 17 00:00:00 2001 From: angeluss Date: Thu, 19 Oct 2017 18:40:57 +0300 Subject: [PATCH 08/21] use filesystem component for upload file --- .../Controller/Backend/MediaController.php | 17 +++++++++++++---- 1 file changed, 13 insertions(+), 4 deletions(-) diff --git a/src/Event/EventBundle/Controller/Backend/MediaController.php b/src/Event/EventBundle/Controller/Backend/MediaController.php index 8c6879a..d8b876f 100644 --- a/src/Event/EventBundle/Controller/Backend/MediaController.php +++ b/src/Event/EventBundle/Controller/Backend/MediaController.php @@ -4,6 +4,8 @@ use Guzzle\Http\Client; use Symfony\Component\Config\Definition\Exception\Exception; +use Symfony\Component\Filesystem\Filesystem; +use Symfony\Component\Filesystem\Exception\IOExceptionInterface; use Symfony\Component\HttpFoundation\Request; use Event\EventBundle\Controller\Controller; use Event\EventBundle\Entity\Media; @@ -53,10 +55,17 @@ public function manageAction(Request $request, $id = null) $path = $this->getUploadPath(); $media->setFilename(time() . '_' . $media->getFilename()); $file = $this->getFile($url); - file_put_contents($path . $media->getFilename(), $file); - chmod($path . $media->getFilename(), 0777); - if (is_file($path . $oldFileName)) { - unlink($path . $oldFileName); + + $fs = new Filesystem(); + + try { + $fs->dumpFile($path . $media->getFilename(), $file, 0777); + } catch (IOExceptionInterface $e) { + throw new Exception($e->getMessage()); + } + + if ($oldFileName !== '' && $fs->exists($path . $oldFileName)) { + $fs->remove($path . $oldFileName); } } From 140b42d720b2b8755d6dd5b386fc3e219bb5df24 Mon Sep 17 00:00:00 2001 From: angeluss Date: Thu, 19 Oct 2017 18:54:13 +0300 Subject: [PATCH 09/21] add fs_api parameter --- app/config/parameters.yml.dist | 4 ++++ src/Event/EventBundle/Controller/Backend/MediaController.php | 1 + .../Resources/views/Backend/Media/manage.html.twig | 2 +- 3 files changed, 6 insertions(+), 1 deletion(-) diff --git a/app/config/parameters.yml.dist b/app/config/parameters.yml.dist index b989851..4bc4227 100644 --- a/app/config/parameters.yml.dist +++ b/app/config/parameters.yml.dist @@ -21,3 +21,7 @@ parameters: from_email: bot@email.com admin_password: admin + + media.uploadPath: %kernel.root_dir%/../web/uploads/media/ + media.uploadUrl: /uploads/media/ + filestack.api_key: A3w1aZwZmQBibC09rAcZnz diff --git a/src/Event/EventBundle/Controller/Backend/MediaController.php b/src/Event/EventBundle/Controller/Backend/MediaController.php index d8b876f..fa64c2b 100644 --- a/src/Event/EventBundle/Controller/Backend/MediaController.php +++ b/src/Event/EventBundle/Controller/Backend/MediaController.php @@ -85,6 +85,7 @@ public function manageAction(Request $request, $id = null) return $this->render('EventEventBundle:Backend/Media:manage.html.twig', [ 'media' => $media, 'form' => $form->createView(), + 'fs_api' => $this->container->getParameter('filestack.api_key'), ]); } diff --git a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig index fefe6cb..7b531cc 100644 --- a/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig +++ b/src/Event/EventBundle/Resources/views/Backend/Media/manage.html.twig @@ -36,7 +36,7 @@