Extra fields: Add google-map-form-type-bundle for google map extra fields.

Requires google plugin + GOOGLE_MAPS_API_KEY
pull/4032/head
Julio 4 years ago
parent 081c7acc69
commit ee91d72d59
  1. 2
      .env
  2. 1
      composer.json
  3. 1
      config/bundles.php
  4. 2
      config/packages/oh_google_map_form_type.yaml
  5. 1
      config/packages/twig.yaml
  6. 71
      src/CoreBundle/Form/ExtraFieldType.php
  7. 1
      src/CoreBundle/Repository/TagRepository.php
  8. 96
      src/CoreBundle/Resources/views/Account/edit.html.twig

@ -28,6 +28,8 @@ APP_LOCALE='en_US'
APP_MULTIPLE_ACCESS_URL=''
GOOGLE_MAPS_API_KEY=''
#APP_API_PLATFORM_URL='http://localhost/api/' #deprecated
###< chamilo ###

@ -81,6 +81,7 @@
"knplabs/doctrine-behaviors": "~2.0",
"knplabs/knp-components": "~3.0",
"knpuniversity/oauth2-client-bundle": "~2.0",
"krixer/google-map-form-type-bundle": "^1.3",
"laminas/laminas-config": "~3.5",
"laminas/laminas-feed": "~2.14",
"laminas/laminas-filter": "~2.9",

@ -37,4 +37,5 @@ return [
Fidry\AliceDataFixtures\Bridge\Symfony\FidryAliceDataFixturesBundle::class => ['dev' => true, 'test' => true],
Hautelook\AliceBundle\HautelookAliceBundle::class => ['dev' => true, 'test' => true],
Sonata\Exporter\Bridge\Symfony\SonataExporterSymfonyBundle::class => ['all' => true],
Oh\GoogleMapFormTypeBundle\OhGoogleMapFormTypeBundle::class => ['all' => true],
];

@ -0,0 +1,2 @@
oh_google_map_form_type:
api_key: '%env(GOOGLE_MAPS_API_KEY)%'

@ -7,6 +7,7 @@ twig:
- '%kernel.project_dir%/public/plugin'
- '%kernel.project_dir%/src/CoreBundle/Resources/views/Layout'
form_themes:
- '@OhGoogleMapFormType/Form/fields.html.twig'
- 'form-theme.html.twig'
globals:

@ -12,10 +12,13 @@ use Chamilo\CoreBundle\Repository\ExtraFieldRepository;
use Chamilo\CoreBundle\Repository\ExtraFieldValuesRepository;
use Chamilo\CoreBundle\Repository\TagRepository;
use DateTime;
use GoogleMapsPlugin;
use Oh\GoogleMapFormTypeBundle\Form\Type\GoogleMapType;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\Extension\Core\Type\ChoiceType;
use Symfony\Component\Form\Extension\Core\Type\DateTimeType;
use Symfony\Component\Form\Extension\Core\Type\DateType;
use Symfony\Component\Form\Extension\Core\Type\HiddenType;
use Symfony\Component\Form\Extension\Core\Type\IntegerType;
use Symfony\Component\Form\Extension\Core\Type\NumberType;
use Symfony\Component\Form\Extension\Core\Type\TelType;
@ -64,6 +67,9 @@ class ExtraFieldType extends AbstractType
$data[$value->getField()->getVariable()] = $value->getValue();
}
$gMapsPlugin = GoogleMapsPlugin::create();
$geolocalization = 'true' === $gMapsPlugin->get('enable_api');
foreach ($extraFields as $extraField) {
$text = $extraField->getDisplayText();
$variable = $extraField->getVariable();
@ -86,11 +92,32 @@ class ExtraFieldType extends AbstractType
case \ExtraField::FIELD_TYPE_FILE:
case \ExtraField::FIELD_TYPE_LETTERS_SPACE:
case \ExtraField::FIELD_TYPE_ALPHANUMERIC_SPACE:
case \ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
case \ExtraField::FIELD_TYPE_GEOLOCALIZATION:
case \ExtraField::FIELD_TYPE_SELECT_WITH_TEXT_FIELD:
case \ExtraField::FIELD_TYPE_TRIPLE_SELECT:
//@todo
break;
case \ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
case \ExtraField::FIELD_TYPE_GEOLOCALIZATION:
if (!$geolocalization) {
break 2;
}
$defaultOptions['addr_options'] = [
'disabled' =>'disabled'
];
if (!empty($value)) {
$parts = explode('::', $value);
$coordinates = explode(',', $parts[1]);
$mapArray = [
'address' => $parts[0],
'latitude' => $coordinates[0],
'longitude' => $coordinates[1],
];
$defaultOptions['data'] = $mapArray;
}
$builder->add($variable, GoogleMapType::class, $defaultOptions);
break;
case \ExtraField::FIELD_TYPE_TAG:
$defaultOptions['expanded'] = false;
@ -220,30 +247,40 @@ class ExtraFieldType extends AbstractType
}
);*/
$builder->addEventListener(
FormEvents::PRE_SUBMIT,
function (FormEvent $event) use ($item, $extraFields): void {
$data = $event->getData();
foreach ($extraFields as $extraField) {
$newValue = $data[$extraField->getVariable()] ?? null;
if (\ExtraField::FIELD_TYPE_TAG === $extraField->getFieldType()) {
$formItem = $event->getForm()->get($extraField->getVariable());
$options = $formItem->getConfig()->getOptions();
$options['choices'] = $newValue;
$event->getForm()->add($extraField->getVariable(), ChoiceType::class, $options);
foreach ($newValue as $tag) {
$this->tagRepository->addTagToUser($extraField, $item, $tag);
}
continue;
}
switch ($extraField->getFieldType()) {
case \ExtraField::FIELD_TYPE_GEOLOCALIZATION_COORDINATES:
case \ExtraField::FIELD_TYPE_GEOLOCALIZATION:
if (!empty($newValue)) {
$newValue = $newValue['address'].'::'.$newValue['latitude'].','.$newValue['longitude'];
}
$this->extraFieldValuesRepository->updateItemData($extraField, $item, $newValue);
break;
case \ExtraField::FIELD_TYPE_TAG:
$formItem = $event->getForm()->get($extraField->getVariable());
$options = $formItem->getConfig()->getOptions();
$options['choices'] = $newValue;
$event->getForm()->add($extraField->getVariable(), ChoiceType::class, $options);
foreach ($newValue as $tag) {
$this->tagRepository->addTagToUser($extraField, $item, $tag);
}
break;
default:
if (empty($newValue)) {
break;
}
$this->extraFieldValuesRepository->updateItemData($extraField, $item, $newValue);
if (empty($newValue)) {
continue;
break;
}
$this->extraFieldValuesRepository->updateItemData($extraField, $item, $newValue);
}
}
);

@ -87,7 +87,6 @@ class TagRepository extends ServiceEntityRepository
->setTag($entityTag)
;
/** @var UserRelTag $element */
$exists = $user->getUserRelTags()->exists(
function ($key, $element) use ($userRelTag) {
return $userRelTag->getTag() === $element->getTag();

@ -1,70 +1,62 @@
{% extends "@ChamiloCore/Layout/layout_one_col.html.twig" %}
{% block content %}
{% autoescape false %}
{% autoescape false %}
<script>
document.addEventListener("DOMContentLoaded", function () {
$('.select2_user_rel_tag').each(function(i, obj) {
let fieldId = '&field_id=' + $(this).attr('data.field_id');
$(this).select2({
ajax: {
url: '{{ url('legacy_main', { 'name' : 'inc/ajax/extra_field.ajax.php', 'a': 'search_tags', 'type': 'user'}) }}' + fieldId,
processResults: function (data) {
/*let results = data.items.map(function(item){
let newItem = {
id: item.text,
text: item.text
};
return newItem;
});*/
return {
results: data.items
}
document.addEventListener("DOMContentLoaded", function () {
$('.select2_user_rel_tag').each(function(i, obj) {
let fieldId = '&field_id=' + $(this).attr('data.field_id');
$(this).select2({
ajax: {
url: '{{ url('legacy_main', { 'name' : 'inc/ajax/extra_field.ajax.php', 'a': 'search_tags', 'type': 'user'}) }}' + fieldId,
processResults: function (data) {
return {
results: data.items
}
},
cache: false,
tags: true,
tokenSeparators: [','],
});
}
},
cache: false,
tags: true,
tokenSeparators: [','],
});
$(this).on('select2:select', function (e) {
const data = e.params.data;
$(this).children('[value="' + data['id'] + '"]').attr({
'data-id': data['id'],
'value': data['text']
});
$(this).on('select2:select', function (e) {
const data = e.params.data;
$(this).children('[value="' + data['id'] + '"]').attr({
'data-id': data['id'],
'value': data['text']
});
});
$(this).on('select2:unselect', function (e) {
//let fieldId = '&tag_id=' + e.params.data.id;
let fieldId = '&tag_id=' + e.params.data.element.dataset.id;
$.ajax({
url: '{{ url('legacy_main', { 'name' : 'inc/ajax/extra_field.ajax.php', 'a': 'delete_tag', 'type': 'user'}) }}' + fieldId,
success: function(data) {
}
});
$(this).on('select2:unselect', function (e) {
//let fieldId = '&tag_id=' + e.params.data.id;
let fieldId = '&tag_id=' + e.params.data.element.dataset.id;
$.ajax({
url: '{{ url('legacy_main', { 'name' : 'inc/ajax/extra_field.ajax.php', 'a': 'delete_tag', 'type': 'user'}) }}' + fieldId,
success: function(data) {
}
});
});
$('.select2_user_rel_tag').refresh
});
});
</script>
<h3>{{ user.userIdentifier }}</h3>
<h3>{{ user.userIdentifier }}</h3>
{# <a href="{{ url('app_forgot_password_request') }}" class="btn btn-primary">#}
{# Reset password#}
{# </a>#}
<img class="inline-block h-16 w-16 rounded-full ring-2 ring-white"
src="{{ user | illustration }}?w=120&h=120&fit=crop"
alt=""
/>
<img class="inline-block h-16 w-16 rounded-full ring-2 ring-white"
src="{{ user | illustration }}?w=120&h=120&fit=crop"
alt=""
/>
{{ form_start(form, { 'action': path('chamilo_core_account_edit'), 'attr': { 'class': 'edit' } }) }}
{{ form_widget(form) }}
{{ form_start(form, { 'action': path('chamilo_core_account_edit'), 'attr': { 'class': 'edit' } }) }}
{{ form_widget(form) }}
<div>
<input class="btn btn-primary" name="update_profile" type="submit" value="{{ 'Update profile'|trans }}" />
</div>
{{ form_end(form) }}
{% endautoescape %}
<div>
<input class="btn btn-primary" name="update_profile" type="submit" value="{{ 'Update profile'|trans }}" />
</div>
{{ form_end(form) }}
{% endautoescape %}
{% endblock %}

Loading…
Cancel
Save