Issues (17)

src/Extension/Admin/PageHasMedia.php (1 issue)

Labels
Severity
1
<?php
2
3
namespace PiedWeb\CMSBundle\Extension\Admin;
4
5
use Sonata\AdminBundle\Admin\AbstractAdmin;
6
use Sonata\AdminBundle\Datagrid\ListMapper;
7
use Sonata\AdminBundle\Form\FormMapper;
8
use Sonata\AdminBundle\Form\Type\ModelListType;
9
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
10
11
class PageHasMedia extends AbstractAdmin
12
{
13
    private $liipImage;
14
15
    public function setLiipImage($liipImage)
16
    {
17
        $this->liipImage = $liipImage;
18
    }
19
20
    protected function getMedialHelp($media)
21
    {
22
        if (! ($media && $media->getMedia() && false !== strpos($media->getMimeType(), 'image/'))) {
23
            return null;
24
        }
25
26
        $fullPath = '/'.$media->getRelativeDir().'/'.$media->getMedia();
27
28
        $editUrl = $this->routeGenerator->generate('admin_app_media_edit', ['id' => $media->getId()]);
0 ignored issues
show
The method generate() does not exist on null. ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-call  annotation

28
        /** @scrutinizer ignore-call */ 
29
        $editUrl = $this->routeGenerator->generate('admin_app_media_edit', ['id' => $media->getId()]);

This check looks for calls to methods that do not seem to exist on a given type. It looks for the method on the type itself as well as in inherited classes or implemented interfaces.

This is most likely a typographical error or the method has been renamed.

Loading history...
29
        $thumbUrl = $this->liipImage->getBrowserPath($fullPath, 'thumb');
30
        $defaultUrl = $this->liipImage->getBrowserPath($fullPath, 'default');
31
32
        $help = '<a href="'.$editUrl.'" target=_blank>';
33
        $help .= '<img src="'.$thumbUrl.'" style="width:100%; max-width:300px">';
34
        $help .= '</a>';
35
        $help .= '<pre onclick="copyElementText(this);" class="btn"';
36
        $help .= ' style="font-size:80%;text-overflow:ellipsis;margin-top:10px;max-width:160px;white-space:nowrap;';
37
        $help .= 'overflow:hidden">';
38
        $help .= '!['.str_replace(['[', '"', ']'], ' ', $media->getName()).']('.$defaultUrl.')';
39
        $help .= '</pre>';
40
41
        return $help;
42
    }
43
44
    protected function configureFormFields(FormMapper $formMapper)
45
    {
46
        $media = $this->getSubject() ? $this->getSubject()->getMedia() : null;
47
48
        $formMapper
49
            ->add(
50
                'media',
51
                ModelListType::class,
52
                [
53
                'required' => false,
54
                'btn_delete' => false,
55
                'btn_edit' => false,
56
                'btn_add' => (! $media) ? ' ' : false,
57
                'btn_list' => (! $media) ? ' ' : false,
58
                'sonata_help' => $this->getMedialHelp($media),
59
                ]
60
            )
61
            ->add('position', HiddenType::class);
62
    }
63
64
    protected function configureListFields(ListMapper $listMapper)
65
    {
66
        $listMapper
67
            ->add('media')
68
            ->add('page');
69
    }
70
}
71