From 14713b0ec7ed93ca45578da069ad4e19a7d8addf Mon Sep 17 00:00:00 2001 From: Frederic Guillot Date: Sat, 28 May 2016 19:48:22 -0400 Subject: Rename all models --- app/Model/SettingModel.php | 113 +++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 113 insertions(+) create mode 100644 app/Model/SettingModel.php (limited to 'app/Model/SettingModel.php') diff --git a/app/Model/SettingModel.php b/app/Model/SettingModel.php new file mode 100644 index 00000000..5b2ee54f --- /dev/null +++ b/app/Model/SettingModel.php @@ -0,0 +1,113 @@ +db->hashtable(self::TABLE)->getAll('option', 'value'); + } + + /** + * Get a setting value + * + * @access public + * @param string $name + * @param string $default + * @return mixed + */ + public function getOption($name, $default = '') + { + $value = $this->db + ->table(self::TABLE) + ->eq('option', $name) + ->findOneColumn('value'); + + return $value === null || $value === false || $value === '' ? $default : $value; + } + + /** + * Return true if a setting exists + * + * @access public + * @param string $name + * @return boolean + */ + public function exists($name) + { + return $this->db + ->table(self::TABLE) + ->eq('option', $name) + ->exists(); + } + + /** + * Update or insert new settings + * + * @access public + * @param array $values + * @return boolean + */ + public function save(array $values) + { + $results = array(); + $values = $this->prepare($values); + $user_id = $this->userSession->getId(); + $timestamp = time(); + + $this->db->startTransaction(); + + foreach ($values as $option => $value) { + if ($this->exists($option)) { + $results[] = $this->db->table(self::TABLE)->eq('option', $option)->update(array( + 'value' => $value, + 'changed_on' => $timestamp, + 'changed_by' => $user_id, + )); + } else { + $results[] = $this->db->table(self::TABLE)->insert(array( + 'option' => $option, + 'value' => $value, + 'changed_on' => $timestamp, + 'changed_by' => $user_id, + )); + } + } + + $this->db->closeTransaction(); + + return ! in_array(false, $results, true); + } +} -- cgit v1.2.3