Class humhub\modules\file\widgets\Upload

Inheritancehumhub\modules\file\widgets\Upload » humhub\components\Widget » yii\base\Widget
Available since version1.3.5

Base upload utility component which combines upload input/button, preview and progress components.

An UploadInput serves as core upload component and is optionally linked to a progress and preview widget. An UploadButton will wrap the input widget in a button and provides some further configurations e.g. to for styling and labels.

If the postState flag is set to true (default), this component will try to fetch submitted files and auto attach them as input fields to the surrounding form. This will preserve the upload state after failed form validations.

This behavior may be deactivated or requires manual form cleanup in case the success result also contains the same upload component. In such a case the reset flag can be activated in order to exclude all files no matter of the model or post state.

Example:

Model:

class MyModel extends ActiveRecord
{
     public $myFiles;

     public function rules()
     {
         return [
              [['files'], 'safe']
         ]
     }

     public function afterSave($insert, $changedAttributes)
     {
         $this->fileManager->attach($this->myFiles);
         parent::afterSave($insert, $changedAttributes);
     }
}

Controller:

public actionEdit($id)
{
    $model = MyModel::findOne(['id' => '$id']);
    if($model->load(Yii::$app->request->post()) && $model->save()) {
          $this->view->success();
          return $this->redirect(...);
    }

    return $this->render(['model' => $model]);
}

View:

// Create initial upload component
$upload = Upload::forModel($model, $attribute);


// Output upload button with additional settings
echo $upload->button(['label' => true]);

// Output upload progress bar widget
echo $upload->progress();

// Output upload file preview widget
echo $upload->preview();

See also:

Public Properties

Hide inherited properties

PropertyTypeDescriptionDefined By
$attribute string Model attribute name containing files humhub\modules\file\widgets\Upload
$max integer|boolean Max allowed file upload, if set to true a default attachment maximum will be used humhub\modules\file\widgets\Upload
$model \yii\base\Model The model the file array should be humhub\modules\file\widgets\Upload
$name string Upload input name humhub\modules\file\widgets\Upload
$postState boolean Defines weather or not the component will auto attach posted file inputs to the form fetched from the a post request. humhub\modules\file\widgets\Upload
$render boolean If set to false this widget won't be rendered humhub\components\Widget
$reset boolean If set to true the upload component won't include any any file no matter of the model or post state humhub\modules\file\widgets\Upload
$submitName string Defines the input name of file items attached to the form after the upload humhub\modules\file\widgets\Upload
$url string Can be set to overwrite default file upload url humhub\modules\file\widgets\Upload
$widgetLayout string Defines an optional layout humhub\components\Widget

Public Methods

Hide inherited methods

MethodDescriptionDefined By
button() Renders a humhub\modules\file\widgets\UploadButton widget for this upload. humhub\modules\file\widgets\Upload
create() Static initializer humhub\modules\file\widgets\Upload
forModel() Static initializer for model based forms. humhub\modules\file\widgets\Upload
getLayoutViewParams() Returns an array of view parameter used if \humhub\components\layout is set. humhub\components\Widget
init() humhub\modules\file\widgets\Upload
input() Renders a humhub\modules\file\widgets\UploadInput widgets for this upload. humhub\modules\file\widgets\Upload
postState() Used to define the postState flag which manages the input attachment of posted files. humhub\modules\file\widgets\Upload
preview() Renders a preview component for this upload. humhub\modules\file\widgets\Upload
process() Process is a wrapper for the run method humhub\components\Widget
progress() Renders a progress bar component for this upload. humhub\modules\file\widgets\Upload
reset() If condition is true, this upload component won't include any files for the input and preview component. humhub\modules\file\widgets\Upload
submitName() humhub\modules\file\widgets\Upload
uploadName() humhub\modules\file\widgets\Upload
widget() Creates a widget instance and runs it. humhub\components\Widget
withName() Static initializer for simple form name based uploads humhub\modules\file\widgets\Upload

Protected Methods

Hide inherited methods

MethodDescriptionDefined By
getPreviewFiles() Assembles files for the preview widget. humhub\modules\file\widgets\Upload

Events

Hide inherited events

EventTypeDescriptionDefined By
EVENT_CREATE humhub\libs\WidgetCreateEvent An event raised before creating a widget. humhub\components\Widget

Constants

Hide inherited constants

ConstantValueDescriptionDefined By
DEFAULT_ATTRIBUTE_NAME 'files' humhub\modules\file\widgets\Upload
DEFAULT_SUBMIT_NAME 'fileList[]' humhub\modules\file\widgets\Upload
DEFAULT_UPLOAD_NAME 'files[]' humhub\modules\file\widgets\Upload

Property Details

$attribute public property

Model attribute name containing files

public string $attribute null
$max public property

Max allowed file upload, if set to true a default attachment maximum will be used

public integer|boolean $max true
$model public property

The model the file array should be

public \yii\base\Model $model null
$name public property

Upload input name

public string $name null
$postState public property

Defines weather or not the component will auto attach posted file inputs to the form fetched from the a post request.

public boolean $postState true
$reset public property

If set to true the upload component won't include any any file no matter of the model or post state

public boolean $reset false
$submitName public property

Defines the input name of file items attached to the form after the upload

public string $submitName null
$url public property

Can be set to overwrite default file upload url

public string $url null

Method Details

button() public method
public string button ( $cfg = [] )
$cfg array
throws Exception
create() public static method

Static initializer

public static static|object create ( $cfg = [] )
$cfg array
throws \yii\base\InvalidConfigException
forModel() public static method

Static initializer for model based forms.

public static static forModel ( $model, $attribute self::DEFAULT_ATTRIBUTE_NAME, $cfg = [] )
$model
$attribute string
$cfg array
throws \yii\base\InvalidConfigException
getPreviewFiles() protected method

Assembles files for the preview widget.

protected array getPreviewFiles ( $showInStream null )
$showInStream null
init() public method

public void init ( )
input() public method

Renders a humhub\modules\file\widgets\UploadInput widgets for this upload.

The input widget doesn't have to be rendered manually if the result of button() was already included.

See also humhub\modules\file\widgets\UploadInput.

public string input ( $cfg = [] )
$cfg array
throws Exception
postState() public method

Used to define the postState flag which manages the input attachment of posted files.

If set to true (default) the upload component will try to fetch uploaded files from the request and attach input fields to the surrounding form.

public $this postState ( $postState true )
$postState boolean
preview() public method

Renders a preview component for this upload.

The resulting preview widget will have an id of the following form:

<upload_id>_preview

See also humhub\modules\file\widgets\FilePreview.

public string preview ( $cfg = [] )
$cfg array
throws Exception
progress() public method

Renders a progress bar component for this upload.

The resulting progress bar widget will have an id of the following form:

<upload_id>_progress

See also humhub\modules\file\widgets\UploadProgress.

public string progress ( $cfg = [] )
$cfg array
throws Exception
reset() public method

If condition is true, this upload component won't include any files for the input and preview component.

public void reset ( $condition true )
$condition boolean
submitName() public method

public $this submitName ( $submitName self::DEFAULT_SUBMIT_NAME )
$submitName
uploadName() public method

public $this uploadName ( $uploadName self::DEFAULT_UPLOAD_NAME )
$uploadName
withName() public static method

Static initializer for simple form name based uploads

public static static withName ( $submitName self::DEFAULT_SUBMIT_NAME, $uploadName self::DEFAULT_UPLOAD_NAME, $cfg = [] )
$submitName
$uploadName string|array
$cfg array
throws \yii\base\InvalidConfigException