Skip to content

Commit

Permalink
Fixed multi-level array nesting for locations
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed May 7, 2015
1 parent 139dd13 commit 2d7a348
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 31 deletions.
4 changes: 2 additions & 2 deletions app/config/version.php
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
<?php
return array (
'app_version' => 'v1.2.7-11',
'hash_version' => 'v1.2.7-11-gaab52d0',
'app_version' => 'v1.2.7-12',
'hash_version' => 'v1.2.7-12-g2f52369',
);
29 changes: 10 additions & 19 deletions app/controllers/admin/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,25 +37,11 @@ public function getIndex()
*/
public function getCreate()
{
$location_options = array();
$location_options_array = Location::getLocationHierarchy();

foreach ($location_options_array as $id => $value) {
$location_options[$id] = $value['name'];
foreach ($value as $child_id => $children) {
if (is_array($children)) {
foreach ($children as $child_id => $child_locations) {
$location_options[$child_id] = '-- '.$child_locations['name'];
}
}
}

}
$locations = Location::orderBy('name','ASC')->get();

/*echo '<pre>Location options:';
print_r($location_options);
echo '</pre>';
*/
$location_options_array = Location::getLocationHierarchy($locations);
$location_options = Location::flattenLocationsArray($location_options_array);
$location_options = array('' => 'Top Level') + $location_options;

return View::make('backend/locations/edit')
->with('location_options',$location_options)
Expand Down Expand Up @@ -129,7 +115,12 @@ public function getEdit($locationId = null)
// Show the page
//$location_options = array('' => 'Top Level') + Location::lists('name', 'id');

$location_options = array('' => 'Top Level') + DB::table('locations')->where('id', '!=', $locationId)->lists('name', 'id');
$locations = Location::orderBy('name','ASC')->get();

$location_options_array = Location::getLocationHierarchy($locations);
$location_options = Location::flattenLocationsArray($location_options_array);
$location_options = array('' => 'Top Level') + $location_options;

return View::make('backend/locations/edit', compact('location'))->with('location_options',$location_options);
}

Expand Down
45 changes: 36 additions & 9 deletions app/models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -31,30 +31,57 @@ public function childLocations() {
return $this->hasMany('Location')->where('parent_id','=',$this->id);
}

public static function getLocationHierarchy($parent_id = null) {
public static function getLocationHierarchy($locations, $parent_id = null) {


$locations = Location::orderBy('name','ASC')->where('parent_id','=',$parent_id)->get();
$op = array();

foreach($locations as $location) {

if ($location['parent_id'] == $parent_id) {
$op[$location['id']] = array(
'name' => $location['name'],
'parent_id' => $location['parent_id']
);
$op[$location['id']] =
array(
'name' => $location['name'],
'parent_id' => $location['parent_id']
);

// using recursion
$children = Location::getLocationHierarchy($location['id']);
// Using recursion
$children = Location::getLocationHierarchy($locations, $location['id']);
if ($children) {
$op[$location['id']]['children'] = $children;
//echo '<li>'.$location['id'].'has children';
}

}

}
return $op;
}


public static function flattenLocationsArray ($location_options_array = null) {

foreach ($location_options_array as $id => $value) {

// get the top level key value
$location_options[$id] = $value['name'];

// If there is a key named children, it has child locations and we have to walk it
if (array_key_exists('children',$value)) {

foreach ($value['children'] as $child_id => $child_location_array) {
$child_location_options = Location::flattenLocationsArray($value['children']);

foreach ($child_location_options as $child_id => $child_name) {
$location_options[$child_id] = '--'.$child_name;
}
}

}

}

return $location_options;
}


}
2 changes: 1 addition & 1 deletion app/views/backend/locations/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@
{{ $errors->first('name', '<br><span class="alert-msg"><i class="fa fa-times"></i> :message</span>') }}
</div>
</div>

<!-- Parent-->
<div class="form-group {{ $errors->has('parent_id') ? ' has-error' : '' }}">
<label for="parent_id" class="col-md-2 control-label">@lang('admin/locations/table.parent')
Expand Down

0 comments on commit 2d7a348

Please sign in to comment.