metadata.php 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. /**
  3. * @author Arthur Schiwon <blizzz@owncloud.com>
  4. * @author Lukas Reschke <lukas@owncloud.com>
  5. * @author Morris Jobke <hey@morrisjobke.de>
  6. * @author Stephan Peijnik <speijnik@anexia-it.com>
  7. * @author Thomas Müller <thomas.mueller@tmit.eu>
  8. *
  9. * @copyright Copyright (c) 2015, ownCloud, Inc.
  10. * @license AGPL-3.0
  11. *
  12. * This code is free software: you can redistribute it and/or modify
  13. * it under the terms of the GNU Affero General Public License, version 3,
  14. * as published by the Free Software Foundation.
  15. *
  16. * This program is distributed in the hope that it will be useful,
  17. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. * GNU Affero General Public License for more details.
  20. *
  21. * You should have received a copy of the GNU Affero General Public License, version 3,
  22. * along with this program. If not, see <http://www.gnu.org/licenses/>
  23. *
  24. */
  25. namespace OC\Group;
  26. class MetaData {
  27. const SORT_NONE = 0;
  28. const SORT_USERCOUNT = 1;
  29. /**
  30. * @var string $user
  31. */
  32. protected $user;
  33. /**
  34. * @var bool $isAdmin
  35. */
  36. protected $isAdmin;
  37. /**
  38. * @var array $metaData
  39. */
  40. protected $metaData = array();
  41. /**
  42. * @var \OCP\IGroupManager $groupManager
  43. */
  44. protected $groupManager;
  45. /**
  46. * @var int $sorting
  47. */
  48. protected $sorting = false;
  49. /**
  50. * @param string $user the uid of the current user
  51. * @param bool $isAdmin whether the current users is an admin
  52. * @param \OCP\IGroupManager $groupManager
  53. */
  54. public function __construct(
  55. $user,
  56. $isAdmin,
  57. \OCP\IGroupManager $groupManager
  58. ) {
  59. $this->user = $user;
  60. $this->isAdmin = (bool)$isAdmin;
  61. $this->groupManager = $groupManager;
  62. }
  63. /**
  64. * returns an array with meta data about all available groups
  65. * the array is structured as follows:
  66. * [0] array containing meta data about admin groups
  67. * [1] array containing meta data about unprivileged groups
  68. * @param string $groupSearch only effective when instance was created with
  69. * isAdmin being true
  70. * @param string $userSearch the pattern users are search for
  71. * @return array
  72. */
  73. public function get($groupSearch = '', $userSearch = '') {
  74. $key = $groupSearch . '::' . $userSearch;
  75. if(isset($this->metaData[$key])) {
  76. return $this->metaData[$key];
  77. }
  78. $adminGroups = array();
  79. $groups = array();
  80. $sortGroupsIndex = 0;
  81. $sortGroupsKeys = array();
  82. $sortAdminGroupsIndex = 0;
  83. $sortAdminGroupsKeys = array();
  84. foreach($this->getGroups($groupSearch) as $group) {
  85. $groupMetaData = $this->generateGroupMetaData($group, $userSearch);
  86. if (strtolower($group->getGID()) !== 'admin') {
  87. $this->addEntry(
  88. $groups,
  89. $sortGroupsKeys,
  90. $sortGroupsIndex,
  91. $groupMetaData);
  92. } else {
  93. //admin group is hard coded to 'admin' for now. In future,
  94. //backends may define admin groups too. Then the if statement
  95. //has to be adjusted accordingly.
  96. $this->addEntry(
  97. $adminGroups,
  98. $sortAdminGroupsKeys,
  99. $sortAdminGroupsIndex,
  100. $groupMetaData);
  101. }
  102. }
  103. //whether sorting is necessary is will be checked in sort()
  104. $this->sort($groups, $sortGroupsKeys);
  105. $this->sort($adminGroups, $sortAdminGroupsKeys);
  106. $this->metaData[$key] = array($adminGroups, $groups);
  107. return $this->metaData[$key];
  108. }
  109. /**
  110. * sets the sort mode, currently 0 (none) and 1 (user entries,
  111. * descending) are supported
  112. * @param int $sortMode (SORT_NONE, SORT_USERCOUNT)
  113. */
  114. public function setSorting($sortMode) {
  115. if($sortMode >= 0 && $sortMode <= 1) {
  116. $this->sorting = $sortMode;
  117. } else {
  118. $this->sorting = 0;
  119. }
  120. }
  121. /**
  122. * adds an group entry to the resulting array
  123. * @param array $entries the resulting array, by reference
  124. * @param array $sortKeys the sort key array, by reference
  125. * @param int $sortIndex the sort key index, by reference
  126. * @param array $data the group's meta data as returned by generateGroupMetaData()
  127. * @return null
  128. */
  129. private function addEntry(&$entries, &$sortKeys, &$sortIndex, $data) {
  130. $entries[] = $data;
  131. if($this->sorting === 1) {
  132. $sortKeys[$sortIndex] = $data['usercount'];
  133. $sortIndex++;
  134. }
  135. }
  136. /**
  137. * creates an array containing the group meta data
  138. * @param \OC\Group\Group $group
  139. * @param string $userSearch
  140. * @return array with the keys 'id', 'name' and 'usercount'
  141. */
  142. private function generateGroupMetaData(\OC\Group\Group $group, $userSearch) {
  143. return array(
  144. 'id' => $group->getGID(),
  145. 'name' => $group->getGID(),
  146. 'usercount' => $group->count($userSearch)
  147. );
  148. }
  149. /**
  150. * sorts the result array, if applicable
  151. * @param array $entries the result array, by reference
  152. * @param array $sortKeys the array containing the sort keys
  153. * @param return null
  154. */
  155. private function sort(&$entries, $sortKeys) {
  156. if($this->sorting > 0) {
  157. array_multisort($sortKeys, SORT_DESC, $entries);
  158. }
  159. }
  160. /**
  161. * returns the available groups
  162. * @param string $search a search string
  163. * @return \OC\Group\Group[]
  164. */
  165. private function getGroups($search = '') {
  166. if($this->isAdmin) {
  167. return $this->groupManager->search($search);
  168. } else {
  169. // FIXME: Remove static method call
  170. $groupIds = \OC_SubAdmin::getSubAdminsGroups($this->user);
  171. /* \OC_SubAdmin::getSubAdminsGroups() returns an array of GIDs, but this
  172. * method is expected to return an array with the GIDs as keys and group objects as
  173. * values, so we need to convert this information.
  174. */
  175. $groups = array();
  176. foreach($groupIds as $gid) {
  177. $group = $this->groupManager->get($gid);
  178. if (!is_null($group)) {
  179. $groups[$gid] = $group;
  180. }
  181. }
  182. return $groups;
  183. }
  184. }
  185. }