Mapper.php 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163
  1. <?php
  2. /**
  3. * @copyright Copyright (c) 2016 Robin Appelman <robin@icewind.nl>
  4. *
  5. * @author Robin Appelman <robin@icewind.nl>
  6. *
  7. * @license GNU AGPL version 3 or any later version
  8. *
  9. * This program is free software: you can redistribute it and/or modify
  10. * it under the terms of the GNU Affero General Public License as
  11. * published by the Free Software Foundation, either version 3 of the
  12. * License, or (at your option) any later version.
  13. *
  14. * This program is distributed in the hope that it will be useful,
  15. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  16. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  17. * GNU Affero General Public License for more details.
  18. *
  19. * You should have received a copy of the GNU Affero General Public License
  20. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  21. *
  22. */
  23. namespace OC\Settings;
  24. use OCP\IDBConnection;
  25. class Mapper {
  26. const TABLE_ADMIN_SETTINGS = 'admin_settings';
  27. const TABLE_ADMIN_SECTIONS = 'admin_sections';
  28. /** @var IDBConnection */
  29. private $dbc;
  30. /**
  31. * @param IDBConnection $dbc
  32. */
  33. public function __construct(IDBConnection $dbc) {
  34. $this->dbc = $dbc;
  35. }
  36. /**
  37. * Get the configured admin settings from the database for the provided section
  38. *
  39. * @param string $section
  40. * @return array[] [['class' => string, 'priority' => int], ...]
  41. */
  42. public function getAdminSettingsFromDB($section) {
  43. $query = $this->dbc->getQueryBuilder();
  44. $query->select(['class', 'priority'])
  45. ->from(self::TABLE_ADMIN_SETTINGS)
  46. ->where($query->expr()->eq('section', $this->dbc->getQueryBuilder()->createParameter('section')))
  47. ->setParameter('section', $section);
  48. $result = $query->execute();
  49. return $result->fetchAll();
  50. }
  51. /**
  52. * Get the configured admin sections from the database
  53. *
  54. * @return array[] [['class' => string, 'priority' => int], ...]
  55. */
  56. public function getAdminSectionsFromDB() {
  57. $query = $this->dbc->getQueryBuilder();
  58. $query->selectDistinct('s.class')
  59. ->addSelect('s.priority')
  60. ->from(self::TABLE_ADMIN_SECTIONS, 's')
  61. ->from(self::TABLE_ADMIN_SETTINGS, 'f')
  62. ->where($query->expr()->eq('s.id', 'f.section'));
  63. $result = $query->execute();
  64. return array_map(function ($row) {
  65. $row['priority'] = (int)$row['priority'];
  66. return $row;
  67. }, $result->fetchAll());
  68. }
  69. /**
  70. * @param string $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
  71. * @param array $values
  72. */
  73. public function add($table, array $values) {
  74. $query = $this->dbc->getQueryBuilder();
  75. $values = array_map(function ($value) use ($query) {
  76. return $query->createNamedParameter($value);
  77. }, $values);
  78. $query->insert($table)->values($values);
  79. $query->execute();
  80. }
  81. /**
  82. * returns the registered classes in the given table
  83. *
  84. * @param $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
  85. * @return string[]
  86. */
  87. public function getClasses($table) {
  88. $q = $this->dbc->getQueryBuilder();
  89. $resultStatement = $q->select('class')
  90. ->from($table)
  91. ->execute();
  92. $data = $resultStatement->fetchAll();
  93. $resultStatement->closeCursor();
  94. return array_map(function ($row) {
  95. return $row['class'];
  96. }, $data);
  97. }
  98. /**
  99. * Check if a class is configured in the database
  100. *
  101. * @param string $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
  102. * @param string $className
  103. * @return bool
  104. */
  105. public function has($table, $className) {
  106. $query = $this->dbc->getQueryBuilder();
  107. $query->select('class')
  108. ->from($table)
  109. ->where($query->expr()->eq('class', $query->createNamedParameter($className)))
  110. ->setMaxResults(1);
  111. $result = $query->execute();
  112. $row = $result->fetch();
  113. $result->closeCursor();
  114. return (bool)$row;
  115. }
  116. /**
  117. * deletes an settings or admin entry from the given table
  118. *
  119. * @param $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
  120. * @param $className
  121. */
  122. public function remove($table, $className) {
  123. $query = $this->dbc->getQueryBuilder();
  124. $query->delete($table)
  125. ->where($query->expr()->eq('class', $query->createNamedParameter($className)));
  126. $query->execute();
  127. }
  128. /**
  129. * @param $table Mapper::TABLE_ADMIN_SECTIONS or Mapper::TABLE_ADMIN_SETTINGS
  130. * @param $idCol
  131. * @param $id
  132. * @param $values
  133. */
  134. public function update($table, $idCol, $id, $values) {
  135. $query = $this->dbc->getQueryBuilder();
  136. $query->update($table);
  137. foreach ($values as $key => $value) {
  138. $query->set($key, $query->createNamedParameter($value));
  139. }
  140. $query
  141. ->where($query->expr()->eq($idCol, $query->createParameter($idCol)))
  142. ->setParameter($idCol, $id)
  143. ->execute();
  144. }
  145. }