array('numeric'), 'name' => array('notempty'), 'key' => array('notempty') ); var $hasMany = array( 'LocksUnits' ); var $hasAndBelongsToMany = array( 'Unit' ); var $default_log_level = array('log' => 30, 'show' => 15); /************************************************************************** ************************************************************************** ************************************************************************** * function: saveLock * - save data about a new or existing lock */ function saveLock($data) { $this->prEnter(compact('data')); $id = $data['Lock']['id']; if ($id) { $this->id = $id; // Save the old key $oldkey = $this->field('key'); $this->pr(5, compact('oldkey')); if ($this->field('key') != $data['Lock']['key']) { $data['Lock']['key_last'] = $this->field('key'); $data['Lock']['key_ts'] = date('Y-m-d G:i:s'); } /* // Find the number of outstanding locks in use */ /* $locks = $this->find('first', */ /* array('link' => array('Unit' => array('fields' => array('Unit.id'))), */ /* 'fields' => 'SUM(Unit.id) AS inuse', */ /* 'conditions' => array('Lock.id' => $id), */ /* )); */ /* $this->pr(5, compact('locks')); */ /* // Can't reduce the locks if there are all in use */ /* if ($locks[0]['inuse'] > $data['Lock']['qty']) */ /* return $this->prReturn(false); */ } else { // Brand new lock } if (!$data['Lock']['qty']) $data['Lock']['qty'] = 1; // Everything looks good... save it! 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' => array('name'), )); } }