summaryrefslogtreecommitdiff
path: root/app/Model
diff options
context:
space:
mode:
Diffstat (limited to 'app/Model')
-rw-r--r--app/Model/Config.php68
-rw-r--r--app/Model/Metadata.php98
-rw-r--r--app/Model/ProjectMetadata.php30
-rw-r--r--app/Model/Setting.php96
-rw-r--r--app/Model/TaskMetadata.php30
-rw-r--r--app/Model/UserMetadata.php30
6 files changed, 302 insertions, 50 deletions
diff --git a/app/Model/Config.php b/app/Model/Config.php
index 6561efc8..cf634f80 100644
--- a/app/Model/Config.php
+++ b/app/Model/Config.php
@@ -12,16 +12,9 @@ use Kanboard\Core\Session;
* @package model
* @author Frederic Guillot
*/
-class Config extends Base
+class Config extends Setting
{
/**
- * SQL table name
- *
- * @var string
- */
- const TABLE = 'settings';
-
- /**
* Get available currencies
*
* @access public
@@ -170,8 +163,7 @@ class Config extends Base
public function get($name, $default_value = '')
{
if (! Session::isOpen()) {
- $value = $this->db->table(self::TABLE)->eq('option', $name)->findOneColumn('value');
- return $value ?: $default_value;
+ return $this->getOption($name, $default_value);
}
// Cache config in session
@@ -187,43 +179,6 @@ class Config extends Base
}
/**
- * Get all settings
- *
- * @access public
- * @return array
- */
- public function getAll()
- {
- return $this->db->hashtable(self::TABLE)->getAll('option', 'value');
- }
-
- /**
- * Save settings in the database
- *
- * @access public
- * @param $values array Settings values
- * @return boolean
- */
- public function save(array $values)
- {
- foreach ($values as $option => $value) {
-
- // Be sure that a trailing slash is there for the url
- if ($option === 'application_url' && ! empty($value) && substr($value, -1) !== '/') {
- $value .= '/';
- }
-
- $result = $this->db->table(self::TABLE)->eq('option', $option)->update(array('value' => $value));
-
- if (! $result) {
- return false;
- }
- }
-
- return true;
- }
-
- /**
* Reload settings in the session and the translations
*
* @access public
@@ -310,8 +265,21 @@ class Config extends Base
*/
public function regenerateToken($option)
{
- return $this->db->table(self::TABLE)
- ->eq('option', $option)
- ->update(array('value' => Security::generateToken()));
+ $this->save(array($option => Security::generateToken()));
+ }
+
+ /**
+ * Prepare data before save
+ *
+ * @access public
+ * @return array
+ */
+ public function prepare(array $values)
+ {
+ if (! empty($values['application_url']) && substr($values['application_url'], -1) !== '/') {
+ $values['application_url'] = $values['application_url'].'/';
+ }
+
+ return $values;
}
}
diff --git a/app/Model/Metadata.php b/app/Model/Metadata.php
new file mode 100644
index 00000000..83c8f499
--- /dev/null
+++ b/app/Model/Metadata.php
@@ -0,0 +1,98 @@
+<?php
+
+namespace Kanboard\Model;
+
+/**
+ * Metadata
+ *
+ * @package model
+ * @author Frederic Guillot
+ */
+abstract class Metadata extends Base
+{
+ /**
+ * Define the entity key
+ *
+ * @abstract
+ * @access protected
+ * @return string
+ */
+ abstract protected function getEntityKey();
+
+ /**
+ * Get all metadata for the entity
+ *
+ * @access public
+ * @param integer $entity_id
+ * @return array
+ */
+ public function getAll($entity_id)
+ {
+ return $this->db
+ ->hashtable(static::TABLE)
+ ->eq($this->getEntityKey(), $entity_id)
+ ->asc('name')
+ ->getAll('name', 'value');
+ }
+
+ /**
+ * Get a metadata for the given entity
+ *
+ * @access public
+ * @param integer $entity_id
+ * @param string $name
+ * @param string $default
+ * @return mixed
+ */
+ public function get($entity_id, $name, $default = '')
+ {
+ return $this->db
+ ->table(static::TABLE)
+ ->eq($this->getEntityKey(), $entity_id)
+ ->eq('name', $name)
+ ->findOneColumn('value') ?: $default;
+ }
+
+ /**
+ * Return true if a metadata exists
+ *
+ * @access public
+ * @param integer $entity_id
+ * @param string $name
+ * @return boolean
+ */
+ public function exists($entity_id, $name)
+ {
+ return $this->db
+ ->table(static::TABLE)
+ ->eq($this->getEntityKey(), $entity_id)
+ ->eq('name', $name)
+ ->exists();
+ }
+
+ /**
+ * Update or insert new metadata
+ *
+ * @access public
+ * @param integer $entity_id
+ * @param array $values
+ */
+ public function save($entity_id, array $values)
+ {
+ $results = array();
+
+ $this->db->startTransaction();
+
+ foreach ($values as $key => $value) {
+ if ($this->exists($entity_id, $key)) {
+ $results[] = $this->db->table(static::TABLE)->eq($this->getEntityKey(), $entity_id)->eq('name', $key)->update(array('value' => $value));
+ } else {
+ $results[] = $this->db->table(static::TABLE)->insert(array('name' => $key, 'value' => $value, $this->getEntityKey() => $entity_id));
+ }
+ }
+
+ $this->db->closeTransaction();
+
+ return ! in_array(false, $results, true);
+ }
+}
diff --git a/app/Model/ProjectMetadata.php b/app/Model/ProjectMetadata.php
new file mode 100644
index 00000000..85498053
--- /dev/null
+++ b/app/Model/ProjectMetadata.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Kanboard\Model;
+
+/**
+ * Project Metadata
+ *
+ * @package model
+ * @author Frederic Guillot
+ */
+class ProjectMetadata extends Metadata
+{
+ /**
+ * SQL table name
+ *
+ * @var string
+ */
+ const TABLE = 'project_has_metadata';
+
+ /**
+ * Define the entity key
+ *
+ * @access protected
+ * @return string
+ */
+ protected function getEntityKey()
+ {
+ return 'project_id';
+ }
+}
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);
+ }
+}
diff --git a/app/Model/TaskMetadata.php b/app/Model/TaskMetadata.php
new file mode 100644
index 00000000..1fd18415
--- /dev/null
+++ b/app/Model/TaskMetadata.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Kanboard\Model;
+
+/**
+ * Task Metadata
+ *
+ * @package model
+ * @author Frederic Guillot
+ */
+class TaskMetadata extends Metadata
+{
+ /**
+ * SQL table name
+ *
+ * @var string
+ */
+ const TABLE = 'task_has_metadata';
+
+ /**
+ * Define the entity key
+ *
+ * @access protected
+ * @return string
+ */
+ protected function getEntityKey()
+ {
+ return 'task_id';
+ }
+}
diff --git a/app/Model/UserMetadata.php b/app/Model/UserMetadata.php
new file mode 100644
index 00000000..6f1222e2
--- /dev/null
+++ b/app/Model/UserMetadata.php
@@ -0,0 +1,30 @@
+<?php
+
+namespace Kanboard\Model;
+
+/**
+ * User Metadata
+ *
+ * @package model
+ * @author Frederic Guillot
+ */
+class UserMetadata extends Metadata
+{
+ /**
+ * SQL table name
+ *
+ * @var string
+ */
+ const TABLE = 'user_has_metadata';
+
+ /**
+ * Define the entity key
+ *
+ * @access protected
+ * @return string
+ */
+ protected function getEntityKey()
+ {
+ return 'user_id';
+ }
+} \ No newline at end of file