diff options
author | Frederic Guillot <fred@kanboard.net> | 2015-10-17 15:27:43 -0400 |
---|---|---|
committer | Frederic Guillot <fred@kanboard.net> | 2015-10-17 15:27:43 -0400 |
commit | 9283fb88d80cb355ff98364a9a57b657fc511d98 (patch) | |
tree | 52c99cd8adf28e3eb2c30c3d625817b48a8cd4ec /app/Model/Setting.php | |
parent | 9153c6ff0ddb3170928d33599d9178e67ca466b6 (diff) |
Add metadata for users, tasks and projects
Diffstat (limited to 'app/Model/Setting.php')
-rw-r--r-- | app/Model/Setting.php | 96 |
1 files changed, 96 insertions, 0 deletions
diff --git a/app/Model/Setting.php b/app/Model/Setting.php new file mode 100644 index 00000000..3507d424 --- /dev/null +++ b/app/Model/Setting.php @@ -0,0 +1,96 @@ +<?php + +namespace Kanboard\Model; + +/** + * Application Settings + * + * @package model + * @author Frederic Guillot + */ +abstract class Setting extends Base +{ + /** + * SQL table name + * + * @var string + */ + const TABLE = 'settings'; + + /** + * Prepare data before save + * + * @abstract + * @access public + * @return array + */ + abstract public function prepare(array $values); + + /** + * Get all settings + * + * @access public + * @return array + */ + public function getAll() + { + return $this->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 = '') + { + return $this->db + ->table(self::TABLE) + ->eq('option', $name) + ->findOneColumn('value') ?: $default; + } + + /** + * 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 + */ + public function save(array $values) + { + $results = array(); + $values = $this->prepare($values); + + $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)); + } else { + $results[] = $this->db->table(self::TABLE)->insert(array('option' => $option, 'value' => $value)); + } + } + + $this->db->closeTransaction(); + + return ! in_array(false, $results, true); + } +} |