Pool.php 5.8 KB

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