Permalink
Browse files

Put resource editing in project admin view

  • Loading branch information...
trezker committed Apr 15, 2012
1 parent d065262 commit a8a9d07fa9553458fa5dd6000e813fe19335b7f7
@@ -17,7 +17,9 @@ public function Index()
$this->Load_model('Project_model');
$recipes = $this->Project_model->Get_recipes();
- $this->Load_view('project_admin_view', array('recipes' => $recipes));
+ $this->Load_model('Resource_model');
+ $resources = $this->Resource_model->Get_resources();
+ $this->Load_view('project_admin_view', array('recipes' => $recipes, 'resources' => $resources));
}
public function edit_recipe() {
@@ -34,8 +36,8 @@ public function edit_recipe() {
$this->Load_model('Project_model');
$recipe = $this->Project_model->Get_recipe($_POST['id']);
- $this->Load_model('Location_model');
- $resources = $this->Location_model->Get_resources();
+ $this->Load_model('Resource_model');
+ $resources = $this->Resource_model->Get_resources();
if($recipe['recipe'] == false) {
$recipe['recipe'] = array(
@@ -159,6 +161,54 @@ public function remove_recipe_input() {
echo json_encode(array('success' => $success));
}
+
+ public function edit_resource() {
+ header('Content-type: application/json');
+ $this->Load_controller('User');
+ if(!$this->User->Logged_in()) {
+ echo json_encode(array('success' => false, 'reason' => 'Not logged in'));
+ return;
+ }
+ if($_SESSION['admin'] != true) {
+ echo json_encode(array('success' => false, 'reason' => 'Not admin'));
+ return;
+ }
+
+ $this->Load_model('Resource_model');
+ $resource = $this->Resource_model->Get_resource($_POST['id']);
+ $measures = $this->Resource_model->Get_measures();
+
+ if($resource == false) {
+ $resource = array(
+ 'ID' => '-1',
+ 'Name' => '',
+ 'Measure' => NULL,
+ 'Is_natural' => false
+ );
+ }
+ $edit_resource_view = $this->Load_view('resource_edit_view',array('resource' => $resource,
+ 'measures' => $measures), true);
+
+ echo json_encode(array('success' => true, 'data' => $edit_resource_view));
+ }
+
+ public function save_resource() {
+ header('Content-type: application/json');
+ $this->Load_controller('User');
+ if(!$this->User->Logged_in()) {
+ echo json_encode(array('success' => false, 'reason' => 'Not logged in'));
+ return;
+ }
+ if($_SESSION['admin'] != true) {
+ echo json_encode(array('success' => false, 'reason' => 'Not admin'));
+ return;
+ }
+
+ $this->Load_model('Resource_model');
+ $result = $this->Resource_model->Save_resource($_POST);
+
+ echo json_encode(array('success' => $result));
+ }
}
?>
@@ -87,14 +87,13 @@ public function Edit_location()
$landscapes = $this->Location_model->Get_landscapes();
$landscapes_view = $this->Load_view('landscapes_view', array('landscapes' => $landscapes, 'location_resources' => $all_location_resources), true);
- $resources = $this->Location_model->Get_resources();
- $resources_view = "";//$this->Load_view('resources_view', array('resources' => $resources, 'location_resources' => $location_resources), true);
+ $this->Load_model('Resource_model');
+ $resources = $this->Resource_model->Get_resources();
$location_admin_view = $this->Load_view('location_edit_view',
array('biomes_view' => $biomes_view,
'landscapes' => $landscapes,
'location' => $location,
- 'resources_view' => $resources_view,
'landscapes_view' => $landscapes_view,
'location_resources' => $location_resources), true);
@@ -116,7 +115,8 @@ public function get_landscape_resources()
$this->Load_model('Location_model');
$location_resources = $this->Location_model->Get_location_resources($_POST['location'], $_POST['landscape']);
- $resources = $this->Location_model->Get_resources();
+ $this->Load_model('Resource_model');
+ $resources = $this->Resource_model->Get_resources();
$resources_view = $this->Load_view('resources_view', array('resources' => $resources,
'location_resources' => $location_resources), true);
@@ -152,35 +152,6 @@ public function Add_biome()
echo json_encode(array('success' => true, 'data' => $biomes_view));
}
- public function Add_resource()
- {
- header('Content-type: application/json');
- $this->Load_controller('User');
- if(!$this->User->Logged_in()) {
- echo json_encode(array('success' => false, 'reason' => 'Not logged in'));
- return;
- }
- if($_SESSION['admin'] != true) {
- echo json_encode(array('success' => false, 'reason' => 'Requires admin privilege'));
- return;
- }
- if(!is_string($_POST['name']) || $_POST['name'] == '') {
- echo json_encode(array('success' => false, 'reason' => $_POST['name'].'Must give a name'));
- return;
- }
-
- $this->Load_model('Location_model');
- if(!$this->Location_model->Add_resource($_POST['name'], $_POST['natural'])) {
- echo json_encode(array('success' => false, 'reason' => 'Failed to add resource'));
- return;
- }
- $resources = $this->Location_model->Get_resources();
- $location_resources = $this->Location_model->Get_location_resources($_POST['location_id'], $_POST['landscape_id']);
- $resources_view = $this->Load_view('resources_view', array('resources' => $resources, 'location_resources' => $location_resources), true);
-
- echo json_encode(array('success' => true, 'data' => $resources_view));
- }
-
public function Add_landscape()
{
header('Content-type: application/json');
@@ -1,4 +1,5 @@
var current_recipe = -1;
+var current_resource = -1;
function edit_recipe(id)
{
@@ -164,3 +165,46 @@ function remove_input(id) {
}
});
}
+
+function edit_resource(id) {
+ $.ajax({
+ type: 'POST',
+ url: 'project_admin/edit_resource',
+ data: {
+ id: id
+ },
+ success: function(data) {
+ if(ajax_logged_out(data)) return;
+ if(data !== false) {
+ current_resource = id;
+ $('#edit_resource').html(data.data);
+ }
+ }
+ });
+}
+
+function save_resource() {
+ var id = current_resource;
+ var name = $('#resource_name').val();
+ var is_natural = $('#is_natural').attr('checked');
+ var measure = $('#measure').val();
+
+ $.ajax({
+ type: 'POST',
+ url: 'project_admin/save_resource',
+ data: {
+ id: id,
+ name: name,
+ is_natural: is_natural,
+ measure: measure
+ },
+ success: function(data) {
+ if(ajax_logged_out(data)) return;
+ if(data !== false) {
+ current_resource = id;
+ $('#edit_resource').html(data.data);
+ }
+ edit_resource(id);
+ }
+ });
+}
View
@@ -210,46 +210,6 @@ public function Add_landscape($name)
return true;
}
- public function Get_resources()
- {
- $db = Load_database();
-
- $rs = $db->Execute('
- select R.ID, R.Name from Resource R
- ', array());
-
- if(!$rs)
- {
- return false;
- }
-
- $resources = array();
- foreach ($rs as $row) {
- $resources[] = $row;
- }
- return $resources;
- }
-
- public function Add_resource($name, $natural)
- {
- $db = Load_database();
-
- if(((boolean)$natural) or $natural == 'true')
- $natural = 1;
- else
- $natural = 0;
-
- $rs = $db->Execute('
- insert into Resource(Name, Is_natural) values(?, ?)
- ', array($name, $natural));
-
- if(!$rs)
- {
- return false;
- }
- return true;
- }
-
public function Get_location($id)
{
$db = Load_database();
View
@@ -0,0 +1,96 @@
+<?php
+
+require_once '../models/database.php';
+
+class Resource_model
+{
+ public function Get_resources()
+ {
+ $db = Load_database();
+
+ $rs = $db->Execute('
+ select R.ID, R.Name from Resource R
+ ', array());
+
+ if(!$rs)
+ {
+ return false;
+ }
+
+ $resources = array();
+ foreach ($rs as $row) {
+ $resources[] = $row;
+ }
+ return $resources;
+ }
+
+ public function Get_resource($resource_id) {
+ if($resource_id == -1) {
+ return false;
+ }
+
+ $db = Load_database();
+
+ $rs = $db->Execute('
+ select ID, Name, Is_natural, Measure from Resource where ID = ?
+ ', array($resource_id));
+
+ if(!$rs || $rs->RecordCount() == 0) {
+ return false;
+ }
+
+ return $rs->fields;
+ }
+
+ public function Save_resource($resource) {
+ $db = Load_database();
+
+ if($resource['is_natural'] == 'true')
+ $resource['is_natural'] = 1;
+ else
+ $resource['is_natural'] = 0;
+
+ if($resource['id'] == -1) {
+ $args = array( $resource['name'],
+ $resource['is_natural'],
+ $resource['measure']
+ );
+
+ $rs = $db->Execute('
+ insert into Resource (Name, Is_natural, Measure) values (?, ?, ?)
+ ', $args);
+ } else {
+ $args = array( $resource['name'],
+ $resource['is_natural'],
+ $resource['measure'],
+ $resource['id']
+ );
+
+ $rs = $db->Execute('
+ update Resource set Name = ?, Is_natural = ?, Measure = ? where ID = ?
+ ', $args);
+ }
+
+ if(!$rs) {
+ echo $db->ErrorMsg();
+ return false;
+ }
+
+ return true;
+ }
+
+ public function Get_measures() {
+ $db = Load_database();
+
+ $rs = $db->Execute('
+ select ID, Name from Measure
+ ', array());
+
+ if(!$rs) {
+ return false;
+ }
+
+ return $rs->GetArray();
+ }
+}
+?>
@@ -21,8 +21,5 @@
<div style="float: left;">
<h3 class="list_title">Resources</h3>
<div id="resource_list">
- <?php echo $resources_view; ?>
</div>
- <input type="text" id="new_resource" />
- <br/><span class="action" onclick="add_resource();">Add resource</span>
</div>
@@ -12,7 +12,7 @@
<h1>Project administration</h1>
<p><span class="action" onclick="window.location = 'user'">Back</span></p>
- <div class="recipes">
+ <div class="recipes" style="float: left;">
<h2>Recipes</h2>
<div id="recipes">
<ul>
@@ -27,8 +27,26 @@
</ul>
</div>
</div>
-
<div id="edit_recipe" style="float: left;">
</div>
+
+ <div class="resources" style="float: left;">
+ <h2>Resources</h2>
+ <div id="resources">
+ <ul>
+ <li><span class="action" onclick="edit_resource(-1);">Create a new resource</span></li>
+ <?php
+ foreach ($resources as $resource) {
+ echo expand_template(
+ '<li><span class="action" onclick="edit_resource({ID});">{Name}</span></li>',
+ $resource);
+ }
+ ?>
+ </ul>
+ </div>
+ </div>
+
+ <div id="edit_resource" style="float: left;">
+ </div>
</body>
</html>
Oops, something went wrong.

0 comments on commit a8a9d07

Please sign in to comment.