CustomFieldICPC2Code.php 4.15 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
<?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;
30
use Chill\MainBundle\Templating\TranslatableStringHelper;
Marc Ducobu's avatar
Marc Ducobu committed
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54

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

    /**
     *
     * @var EntityManagerInterface
     */
55
56
57
58
59
60
61
    private $om;
    
    /**
     *
     * @var TranslatableStringHelper 
     */
    private $translatableStringHelper;
Marc Ducobu's avatar
Marc Ducobu committed
62
63

    public function __construct(EntityManagerInterface $om, RequestStack $requestStack, 
64
        $defaultLocale, TwigEngine $templating, TranslatableStringHelper $translatableStringHelper)
Marc Ducobu's avatar
Marc Ducobu committed
65
66
67
68
69
    {
        $this->om = $om;
        $this->requestStack = $requestStack;
        $this->defaultLocale = $defaultLocale;
        $this->templating = $templating;
70
        $this->translatableStringHelper = $translatableStringHelper;
Marc Ducobu's avatar
Marc Ducobu committed
71
72
73
74
75
76
77
78
79
80
81
82
83
    }

    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
84
            $chap_locale = $chap->getNameByLocale($locale, $this->defaultLocale).' ('.$chap->getSlug().')';
Marc Ducobu's avatar
Marc Ducobu committed
85
86
87
            $codesChoice[$chap_locale] = array();

            foreach ($chap->getCodes() as $c) {
Marc Ducobu's avatar
Marc Ducobu committed
88
                $codesChoice[$chap_locale][$c->getCode()] = $c->getCode().' - '.$c->getNameByLocale($locale,$this->defaultLocale);
Marc Ducobu's avatar
Marc Ducobu committed
89
90
91
            }
        }

92
        $builder->add($customField->getSlug(), 'select2_choice', array(
Marc Ducobu's avatar
Marc Ducobu committed
93
            'choices' => $codesChoice,
Julien Fastré's avatar
Julien Fastré committed
94
            'empty_value' => 'Choose icpc code',
95
96
            'required' => false,
            'label' => $this->translatableStringHelper->localize($customField->getName())
Marc Ducobu's avatar
Marc Ducobu committed
97
98
99
100
101
102
103
104
        ));
    }

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

105
    public function render($value, CustomField $customField, $documentType = 'html')
Marc Ducobu's avatar
Marc Ducobu committed
106
    {
107
108
        $code = $this->deserialize($value, $customField);
        
109
110
111
112
        if (NULL === $code) {
            return '';
        }
        
113
114
115
116
        return $this->templating->render("ChillICPC2Bundle:CustomFieldsRendering:"
              . "icpc2.html.twig", array(
                 'icpc' => $code
              ));
Marc Ducobu's avatar
Marc Ducobu committed
117
118
119
120
121
122
123
124
125
126
    }

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

    public function deserialize($serialized, CustomField $customField)
    {
        return $this->om->getRepository('ChillICPC2Bundle:Code')
127
            ->findOneBy(array('code' => $serialized));
Marc Ducobu's avatar
Marc Ducobu committed
128
129
130
131
132
133
134
135
136
137
138
139
140
141
    }

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

}