-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #151 from Kunstmaan/icon_font_field_type
Icon font field type
- Loading branch information
Showing
11 changed files
with
432 additions
and
7 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\MediaBundle\Controller; | ||
|
||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Route; | ||
use Sensio\Bundle\FrameworkExtraBundle\Configuration\Template; | ||
use Symfony\Bundle\FrameworkBundle\Controller\Controller; | ||
use Symfony\Component\HttpFoundation\Request; | ||
|
||
/** | ||
* IconFontController | ||
*/ | ||
class IconFontController extends Controller | ||
{ | ||
/** | ||
* @param Request $request | ||
* | ||
* @Route("/chooser", name="KunstmaanMediaBundle_icon_font_chooser") | ||
* @Template() | ||
* | ||
* @return array | ||
*/ | ||
public function iconFontChooserAction(Request $request) | ||
{ | ||
$loader = $request->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 | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,96 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\MediaBundle\Form\Type; | ||
|
||
use Kunstmaan\MediaBundle\Helper\IconFont\IconFontManager; | ||
use Symfony\Component\Form\AbstractType; | ||
use Symfony\Component\Form\FormBuilderInterface; | ||
use Symfony\Component\Form\FormInterface; | ||
use Symfony\Component\Form\FormView; | ||
use Symfony\Component\OptionsResolver\OptionsResolverInterface; | ||
|
||
/** | ||
* IconFontType | ||
*/ | ||
class IconFontType extends AbstractType | ||
{ | ||
/** | ||
* @var IconFontManager | ||
*/ | ||
private $iconFontManager; | ||
|
||
/** | ||
* @param IconFontManager $iconFontManager | ||
*/ | ||
public function __construct(IconFontManager $iconFontManager) | ||
{ | ||
$this->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')); | ||
} | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\MediaBundle\Helper\IconFont; | ||
|
||
use Symfony\Component\HttpKernel\KernelInterface; | ||
|
||
/** | ||
* AbstractIconFontLoader | ||
*/ | ||
abstract class AbstractIconFontLoader implements IconFontLoaderInterface | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
protected $rootPath; | ||
|
||
/** | ||
* @param KernelInterface $kernel | ||
*/ | ||
public function __construct(KernelInterface $kernel) | ||
{ | ||
$this->rootPath = dirname($kernel->getRootDir()); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,56 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\MediaBundle\Helper\IconFont; | ||
|
||
use Symfony\Component\OptionsResolver\Exception\InvalidOptionsException; | ||
use Symfony\Component\OptionsResolver\Exception\MissingOptionsException; | ||
|
||
/** | ||
* DefaultIconFontLoader | ||
*/ | ||
class DefaultIconFontLoader extends AbstractIconFontLoader | ||
{ | ||
/** | ||
* @var string | ||
*/ | ||
private $cssPath; | ||
|
||
/** | ||
* @param array $data | ||
* @throws MissingOptionsException | ||
* @throws InvalidOptionsException | ||
*/ | ||
public function setData(array $data) | ||
{ | ||
if (!array_key_exists('css', $data)) { | ||
throw new MissingOptionsException('Missing required loader_data option: "css"'); | ||
} | ||
|
||
$this->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]; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\MediaBundle\Helper\IconFont; | ||
|
||
interface IconFontLoaderInterface | ||
{ | ||
public function setData(array $data); | ||
public function getCssLink(); | ||
public function getCssClasses(); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,61 @@ | ||
<?php | ||
|
||
namespace Kunstmaan\MediaBundle\Helper\IconFont; | ||
|
||
/** | ||
* IconFontManager | ||
*/ | ||
class IconFontManager | ||
{ | ||
/** | ||
* @var IconFontLoaderInterface[] | ||
*/ | ||
protected $loaders = array(); | ||
|
||
/** | ||
* @var IconFontLoaderInterface | ||
*/ | ||
protected $defaultLoader = null; | ||
|
||
/** | ||
* @param IconFontLoaderInterface $loader | ||
* @param string $serviceId | ||
*/ | ||
public function addLoader(IconFontLoaderInterface $loader, $serviceId) | ||
{ | ||
$this->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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.