Adding user extra fields in user edit page (not ready yet).

1.10.x
Julio Montoya 10 years ago
parent f27a4b8212
commit 75ce93ceca
  1. 2
      composer.json
  2. 50
      main/admin/user_edit.php
  3. 5
      src/Chamilo/CoreBundle/Entity/ExtraFieldValues.php
  4. 69
      src/Chamilo/CoreBundle/Entity/UserField.php
  5. 56
      src/Chamilo/CoreBundle/Entity/UserFieldValues.php
  6. 12
      src/Chamilo/CoreBundle/Resources/config/services.yml
  7. 1
      src/Chamilo/CoreBundle/Resources/views/Form/fields.html.twig
  8. 23
      src/Chamilo/CoreBundle/Resources/views/Legacy/form.html.twig
  9. 138
      src/Chamilo/CoreBundle/Resources/views/User/Form/_attributes.html.twig
  10. 22
      src/Chamilo/CoreBundle/Resources/views/User/Form/_main.html.twig
  11. 4
      src/Chamilo/CoreBundle/Resources/views/User/Form/_tabs.html.twig
  12. 12
      src/Chamilo/CoreBundle/Resources/views/User/_form.html.twig
  13. 8
      src/Chamilo/CoreBundle/Resources/views/User/create.html.twig
  14. 135
      src/Chamilo/CoreBundle/Resources/views/User/forms.html.twig
  15. 105
      src/Chamilo/UserBundle/Entity/User.php
  16. 61
      src/Chamilo/UserBundle/Form/Type/AttributeChoiceType.php
  17. 28
      src/Chamilo/UserBundle/Form/Type/AttributeEntityChoiceType.php
  18. 65
      src/Chamilo/UserBundle/Form/Type/AttributeType.php
  19. 105
      src/Chamilo/UserBundle/Form/Type/AttributeValueType.php
  20. 35
      src/Chamilo/UserBundle/Form/UserType.php

@ -143,6 +143,8 @@
"sylius/resource": "0.11",
"sylius/flow-bundle": "0.11",
"sylius/settings-bundle": "0.11",
"sylius/attribute-bundle": "0.11",
"sylius/variation": "0.11",
"apy/datagrid-bundle": "dev-master",
"jms/translation-bundle": "~1.0",

@ -11,48 +11,6 @@ $user_id = isset($_GET['user_id']) ? intval($_GET['user_id']) : intval($_POST['u
api_protect_super_admin($user_id, null, true);
$is_platform_admin = api_is_platform_admin() ? 1 : 0;
$htmlHeadXtra[] = '<script>
var is_platform_id = "'.$is_platform_admin.'";
function enable_expiration_date() {
document.user_edit.radio_expiration_date[0].checked=false;
document.user_edit.radio_expiration_date[1].checked=true;
}
function password_switch_radio_button(){
var input_elements = document.getElementsByTagName("input");
for (var i = 0; i < input_elements.length; i++) {
if(input_elements.item(i).name == "reset_password" && input_elements.item(i).value == "2") {
input_elements.item(i).checked = true;
}
}
}
function display_drh_list(){
var $radios = $("input:radio[name=platform_admin]");
if (document.getElementById("status_select").value=='.COURSEMANAGER.') {
if (is_platform_id == 1)
document.getElementById("id_platform_admin").style.display="block";
} else if (document.getElementById("status_select").value=='.STUDENT.') {
if (is_platform_id == 1)
document.getElementById("id_platform_admin").style.display="none";
$radios.filter("[value=0]").attr("checked", true);
} else {
if (is_platform_id == 1)
document.getElementById("id_platform_admin").style.display="none";
$radios.filter("[value=0]").attr("checked", true);
}
}
function show_image(image,width,height) {
width = parseInt(width) + 20;
height = parseInt(height) + 20;
window_x = window.open(image,\'windowX\',\'width=\'+ width + \', height=\'+ height + \' , resizable=0\');
}
</script>';
$noPHP_SELF = true;
$tool_name = get_lang('ModifyUserInfo');
$interbreadcrumb[] = array('url' => 'index.php', "name" => get_lang('PlatformAdmin'));
@ -405,10 +363,6 @@ $url_big_image = $big_image.'?rnd='.time();
// Display form
$content = $form->return_form();
$app['title'] = $tool_name;
//echo $message;
//echo $content;
$em = Container::getEntityManager();
$request = Container::getRequest();
@ -418,7 +372,7 @@ if (!empty($user_id)) {
}
$builder = Container::getFormFactory()->createBuilder(
new UserType(),
new UserType(Container::getSecurity()),
$user
);
@ -438,7 +392,7 @@ if ($form->isValid()) {
$urlAction = api_get_self().'?user_id='.$user_id;
echo Container::getTemplate()->render(
'ChamiloCoreBundle:Legacy:form.html.twig',
'ChamiloCoreBundle:User:create.html.twig',
array(
'form' => $form->createView(),
'url' => $urlAction

@ -56,6 +56,11 @@ class ExtraFieldValues
*/
protected $user;
public function __construct()
{
$this->tms = new \DateTime();
}
/**
* @param $user
* @return $this

@ -19,83 +19,116 @@ class UserField
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
protected $id;
/**
* @var integer
*
* @ORM\Column(name="field_type", type="integer", precision=0, scale=0, nullable=false, unique=false)
*/
private $fieldType;
protected $fieldType;
/**
* @var string
*
* @ORM\Column(name="field_variable", type="string", length=64, precision=0, scale=0, nullable=false, unique=false)
*/
private $fieldVariable;
protected $fieldVariable;
/**
* @var string
*
* @ORM\Column(name="field_display_text", type="string", length=64, precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldDisplayText;
protected $fieldDisplayText;
/**
* @var string
*
* @ORM\Column(name="field_default_value", type="text", precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldDefaultValue;
protected $fieldDefaultValue;
/**
* @var integer
*
* @ORM\Column(name="field_order", type="integer", precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldOrder;
protected $fieldOrder;
/**
* @var boolean
*
* @ORM\Column(name="field_visible", type="boolean", precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldVisible;
protected $fieldVisible;
/**
* @var boolean
*
* @ORM\Column(name="field_changeable", type="boolean", precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldChangeable;
protected $fieldChangeable;
/**
* @var boolean
*
* @ORM\Column(name="field_filter", type="boolean", precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldFilter;
protected $fieldFilter;
/**
* @var integer
*
* @ORM\Column(name="field_loggeable", type="integer", precision=0, scale=0, nullable=true, unique=false)
*/
private $fieldLoggeable;
protected $fieldLoggeable;
/**
* @var \DateTime
*
* @ORM\Column(name="tms", type="datetime", precision=0, scale=0, nullable=false, unique=false)
*/
private $tms;
protected $tms;
/**
* @var array
*
* @ORM\Column(name="configuration", type="text")
*/
protected $configuration = array();
public function __construct()
{
$this->tms = new \DateTime();
}
/**
* {@inheritdoc}
*/
public function __toString()
{
return $this->getFieldVariable();
}
/**
* {@inheritdoc}
*/
public function getConfiguration()
{
return $this->configuration;
}
/**
* {@inheritdoc}
*/
public function setConfiguration(array $configuration)
{
$this->configuration = $configuration;
return $this;
}
/**
* Get id
*
@ -129,6 +162,20 @@ class UserField
return $this->fieldType;
}
public function getFieldTypeToString()
{
switch ($this->fieldType) {
case \ExtraField::FIELD_TYPE_TEXT:
case \ExtraField::FIELD_TYPE_TEXTAREA:
return 'text';
case \ExtraField::FIELD_TYPE_RADIO:
case \ExtraField::FIELD_TYPE_SELECT:
return 'choice';
break;
}
}
/**
* Set fieldVariable
*

@ -4,6 +4,7 @@ namespace Chamilo\CoreBundle\Entity;
use Doctrine\ORM\Mapping as ORM;
use Gedmo\Mapping\Annotation as Gedmo;
//use Chamilo\UserBundle\Model\AttributeValueInterface;
/**
* UserFieldValues
@ -14,8 +15,8 @@ use Gedmo\Mapping\Annotation as Gedmo;
*/
class UserFieldValues extends ExtraFieldValues
{
/**
* The current user
* @var integer
*
* @ORM\Column(name="author_id", type="string", precision=0, scale=0, nullable=false, unique=false)
@ -62,7 +63,8 @@ class UserFieldValues extends ExtraFieldValues
/**
* Set questionId
*
* @param integer $questionId
* @param integer $id
*
* @return QuestionFieldValues
*/
public function setAuthorId($id)
@ -80,4 +82,54 @@ class UserFieldValues extends ExtraFieldValues
{
return $this->authorId;
}
/**
* @return ExtraField
*/
public function getExtraField()
{
return $this->field;
}
/**
* {@inheritdoc}
*/
public function setExtraField($attribute)
{
$this->field = $attribute;
return $this;
}
/**
* {@inheritdoc}
*/
public function getValue()
{
if ($this->fieldValue && AttributeTypes::CHECKBOX === $this->getExtraField()->getType()) {
return (Boolean) $this->fieldValue;
}
return $this->fieldValue;
}
/**
* {@inheritdoc}
*/
public function setValue($value)
{
$this->setFieldValue($value);
return $this;
}
public function getName()
{
return $this->getExtraField()->getFieldVariable();
}
public function getConfiguration()
{
return $this->getExtraField()->getConfiguration();
}
}

@ -20,6 +20,18 @@ services:
tags:
- { name: form.type, alias: yes_no }
chamilo_user_extra_field_value:
class: Chamilo\UserBundle\Form\Type\AttributeValueType
arguments: [Chamilo\CoreBundle\Entity\UserFieldValues, ["registration"], 'user']
tags:
- { name: form.type }
chamilo_user_extra_field_choice:
class: Chamilo\UserBundle\Form\Type\AttributeEntityChoiceType
arguments: [ 'user', Chamilo\CoreBundle\Entity\UserField]
tags:
- { name: form.type }
chamilo_core.html_editor:
class: Chamilo\CoreBundle\Component\Editor\CkEditor\CkEditor
arguments: [@translator, @router]

@ -15,3 +15,4 @@
{% endif %}
{% endspaceless %}
{% endblock %}

@ -4,5 +4,26 @@
<div class="box-body">
{{ form_widget(form) }}
</div>
</form>
{#<div class="tab-pane" id="attributes">#}
{#<div id="sylius-assortment-product-attributes" class="collection-container" data-prototype="{{ ('<div id="sylius_product_attributes___name__">' ~ form_row(form.extraFields.vars.prototype.attribute, {'attr': {'class': 'attribute-chooser'}}))|e }}{{ (form_row(form.extraFields.vars.prototype.value) ~ '</div>')|e }}">#}
{#{% for attributeForm in form.extraFields %}#}
{#<div class="sylius-assortment-product-attributes-attribute row">#}
{#<div class="col-md-10">#}
{#{{ form_widget(attributeForm) }}#}
{#</div>#}
{#<div class="col-md-2">#}
{#<a href="#" class="btn btn-danger" data-collection-button="delete" data-collection="sylius-assortment-product-attributes" data-collection-item="attribute"><i class="glyphicon glyphicon-trash"></i>&nbsp;{{ 'sylius.product.remove_attribute'|trans }}</a>#}
{#</div>#}
{#</div>#}
{#{% endfor %}#}
{#{% for key, prototype in form.extraFields.vars.prototype.vars.prototypes %}#}
{#<div id="attribute-prototype_{{ key }}" class="attribute-prototypes" data-prototype="{{ form_widget(prototype)|e }}"></div>#}
{#{% endfor %}#}
{#</div>#}
{#<a href="#" class="btn btn-success btn-block" data-collection-button="add" data-prototype="sylius-assortment-product-attributes" data-collection="sylius-assortment-product-attributes">#}
{#{{ 'sylius.product.add_attribute'|trans }}#}
{#</a>#}
{#</div>#}
{#</form>#}
</div>

@ -0,0 +1,138 @@
<script>
(function ($) {
'use strict';
var methods = {
init: function(options) {
var settings = $.extend({
'prototypePrefix': false,
'prototypeElementPrefix': '<hr />',
'containerSelector': false
}, options);
return this.each(function() {
show($(this), false);
$(this).change(function() {
show($(this), true);
});
function show(element, replace) {
var id = element.attr('id');
var selectedValue = element.val();
var prototypePrefix = id;
if (false != settings.prototypePrefix) {
prototypePrefix = settings.prototypePrefix;
}
var prototypeElement = $('#' + prototypePrefix + '_' + selectedValue);
var container;
if (settings.containerSelector) {
container = $(settings.containerSelector);
} else {
container = $(prototypeElement.data('container'));
}
if (!container.length) {
return;
}
if (!prototypeElement.length) {
container.empty();
return;
}
if (replace || !container.html().trim()) {
container.html(settings.prototypeElementPrefix + prototypeElement.data('prototype'));
}
}
});
}
};
$.fn.handlePrototypes = function(method) {
if (methods[method]) {
return methods[method].apply(this, Array.prototype.slice.call(arguments, 1));
} else if (typeof method === 'object' || !method) {
return methods.init.apply(this, arguments);
} else {
$.error( 'Method ' + method + ' does not exist on jQuery.handlePrototypes' );
}
};
})(jQuery);
var $collectionHolder;
// setup an "add a tag" link
var $addTagLink = $('<a href="#" class="add_tag_link">Add a tag</a>');
var $newLinkLi = $('<li></li>').append($addTagLink);
jQuery(document).ready(function() {
// Get the ul that holds the collection of tags
$collectionHolder = $('ul.tags');
// add the "add a tag" anchor and li to the tags ul
$collectionHolder.append($newLinkLi);
// count the current form inputs we have (e.g. 2), use that as the new
// index when inserting a new item (e.g. 2)
$collectionHolder.data('index', $collectionHolder.find(':input').length);
$addTagLink.on('click', function(e) {
// prevent the link from creating a "#" on the URL
e.preventDefault();
// add a new tag form (see next code block)
addTagForm($collectionHolder, $newLinkLi);
});
});
function addTagForm($collectionHolder, $newLinkLi) {
// Get the data-prototype explained earlier
var prototype = $collectionHolder.data('prototype');
// get the new index
var index = $collectionHolder.data('index');
// Replace '__name__' in the prototype's HTML to
// instead be a number based on how many items we have
var newForm = prototype.replace(/__name__/g, index);
// increase the index with one for the next item
$collectionHolder.data('index', index + 1);
// Display the form in the page in an li, before the "Add a tag" link li
var $newFormLi = $('<li></li>').append(newForm);
$newLinkLi.before($newFormLi);
}
</script>
<div class="tab-pane" id="attributes">
<ul class="tags" data-prototype="{{ form_widget(form.extraFields.vars.prototype)|e }}">
</ul>
{#<div id="sylius-assortment-product-attributes" class="collection-container" data-prototype="{{ ('<div id="sylius_product_attributes___name__">' ~ form_row(form.extraFields.vars.prototype.attribute, {'attr': {'class': 'attribute-chooser'}}))|e }}{{ (form_row(form.extraFields.vars.prototype.value) ~ '</div>')|e }}">#}
{#{% for attributeForm in form.extraFields %}#}
{#<div class="sylius-assortment-product-attributes-attribute row">#}
{#<div class="col-md-10">#}
{#{{ form_widget(attributeForm) }}#}
{#</div>#}
{#<div class="col-md-2">#}
{#<a href="#" class="btn btn-danger" data-collection-button="delete" data-collection="sylius-assortment-product-attributes" data-collection-item="attribute">#}
{#<i class="glyphicon glyphicon-trash"></i>&nbsp;{{ 'sylius.product.remove_attribute'|trans }}#}
{#</a>#}
{#</div>#}
{#</div>#}
{#{% endfor %}#}
{#{% for key, prototype in form.extraFields.vars.prototype.vars.prototypes %}#}
{#<div id="attribute-prototype_{{ key }}" class="attribute-prototypes" data-prototype="{{ form_widget(prototype)|e }}"></div>#}
{#{% endfor %}#}
{#</div>#}
<a href="#" class="btn btn-success btn-block" data-collection-button="add" data-prototype="sylius-assortment-product-attributes" data-collection="sylius-assortment-product-attributes">
{{ 'sylius.product.add_attribute'|trans }}
</a>
</div>

@ -0,0 +1,22 @@
<div class="tab-pane active" id="main">
{{ form_row(form.username) }}
{{ form_row(form.email) }}
{{ form_row(form.firstname) }}
{{ form_row(form.lastname) }}
{{ form_row(form.phone) }}
{{ form_row(form.official_code) }}
{{ form_row(form.timezone) }}
{{ form_row(form.locale) }}
{{ form_row(form.picture_uri) }}
<hr />
<div class="row">
<div class="col-md-6">
<hr />
</div>
<div class="col-md-6">
</div>
</div>
</div>

@ -0,0 +1,4 @@
<ul class="nav nav-tabs">
<li class="active"><a href="#main" data-toggle="tab">{{ 'User'|trans }}</a></li>
<li><a href="#attributes" data-toggle="tab">{{ 'Extra fields'|trans }}</a></li>
</ul>

@ -0,0 +1,12 @@
{% form_theme form '@ChamiloCore/User/forms.html.twig' %}
{{ form_errors(form) }}
{% include '@ChamiloCore/User/Form/_tabs.html.twig' %}
<br>
<div class="tab-content">
{% include '@ChamiloCore/User/Form/_main.html.twig' %}
{% include '@ChamiloCore/User/Form/_attributes.html.twig' %}
</div>
<hr>
{{ form_widget(form._token) }}

@ -0,0 +1,8 @@
{% from 'SyliusResourceBundle:Macros:actions.html.twig' import create %}
{{ form_errors(form) }}
<form action="{{ url }}" method="post" class="form-horizontal" {{ form_enctype(form) }} novalidate>
{% include '@ChamiloCore/User/_form.html.twig' %}
{{ create() }}
</form>

@ -0,0 +1,135 @@
{% extends 'form_div_layout.html.twig' %}
{% block choice_widget_collapsed %}
{% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
{{ parent() }}
{% endblock choice_widget_collapsed %}
{% block textarea_widget %}
{% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
{{ parent() }}
{% endblock textarea_widget %}
{% block form_widget_simple %}
{% spaceless %}
{% set attr = attr|merge({'class': attr.class|default('') ~ ' form-control'}) %}
{% set type = type|default('text') %}
{% if 'file' == type %}
<span class="btn-primary btn-lg file-overlay"><i class="icon-folder-open"></i> {{ 'sylius.form.choose_file'|trans }}</span>
{% endif %}
{{ parent() }}
{% endspaceless %}
{% endblock form_widget_simple %}
{% block form_label %}
{% spaceless %}
{% set label_attr = label_attr|merge({'class': label_attr.class|default('col-lg-2') ~ ' control-label'}) %}
{{ parent() }}
{% endspaceless %}
{% endblock form_label %}
{% block money_widget %}
{% spaceless %}
<div class="input-group">
<span class="input-group-addon">{{ money_pattern|replace({'{{ widget }}': ''}) }}</span>
{{ block('form_widget_simple') }}
</div>
{% endspaceless %}
{% endblock money_widget %}
{% block percent_widget %}
{% spaceless %}
<div class="input-group">
<span class="input-group-addon">%</span>
{{ block('form_widget_simple') }}
</div>
{% endspaceless %}
{% endblock percent_widget %}
{% block datetime_widget %}
{% spaceless %}
{% if widget == 'single_text' %}
{{ block('form_widget_simple') }}
{% else %}
<div class="form-group">
{{ form_widget(form.date) }}
{{ form_widget(form.time) }}
</div>
{% endif %}
{% endspaceless %}
{% endblock datetime_widget %}
{% block date_widget %}
{% spaceless %}
{% if widget == 'single_text' %}
{{ block('form_widget_simple') }}
{% else %}
{{ '{{ year }} / {{ month }} / {{ day }}'|replace({
'{{ year }}': form_widget(form.year, {'attr': {'style': 'display: inline; width: 100px;'}}),
'{{ month }}': form_widget(form.month, {'attr': {'style': 'display: inline; width: 100px;'}}),
'{{ day }}': form_widget(form.day, {'attr': {'style': 'display: inline; width: 100px;'}}),
})|raw }}
{% endif %}
{% endspaceless %}
{% endblock date_widget %}
{% block time_widget %}
{% spaceless %}
{% if widget == 'single_text' %}
{{ block('form_widget_simple') }}
{% else %}
{% set vars = widget == 'text' ? { 'attr': { 'size': 1, 'style': 'width: 60px; display: inline;' }} : {} %}
&nbsp;{{ form_widget(form.hour, vars) }}{% if with_minutes %}:{{ form_widget(form.minute, vars) }}{% endif %}{% if with_seconds %} {{ form_widget(form.second, vars) }}{% endif %}
{% endif %}
{% endspaceless %}
{% endblock time_widget %}
{% block form_row %}
{% spaceless %}
<div class="form-group{% if errors|length > 0 %} has-error{% endif %}">
{{ form_label(form) }}
<div class="col-lg-10">
{{ form_widget(form) }}
{% for error in errors %}
<span class="help-block form-error">
{{
error.messagePluralization is null
? error.messageTemplate|trans(error.messageParameters, 'validators')
: error.messageTemplate|transchoice(error.messagePluralization, error.messageParameters, 'validators')
}}
</span>
{% endfor %}
</div>
</div>
{% endspaceless %}
{% endblock form_row %}
{% block sonata_media_type_widget %}
<div class="col-xs-3 pull-left">
{% if value is not empty and value.providerReference %}
<div class="pull-left" style="margin-right: 5px">
{% thumbnail value, 'admin' with {'class': 'img-polaroid media-object'} %}
</div>
{% if sonata_admin_enabled is defined and sonata_admin_enabled %}
<a href="{{ url('admin_sonata_media_media_edit', {id: value.id}) }}"><strong>{{ value.name }}</strong></a>
{% else %}
<strong>{{ value.name }}</strong>
{% endif %}
<br />
<span type="label">{{ value.providerName|trans({}, 'SonataMediaBundle') }}</span> ~ {{ value.context }}
{% else %}
<div class="pull-left" style="margin-right: 5px">
<img src="{{ asset('bundles/sonatamedia/grey.png') }}" class="img-polaroid media-object" style="width: 85px; height: 85px"/>
</div>
<strong>{{ 'no_linked_media'|trans({}, 'SonataMediaBundle') }}</strong> <br />
<span type="label">{{ form.vars['provider']|trans({}, 'SonataMediaBundle') }} ~ {{ form.vars['context']|trans({}, 'SonataMediaBundle') }}</span>
{% endif %}
</div>
<div class="span3 pull-left">
{{ 'link_media'|trans({}, 'SonataMediaBundle') }}
{{ block('form_widget') }}
</div>
{% endblock sonata_media_type_widget %}

@ -17,6 +17,14 @@ use Chamilo\AdminThemeBundle\Model\UserInterface as ThemeUser;
//use Vich\UploaderBundle\Mapping\Annotation as Vich;
use Symfony\Component\HttpFoundation\File\File;
use Application\Sonata\MediaBundle\Entity\Media;
use Chamilo\UserBundle\Model\UserInterface as UserInterfaceModel;
use Doctrine\Common\Collections\Collection;
use Sylius\Component\Attribute\Model\AttributeValueInterface as BaseAttributeValueInterface;
use Sylius\Component\Variation\Model\OptionInterface as BaseOptionInterface;
use Sylius\Component\Variation\Model\VariantInterface as BaseVariantInterface;
use Chamilo\CoreBundle\Entity\ExtraFieldValues;
/**
* @ORM\HasLifecycleCallbacks
@ -310,7 +318,8 @@ class User extends BaseUser implements ParticipantInterface, ThemeUser
protected $sessionAsGeneralCoach;
/**
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\UserFieldValues", mappedBy="user")
* @var Collection
* @ORM\OneToMany(targetEntity="Chamilo\CoreBundle\Entity\UserFieldValues", mappedBy="user", orphanRemoval=true, cascade={"all"})
**/
protected $extraFields;
@ -1353,4 +1362,98 @@ class User extends BaseUser implements ParticipantInterface, ThemeUser
{
return $this->imageName;
}
// Model
public function getSlug()
{
return $this->getUsername();
}
public function setSlug($slug)
{
return $this->setUsername($slug);
}
/**
* {@inheritdoc}
*/
public function getExtraFields()
{
return $this->extraFields;
}
/**
* {@inheritdoc}
*/
public function setExtraFields(Collection $attributes)
{
foreach ($attributes as $attribute) {
$this->addExtraField($attribute);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function addExtraField(ExtraFieldValues $attribute)
{
if (!$this->hasExtraField($attribute)) {
$attribute->setUser($this);
$this->extraFields->add($attribute);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function removeExtraField(ExtraFieldValues $attribute)
{
if ($this->hasExtraField($attribute)) {
$this->extraFields->removeElement($attribute);
$attribute->setUser($this);
}
return $this;
}
/**
* {@inheritdoc}
*/
public function hasExtraField($attribute)
{
return $this->extraFields->contains($attribute);
}
/**
* {@inheritdoc}
*/
public function hasExtraFieldByName($attributeName)
{
foreach ($this->extraFields as $attribute) {
if ($attribute->getName() === $attributeName) {
return true;
}
}
return false;
}
/**
* {@inheritdoc}
*/
public function getExtraFieldByName($attributeName)
{
foreach ($this->extraFields as $attribute) {
if ($attribute->getName() === $attributeName) {
return $attribute;
}
}
return null;
}
}

@ -0,0 +1,61 @@
<?php
namespace Chamilo\UserBundle\Form\Type;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
/**
* Attribute choice form type.
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
abstract class AttributeChoiceType extends AbstractType
{
/**
* Name of the attributes subject.
*
* @var string
*/
protected $subjectName;
/**
* Attribute class name.
*
* @var string
*/
protected $className;
/**
* Constructor.
*
* @param string $subjectName
* @param string $className
*/
public function __construct($subjectName, $className)
{
$this->subjectName = $subjectName;
$this->className = $className;
}
/**
* {@inheritdoc}
*/
public function setDefaultOptions(OptionsResolverInterface $resolver)
{
$resolver
->setDefaults(array(
'class' => $this->className
))
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return sprintf('chamilo_%s_extra_field_choice', $this->subjectName);
}
}

@ -0,0 +1,28 @@
<?php
/*
* This file is part of the Sylius package.
*
* (c) Paweł Jędrzejewski
*
* For the full copyright and license information, please view the LICENSE
* file that was distributed with this source code.
*/
namespace Chamilo\UserBundle\Form\Type;
/**
* Attribute choice form type.
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
class AttributeEntityChoiceType extends AttributeChoiceType
{
/**
* {@inheritdoc}
*/
public function getParent()
{
return 'entity';
}
}

@ -0,0 +1,65 @@
<?php
namespace Chamilo\UserBundle\Form;
use Sylius\Bundle\AttributeBundle\Form\EventListener\BuildAttributeFormChoicesListener;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
use Sylius\Component\Attribute\Model\AttributeTypes;
use Symfony\Component\Form\FormBuilderInterface;
/**
* Attribute type.
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
* @author Leszek Prabucki <leszek.prabucki@gmail.com>
*/
class AttributeType extends AbstractResourceType
{
/**
* Subject name.
*
* @var string
*/
protected $subjectName;
/**
* Constructor.
*
* @param string $dataClass
* @param array $validationGroups
* @param string $subjectName
*/
public function __construct($dataClass, array $validationGroups, $subjectName)
{
parent::__construct($dataClass, $validationGroups);
$this->subjectName = $subjectName;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('name', 'text', array(
'label' => 'sylius.form.attribute.name'
))
->add('presentation', 'text', array(
'label' => 'sylius.form.attribute.presentation'
))
->add('type', 'choice', array(
'choices' => AttributeTypes::getChoices()
))
->addEventSubscriber(new BuildAttributeFormChoicesListener($builder->getFormFactory()))
;
}
/**
* {@inheritdoc}
*/
public function getName()
{
return sprintf('%s_extra_field', $this->subjectName);
}
}

@ -0,0 +1,105 @@
<?php
namespace Chamilo\UserBundle\Form\Type;
use Sylius\Bundle\AttributeBundle\Form\EventListener\BuildAttributeValueFormListener;
use Sylius\Bundle\ResourceBundle\Form\Type\AbstractResourceType;
//use Sylius\Component\Product\Model\AttributeInterface;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\Form\FormInterface;
use Symfony\Component\Form\FormView;
/**
* Attribute value form type.
*
* @author Paweł Jędrzejewski <pawel@sylius.org>
*/
class AttributeValueType extends AbstractResourceType
{
/**
* Attributes subject name.
*
* @var string
*/
protected $subjectName;
/**
* Constructor.
*
* @param string $dataClass
* @param array $validationGroups
* @param string $subjectName
*/
public function __construct($dataClass, array $validationGroups, $subjectName)
{
parent::__construct($dataClass, $validationGroups);
$this->subjectName = $subjectName;
}
/**
* {@inheritdoc}
*/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$builder
->add('extraField', sprintf('chamilo_%s_extra_field_choice', $this->subjectName))
->addEventSubscriber(new BuildAttributeValueFormListener($builder->getFormFactory()))
;
$prototypes = array();
foreach ($this->getAttributes($builder) as $attribute) {
$configuration = $attribute->getConfiguration();
$type = $attribute->getFieldTypeToString();
if (!is_array($configuration)) {
$configuration = array();
}
if (empty($type)) {
continue;
}
$prototypes[] = $builder->create(
'value',
$type,
$configuration
)->getForm();
}
$builder->setAttribute('prototypes', $prototypes);
}
/**
* {@inheritdoc}
*/
public function buildView(FormView $view, FormInterface $form, array $options)
{
$view->vars['prototypes'] = array();
foreach ($form->getConfig()->getAttribute('prototypes', array()) as $name => $prototype) {
$view->vars['prototypes'][$name] = $prototype->createView($view);
}
}
/**
* {@inheritdoc}
*/
public function getName()
{
return sprintf('chamilo_%s_extra_field_value', $this->subjectName);
//return 'chamilo_user_extra_field_value';
}
/**
* Get attributes
*
* @param FormBuilderInterface $builder
*
* @return AttributeInterface[]
*/
private function getAttributes(FormBuilderInterface $builder)
{
return $builder->get('extraField')->getOption('choice_list')->getChoices();
}
}

@ -3,18 +3,34 @@
namespace Chamilo\UserBundle\Form;
use Chamilo\UserBundle\Entity\User;
use Symfony\Component\Form\AbstractType;
use Symfony\Component\Form\FormBuilderInterface;
use Symfony\Component\OptionsResolver\OptionsResolverInterface;
use Chamilo\CoreBundle\Entity\Session;
use Symfony\Component\Security\Core\SecurityContext;
use Symfony\Component\Form\FormEvent;
use Symfony\Component\Form\FormEvents;
/**
* Class UserType
* @package Chamilo\UserBundle\Form
*/
class UserType extends AbstractType
{
private $securityContext;
public function __construct(SecurityContext $securityContext)
{
$this->securityContext = $securityContext;
}
/**
* @inheritdoc
**/
public function buildForm(FormBuilderInterface $builder, array $options)
{
$currentUser = $this->securityContext->getToken()->getUser();
$builder
->add('firstname', 'text')
->add('lastname', 'text')
@ -29,8 +45,25 @@ class UserType extends AbstractType
'context' => 'user_image',
'required' => false
))
->add('extraFields', 'collection', array(
'required' => false,
'type' => 'chamilo_user_extra_field_value',
'allow_add' => true,
'allow_delete' => true,
'by_reference' => false
))
->add('save', 'submit', array('label' => 'Update'))
;
// Update Author id
$builder->addEventListener(FormEvents::POST_SUBMIT, function (FormEvent $event) use ($currentUser) {
/** @var User $user */
$user = $event->getData();
$extraFields = $user->getExtrafields();
foreach ($extraFields as $extraField) {
$extraField->setAuthorId($currentUser->getId());
}
});
}
/**

Loading…
Cancel
Save