ruian / RuianUploadifyBundle

Add Uploadify support for symfony2
21 stars 8 forks source link

configure with SonataAdmin #1

Closed Sgnx closed 12 years ago

Sgnx commented 12 years ago

I tried to implement it but without success.

How should function configureFormFields(FormMapper $formMapper) in EntityAdmin class and coresponding controller look like ?

ruian commented 12 years ago

Can you expose some code of your project ?

Sgnx commented 12 years ago
public function configureFormFields(FormMapper $formMapper)
{
       $formMapper
            ->add('title', NULL, array (), array ())
            ->add('description1', NULL, array('required' => false,  'attr' => array('class' => 'tinymce')))
            ->add('description2', NULL, array('required' => false,  'attr' => array('class' => 'tinymce')))
            ->add('www', NULL, array (), array ())
            ->add('fromdate', NULL, array (), array ())
            ->add('todate', NULL, array (), array ())
       ->add('image', 'hidden', array( 'attr' => array('id' => 'image')))
            ->add('image_uploadify', new RessourceType(), array('attr' => array('id' => 'image_uploadify')))
            ->add('position', NULL, array (), array ())
            ->add('createdAt', NULL , array('required' => false, 'widget' => 'single_text', 'attr' => array('class' =>'datepicker')))
            ->add('updatedAt', NULL, array (), array ())
            ->add('slug', NULL, array (), array ())
        ;
    }

in Entity News.php

    protected $image;

    protected $image_uploadify;

    function __construct()
    {
        $this->image_uploadify = new Ressource();
    }

/**
     * @param string $image
     * @return void
     */
    public function setImage($image)
    {
        $this->image = $image;
    }

    /**
     * @return string
     */
    public function getImage()
    {
        return $this->image;
    }

    /**
     * @param string $image_file
     * @return void
     */
    public function setImageUploadify($image_uploadify)
    {
        $this->image_uploadify = $image_uploadify;
    }

    /**
     * @return string
     */
    public function getImageUploadify()
    {
        return $this->image_uploadify;
    }

Controller:

class NewsAdminController extends Controller
{
    public function uploadAction()
    {
        $request = $this->get('request');

        $entity = new Ressource();

        $entity->setFolder($request->request->get('folder'));
        $entity->setFile($request->files->get('Filedata'));

        $entity->upload();

        $response = new Response(json_encode($entity->toArray()));
        $response->headers->set('Content-Type', 'application/json; charset=UTF-8');
        return $response;
    }
}

in layout:

<script type="text/javascript">

                $('#image').attr('val', response.folder+response.file);
//set the value of id_of_input_avatar with the uploadify response with filename and folder
$('#image_uploadify').uploadify({
    'uploader'  : '/bundles/ruianuploadify/swf/uploadify.swf',
    'script'    : $('#image_uploadify').attr('data-path'),
    'folder'    : $('#image_uploadify').attr('data-folder'),
    'scriptData'   : {
        '_uploadify' : true,
        '_session' : $('#image_uploadify').attr('data-session'),
    },
    'cancelImg' : '/bundles/ruianuploadify/images/cancel.png',
    'auto'      : true,
    'onComplete'  : function(event, ID, fileObj, response, data) {
        response = jQuery.parseJSON(response);
        $('#avatar-preview').html("<img src='"+response.folder+response.file+"' />");
        $('#image').attr('val', response.folder+response.file);
    }
  });
</script>

Is this configuration going in a good way ?

Sgnx commented 12 years ago

Error: Please define a type for field image_uploadify in Acme\AdminBundle\Admin\NewsAdmin

ruian commented 12 years ago

Did you import namespace from RuianUploadifyBundle in your Acme\AdminBundle\Admin\NewsAdmin ?

And you have some typo on your layout take this one for now.

<script>
$('#image_uploadify').uploadify({
    'uploader'  : '/bundles/ruianuploadify/swf/uploadify.swf',
    'script'    : $('#image_uploadify').attr('data-path'),
    'folder'    : $('#image_uploadify').attr('data-folder'),
    'scriptData'   : {
        '_uploadify' : true,
        '_session' : $('#image_uploadify').attr('data-session'),
    },
    'cancelImg' : '/bundles/ruianuploadify/images/cancel.png',
    'auto'      : true,
    'onComplete'  : function(event, ID, fileObj, response, data) {
        response = jQuery.parseJSON(response);
        $('#image-preview').html("<img src='"+response.folder+response.file+"' />");
        $('#image').attr('val', response.folder+response.file);
    }
  });
</script>
Sgnx commented 12 years ago

in NewsAdmin i have: use Ruian\UploadifyBundle\Form\RessourceType;

ruian commented 12 years ago

can you give more details on your error ? logs etc..

Sgnx commented 12 years ago

Error: Stack Trace in /home/platne/sgnx/public_html/vendor/bundles/Sonata/DoctrineORMAdminBundle/Builder/FormContractor.php at line 60

} if (!$fieldDescription->getType()) { throw new \RuntimeException(sprintf('Please define a type for field %s in %s', $fieldDescription->getName(), get_class($admin))); } $fieldDescription->setAdmin($admin); at FormContractor ->fixFieldDescription (object(NewsAdmin), object(FieldDescription), array()) in /home/platne/sgnx/public_html/vendor/bundles/Sonata/AdminBundle/Form/FormMapper.php at line 97

at FormMapper ->add ('image_uploadify', object(RessourceType), array('attr' => array('id' => 'image_uploadify'))) in /home/platne/sgnx/public_html/src/Acme/AdminBundle/Admin/NewsAdmin.php at line 49

at NewsAdmin ->configureFormFields (object(FormMapper)) in /home/platne/sgnx/public_html/vendor/bundles/Sonata/AdminBundle/Admin/Admin.php at line 1097

at Admin ->defineFormBuilder (object(FormBuilder)) in /home/platne/sgnx/public_html/vendor/bundles/Sonata/AdminBundle/Admin/Admin.php at line 1084

at Admin ->getFormBuilder () in /home/platne/sgnx/public_html/vendor/bundles/Sonata/AdminBundle/Admin/Admin.php at line 690

at Admin ->buildForm () in /home/platne/sgnx/public_html/vendor/bundles/Sonata/AdminBundle/Admin/Admin.php at line 1139

at Admin ->getForm () in /home/platne/sgnx/public_html/vendor/bundles/Sonata/AdminBundle/Controller/CRUDController.php at line 239

at CRUDController ->editAction ('16') at call_user_func_array (array(object(NewsAdminController), 'editAction'), array('16')) in kernel.root_dir/cache/dev/classes.php at line 4175

at HttpKernel ->handleRaw (object(Request), '1') in kernel.root_dir/cache/dev/classes.php at line 4139

at HttpKernel ->handle (object(Request), '1', true) in kernel.root_dir/cache/dev/classes.php at line 5119

at HttpKernel ->handle (object(Request), '1', true) in kernel.root_dir/bootstrap.php.cache at line 564

at Kernel ->handle (object(Request)) in /home/platne/sgnx/public_html/web/app_dev.php at line 25

Log: Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". Notified event "kernel.request" to listener "Ruian\UploadifyBundle\Listener\RequestListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". Matched route "admin_acme_demo_news_edit" (parameters: "_controller": "Acme\AdminBundle\Controller\NewsAdminController::editAction", "_sonata_admin": "acme.adminbundle.admin.news", "_sonata_name": "admin_acme_demo_news_edit", "id": "16", "_route": "admin_acme_demo_news_edit") Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". Read SecurityContext from the session Reloading user from user provider. SET NAMES UTF8 ([]) SELECT t0.username AS username1, t0.username_canonical AS username_canonical2, t0.email AS email3, t0.email_canonical AS email_canonical4, t0.enabled AS enabled5, t0.salt AS salt6, t0.password AS password7, t0.last_login AS last_login8, t0.locked AS locked9, t0.expired AS expired10, t0.expires_at AS expires_at11, t0.confirmation_token AS confirmation_token12, t0.password_requested_at AS password_requested_at13, t0.roles AS roles14, t0.credentials_expired AS credentials_expired15, t0.credentials_expire_at AS credentials_expire_at16, t0.id AS id17, t0.firstname AS firstname18, t0.lastname AS lastname19, t0.facebookId AS facebookId20 FROM fos_user t0 WHERE t0.username_canonical = ? LIMIT 1 (["admin"]) Username "admin" was reloaded from user provider. Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". Notified event "kernel.request" to listener "SimpleThings\EntityAudit\Request\CurrentUserListener::handle". Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::onKernelController". Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". Notified event "kernel.controller" to listener "Acme\DemoBundle\ControllerListener::onKernelController". SELECT t0.id AS id1, t0.title AS title2, t0.description1 AS description13, t0.description2 AS description24, t0.www AS www5, t0.fromdate AS fromdate6, t0.todate AS todate7, t0.publish AS publish8, t0.position AS position9, t0.created_at AS created_at10, t0.updated_at AS updated_at11, t0.slug AS slug12 FROM news t0 WHERE t0.id = ? (["16"]) Notified event "kernel.exception" to listener "Symfony\Component\Security\Http\Firewall\ExceptionListener::onKernelException". Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelException". Notified event "kernel.exception" to listener "Symfony\Component\HttpKernel\EventListener\ExceptionListener::onKernelException". RuntimeException: Please define a type for field image_uploadify in Acme\AdminBundle\Admin\NewsAdmin (uncaught exception) at /home/platne/sgnx/public_html/vendor/bundles/Sonata/DoctrineORMAdminBundle/Builder/FormContractor.php line 60 Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\ProfilerListener::onKernelRequest". Notified event "kernel.request" to listener "Ruian\UploadifyBundle\Listener\RequestListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Bundle\FrameworkBundle\EventListener\SessionListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\RouterListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Component\HttpKernel\EventListener\LocaleListener::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Component\Security\Http\Firewall::onKernelRequest". Notified event "kernel.request" to listener "Symfony\Bundle\AsseticBundle\EventListener\RequestListener::onKernelRequest". Notified event "kernel.request" to listener "SimpleThings\EntityAudit\Request\CurrentUserListener::handle". Notified event "kernel.controller" to listener "Symfony\Bundle\FrameworkBundle\DataCollector\RequestDataCollector::onKernelController". Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ControllerListener::onKernelController". Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\ParamConverterListener::onKernelController". Notified event "kernel.controller" to listener "Sensio\Bundle\FrameworkExtraBundle\EventListener\TemplateListener::onKernelController". Notified event "kernel.controller" to listener "Acme\DemoBundle\ControllerListener::onKernelController".

ruian commented 12 years ago

Please update your build of this bundle to the latest. I merged an update to make ressourceType has a service. So now you should use "uploadify_ressource" instead new RessourceType() in your FormBuilder or formMapper.

Example:

public function configureFormFields(FormMapper $formMapper)
{
       $formMapper
            ->add('title', NULL, array (), array ())
            ->add('description1', NULL, array('required' => false,  'attr' => array('class' => 'tinymce')))
            ->add('description2', NULL, array('required' => false,  'attr' => array('class' => 'tinymce')))
            ->add('www', NULL, array (), array ())
            ->add('fromdate', NULL, array (), array ())
            ->add('todate', NULL, array (), array ())
       ->add('image', 'hidden', array( 'attr' => array('id' => 'image')))
            ->add('image_uploadify', 'uploadify_ressource', array('attr' => array('id' => 'image_uploadify')))
            ->add('position', NULL, array (), array ())
            ->add('createdAt', NULL , array('required' => false, 'widget' => 'single_text', 'attr' => array('class' =>'datepicker')))
            ->add('updatedAt', NULL, array (), array ())
            ->add('slug', NULL, array (), array ())
        ;
    }