From 9b6ad74e6af0495cbd23b28009a9d37bb4313e58 Mon Sep 17 00:00:00 2001 From: Ivan Diaz Date: Mon, 2 Nov 2015 21:55:59 -0300 Subject: [PATCH] [Ivan Diaz] - Create datastore abstract class --- server/models/DataStore.php | 53 ++++++++++++++++++++++++++++++++++--- server/models/Log.php | 17 ------------ server/models/User.php | 52 +++++------------------------------- 3 files changed, 57 insertions(+), 65 deletions(-) delete mode 100644 server/models/Log.php diff --git a/server/models/DataStore.php b/server/models/DataStore.php index 7160faac..4f87fa54 100644 --- a/server/models/DataStore.php +++ b/server/models/DataStore.php @@ -1,10 +1,57 @@ _bean = $beanInstance; + } + else { + $this->_bean = RedBean::dispense(static::TABLE); + $defaultProperties = $this->getDefaultProperties(); + foreach ($defaultProperties as $PROP => $VALUE) { + $this->_bean[$PROP] = $VALUE; + } + } + } + + protected function deleteDataStore($dataStore) { + if ($dataStore instanceof DataStore) { + RedBean::trash($dataStore->getBeanInstance()); + unset($dataStore); + return true; + } + else { + return false; + } + } + + protected function getBeanInstance() { + return $this->_bean; + } + + public function setProperties($properties) { + foreach (static::PROPERTIES as $PROP) { + $this->_bean[$PROP] = $properties[$PROP]; + } + } + + public function __get($name) { + if ($this->_bean[$name]) { + return $this->_bean[$name]; + } + else { + return null; + } + } + + public function store() { + return RedBean::store($this->_bean); } } -*/ diff --git a/server/models/Log.php b/server/models/Log.php deleted file mode 100644 index 6d14470b..00000000 --- a/server/models/Log.php +++ /dev/null @@ -1,17 +0,0 @@ -log = RedBean::dispense('LOGS'); - $log->date = RedBean::isoDateTime(); - $log->type = $properties['type']; - $log->content = $properties['content']; - } - - public function showUserDetails() { - return $this->_user; - } -} diff --git a/server/models/User.php b/server/models/User.php index cca359c1..30300343 100644 --- a/server/models/User.php +++ b/server/models/User.php @@ -1,7 +1,8 @@ 'User or password is not defined' ); - private $_id; - private $_user; - public static function getUser($value, $property = 'id') { if ($property === 'id') { $mapValue = 'id=:value'; @@ -28,49 +26,13 @@ class User { } public static function deleteUser($user) { - if ($user instanceof User) { - RedBean::trash($user); - unset($user); - return true; - } - else { - return false; - } + parent::deleteDataStore($user); } - public function __construct($user = null) { - - if ($user) { - $this->_user = $user; - } - else { - $this->_user = RedBean::dispense('users'); - $this->setDefaultProperties(); - } - } - - public function setDefaultProperties() { - - } - - public function setProperties($properties) { - - foreach (self::PROPERTIES as $PROP) { - $this->_user[$PROP] = $properties[$PROP]; - } - } - - public function __get($name) { - if ($this->_user[$name]) { - return $this->_user[$name]; - } - else { - return null; - } - } - - public function store() { - return RedBean::store($this->_user); + public function getDefaultProperties() { + return [ + 'admin' => 0 + ]; } public function showUserDetails() {