Symfony alternate new action

人走茶凉 提交于 2020-01-05 07:34:48

问题


I have a server where I need to store some images. Now the images can be either uploaded or created on the fly let us say just by adding some text to some default picture(So I made a file MakenewForm.php for that) . The table in my database stores filename on the local filesystem. Now upload is simple, I can just use the default _new action for that. For creating picture, I made a new action, say makenew. new and makenew both are diplayed on the list view. I copied newSuccess.php to makenewSuccess.php. Now I want a different set of submit buttons for them. I can't seem to figure out how to do that. All I see is this:

<div id="sf_admin_content">
<?php include_partial('poster/form', array('poster' => $poster, 'form' => $form,       'configuration' => $configuration, 'helper' => $helper)) ?>
</div>

I don't know what is $configuration and what is $helper. Can someone tell me about them? Which one do I need to change and How?

Also, as you can infer, the submit action of the new action only needs to do a $form->save() but the submit action of makenew needs to take all the text input and write an image file(let's say using imagejpeg)

I need some pointers towards achieving this.


回答1:


I hope to well focus about what you are looking for.

About $configuration and $helper, as you can find in cache\mypplication\dev\modules\autoMymodule\actions\actions.class.php the function preExecute() is used also to create them:

$this->configuration = new mymoduleGeneratorConfiguration();
$this->helper = new mymoduleGeneratorHelper();

These classes are in apps\docdesk\modules\mymodule\lib. You can find base classes in cache\mypplication\dev\modules\autoMymodule\lib. Have a look to these base classes to understand how are used and so which configuration functions and helper functions the admin generator makes available.

To deal with your new action, I don't know the feature you're developing so I'll try to imagine two possible scenarios. You can use only a form switching your functions of upload/create picture trough a custom widget, overriding the template _form.php, called in your snippet, and the new action and so on or, this is the way you're following, create a completely separate action, form and all needed templates. So in your makenewSucces.php you can include a template _makeform.php called with

<?php include_partial('poster/makeform', array('poster' => $poster, 'form' => $form, 'configuration' => $configuration, 'helper' => $helper)) ?>

Of course you have to put the template _makeform.php in apps\docdesk\modules\mymodule\template as each new or overrided one.

I don't well understand your trouble about the image saving... I suppose you're asking for the saving chain of an image and how and where you can manage what you need. To add the widget to upload an image inside your PosterForm class you can use a snippet like this where we suppose photo as widget name, according with a schema field photo: { type: varchar(255), required: true }, that is of course to customize:

    public function configure()
    {
        $photo = $this->getObject()->getPhoto(); // get the photo name
        $photo = sfConfig::get('sf_upload_image_dir').$photo;

        $this->widgetSchema['photo'] = new sfWidgetFormInputFileEditable(array(
            'label'     => 'Photo',
            'file_src'  => $photo,
            'is_image'  => true,
            'edit_mode' => !$this->isNew(),
            'delete_label' => 'check to remove',
            'template'  => '<div>%input%<br/><br/>%file%<br/><br/>%delete%<p>%delete_label%<br/><p></div>'
        ));

        $this->validatorSchema['photo'] = new sfValidatorFile(array(
            'required'   => true,
            'path'            => sfConfig::get('sf_upload_image_dir'),
            'mime_categories' => array('web_images' => array(
                'image/jpeg',
                'image/pjpeg',
                'image/png',
                'image/x-png'
            )),
            'mime_types' => 'web_images',
        ));

        $this->validatorSchema['photo_delete'] = new sfValidatorPass();
    }

Note that my setting for upload_image_dir is %SF_UPLOAD_DIR%/images/

Symfony will upload and save the image for you!

Then you can override the doSave function, according to your needs, putting the same one yet in your PosterForm class:

protected function doSave($con = null)
{
    $delete = $this->getValue('photo_delete');
    if ( $delete )
    {
        // ...
    }

    $upload = $this->getValue('photo');
    if ( $upload )
    {
        // ...
    }
    return parent::doSave($con);
}

Finally to remove your image, that is the file, when you delete the object, that is the db record where the image name is a field, you have to put this code in your model class Poster:

public function delete(PropelPDO $con = null) // using Propel
{
    $photo = sfConfig::get('sf_upload_image_dir').$this->getPhoto();
    if ( file_exists($photo) )
        unlink($photo);

    return parent::delete($con);
}

I hope this can help you.



来源:https://stackoverflow.com/questions/8445441/symfony-alternate-new-action

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!