Updating modules/backend/formwidgets

This commit is contained in:
Stefan Talen 2014-10-10 23:50:05 +02:00
parent ae1c9e95c2
commit f29151100b
7 changed files with 84 additions and 47 deletions

View File

@ -118,5 +118,4 @@ class CodeEditor extends FormWidgetBase
$this->addJs('vendor/ace/ace.js', 'core');
$this->addJs('js/codeeditor.js', 'core');
}
}
}

View File

@ -99,16 +99,19 @@ class DataGrid extends FormWidgetBase
{
$methodName = 'get'.studly_case($this->fieldName).'AutocompleteValues';
if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getGridAutocompleteValues'))
if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getGridAutocompleteValues')) {
throw new ApplicationException('Model :model does not contain a method getGridAutocompleteValues()');
}
if ($this->model->methodExists($methodName))
if ($this->model->methodExists($methodName)) {
$result = $this->model->$methodName($field, $value, $data);
else
} else {
$result = $this->model->getGridAutocompleteValues($this->fieldName, $field, $value, $data);
}
if (!is_array($result))
if (!is_array($result)) {
$result = [];
}
return $result;
}
@ -122,16 +125,19 @@ class DataGrid extends FormWidgetBase
{
$methodName = 'get'.studly_case($this->fieldName).'DataSourceValues';
if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getGridDataSourceValues'))
if (!$this->model->methodExists($methodName) && !$this->model->methodExists('getGridDataSourceValues')) {
throw new ApplicationException('Model :model does not contain a method getGridDataSourceValues()');
}
if ($this->model->methodExists($methodName))
if ($this->model->methodExists($methodName)) {
$result = $this->model->$methodName();
else
} else {
$result = $this->model->getGridDataSourceValues($this->fieldName);
}
if (!is_array($result))
if (!is_array($result)) {
$result = [];
}
return $result;
}

View File

@ -60,10 +60,11 @@ class Datepicker extends FormWidgetBase
$value = $this->getLoadData();
if ($this->mode != 'datetime' && $value) {
if (is_string($value))
if (is_string($value)) {
$value = substr($value, 0, 10);
elseif (is_object($value))
} elseif (is_object($value)) {
$value = $value->toDateString();
}
}
$this->vars['value'] = $value ?: '';
@ -92,4 +93,4 @@ class Datepicker extends FormWidgetBase
{
return strlen($value) ? $value : null;
}
}
}

View File

@ -39,7 +39,10 @@ class FileUpload extends FormWidgetBase
{
$this->imageHeight = $this->getConfig('imageHeight', 100);
$this->imageWidth = $this->getConfig('imageWidth', 100);
$this->previewNoFilesMessage = $this->getConfig('previewNoFilesMessage', 'backend::lang.form.preview_no_files_message');
$this->previewNoFilesMessage = $this->getConfig(
'previewNoFilesMessage',
'backend::lang.form.preview_no_files_message'
);
$this->checkUploadPostback();
}
@ -87,8 +90,9 @@ class FileUpload extends FormWidgetBase
{
$mode = $this->getConfig('mode', 'image');
if (str_contains($mode, '-'))
if (str_contains($mode, '-')) {
return $mode;
}
$relationType = $this->getRelationType();
$mode .= ($relationType == 'attachMany' || $relationType == 'morphMany') ? '-multi' : '-single';
@ -171,8 +175,7 @@ class FileUpload extends FormWidgetBase
}
throw new SystemException('Unable to find file, it may no longer exist');
}
catch (Exception $ex) {
} catch (Exception $ex) {
return json_encode(['error' => $ex->getMessage()]);
}
}
@ -200,8 +203,9 @@ class FileUpload extends FormWidgetBase
*/
protected function checkUploadPostback()
{
if (!($uniqueId = post('X_OCTOBER_FILEUPLOAD')) || $uniqueId != $this->getId())
if (!($uniqueId = post('X_OCTOBER_FILEUPLOAD')) || $uniqueId != $this->getId()) {
return;
}
try {
$uploadedFile = Input::file('file_data');
@ -209,19 +213,22 @@ class FileUpload extends FormWidgetBase
$isImage = starts_with($this->getDisplayMode(), 'image');
$validationRules = ['max:'.File::getMaxFilesize()];
if ($isImage)
if ($isImage) {
$validationRules[] = 'mimes:jpg,jpeg,bmp,png,gif';
}
$validation = Validator::make(
['file_data' => $uploadedFile],
['file_data' => $validationRules]
);
if ($validation->fails())
if ($validation->fails()) {
throw new ValidationException($validation);
}
if (!$uploadedFile->isValid())
if (!$uploadedFile->isValid()) {
throw new SystemException('File is not valid');
}
$fileRelation = $this->getRelationObject();
@ -235,11 +242,10 @@ class FileUpload extends FormWidgetBase
$file->thumb = $file->getThumb($this->imageWidth, $this->imageHeight, ['mode' => 'crop']);
$result = $file;
}
catch (Exception $ex) {
} catch (Exception $ex) {
$result = json_encode(['error' => $ex->getMessage()]);
}
die($result);
}
}
}

View File

@ -84,11 +84,21 @@ class RecordFinder extends FormWidgetBase
$this->nameFrom = $this->getConfig('nameFrom', $this->nameFrom);
$this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom);
/* @todo Remove line if year >= 2015 */ if ($this->getConfig('nameColumn')) $this->nameFrom = $this->getConfig('nameColumn');
/* @todo Remove line if year >= 2015 */ if ($this->getConfig('descriptionColumn')) $this->descriptionFrom = $this->getConfig('descriptionColumn');
/* @todo Remove lines if year >= 2015 */
if ($this->getConfig('nameColumn')) {
$this->nameFrom = $this->getConfig('nameColumn');
}
/* @todo Remove lines if year >= 2015 */
if ($this->getConfig('descriptionColumn')) {
$this->descriptionFrom = $this->getConfig('descriptionColumn');
}
if (!$this->model->hasRelation($this->relationName))
throw new SystemException(Lang::get('backend::lang.model.missing_relation', ['class'=>get_class($this->controller), 'relation'=>$this->relationName]));
if (!$this->model->hasRelation($this->relationName)) {
throw new SystemException(Lang::get('backend::lang.model.missing_relation', [
'class' => get_class($this->controller),
'relation' => $this->relationName
]));
}
if (post('recordfinder_flag')) {
$this->listWidget = $this->makeListWidget();
@ -100,7 +110,7 @@ class RecordFinder extends FormWidgetBase
/*
* Link the Search Widget to the List Widget
*/
$this->searchWidget->bindEvent('search.submit', function() {
$this->searchWidget->bindEvent('search.submit', function () {
$this->listWidget->setSearchTerm($this->searchWidget->getActiveTerm());
return $this->listWidget->onRefresh();
});
@ -162,24 +172,27 @@ class RecordFinder extends FormWidgetBase
public function getKeyValue()
{
if (!$this->relationModel)
if (!$this->relationModel) {
return null;
}
return $this->relationModel->{$this->keyFrom};
}
public function getNameValue()
{
if (!$this->relationModel || !$this->nameFrom)
if (!$this->relationModel || !$this->nameFrom) {
return null;
}
return $this->relationModel->{$this->nameFrom};
}
public function getDescriptionValue()
{
if (!$this->relationModel || !$this->descriptionFrom)
if (!$this->relationModel || !$this->descriptionFrom) {
return null;
}
return $this->relationModel->{$this->descriptionFrom};
}
@ -226,4 +239,4 @@ class RecordFinder extends FormWidgetBase
$widget->cssClasses[] = 'recordfinder-search';
return $widget;
}
}
}

View File

@ -61,11 +61,21 @@ class Relation extends FormWidgetBase
$this->descriptionFrom = $this->getConfig('descriptionFrom', $this->descriptionFrom);
$this->emptyOption = $this->getConfig('emptyOption');
/* @todo Remove line if year >= 2015 */ if ($this->getConfig('nameColumn')) $this->nameFrom = $this->getConfig('nameColumn');
/* @todo Remove line if year >= 2015 */ if ($this->getConfig('descriptionColumn')) $this->descriptionFrom = $this->getConfig('descriptionColumn');
/* @todo Remove lines if year >= 2015 */
if ($this->getConfig('nameColumn')) {
$this->nameFrom = $this->getConfig('nameColumn');
}
/* @todo Remove lines if year >= 2015 */
if ($this->getConfig('descriptionColumn')) {
$this->descriptionFrom = $this->getConfig('descriptionColumn');
}
if (!$this->model->hasRelation($this->relationName))
throw new SystemException(Lang::get('backend::lang.model.missing_relation', ['class'=>get_class($this->controller), 'relation'=>$this->relationName]));
if (!$this->model->hasRelation($this->relationName)) {
throw new SystemException(Lang::get(
'backend::lang.model.missing_relation',
['class'=>get_class($this->controller), 'relation'=>$this->relationName]
));
}
}
/**
@ -90,7 +100,7 @@ class Relation extends FormWidgetBase
*/
protected function makeRenderFormField()
{
return $this->renderFormField = RelationBase::noConstraints(function() {
return $this->renderFormField = RelationBase::noConstraints(function () {
$field = clone $this->formField;
@ -100,13 +110,12 @@ class Relation extends FormWidgetBase
if (in_array($this->relationType, ['belongsToMany', 'morphToMany', 'morphedByMany'])) {
$field->type = 'checkboxlist';
}
else if ($this->relationType == 'belongsTo') {
} elseif ($this->relationType == 'belongsTo') {
$field->type = 'dropdown';
$field->placeholder = $this->emptyOption;
}
// It is safe to assume that if the model and related model are of
// It is safe to assume that if the model and related model are of
// the exact same class, then it cannot be related to itself
if ($model->exists && (get_class($model) == get_class($relatedObj))) {
$query->where($relatedObj->getKeyName(), '<>', $model->getKey());
@ -117,10 +126,11 @@ class Relation extends FormWidgetBase
$query->getQuery()->getQuery()->joins = [];
$treeTraits = ['October\Rain\Database\Traits\NestedTree', 'October\Rain\Database\Traits\SimpleTree'];
if (count(array_intersect($treeTraits, class_uses($relatedObj))) > 0)
if (count(array_intersect($treeTraits, class_uses($relatedObj))) > 0) {
$field->options = $query->listsNested($this->nameFrom, $relatedObj->getKeyName());
else
} else {
$field->options = $query->lists($this->nameFrom, $relatedObj->getKeyName());
}
return $field;
});
@ -131,12 +141,14 @@ class Relation extends FormWidgetBase
*/
public function getSaveData($value)
{
if (is_string($value) && !strlen($value))
if (is_string($value) && !strlen($value)) {
return null;
}
if (is_array($value) && !count($value))
if (is_array($value) && !count($value)) {
return null;
}
return $value;
}
}
}

View File

@ -61,4 +61,4 @@ class RichEditor extends FormWidgetBase
$this->addJs('js/richeditor.js', 'core');
}
}
}