From 13f62edbd72a0d94f94db0c13918a7927b32d40a Mon Sep 17 00:00:00 2001 From: Abijah Date: Mon, 3 Mar 2014 06:33:56 +0000 Subject: [PATCH] Added ability to delete a lock git-svn-id: file:///svn-source/pmgr/branches/v0.3_work@1040 97e9348a-65ac-dc4b-aefc-98561f571b83 --- site/controllers/locks_controller.php | 59 ++++++++++++++++++++++++++- site/models/lock.php | 35 ++++++++++++++++ site/views/locks/delete.ctp | 18 ++++++++ 3 files changed, 110 insertions(+), 2 deletions(-) create mode 100644 site/views/locks/delete.ctp diff --git a/site/controllers/locks_controller.php b/site/controllers/locks_controller.php index 8f60a1f..81115be 100644 --- a/site/controllers/locks_controller.php +++ b/site/controllers/locks_controller.php @@ -75,10 +75,10 @@ class LocksController extends AppController { function view($id = null) { if (!$id) { $this->Session->setFlash(__('Invalid Item.', true)); - $this->redirect(array('controller' => 'accounts', 'action'=>'index')); + $this->redirect(array('controller' => 'locks', 'action'=>'index')); } - // Get the UnitSize and related fields + // Get the lock and related fields $this->Lock->id = $id; $lock = $this->Lock->find ('first', array @@ -91,6 +91,10 @@ class LocksController extends AppController { array('action' => 'edit', $id), null, 'ACTION'); + $this->addSideMenuLink('Delete', + array('action' => 'delete', $id), null, + 'ACTION'); + $this->set(compact('lock')); // Prepare to render. @@ -158,4 +162,55 @@ class LocksController extends AppController { $this->edit(); } + /************************************************************************** + ************************************************************************** + ************************************************************************** + * action: delete + * - Deletes an old lock + */ + function delete($id) { + if (isset($this->data)) { + // Check to see if the operation was cancelled. + if (isset($this->params['form']['cancel'])) { + if (isset($this->data['Lock']['id'])) + $this->redirect(array('action'=>'view', $this->data['Lock']['id'])); + + $this->redirect(array('action'=>'index')); + } + + // Delete the lock and all associated data + if (!$this->Lock->destroy($this->data['Lock']['id'])) { + $this->Session->setFlash(__('Failed to delete lock.', true)); + $this->redirect(array('action'=>'view', $this->data['Lock']['id'])); + } + + // It's gone. Go back to the list of locks + $this->redirect(array('controller' => 'locks', 'action'=>'index')); + } + + // User must specify an ID. + if (!$id) { + $this->Session->setFlash(__('Invalid Item.', true)); + $this->redirect(array('controller' => 'locks', 'action'=>'index')); + } + + // Get the lock and related fields + $this->Lock->id = $id; + $lock = $this->Lock->find + ('first', array + ('contain' => array('Unit'), + )); + + // Make sure the lock isn't in use. + if (isset($lock['Unit']) && count($lock['Unit']) > 0) { + $this->Session->setFlash(__('Lock currently on units. Cannot be deleted!', true)); + $this->redirect(array('action'=>'view', $id)); + } + + // Prepare to render. + $this->data = $lock; + $title = "Delete Lock : {$lock['Lock']['name']}"; + $this->set(compact('title')); + } + } diff --git a/site/models/lock.php b/site/models/lock.php index 66a68a4..22dfa31 100644 --- a/site/models/lock.php +++ b/site/models/lock.php @@ -57,6 +57,41 @@ class Lock extends AppModel { return $this->prReturn($this->save($data, false)); } + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: destroy + * - destroys a lock + */ + + function destroy($id) { + $this->prEnter(compact('id')); + + // Can't delete a lock that's in use... check. + $this->id = $id; + $lock = $this->find + ('first', array + ('contain' => array('Unit'), + )); + + // If it's in use, bail with error + $this->pr(1, $lock); + if (isset($lock['Unit']) && count($lock['Unit']) > 0) + return $this->prReturn(false); + + // Otherwise, attempt to delete the lock from the database + return $this->prReturn($this->delete()); + } + + + /************************************************************************** + ************************************************************************** + ************************************************************************** + * function: lockList + * - list of all locks in the system + */ + function lockList() { return $this->find('list', array('order' => diff --git a/site/views/locks/delete.ctp b/site/views/locks/delete.ctp new file mode 100644 index 0000000..16c2f05 --- /dev/null +++ b/site/views/locks/delete.ctp @@ -0,0 +1,18 @@ +' . "\n"; + +echo $form->create('Lock', array('action' => 'delete')) . "\n"; +echo $form->input('id') . "\n"; +echo $form->submit('Delete Lock') . "\n"; +echo $form->submit('Cancel', array('name' => 'cancel')) . "\n"; +echo $form->end() . "\n"; +echo '' . "\n";