Pool.php 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313
  1. <?php
  2. /*
  3. * This file is part of the Sonata package.
  4. *
  5. * (c) Thomas Rabaix <thomas.rabaix@sonata-project.org>
  6. *
  7. * For the full copyright and license information, please view the LICENSE
  8. * file that was distributed with this source code.
  9. */
  10. namespace Sonata\AdminBundle\Admin;
  11. use Symfony\Component\DependencyInjection\ContainerInterface;
  12. class Pool
  13. {
  14. protected $container = null;
  15. protected $adminServiceIds = array();
  16. protected $adminGroups = array();
  17. protected $adminClasses = array();
  18. protected $templates = array();
  19. protected $title;
  20. protected $titleLogo;
  21. protected $options;
  22. /**
  23. * @param \Symfony\Component\DependencyInjection\ContainerInterface $container
  24. * @param string $title
  25. * @param string $logoTitle
  26. * @param array $options
  27. */
  28. public function __construct(ContainerInterface $container, $title, $logoTitle, $options = array())
  29. {
  30. $this->container = $container;
  31. $this->title = $title;
  32. $this->titleLogo = $logoTitle;
  33. $this->options = $options;
  34. }
  35. /**
  36. * @return array
  37. */
  38. public function getGroups()
  39. {
  40. $groups = $this->adminGroups;
  41. foreach ($this->adminGroups as $name => $adminGroup) {
  42. foreach ($adminGroup as $id => $options) {
  43. $groups[$name][$id] = $this->getInstance($id);
  44. }
  45. }
  46. return $groups;
  47. }
  48. /**
  49. * Returns whether an admin group exists or not.
  50. *
  51. * @param string $group
  52. * @return bool
  53. */
  54. public function hasGroup($group)
  55. {
  56. return isset($this->adminGroups[$group]);
  57. }
  58. /**
  59. * @return array
  60. */
  61. public function getDashboardGroups()
  62. {
  63. $groups = $this->adminGroups;
  64. foreach ($this->adminGroups as $name => $adminGroup) {
  65. if (isset($adminGroup['items'])) {
  66. foreach ($adminGroup['items'] as $key => $id) {
  67. $admin = $this->getInstance($id);
  68. if ($admin->showIn(Admin::CONTEXT_DASHBOARD)) {
  69. $groups[$name]['items'][$key] = $admin;
  70. } else {
  71. unset($groups[$name]['items'][$key]);
  72. }
  73. }
  74. }
  75. if (empty($groups[$name]['items'])) {
  76. unset($groups[$name]);
  77. }
  78. }
  79. return $groups;
  80. }
  81. /**
  82. * Returns all admins related to the given $group
  83. *
  84. * @param string $group
  85. * @return array
  86. * @throws \InvalidArgumentException
  87. */
  88. public function getAdminsByGroup($group)
  89. {
  90. if (!isset($this->adminGroups[$group])) {
  91. throw new \InvalidArgumentException(sprintf('Group "%s" not found in admin pool.', $group));
  92. }
  93. $admins = array();
  94. if (!isset($this->adminGroups[$group]['items'])) {
  95. return $admins;
  96. }
  97. foreach ($this->adminGroups[$group]['items'] as $id) {
  98. $admins[] = $this->getInstance($id);
  99. }
  100. return $admins;
  101. }
  102. /**
  103. * return the admin related to the given $class
  104. *
  105. * @param string $class
  106. *
  107. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  108. */
  109. public function getAdminByClass($class)
  110. {
  111. if (!$this->hasAdminByClass($class)) {
  112. return null;
  113. }
  114. return $this->getInstance($this->adminClasses[$class]);
  115. }
  116. /**
  117. * @param string $class
  118. *
  119. * @return bool
  120. */
  121. public function hasAdminByClass($class)
  122. {
  123. return isset($this->adminClasses[$class]);
  124. }
  125. /**
  126. * Returns an admin class by its Admin code
  127. * ie : sonata.news.admin.post|sonata.news.admin.comment => return the child class of post
  128. *
  129. * @param string $adminCode
  130. *
  131. * @return \Sonata\AdminBundle\Admin\AdminInterface|null
  132. */
  133. public function getAdminByAdminCode($adminCode)
  134. {
  135. $codes = explode('|', $adminCode);
  136. $admin = false;
  137. foreach ($codes as $code) {
  138. if ($admin == false) {
  139. $admin = $this->getInstance($code);
  140. } elseif ($admin->hasChild($code)) {
  141. $admin = $admin->getChild($code);
  142. }
  143. }
  144. return $admin;
  145. }
  146. /**
  147. * Returns a new admin instance depends on the given code
  148. *
  149. * @param string $id
  150. *
  151. * @return \Sonata\AdminBundle\Admin\AdminInterface
  152. */
  153. public function getInstance($id)
  154. {
  155. return $this->container->get($id);
  156. }
  157. /**
  158. * @return null|\Symfony\Component\DependencyInjection\ContainerInterface
  159. */
  160. public function getContainer()
  161. {
  162. return $this->container;
  163. }
  164. /**
  165. * @param array $adminGroups
  166. *
  167. * @return void
  168. */
  169. public function setAdminGroups(array $adminGroups)
  170. {
  171. $this->adminGroups = $adminGroups;
  172. }
  173. /**
  174. * @return array
  175. */
  176. public function getAdminGroups()
  177. {
  178. return $this->adminGroups;
  179. }
  180. /**
  181. * @param array $adminServiceIds
  182. *
  183. * @return void
  184. */
  185. public function setAdminServiceIds(array $adminServiceIds)
  186. {
  187. $this->adminServiceIds = $adminServiceIds;
  188. }
  189. /**
  190. * @return array
  191. */
  192. public function getAdminServiceIds()
  193. {
  194. return $this->adminServiceIds;
  195. }
  196. /**
  197. * @param array $adminClasses
  198. *
  199. * @return void
  200. */
  201. public function setAdminClasses(array $adminClasses)
  202. {
  203. $this->adminClasses = $adminClasses;
  204. }
  205. /**
  206. * @return array
  207. */
  208. public function getAdminClasses()
  209. {
  210. return $this->adminClasses;
  211. }
  212. /**
  213. * @param array $templates
  214. *
  215. * @return void
  216. */
  217. public function setTemplates(array $templates)
  218. {
  219. $this->templates = $templates;
  220. }
  221. /**
  222. * @return array
  223. */
  224. public function getTemplates()
  225. {
  226. return $this->templates;
  227. }
  228. /**
  229. * @param string $name
  230. *
  231. * @return null|string
  232. */
  233. public function getTemplate($name)
  234. {
  235. if (isset($this->templates[$name])) {
  236. return $this->templates[$name];
  237. }
  238. return null;
  239. }
  240. /**
  241. * @return string
  242. */
  243. public function getTitleLogo()
  244. {
  245. return $this->titleLogo;
  246. }
  247. /**
  248. * @return string
  249. */
  250. public function getTitle()
  251. {
  252. return $this->title;
  253. }
  254. /**
  255. * @param $name
  256. *
  257. * @return mixed
  258. */
  259. public function getOption($name)
  260. {
  261. if (isset($this->options[$name])) {
  262. return $this->options[$name];
  263. }
  264. return null;
  265. }
  266. }