Pool.php 5.1 KB

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