Skip to content

Commit

Permalink
Fixes for #759 - location heirarchy
Browse files Browse the repository at this point in the history
  • Loading branch information
snipe committed May 4, 2015
1 parent 5e7a4e8 commit f463cd6
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 28 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-1',
'hash_version' => 'v1.2.7-1-gbbdbd53',
'app_version' => 'v1.2.7-2',
'hash_version' => 'v1.2.7-2-gc8870dd',
);
27 changes: 13 additions & 14 deletions app/controllers/admin/LocationsController.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class LocationsController extends AdminController
public function getIndex()
{
// Grab all the locations
$locations = Location::orderBy('created_at', 'DESC')->get();
$locations = Location::orderBy('created_at', 'DESC')->with('parent')->get();

// Show the page
return View::make('backend/locations/index', compact('locations'));
Expand All @@ -38,7 +38,7 @@ public function getIndex()
public function getCreate()
{
// Show the page
$location_options = array('0' => 'Top Level') + Location::lists('name', 'id');
$location_options = array('' => '') + Location::lists('name', 'id');
return View::make('backend/locations/edit')->with('location_options',$location_options)->with('location',new Location);
}

Expand All @@ -62,12 +62,13 @@ public function postCreate()

// Save the location data
$location->name = e(Input::get('name'));
$location->parent_id = e(Input::get('parent_id'));
$location->address = e(Input::get('address'));
$location->address2 = e(Input::get('address2'));
$location->city = e(Input::get('city'));
$location->state = e(Input::get('state'));
$location->country = e(Input::get('country'));
$location->zip = e(Input::get('zip'));
$location->zip = e(Input::get('zip'));
$location->user_id = Sentry::getId();

// Was the asset created?
Expand Down Expand Up @@ -104,7 +105,7 @@ 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');
$location_options = array('' => '') + DB::table('locations')->where('id', '!=', $locationId)->lists('name', 'id');
return View::make('backend/locations/edit', compact('location'))->with('location_options',$location_options);
}

Expand All @@ -128,27 +129,28 @@ public function postEdit($locationId = null)

if ($validator->fails())
{
// The given data did not pass validation
// The given data did not pass validation
return Redirect::back()->withInput()->withErrors($validator->messages());
}
// attempt validation
else {

// Update the location data
$location->name = e(Input::get('name'));
$location->name = e(Input::get('name'));
$location->parent_id = e(Input::get('parent_id'));
$location->address = e(Input::get('address'));
$location->address2 = e(Input::get('address2'));
$location->city = e(Input::get('city'));
$location->state = e(Input::get('state'));
$location->country = e(Input::get('country'));
$location->zip = e(Input::get('zip'));
$location->country = e(Input::get('country'));
$location->zip = e(Input::get('zip'));

// Was the asset created?
if($location->save()) {
// Redirect to the saved location page
return Redirect::to("admin/settings/locations/")->with('success', Lang::get('admin/locations/message.update.success'));
}
}
}

// Redirect to the location management page
return Redirect::to("admin/settings/locations/$locationId/edit")->with('error', Lang::get('admin/locations/message.update.error'));
Expand All @@ -171,14 +173,11 @@ public function getDelete($locationId)


if ($location->has_users() > 0) {

// Redirect to the asset management page
return Redirect::to('admin/settings/locations')->with('error', Lang::get('admin/locations/message.assoc_users'));
} elseif ($location->childLocations->count() > 0) {
return Redirect::to('admin/settings/locations')->with('error', Lang::get('admin/locations/message.assoc_users'));
} else {

$location->delete();

// Redirect to the locations management page
return Redirect::to('admin/settings/locations')->with('success', Lang::get('admin/locations/message.delete.success'));
}

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class AddParentIdToLocationsTable extends Migration {

/**
* Run the migrations.
*
* @return void
*/
public function up()
{
Schema::table('locations', function(Blueprint $table)
{
//
$table->integer('parent_id')->nullable()->default(NULL);
});
}

/**
* Reverse the migrations.
*
* @return void
*/
public function down()
{
Schema::table('locations', function(Blueprint $table)
{
//
$table->dropColumn('parent_id');
});
}

}
11 changes: 6 additions & 5 deletions app/lang/en/admin/locations/table.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,14 +2,15 @@

return array(

'id' => 'ID',
'address' => 'Address',
'city' => 'City',
'state' => 'State',
'country' => 'Country',
'create' => 'Create Location',
'update' => 'Update Location',
'id' => 'ID',
'locations' => 'Locations',
'name' => 'Location Name',
'address' => 'Address',
'parent' => 'Parent Location',
'state' => 'State',
'update' => 'Update Location',
'zip' => 'Postal Code',
'locations' => 'Locations',
);
13 changes: 10 additions & 3 deletions app/models/Location.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,20 @@ class Location extends Elegant
'city' => 'required|alpha_space|min:3|max:255',
'state' => 'alpha_space|min:2|max:32',
'country' => 'required|alpha_space|min:2|max:2|max:2',
'address' => 'required|alpha_space|min:5|max:80',
'address' => 'alpha_space|min:5|max:80',
'address2' => 'alpha_space|min:5|max:80',
'zip' => 'alpha_space|min:3|max:10',
);

public function has_users()
{
public function has_users() {
return $this->hasMany('User', 'location_id')->count();
}

public function parent() {
return $this->belongsTo('Location', 'parent_id');
}

public function childLocations() {
return $this->hasMany('Location')->where('parent_id','=',$this->id);
}
}
16 changes: 14 additions & 2 deletions app/views/backend/locations/edit.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,11 +46,23 @@
{{ $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')
</label>
<div class="col-md-12">
<div class="col-xs-8">
{{ Form::select('parent_id', $location_options , Input::old('parent_id', $location->parent_id), array('class'=>'select2', 'style'=>'width:350px')) }} </div>
{{ $errors->first('parent_id', '<br><span class="alert-msg"><i class="fa fa-times"></i> :message</span>') }}
</div>
</div>

<!-- Address -->
<div class="form-group {{ $errors->has('address') ? ' has-error' : '' }}">
<label for="address" class="col-md-2 control-label">@lang('admin/locations/table.address')
<i class='fa fa-asterisk'></i></label></label>
<label for="address" class="col-md-2 control-label">
@lang('admin/locations/table.address')
</label>
<div class="col-md-12">
<div class="col-xs-8">
<input class="form-control" type="text" name="address" id="address" value="{{{ Input::old('address', $location->address) }}}" />
Expand Down
9 changes: 7 additions & 2 deletions app/views/backend/locations/index.blade.php
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@
<table id="example">
<thead>
<tr role="row">
<th class="col-md-3">@lang('admin/locations/table.name')</th>
<th class="col-md-2">@lang('admin/locations/table.name')</th>
<th class="col-md-2">@lang('admin/locations/table.parent')</th>
<th class="col-md-3">@lang('admin/locations/table.address')</th>
<th class="col-md-2">@lang('admin/locations/table.city'),
@lang('admin/locations/table.state')
Expand All @@ -32,7 +33,11 @@
<tbody>
@foreach ($locations as $location)
<tr>
<td>{{{ $location->name }}}</td>
<td>{{{ $location->name }}}</td><td>
@if ($location->parent)
{{{ $location->parent->name }}}
@endif
</td>
<td>{{{ $location->address }}}
@if($location->address2 != '')
, {{{ $location->address2 }}}
Expand Down

0 comments on commit f463cd6

Please sign in to comment.