Pool.php 5.5 KB

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