@@ -20,6 +20,28 @@ class CroppableImageType extends AbstractType
2020 protected $ galleryDir = null ;
2121 protected $ thumbsDir = null ;
2222
23+ static $ uploadConfig = array (
24+ 'uploadRoute ' => 'comur_api_upload ' ,
25+ 'uploadUrl ' => null ,
26+ 'webDir ' => null ,
27+ 'fileExt ' => '*.jpg;*.gif;*.png;*.jpeg ' ,
28+ 'libraryDir ' => null ,
29+ 'libraryRoute ' => 'comur_api_image_library ' ,
30+ 'showLibrary ' => true ,
31+ 'saveOriginal ' => false , //save original file name
32+ 'generateFilename ' => true //generate an uniq filename
33+ );
34+
35+ static $ cropConfig = array (
36+ // 'disableCrop' => false,
37+ 'minWidth ' => 1 ,
38+ 'minHeight ' => 1 ,
39+ 'aspectRatio ' => true ,
40+ 'cropRoute ' => 'comur_api_crop ' ,
41+ 'forceResize ' => false ,
42+ 'thumbs ' => null
43+ );
44+
2345 // public function getParent()
2446 // {
2547 // return 'text';
@@ -40,7 +62,7 @@ public function buildForm(FormBuilderInterface $builder, array $options)
4062 // $form->getParent()->add($options['uploadConfig']['saveOriginal'], 'hidden');
4163 // }
4264 // var_dump($builder->getDataMapper());exit;
43- if ($ options ['uploadConfig ' ]['saveOriginal ' ]){
65+ if ($ options ['uploadConfig ' ]['saveOriginal ' ]) {
4466 $ builder ->add ($ options ['uploadConfig ' ]['saveOriginal ' ], TextType::class, array (
4567 // 'inherit_data' => true,
4668 // 'property_path' => $options['uploadConfig']['saveOriginal'],
@@ -52,33 +74,51 @@ public function buildForm(FormBuilderInterface $builder, array $options)
5274 'attr ' => array ('style ' => 'opacity: 0;width: 0; max-width: 0; height: 0; max-height: 0; ' )));
5375 }
5476
77+ /**
78+ * Returns upload config normalizer. It can be used by compatible bundles to normalize parameters
79+ * @param $uploadConfig
80+ * @param $isGallery
81+ * @param $galleryDir
82+ * @return \Closure
83+ */
84+ public static function getUploadConfigNormalizer ($ uploadConfig , $ isGallery = false , $ galleryDir = null ) {
85+ return function (Options $ options , $ value ) use ($ uploadConfig , $ isGallery , $ galleryDir ) {
86+ $ config = array_merge ($ uploadConfig , $ value );
87+
88+ if ($ isGallery ) {
89+ $ config ['uploadUrl ' ] = $ config ['uploadUrl ' ] . '/ ' . $ galleryDir ;
90+ $ config ['webDir ' ] = $ config ['webDir ' ] . '/ ' . $ galleryDir ;
91+ $ config ['saveOriginal ' ] = false ;
92+ }
93+
94+ if (!isset ($ config ['libraryDir ' ])) {
95+ $ config ['libraryDir ' ] = $ config ['uploadUrl ' ];
96+ }
97+ // if($config['saveOriginal']){
98+ // $options['compound']=true;
99+ // }
100+ return $ config ;
101+ };
102+ }
103+
104+ /**
105+ * Returns crop config normalizer. It can be used by compatible bundles to normalize parameters
106+ * @param $cropConfig
107+ * @return \Closure
108+ */
109+ public static function getCropConfigNormalizer ($ cropConfig ) {
110+ return function (Options $ options , $ value ) use ($ cropConfig ) {
111+ return array_merge ($ cropConfig , $ value );
112+ };
113+ }
114+
55115 /**
56116 * {@inheritDoc}
57117 */
58118 public function configureOptions (OptionsResolver $ resolver )
59119 {
60-
61- $ uploadConfig = array (
62- 'uploadRoute ' => 'comur_api_upload ' ,
63- 'uploadUrl ' => null ,
64- 'webDir ' => null ,
65- 'fileExt ' => '*.jpg;*.gif;*.png;*.jpeg ' ,
66- 'libraryDir ' => null ,
67- 'libraryRoute ' => 'comur_api_image_library ' ,
68- 'showLibrary ' => true ,
69- 'saveOriginal ' => false , //save original file name
70- 'generateFilename ' => true //generate an uniq filename
71- );
72-
73- $ cropConfig = array (
74- // 'disableCrop' => false,
75- 'minWidth ' => 1 ,
76- 'minHeight ' => 1 ,
77- 'aspectRatio ' => true ,
78- 'cropRoute ' => 'comur_api_crop ' ,
79- 'forceResize ' => true ,
80- 'thumbs ' => null
81- );
120+ $ uploadConfig = self ::$ uploadConfig ;
121+ $ cropConfig = self ::$ cropConfig ;
82122
83123 $ resolver ->setDefaults (array (
84124 'uploadConfig ' => $ uploadConfig ,
@@ -95,28 +135,10 @@ public function configureOptions(OptionsResolver $resolver)
95135 $ galleryDir = $ this ->galleryDir ;
96136
97137 $ resolver ->setNormalizer (
98- 'uploadConfig ' , function (Options $ options , $ value ) use ($ uploadConfig , $ isGallery , $ galleryDir ){
99- $ config = array_merge ($ uploadConfig , $ value );
100-
101- if ($ isGallery ){
102- $ config ['uploadUrl ' ] = $ config ['uploadUrl ' ].'/ ' .$ galleryDir ;
103- $ config ['webDir ' ] = $ config ['webDir ' ].'/ ' .$ galleryDir ;
104- $ config ['saveOriginal ' ] = false ;
105- }
106-
107- if (!isset ($ config ['libraryDir ' ])){
108- $ config ['libraryDir ' ] = $ config ['uploadUrl ' ];
109- }
110- // if($config['saveOriginal']){
111- // $options['compound']=true;
112- // }
113- return $ config ;
114- }
138+ 'uploadConfig ' , self ::getUploadConfigNormalizer ($ uploadConfig , $ isGallery , $ galleryDir )
115139 );
116140 $ resolver ->setNormalizer (
117- 'cropConfig ' , function (Options $ options , $ value ) use ($ cropConfig ){
118- return array_merge ($ cropConfig , $ value );
119- }
141+ 'cropConfig ' , self ::getCropConfigNormalizer ($ cropConfig )
120142 );
121143
122144 }
@@ -140,11 +162,9 @@ public function buildView(FormView $view, FormInterface $form, array $options)
140162 $ cropConfig = $ options ['cropConfig ' ];
141163
142164 $ fieldImage = null ;
143- if (isset ($ cropConfig ['thumbs ' ]) && count ($ thumbs = $ cropConfig ['thumbs ' ]) > 0 )
144- {
165+ if (isset ($ cropConfig ['thumbs ' ]) && count ($ thumbs = $ cropConfig ['thumbs ' ]) > 0 ) {
145166 foreach ($ thumbs as $ thumb ) {
146- if (isset ($ thumb ['useAsFieldImage ' ]) && $ thumb ['useAsFieldImage ' ])
147- {
167+ if (isset ($ thumb ['useAsFieldImage ' ]) && $ thumb ['useAsFieldImage ' ]) {
148168 $ fieldImage = $ thumb ;
149169 }
150170 }
0 commit comments