Pool.php 6.6 KB

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