Skip to content

Commit

Permalink
Super janky miulti-level menu on create - TODO: Fix this ish.
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed May 5, 2015
1 parent 2f52369 commit 139dd13
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 6 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-10',
'hash_version' => 'v1.2.7-10-g71febc1',
'app_version' => 'v1.2.7-11',
'hash_version' => 'v1.2.7-11-gaab52d0',
);
28 changes: 24 additions & 4 deletions app/controllers/admin/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,29 @@ public function getIndex()
*/
public function getCreate()
{
// Show the page
$location_options = array('' => 'Top Level') + Location::lists('name', 'id');
return View::make('backend/locations/edit')->with('location_options',$location_options)->with('location',new Location);
$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'];
}
}
}

}

/*echo '<pre>Location options:';
print_r($location_options);
echo '</pre>';
*/

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


Expand All @@ -65,7 +85,7 @@ public function postCreate()
if (Input::get('parent_id')=='') {
$location->parent_id = null;
} else {
$location->parent_id = e(Input::get('parent_id',''));
$location->parent_id = e(Input::get('parent_id'));
}
$location->address = e(Input::get('address'));
$location->address2 = e(Input::get('address2'));
Expand Down
27 changes: 27 additions & 0 deletions app/models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -30,4 +30,31 @@ public function parent() {
public function childLocations() {
return $this->hasMany('Location')->where('parent_id','=',$this->id);
}

public static function getLocationHierarchy($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']
);

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

}
return $op;
}


}

0 comments on commit 139dd13

Please sign in to comment.