diff --git a/Controller/IconFontController.php b/Controller/IconFontController.php new file mode 100644 index 00000000..e411a949 --- /dev/null +++ b/Controller/IconFontController.php @@ -0,0 +1,40 @@ +query->get('loader'); + $loaderData = unserialize($request->query->get('loader_data')); + + $iconFontManager = $this->get('kunstmaan_media.icon_font_manager'); + if (empty($loader)) { + $loader = $iconFontManager->getDefaultLoader(); + } else { + $loader = $iconFontManager->getLoader($loader); + } + $loader->setData($loaderData); + + return array( + 'loader' => $loader + ); + } +} diff --git a/DependencyInjection/Compiler/MediaHandlerCompilerPass.php b/DependencyInjection/Compiler/MediaHandlerCompilerPass.php index fc7b21fb..d120aa80 100644 --- a/DependencyInjection/Compiler/MediaHandlerCompilerPass.php +++ b/DependencyInjection/Compiler/MediaHandlerCompilerPass.php @@ -18,14 +18,20 @@ class MediaHandlerCompilerPass implements CompilerPassInterface */ public function process(ContainerBuilder $container) { - if (false === $container->hasDefinition('kunstmaan_media.media_manager')) { - return; + if ($container->hasDefinition('kunstmaan_media.media_manager')) { + $definition = $container->getDefinition('kunstmaan_media.media_manager'); + + foreach ($container->findTaggedServiceIds('kunstmaan_media.media_handler') as $id => $attributes) { + $definition->addMethodCall('addHandler', array(new Reference($id))); + } } - $definition = $container->getDefinition('kunstmaan_media.media_manager'); + if ($container->hasDefinition('kunstmaan_media.icon_font_manager')) { + $definition = $container->getDefinition('kunstmaan_media.icon_font_manager'); - foreach ($container->findTaggedServiceIds('kunstmaan_media.media_handler') as $id => $attributes) { - $definition->addMethodCall('addHandler', array(new Reference($id))); + foreach ($container->findTaggedServiceIds('kunstmaan_media.icon_font.loader') as $id => $attributes) { + $definition->addMethodCall('addLoader', array(new Reference($id), $id)); + } } } } diff --git a/Form/Type/IconFontType.php b/Form/Type/IconFontType.php new file mode 100644 index 00000000..76eb16de --- /dev/null +++ b/Form/Type/IconFontType.php @@ -0,0 +1,96 @@ +iconFontManager = $iconFontManager; + } + + /** + * @return string + */ + public function getParent() + { + return 'text'; + } + + /** + * @return string + */ + public function getName() + { + return 'iconfont'; + } + + /** + * Sets the default options for this type. + * + * @param OptionsResolverInterface $resolver The resolver for the options. + */ + public function setDefaultOptions(OptionsResolverInterface $resolver) + { + parent::setDefaultOptions($resolver); + + $resolver->setDefaults(array( + 'loader' => null, + 'loader_data' => null + )); + } + + /** + * Builds the form. + * + * This method is called for each type in the hierarchy starting form the + * top most type. Type extensions can further modify the form. + * + * @param FormBuilderInterface $builder The form builder + * @param array $options The options + * + * @see FormTypeExtensionInterface::buildForm() + */ + public function buildForm(FormBuilderInterface $builder, array $options) + { + if (!$options['loader']) { + $loader = $this->iconFontManager->getDefaultLoader(); + } else { + $loader = $this->iconFontManager->getLoader($options['loader']); + } + $loader->setData($options['loader_data']); + + $builder->setAttribute('loader', $options['loader']); + $builder->setAttribute('loader_object', $loader); + $builder->setAttribute('loader_data', $options['loader_data']); + } + + /** + * {@inheritdoc} + */ + public function buildView(FormView $view, FormInterface $form, array $options) + { + $view->vars['loader'] = $form->getConfig()->getAttribute('loader'); + $view->vars['loader_object'] = $form->getConfig()->getAttribute('loader_object'); + $view->vars['loader_data'] = serialize($form->getConfig()->getAttribute('loader_data')); + } + +} diff --git a/Helper/IconFont/AbstractIconFontLoader.php b/Helper/IconFont/AbstractIconFontLoader.php new file mode 100644 index 00000000..b56cf414 --- /dev/null +++ b/Helper/IconFont/AbstractIconFontLoader.php @@ -0,0 +1,24 @@ +rootPath = dirname($kernel->getRootDir()); + } +} \ No newline at end of file diff --git a/Helper/IconFont/DefaultIconFontLoader.php b/Helper/IconFont/DefaultIconFontLoader.php new file mode 100644 index 00000000..7979b85d --- /dev/null +++ b/Helper/IconFont/DefaultIconFontLoader.php @@ -0,0 +1,56 @@ +cssPath = trim($data['css'], '/'); + + $cssPath = $this->rootPath . '/web/' . $this->cssPath; + if (!file_exists($cssPath)) { + throw new InvalidOptionsException(sprintf('Could not find the css file with this path "%s"', $cssPath)); + } + } + + /** + * @return string + */ + public function getCssLink() + { + return '/' . $this->cssPath; + } + + /** + * @return array + */ + public function getCssClasses() + { + $contents = file_get_contents($this->rootPath . '/web/' . $this->cssPath); + + preg_match_all('/\.([a-zA-Z0-9-_]+):before[ ]*\{[ \n]*content:/', $contents, $matches); + + return $matches[1]; + } +} diff --git a/Helper/IconFont/IconFontLoaderInterface.php b/Helper/IconFont/IconFontLoaderInterface.php new file mode 100644 index 00000000..8447b66e --- /dev/null +++ b/Helper/IconFont/IconFontLoaderInterface.php @@ -0,0 +1,10 @@ +loaders[$serviceId] = $loader; + } + + /** + * @param IconFontLoaderInterface $loader + */ + public function setDefaultLoader(IconFontLoaderInterface $loader) + { + $this->defaultLoader = $loader; + } + + /** + * @param string $serviceId + * @return IconFontLoaderInterface + */ + public function getLoader($serviceId) + { + return $this->loaders[$serviceId]; + } + + /** + * @return IconFontLoaderInterface[] + */ + public function getLoaders() + { + return $this->loaders; + } + + /** + * @return IconFontLoaderInterface|null + */ + public function getDefaultLoader() + { + return $this->defaultLoader; + } +} diff --git a/Resources/config/routing.yml b/Resources/config/routing.yml index c2784ba6..a6797eed 100644 --- a/Resources/config/routing.yml +++ b/Resources/config/routing.yml @@ -16,4 +16,9 @@ KunstmaanMediaBundle_media_chooser: KunstmaanMediaBundle_media_folder: resource: "@KunstmaanMediaBundle/Controller/FolderController.php" type: annotation - prefix: /admin/media/folder + prefix: /admin/media/folder + +KunstmaanMediaBundle_icon_font: + resource: "@KunstmaanMediaBundle/Controller/IconFontController.php" + type: annotation + prefix: /admin/media/icon-font diff --git a/Resources/config/services.yml b/Resources/config/services.yml index 3e89e200..168c0069 100644 --- a/Resources/config/services.yml +++ b/Resources/config/services.yml @@ -3,9 +3,11 @@ parameters: kunstmaan_media.menu.adaptor.class: 'Kunstmaan\MediaBundle\Helper\Menu\MediaMenuAdaptor' kunstmaan_media.listener.doctrine.class: 'Kunstmaan\MediaBundle\EventListener\DoctrineMediaListener' kunstmaan_media.form.type.media.class: 'Kunstmaan\MediaBundle\Form\Type\MediaType' + kunstmaan_media.form.type.iconfont.class: 'Kunstmaan\MediaBundle\Form\Type\IconFontType' + kunstmaan_media.icon_font_manager.class: 'Kunstmaan\MediaBundle\Helper\IconFont\IconFontManager' + kunstmaan_media.icon_font.default_loader.class: 'Kunstmaan\MediaBundle\Helper\IconFont\DefaultIconFontLoader' kunstmaan_media.media_creator_service.class: 'Kunstmaan\MediaBundle\Helper\Services\MediaCreatorService' - services: liip_imagine.data.loader.stream.profile_photos: class: "%liip_imagine.data.loader.stream.class%" @@ -44,6 +46,23 @@ services: tags: - { name: form.type, alias: media } + form.type.iconfont: + class: "%kunstmaan_media.form.type.iconfont.class%" + arguments: ["@kunstmaan_media.icon_font_manager"] + tags: + - { name: form.type, alias: iconfont } + + kunstmaan_media.icon_font_manager: + class: "%kunstmaan_media.icon_font_manager.class%" + calls: + - [ setDefaultLoader, [ "@kunstmaan_media.icon_font.default_loader" ] ] + + kunstmaan_media.icon_font.default_loader: + class: "%kunstmaan_media.icon_font.default_loader.class%" + arguments: ["@kernel"] + tags: + - { name: 'kunstmaan_media.icon_font.loader' } + kunstmaan_media.media_creator_service: class: "%kunstmaan_media.media_creator_service.class%" arguments: ["@service_container"] diff --git a/Resources/views/Form/formWidgets.html.twig b/Resources/views/Form/formWidgets.html.twig index a3d4aa71..c7816e6c 100644 --- a/Resources/views/Form/formWidgets.html.twig +++ b/Resources/views/Form/formWidgets.html.twig @@ -1,3 +1,68 @@ +{% block iconfont_widget %} + {% spaceless %} +
{{ value|default('') }}
+