CustomFieldICPC2Code.php 3.41 KB
Newer Older
Marc Ducobu's avatar
Marc Ducobu committed
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
<?php

/*
 *  Chill is a software for social workers
 * Copyright (C) 2014, Champs Libres Cooperative SCRLFS, <http://www.champs-libres.coop>
 * 
 * This program is free software: you can redistribute it and/or modify
 *  it under the terms of the GNU Affero General Public License as
 * published by the Free Software Foundation, either version 3 of the
 *  License, or (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 * 
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

namespace Chill\ICPC2Bundle\CustomField;

use Symfony\Component\Form\FormBuilderInterface;
use Chill\CustomFieldsBundle\Entity\CustomField;
use Chill\CustomFieldsBundle\CustomFields\CustomFieldInterface;
use Chill\ICPC2BundleBundle\Entity\Code;
use Doctrine\ORM\EntityManagerInterface;
use Symfony\Component\HttpFoundation\RequestStack;
use Symfony\Bridge\Twig\TwigEngine;

/**
 * Todo
 */
class CustomFieldICPC2Code implements CustomFieldInterface
{
    /**
     * 
     * @var RequestStack
     */
    private $requestStack;
    
    private $defaultLocale;
    
    /**
     * 
     * @var TwigEngine
     */
    private $templating;

    /**
     *
     * @var EntityManagerInterface
     */
    public $om;

    public function __construct(EntityManagerInterface $om, RequestStack $requestStack, 
        $defaultLocale, TwigEngine $templating)
    {
        $this->om = $om;
        $this->requestStack = $requestStack;
        $this->defaultLocale = $defaultLocale;
        $this->templating = $templating;
    }

    public function buildForm(FormBuilderInterface $builder, CustomField $customField)
    {
        $ICPC2Chapters =  $this->om->getRepository('ChillICPC2Bundle:Chapter')
            ->findAll();
            //->findBy(array(array(), 'id' => 'ASC'));

        $codesChoice = array();

        $locale = $this->requestStack->getCurrentRequest()->getLocale();

        foreach  ($ICPC2Chapters as $chap) {
Marc Ducobu's avatar
Marc Ducobu committed
76
            $chap_locale = $chap->getNameByLocale($locale, $this->defaultLocale).' ('.$chap->getSlug().')';
Marc Ducobu's avatar
Marc Ducobu committed
77
78
79
            $codesChoice[$chap_locale] = array();

            foreach ($chap->getCodes() as $c) {
Marc Ducobu's avatar
Marc Ducobu committed
80
                $codesChoice[$chap_locale][$c->getCode()] = $c->getCode().' - '.$c->getNameByLocale($locale,$this->defaultLocale);
Marc Ducobu's avatar
Marc Ducobu committed
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
            }
        }

        $builder->add($customField->getSlug(), 'choice', array(
            'choices' => $codesChoice,
            'empty_value' => 'Choisissez le code CISP'
        ));
    }

    public function getName()
    {
        return 'CF ICPC2';
    }

    public function render($value, CustomField $customField)
    {
        
    }

    public function buildOptionsForm(FormBuilderInterface $builder)
    {
        return $builder;
    }

    public function deserialize($serialized, CustomField $customField)
    {
        return $this->om->getRepository('ChillICPC2Bundle:Code')
            ->findByCode($serialized);
    }

    /**
     * 
     * @param \Chill\CustomFieldsBundle\Entity\Adress $value
     * @param CustomField $customField
     * @return type
     */
    public function serialize($value, CustomField $customField)
    {
        return $value->getCode();
    }

}