Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

V2 #13

Merged
merged 2 commits into from
May 18, 2016
Merged

V2 #13

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions src/app/Http/Controllers/CrudController.php
Original file line number Diff line number Diff line change
Expand Up @@ -74,8 +74,10 @@ public function storeCrud(StoreRequest $request = null)
{
$this->crud->hasAccessOrFail('create');


// insert item in the db
$item = $this->crud->create(\Request::all());
$item = $this->crud->create(\Request::except(['redirect_after_save', 'password']));


// show a success message
\Alert::success(trans('backpack::crud.insert_success'))->flash();
Expand Down Expand Up @@ -107,6 +109,8 @@ public function edit($id)
$this->data['fields'] = $this->crud->getUpdateFields($id);
$this->data['title'] = trans('backpack::crud.edit').' '.$this->crud->entity_name;

$this->data['id'] = $id;

// load the view from /resources/views/vendor/backpack/crud/ if it exists, otherwise load the one in the package
return view('crud::edit', $this->data);
}
Expand All @@ -123,7 +127,8 @@ public function updateCrud(UpdateRequest $request = null)
$this->crud->hasAccessOrFail('update');

// update the row in the db
$this->crud->update(\Request::get('id'), \Request::all());

$this->crud->update(\Request::get('id'), \Request::except('redirect_after_save'));

// show a success message
\Alert::success(trans('backpack::crud.update_success'))->flash();
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
<!-- include checklist_dependency js-->
<script>
jQuery(document).ready(function($) {

$('.checklist_dependency').each(function(index, item){

var unique_name = $(this).data('entity');
var dependencyJson = window[unique_name];

var thisField = $(this);
thisField.find('.primary_list').change(function(){

var idCurrent = $(this).data('id');
if($(this).is(':checked')){

//add hidden field with this value
var nameInput = thisField.find('.hidden_fields_primary').data('name');
var inputToAdd = $('<input type="hidden" class="primary_hidden" name="'+nameInput+'[]" value="'+idCurrent+'">');

thisField.find('.hidden_fields_primary').append(inputToAdd);

$.each(dependencyJson[idCurrent], function(key, value){
//check and disable secondies checkbox
thisField.find('input.secondary_list[value="'+value+'"]').prop( "checked", true );
thisField.find('input.secondary_list[value="'+value+'"]').prop( "disabled", true );
//remove hidden fields with secondary dependency if was setted
var hidden = thisField.find('input.secondary_hidden[value="'+value+'"]');
if(hidden)
hidden.remove();
});

}else{
//remove hidden field with this value
thisField.find('input.primary_hidden[value="'+idCurrent+'"]').remove();

// uncheck and active secondary checkboxs if are not in other selected primary.

var secondary = dependencyJson[idCurrent];

var selected = [];
thisField.find('input.primary_hidden').each(function (index, input){
selected.push( $(this).val() );
});

$.each(secondary, function(index, secondaryItem){
var ok = 1;

$.each(selected, function(index2, selectedItem){
if( dependencyJson[selectedItem].indexOf(secondaryItem) != -1 ){
ok =0;
}
});

if(ok){
thisField.find('input.secondary_list[value="'+secondaryItem+'"]').prop('checked', false);
thisField.find('input.secondary_list[value="'+secondaryItem+'"]').prop('disabled', false);
}
});

}
});


thisField.find('.secondary_list').click(function(){

var idCurrent = $(this).data('id');
if($(this).is(':checked')){
//add hidden field with this value
var nameInput = thisField.find('.hidden_fields_secondary').data('name');
var inputToAdd = $('<input type="hidden" class="secondary_hidden" name="'+nameInput+'[]" value="'+idCurrent+'">');

thisField.find('.hidden_fields_secondary').append(inputToAdd);

}else{
//remove hidden field with this value
thisField.find('input.secondary_hidden[value="'+idCurrent+'"]').remove();
}
});

});
});
</script>
10 changes: 5 additions & 5 deletions src/resources/views/fields/checkbox.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,13 @@
<div class="checkbox">
<label>
<input type="hidden" name="{{ $field['name'] }}" value="0">
<input type="checkbox" @foreach ($field as $attribute => $value)
@if (is_string($attribute) && is_string($value))
<input type="checkbox" value="1" @foreach ($field as $attribute => $value)
@if (is_string($attribute) )
@if( $attribute == 'value' )
@if((int) $value == 1)
checked = "checked"
@if( ((int) $value == 1 || old($field['name']) == 1) && old($field['name']) !== '0' )
checked = "checked"
@endif
{{ $attribute }}= "1"

@else
{{ $attribute }}="{{ $value }}"
@endif
Expand Down
31 changes: 31 additions & 0 deletions src/resources/views/fields/checklist.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<!-- select2 -->
<div class="form-group">
<label>{{ $field['label'] }}</label>
<?php $entity_model = $crud->getModel();?>

<div class="row">
@foreach ($field['model']::all() as $connected_entity_entry)
<div class="col-sm-4">
<div class="checkbox">
<label>
<input type="checkbox"
@foreach ($field as $attribute => $value)
@if (is_string($attribute) && $attribute != 'value')
@if ($attribute=='name')
{{ $attribute }}="{{ $value }}[]"
@else
{{ $attribute }}="{{ $value }}"
@endif
@endif
@endforeach
value="{{ $connected_entity_entry->id }}"

@if( ( old( $field["name"] ) && in_array($connected_entity_entry->id, old( $field["name"])) ) || (isset($field['value']) && in_array($connected_entity_entry->id, $field['value']->lists('id', 'id')->toArray())))
checked = "checked"
@endif > {{ $connected_entity_entry->{$field['attribute']} }}
</label>
</div>
</div>
@endforeach
</div>
</div>
161 changes: 161 additions & 0 deletions src/resources/views/fields/checklist_dependency.blade.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
<!-- dependencyJson -->
<div class="form-group checklist_dependency" data-entity ="{{ $field['field_unique_name'] }}">
<label>{{ $field['label'] }}</label>
<?php
$entity_model = $crud->getModel();

//short name for dependency fields
$primary_dependency = $field['dependencies']['primary'];
$secondary_dependency = $field['dependencies']['secondary'];


//all items with relation
$dependencies = $primary_dependency['model']::with($primary_dependency['entity_secondary'])->get();

$dependencyArray = [];

//convert dependency array to simple matrix ( prymary id as key and array with secondaries id )
foreach($dependencies as $primary){
$dependencyArray[$primary->id] = [];
foreach($primary->{$primary_dependency['entity_secondary']} as $secondary){
$dependencyArray[$primary->id][] = $secondary->id;
}
}

//for update form, get initial state of the entity
if( isset($id) && $id ){

//get entity with relations for primary dependency
$entity_dependencies = $entity_model->with($primary_dependency['entity'])
->with($primary_dependency['entity'].'.'.$primary_dependency['entity_secondary'])
->where('id', $id)
->first();

$secondaries_from_primary = [];

//convert relation in array
$primary_array = $entity_dependencies->{$primary_dependency['entity']}->toArray();

$secondary_ids = [];

//create secondary dependency from primary relation, used to check what chekbox must be check from second checklist
if( old($primary_dependency['name']) ) {
foreach( old($primary_dependency['name']) as $primary_item ){
foreach($dependencyArray[$primary_item] as $second_item ){
$secondary_ids[$second_item] = $second_item;
}
}
}else{ //create dependecies from relation if not from validate error
foreach( $primary_array as $primary_item ){
foreach($primary_item[$secondary_dependency['entity']] as $second_item ){
$secondary_ids[$second_item['id']] = $second_item['id'];
}
}
}

}

//json encode of dependency matrix
$dependencyJson = json_encode($dependencyArray);
?>
<script>
var {{ $field['field_unique_name'] }} = {!! $dependencyJson !!};
</script>
<div class="row" >
<div class="col-xs-12">
<label>{{ $primary_dependency['label'] }}</label>
</div>

<div class="hidden_fields_primary" data-name = "{{ $primary_dependency['name'] }}">
@if(isset($field['value']))
@if(old($primary_dependency['name']))
@foreach( old($primary_dependency['name']) as $item )
<input type="hidden" class="primary_hidden" name="{{ $primary_dependency['name'] }}[]" value="{{ $item }}">
@endforeach
@else
@foreach( $field['value'][0]->lists('id', 'id')->toArray() as $item )
<input type="hidden" class="primary_hidden" name="{{ $primary_dependency['name'] }}[]" value="{{ $item }}">
@endforeach
@endif
@endif
</div>

@foreach ($primary_dependency['model']::all() as $connected_entity_entry)
<div class="col-sm-{{ isset($primary_dependency['number_columns']) ? intval(12/$primary_dependency['number_columns']) : '4'}}">
<div class="checkbox">
<label>
<input type="checkbox"
data-id = "{{ $connected_entity_entry->id }}"
class = 'primary_list'
@foreach ($primary_dependency as $attribute => $value)
@if (is_string($attribute) && $attribute != 'value')
@if ($attribute=='name')
{{ $attribute }}="{{ $value }}_show[]"
@else
{{ $attribute }}="{{ $value }}"
@endif
@endif
@endforeach
value="{{ $connected_entity_entry->id }}"

@if( ( isset($field['value']) && is_array($field['value']) && in_array($connected_entity_entry->id, $field['value'][0]->lists('id', 'id')->toArray())) || ( old($primary_dependency["name"]) && in_array($connected_entity_entry->id, old( $primary_dependency["name"])) ) )
checked = "checked"
@endif >
{{ $connected_entity_entry->{$primary_dependency['attribute']} }}

</label>
</div>
</div>
@endforeach
</div>

<div class="row">
<div class="col-xs-12">
<label>{{ $secondary_dependency['label'] }}</label>
</div>

<div class="hidden_fields_secondary" data-name="{{ $secondary_dependency['name'] }}">
@if(isset($field['value']))
@if(old($secondary_dependency['name']))
@foreach( old($secondary_dependency['name']) as $item )
<input type="hidden" class="secondary_hidden" name="{{ $secondary_dependency['name'] }}[]" value="{{ $item }}">
@endforeach
@else
@foreach( $field['value'][1]->lists('id', 'id')->toArray() as $item )
<input type="hidden" class="secondary_hidden" name="{{ $secondary_dependency['name'] }}[]" value="{{ $item }}">
@endforeach
@endif
@endif
</div>

@foreach ($secondary_dependency['model']::all() as $connected_entity_entry)
<div class="col-sm-{{ isset($secondary_dependency['number_columns']) ? intval(12/$secondary_dependency['number_columns']) : '4'}}">
<div class="checkbox">
<label>
<input type="checkbox"
class = 'secondary_list'
data-id = "{{ $connected_entity_entry->id }}"
@foreach ($secondary_dependency as $attribute => $value)
@if (is_string($attribute) && $attribute != 'value')
@if ($attribute=='name')
{{ $attribute }}="{{ $value }}_show[]"
@else
{{ $attribute }}="{{ $value }}"
@endif
@endif
@endforeach
value="{{ $connected_entity_entry->id }}"

@if( ( isset($field['value']) && is_array($field['value']) && ( in_array($connected_entity_entry->id, $field['value'][1]->lists('id', 'id')->toArray()) || isset( $secondary_ids[$connected_entity_entry->id])) || ( old($secondary_dependency['name']) && in_array($connected_entity_entry->id, old($secondary_dependency['name'])) )))
checked = "checked"
@if(isset( $secondary_ids[$connected_entity_entry->id]))
disabled = disabled
@endif
@endif > {{ $connected_entity_entry->{$secondary_dependency['attribute']} }}
</label>
</div>
</div>
@endforeach
</div>

</div>
8 changes: 6 additions & 2 deletions src/resources/views/fields/ckeditor.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,13 @@ class="form-control ckeditor"

@foreach ($field as $attribute => $value)
@if (is_string($attribute) && is_string($value))
{{ $attribute }}="{{ $value }}"
@if($attribute == 'value')
{{ $attribute }}="{{ old($field['name']) ? old($field['name']) : $value }}"
@else
{{ $attribute }}="{{ $value }}"
@endif
@endif
@endforeach

>{{ (isset($field['value']))?$field['value']:'' }}</textarea>
>{{ old($field['name']) ? old($field['name']) : ((isset($field['value']))?$field['value']:'') }}</textarea>
</div>
2 changes: 1 addition & 1 deletion src/resources/views/fields/date.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class="form-control"
@foreach ($field as $attribute => $value)
@if (is_string($attribute) && is_string($value))
@if ($attribute=='value')
value="{{ $field['value'] }}"
value="{{ old($field['name']) ? old($field['name']) : $value }}"
@else
{{ $attribute }}="{{ $value }}"
@endif
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/fields/datetime.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class="form-control"
@foreach ($field as $attribute => $value)
@if (is_string($attribute) && is_string($value))
@if ($attribute == 'value')
value="{{ strftime('%Y-%m-%dT%H:%M:%S', strtotime($value)) }}"
value="{{ strftime('%Y-%m-%dT%H:%M:%S', strtotime(old($field['name']) ? old($field['name']) : $value )) }}"
@else
{{ $attribute }}="{{ $value }}"
@endif
Expand Down
6 changes: 5 additions & 1 deletion src/resources/views/fields/email.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,11 @@ class="form-control"

@foreach ($field as $attribute => $value)
@if (is_string($attribute) && is_string($value))
{{ $attribute }}="{{ $value }}"
@if($attribute == 'value')
{{ $attribute }}="{{ old($field['name']) ? old($field['name']) : $value }}"
@else
{{ $attribute }}="{{ $value }}"
@endif
@endif
@endforeach
>
Expand Down
2 changes: 1 addition & 1 deletion src/resources/views/fields/enum.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ class="form-control"
@if (count($entity_model::getPossibleEnumValues($field['name'])))
@foreach ($entity_model::getPossibleEnumValues($field['name']) as $possible_value)
<option value="{{ $possible_value }}"
@if (isset($field['value']) && $field['value']==$possible_value)
@if (( old($field['name']) && old($field['name']) == $possible_value) || (isset($field['value']) && $field['value']==$possible_value))
selected
@endif
>{{ $possible_value }}</option>
Expand Down
6 changes: 5 additions & 1 deletion src/resources/views/fields/hidden.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,11 @@ class="form-control"

@foreach ($field as $attribute => $value)
@if (is_string($attribute))
{{ $attribute }}="{{ $value }}"
@if($attribute == 'value')
{{ $attribute }}="{{ old($field['name']) ? old($field['name']) : $value }}"
@else
{{ $attribute }}="{{ $value }}"
@endif
@endif
@endforeach
>
Expand Down
Loading