Jump to content

Uploading images to custom Widgets


Brew Digital

Recommended Posts

I'm in the process of creating some custom widgets. I'd like to be able to upload images to the widget.

So far I've managed to get an image upload working that creates a smaller thumbnail to be used by the widget. 

The problem I have is that I can't figure out how to remove the previously uploaded image before replacing it with a newly uploaded one.

Below is what I have so far, any advice on how to handle this would be much appreciated. 

/**
 * notification Widget
 */
class _notification extends \IPS\Widget\StaticCache
{
    /**
     * @brief    Widget Key
     */
    public $key = 'notification';

    /**
     * @brief    App
     */
    public $app = 'pslwidget';

    /**
     * @brief    Plugin
     */
    public $plugin = '';

    /**
     * Initialise this widget
     *
     * @return void
     */
    public function init()
    {
        parent::init();
    }

    /**
     * Specify widget configuration
     *
     * @param null|\IPS\Helpers\Form $form Form object
     * @return    null|\IPS\Helpers\Form
     */
    public function configuration(&$form = null)
    {
        /** @var _Form $form */
        $form = parent::configuration($form);
        $form->add(new \IPS\Helpers\Form\Upload(
            'widget_field',
            isset($this->configuration['widget_field'])
                ? \IPS\File::get('core_Application', $this->configuration['widget_field']['url']['path'])
                : null, true,
            ['storageExtension' => 'core_Application']));
        $form->add(new \IPS\Helpers\Form\Text('title',
            isset($this->configuration['title'])
                ? $this->configuration['title']
                : ''
        ));

        return $form;
    }

    /**
     * Ran before saving widget configuration
     *
     * @param array $values Values from form
     * @return    array
     */
    public function preConfig($values)
    {
        if (isset($values['widget_field'])) {
            /** @var _File $temp */
            $temp = $values['widget_field'];
            $values['widget_field'] = $temp->thumbnail('core_Application', 400);
            $temp->delete();
        }

        return $values;
    }

    /**
     * Render a widget
     *
     * @return    string
     */
    public function render()
    {
        $title = isset($this->configuration['title']) ? $this->configuration['title'] : null;
        $image = isset($this->configuration['widget_field']) ? $this->configuration['widget_field'] : null;
        if (!$image) {
            return $this->output($title, '#');
        }

        return $this->output($title, $image["url"]["data"]["path"]);
    }
}

 

Link to comment
Share on other sites

Archived

This topic is now archived and is closed to further replies.

  • Recently Browsing   0 members

    • No registered users viewing this page.
×
×
  • Create New...