Commit ebb23257 authored by Marc Ducobu's avatar Marc Ducobu
Browse files

ICPC Code as a custom Field

parent 787bc69f
<?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) {
$chap_locale = $chap->getNameByLocale($locale, $this->defaultLocale);
$codesChoice[$chap_locale] = array();
foreach ($chap->getCodes() as $c) {
$codesChoice[$chap_locale][$c->getCode()] = $c->getNameByLocale($locale,$this->defaultLocale);
}
}
$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();
}
}
......@@ -41,31 +41,31 @@ class LoadChapter extends AbstractFixture implements OrderedFixtureInterface
echo "loading Chapters...\n";
$chapters = [
array("en" => "Process codes", "slug" => "-"),
array("en" => "General and Unspecified", "slug" => "A"),
array("en" => "Blood, Blood Forming, Organs and Immune Mechanism", "slug" => "B"),
array("en" => "Digestive", "slug" => "D"),
array("en" => "Eye", "slug" => "F"),
array("en" => "Ear", "slug" => "H"),
array("en" => "Cardiovascular", "slug" => "K"),
array("en" => "Musculoskeletal", "slug" => "L"),
array("en" => "Neurological", "slug" => "N"),
array("en" => "Psychological", "slug" => "P"),
array("en" => "Respiratory", "slug" => "R"),
array("en" => "Skin", "slug" => "S"),
array("en" => "Endocrine/Metabolic and Nutritional", "slug" => "T"),
array("en" => "Urological", "slug" => "U"),
array("en" => "Pregnancy, Childbearing, Family Planning", "slug" => "W"),
array("en" => "Female Genital", "slug" => "X"),
array("en" => "Male Genital", "slug" => "Y"),
array("en" => "Social Problems", "slug" => "Z")
array("en" => "Process codes", "fr" => "Procédures", "slug" => "-"),
array("en" => "General and Unspecified", "fr" => "Général et non spécifié", "slug" => "A"),
array("en" => "Blood, Blood Forming, Organs and Immune Mechanism", "fr" => "Sang, syst. hématop/ immunol.", "slug" => "B"),
array("en" => "Digestive", "fr" => "Syst. Digestif", "slug" => "D"),
array("en" => "Eye", "fr" => "Oeil", "slug" => "F"),
array("en" => "Ear", "fr" => "Oreille", "slug" => "H"),
array("en" => "Cardiovascular", "fr" => "Cardio-vasculaire", "slug" => "K"),
array("en" => "Musculoskeletal", "fr" => "Ostéo-articulaire", "slug" => "L"),
array("en" => "Neurological", "fr" => "Neurologique", "slug" => "N"),
array("en" => "Psychological", "fr" => "Psychologique", "slug" => "P"),
array("en" => "Respiratory", "fr" => "Respiratoire", "slug" => "R"),
array("en" => "Skin", "fr" => "Peau", "slug" => "S"),
array("en" => "Endocrine/Metabolic and Nutritional", "fr" => "Métabol., nutrit., endocrinien", "slug" => "T"),
array("en" => "Urological", "fr" => "Système Urinaire", "slug" => "U"),
array("en" => "Pregnancy, Childbearing, Family Planning", "fr" => "Grossesse, accouchement et PF", "slug" => "W"),
array("en" => "Female Genital", "fr" => "Syst.génital féminin et sein", "slug" => "X"),
array("en" => "Male Genital", "fr" => "Syst. génital masculin et sein", "slug" => "Y"),
array("en" => "Social Problems", "fr" => "Social", "slug" => "Z")
];
foreach ($chapters as $c) {
echo($c["en"]."\n");
$e = (new Chapter())
->setSlug($c["slug"])
->setName(array("en" => $c["en"]));
->setName(array("en" => $c["en"], "fr" => $c["fr"]));
$this->addReference($c["slug"], $e);
......
......@@ -775,7 +775,7 @@ $codes = [
->setCode($c[1])
->setChapter($this->getReference($c[1][0]))
->setComponent($this->getReference($c[0]))
->setName(array("en" => $c[2]));
->setName(array("fr" => $c[2]));
$manager->persist($e);
}
......
......@@ -21,6 +21,8 @@
namespace Chill\ICPC2Bundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
/**
* Chapter
*/
......@@ -41,6 +43,13 @@ class Chapter
*/
private $slug;
private $codes;
public function __construct()
{
$this->codes = new ArrayCollection();
}
/**
* Get id
......@@ -76,6 +85,20 @@ class Chapter
return $this->name;
}
/**
* Get name by locale
*
* @return string
*/
public function getNameByLocale($lang, $defaultLang)
{
if(array_key_exists($lang, $this->name)) {
return $this->name[$lang];
} else {
return $this->name[$defaultLang];
}
}
/**
* Set slug
*
......@@ -100,5 +123,15 @@ class Chapter
return $this->slug;
}
/**
* Get codes
*
* @return string
*/
public function getCodes()
{
return $this->codes;
}
}
......@@ -157,5 +157,19 @@ class Code
{
return $this->name;
}
/**
* Get name
*
* @return array
*/
public function getNameByLocale($lang, $defaultLang)
{
if(array_key_exists($lang, $this->name)) {
return $this->name[$lang];
} else {
return $this->name[$defaultLang];
}
}
}
......@@ -13,4 +13,10 @@ Chill\ICPC2Bundle\Entity\Chapter:
slug:
type: string
length: 1
oneToMany:
oneToMany:
codes:
orderBy: { 'id': 'ASC' }
targetEntity: Chill\ICPC2Bundle\Entity\Code
mappedBy: chapter
lifecycleCallbacks: { }
services:
# chill_icpc2.example:
# class: Chill\ICPC2Bundle\Example
# arguments: [@service_id, "plain_value", %parameter%]
chill.icp2.type:
class: Chill\ICPC2Bundle\CustomField\CustomFieldICPC2Code
arguments:
- "@doctrine.orm.entity_manager"
- "@request_stack"
- %locale%
- "@templating"
tags:
- { name: 'chill.custom_field', type: 'ICPC2' }
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment