Minor code improvements

This commit is contained in:
Sam Georges 2014-09-06 21:46:19 +10:00
parent fa0ada51c6
commit 91a7ea9da2
1 changed files with 15 additions and 12 deletions

View File

@ -64,24 +64,27 @@ class FileUpload extends FormWidgetBase
$this->vars['imageWidth'] = $this->imageWidth;
}
protected function getColumn()
/**
* Returns the attachment relation object from the model,
* supports nesting via HTML array.
* @return Relation
*/
protected function getRelationObject()
{
$model = $this->model;
$columnName = $this->columnName;
$relations = Str::evalHtmlArray($columnName);
$last_relation = array_pop($relations);
$relations = Str::evalHtmlArray($this->columnName);
$lastField = array_pop($relations);
while ( $relations )
{
$relation = array_shift($relations);
$model = $model->$relation;
foreach ($relations as $relation) {
$model = $model->{$relation};
}
return $model->$last_relation();
return $model->$lastField();
}
protected function getFileList()
{
$list = $this->getColumn()->withDeferred($this->sessionKey)->orderBy('sort_order')->get();
$list = $this->getRelationObject()->withDeferred($this->sessionKey)->orderBy('sort_order')->get();
/*
* Set the thumb for each file
@ -112,7 +115,7 @@ class FileUpload extends FormWidgetBase
public function onRemoveAttachment()
{
if (($file_id = post('file_id')) && ($file = File::find($file_id))) {
$this->getColumn()->remove($file, $this->sessionKey);
$this->getRelationObject()->remove($file, $this->sessionKey);
}
}
@ -203,7 +206,7 @@ class FileUpload extends FormWidgetBase
if (!$uploadedFile->isValid())
throw new SystemException('File is not valid');
$fileRelation = $this->getColumn();
$fileRelation = $this->getRelationObject();
$file = new File();
$file->data = $uploadedFile;