EncryptedContentModel.php 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. namespace Kanboard\Plugin\EncryptedContent\Model;
  3. use Kanboard\Core\Base;
  4. /**
  5. *
  6. * EncryptedContent model
  7. * @author Valentino Pesce
  8. *
  9. */
  10. class EncryptedContentModel extends Base
  11. {
  12. /**
  13. * Get the table
  14. *
  15. * @abstract
  16. * @access protected
  17. * @return string
  18. */
  19. protected function getTable()
  20. {
  21. return 'task_has_encrypted_content';
  22. }
  23. /**
  24. * Define the entity key
  25. *
  26. * @access protected
  27. * @return string
  28. */
  29. protected function getEntityKey()
  30. {
  31. return 'task_id';
  32. }
  33. /**
  34. * Get all metadata for the entity
  35. *
  36. * @access public
  37. * @param integer $entity_id
  38. * @return array
  39. */
  40. public function getAll($entity_id)
  41. {
  42. return $this->db
  43. ->hashtable($this->getTable())
  44. ->eq($this->getEntityKey(), $entity_id)
  45. ->asc('name')
  46. ->getAll('name', 'value');
  47. }
  48. /**
  49. * Get a metadata for the given entity
  50. *
  51. * @access public
  52. * @param integer $entity_id
  53. * @param string $name
  54. * @param string $default
  55. * @return mixed
  56. */
  57. public function get($entity_id, $name, $default = '')
  58. {
  59. return $this->db
  60. ->table($this->getTable())
  61. ->eq($this->getEntityKey(), $entity_id)
  62. ->eq('name', $name)
  63. ->findOneColumn('value') ?: $default;
  64. }
  65. /**
  66. * Return true if a metadata exists
  67. *
  68. * @access public
  69. * @param integer $entity_id
  70. * @param string $name
  71. * @return boolean
  72. */
  73. public function exists($entity_id, $name)
  74. {
  75. return $this->db
  76. ->table($this->getTable())
  77. ->eq($this->getEntityKey(), $entity_id)
  78. ->eq('name', $name)
  79. ->exists();
  80. }
  81. /**
  82. * Update or insert new metadata
  83. *
  84. * @access public
  85. * @param integer $entity_id
  86. * @param array $values
  87. * @return boolean
  88. */
  89. public function save($entity_id, array $values)
  90. {
  91. $results = array();
  92. $user_id = $this->userSession->getId();
  93. $timestamp = time();
  94. $this->db->startTransaction();
  95. foreach ($values as $key => $value) {
  96. if ($this->exists($entity_id, $key)) {
  97. $results[] = $this->db->table($this->getTable())
  98. ->eq($this->getEntityKey(), $entity_id)
  99. ->eq('name', $key)
  100. ->update(array(
  101. 'value' => $value,
  102. 'changed_on' => $timestamp,
  103. 'changed_by' => $user_id,
  104. ));
  105. } else {
  106. $results[] = $this->db->table($this->getTable())->insert(array(
  107. 'value' => $value,
  108. $this->getEntityKey() => $entity_id,
  109. 'changed_on' => $timestamp,
  110. 'changed_by' => $user_id,
  111. ));
  112. }
  113. }
  114. $this->db->closeTransaction();
  115. return ! in_array(false, $results, true);
  116. }
  117. /**
  118. * Remove a metadata
  119. *
  120. * @access public
  121. * @param integer $entity_id
  122. * @param string $name
  123. * @return bool
  124. */
  125. public function remove($entity_id, $name)
  126. {
  127. return $this->db->table($this->getTable())
  128. ->eq($this->getEntityKey(), $entity_id)
  129. ->eq('name', $name)
  130. ->remove();
  131. }
  132. /**
  133. * Remove all metadata
  134. *
  135. * @access public
  136. * @param integer $entity_id
  137. * @return bool
  138. */
  139. public function removeAll($entity_id)
  140. {
  141. return $this->db
  142. ->table($this->getTable())
  143. ->eq($this->getEntityKey(), $entity_id)
  144. ->getAll()
  145. ->remove();
  146. }
  147. }