move bundle to root dir for inclusion in packagist refs #259

This commit is contained in:
2014-11-04 17:10:59 +01:00
parent b826d8e132
commit 8011746b26
100 changed files with 5243 additions and 823 deletions

59
Entity/Adress.php Normal file
View File

@@ -0,0 +1,59 @@
<?php
namespace Chill\CustomFieldsBundle\Entity;
/**
* Adress
*/
class Adress
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $data;
public function __toString()
{
return $this->data . '(id:' .$this->id .')';
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set data
*
* @param string $data
*
* @return Adress
*/
public function setData($data)
{
$this->data = $data;
return $this;
}
/**
* Get data
*
* @return string
*/
public function getData()
{
return $this->data;
}
}

161
Entity/BlopEntity.php Normal file
View File

@@ -0,0 +1,161 @@
<?php
namespace Chill\CustomFieldsBundle\Entity;
/**
* BlopEntity
*/
class BlopEntity
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $field1;
/**
* @var string
*/
private $field2;
/**
* @var array
*/
private $customField = array();
private $adress;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
public function setAdress($a)
{
$this->adress = $a;
return $this;
}
/**
* Get field1
*
* @return string
*/
public function getAdress()
{
return $this->adress;
}
/**
* Set field1
*
* @param string $field1
*
* @return BlopEntity
*/
public function setField1($field1)
{
$this->field1 = $field1;
return $this;
}
/**
* Get field1
*
* @return string
*/
public function getField1()
{
return $this->field1;
}
/**
* Set field2
*
* @param string $field2
*
* @return BlopEntity
*/
public function setField2($field2)
{
$this->field2 = $field2;
return $this;
}
/**
* Get field2
*
* @return string
*/
public function getField2()
{
return $this->field2;
}
/**
* Set customField
*
* @param array $customField
*
* @return BlopEntity
*/
public function setCustomField(array $customField)
{
$this->customField = $customField;
return $this;
}
/**
* Get customField
*
* @return array
*/
public function getCustomField()
{
return $this->customField;
}
public function cfGet($key)
{
echo "<br> -1- <br>";
echo gettype($this->customField);
echo "<br> -2- <br>";
echo $this->customField;
echo "<br> -3- <br>";
var_dump(json_decode($this->customField));
echo "<br> -4- <br>";
echo json_last_error_msg();
$customFieldArray = json_decode($this->customField, true);
if(array_key_exists($key, $customFieldArray)) {
return $customFieldArray[$key];
} else {
return null;
}
}
public function cfSet($key, $value)
{
echo "-";
$customFieldArray = json_decode($this->customField, true);
$customFieldArray[$key] = $value;
$this->setCustomField(json_encode($customFieldArray));
var_dump($customFieldArray);
}
}

247
Entity/BlopEntity2.php Normal file
View File

@@ -0,0 +1,247 @@
<?php
namespace Chill\CustomFieldsBundle\Entity;
use Doctrine\ORM\EntityManager;
use Doctrine\Common\Persistence\Event\LifecycleEventArgs;
use Doctrine\ORM\Event\PreFlushEventArgs;
use Doctrine\Common\Collections\ArrayCollection;
use Chill\CustomFieldsBundle\Entity\Adress;
// ATTENTION QD NOUVEL OBJ cree sans appel a doctrine
// on n'a pas la config de custom fields
// ET DONC ON NE SAIT PAS QUELS VARIABLES SONT VIA __GET
// ET __SET SONT DECRITES PAR CUSTOM FIELDS
// IDEE : Travailler avec Lifecycle
// set et get pour JSON Field
// - dans un tableau special
// postLoad (après que l'élément sort du EM) :
// - récupération des customs fields (ok!)
// - json -> obj dans des choses qui existent deja
// preFlush avant mise a jour de la db
// - met a jour les donnees liees au json et le json
// perPersist idem mais pour le persist
/**
* BlopEntity2
*/
class BlopEntity2
{
/**
* @var integer
*/
private $id;
/**
* @var array
*/
private $customFieldData;
private $customFieldDataArray = array(); // customField apres json_decode
private $customFieldDataUnfolded = array(); // mise des entity de customFieldDataArray
private $customFieldConfigs = array();
private $customFieldConfigsLoaded = false;
// CHARGE DE LA DB LA CONFIG DES CUSTOM FIELDS
public function loadCustomFieldConfig(LifecycleEventArgs $args)
{
$em = $args->getObjectManager();
$customFields = $em
->getRepository('ChillCustomFieldsBundle:CustomField')
->findAll();
$customFieldsLablels = array_map(
function($e) { return $e->getLabel(); },
$customFields);
$this->customFieldConfigs = array_combine($customFieldsLablels, $customFields);
$this->customFieldConfigsLoaded = true;
}
// A PARTIR DU JSON CREE LES OBJETS (MIS DANS customFieldDataUnfolded)
public function unfoldCustomFieldData(LifecycleEventArgs $args)
{
$em = $args->getObjectManager();
$customFieldDataArray = json_decode($this->customFieldData,true);
$customFieldDataUnfolded = array();
foreach ($this->customFieldConfigs as $key => $cfConfig) {
$type = $cfConfig->getType();
if(strpos($type,'ManyToMany') === 0) {
$fieldUnfolded = new ArrayCollection();
if(array_key_exists($key, $customFieldDataArray)) {
$entityClass = substr($type, 11, -1);
foreach ($customFieldDataArray[$key] as $idEntity) {
$fieldUnfolded->add($em
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
->findOneById($idEntity));
}
}
$customFieldDataUnfolded[$key] = $fieldUnfolded;
} else if(strpos($type,'ManyToOne') === 0) {
$entityClass = 'Adress'; // substr($type,10,-1);
if(array_key_exists($key, $customFieldDataArray)) {
$customFieldDataUnfolded[$key] = $em
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
->findOneById($customFieldDataArray[$key]);
} else {
// TODO : doit tjs avoir un id
$em
->getRepository('ChillCustomFieldsBundle:' . $entityClass)
->findOneById(1);
}
}
else if ($type === 'text') {
if(array_key_exists($key, $customFieldDataArray)) {
$customFieldDataUnfolded[$key] = $customFieldDataArray[$key];
} else {
$customFieldDataUnfolded[$key] = '';
}
}
}
$this->customFieldDataUnfolded = $customFieldDataUnfolded;
}
// AVANT PERSIST LES ELEMENTS QUI N'ONT PAS D'ID DOIVENT EN AVOIR UN (OM->PERSIST(OBJ))
// PUIS PASSAGE DES OBJETS (SE TROUVANT DANS customFieldDataUnfolded) VERS
// LE JSON (SE TROUVANT DANS customFieldData)
public function prePersist(LifecycleEventArgs $args)
{
$em = $args->getObjectManager();
$this->loadCustomFieldConfig($args);
foreach ($this->customFieldDataUnfolded as $key => $unfoldedData) {
$type = $this->customFieldConfigs[$key]->getType();
if(strpos($type,'ManyToMany') === 0) {
foreach ($this->customFieldDataUnfolded[$key] as $entity) {
if(! $entity->getId()) {
$em->persist($entity);
}
}
} else if(strpos($type,'ManyToOne') === 0) {
if(! $this->customFieldDataUnfolded[$key]->getId()) {
$em->persist($this->customFieldDataUnfolded[$key]);
}
}
}
$this->customFieldDataUnfoldedToCustomField();
}
// PUIS PASSAGE DES OBJETS (SE TROUVANT DANS customFieldDataUnfolded) VERS
// LE JSON (SE TROUVANT DANS customFieldData)
public function preFlush(PreFlushEventArgs $args)
{
$this->customFieldDataUnfoldedToCustomField();
}
// PUIS PASSAGE DES OBJETS (SE TROUVANT DANS customFieldDataUnfolded) VERS
// LE JSON (SE TROUVANT DANS customFieldData)
public function customFieldDataUnfoldedToCustomField()
{
// MISE A JOUR DE customFieldDataArray
foreach ($this->customFieldConfigs as $key => $cfConfig) {
$type = $cfConfig->getType();
if(strpos($type,'ManyToMany') === 0) {
$arrayMapRet = array();
foreach ($this->customFieldDataUnfolded[$key] as $entity) {
$arrayMapRet[] = $entity->getId();
}
$this->customFieldDataArray[$key] = $arrayMapRet; // array_map(function($e) { $e->getId(); }, $this->customFieldDataUnfolded[$key]);
} else if(strpos($type,'ManyToOne') === 0) {
if(array_key_exists($key, $this->customFieldDataUnfolded)) {
$this->customFieldDataArray[$key] = $this->customFieldDataUnfolded[$key]->getId();
} else {
// normalement $this->customFieldDataArray[$key] ne doit pas exister
if(array_key_exists($key, $this->customFieldDataArray)) {
throw new Exception("Error Processing Request", 1);
}
//retirer de $this->customFieldDataArray[$key]
}
} else if ($type === 'text') {
$this->customFieldDataArray[$key] = $this->customFieldDataUnfolded[$key];
}
}
// MISE A JOUR DE CustomFieldData
$this->setCustomFieldData(json_encode($this->customFieldDataArray));
}
public function __set($fieldName, $value) {
$setMethodName = 'set' . ucfirst($fieldName);
if(method_exists($this, $setMethodName)) {
return $this->{$setMethodName}($value);
}
if(array_key_exists($fieldName, $this->customFieldConfigs)) {
$this->customFieldDataUnfolded[$fieldName] = $value;
} else if (!$this->customFieldConfigsLoaded) { // nouvel object pas eu d'appel doctrine avant
$this->customFieldDataUnfolded[$fieldName] = $value;
} else {
throw new Exception("Error Processing Request", 1);
}
}
public function __get($fieldName) {
$getMethodName = 'get' . ucfirst($fieldName);
if(method_exists($this, $getMethodName)) {
return $this->{$getMethodName}();
}
if(array_key_exists($fieldName, $this->customFieldDataUnfolded)) {
return $this->customFieldDataUnfolded[$fieldName];
} else if (!$this->customFieldConfigsLoaded) { // nouvel object pas eu d'appel doctrine avant
return null;
} else if(array_key_exists($fieldName, $this->customFieldConfigs)) { // pas init
return null;
} else {
throw new Exception("Error Processing Request", 1);
}
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set customField
*
* @param array $customField
*
* @return BlopEntity2
*/
public function setCustomFieldData($customFieldData)
{
$this->customFieldData = $customFieldData;
return $this;
}
/**
* Get customField
*
* @return array
*/
public function getCustomFieldData()
{
return $this->customFieldData;
}
}

280
Entity/CustomField.php Normal file
View File

@@ -0,0 +1,280 @@
<?php
namespace Chill\CustomFieldsBundle\Entity;
/**
* CustomField
*/
class CustomField
{
/**
* @var integer
*/
private $id;
/**
* @var string
*/
private $label;
private $slug;
/**
* @var string
*/
private $type;
/**
* @var boolean
*/
private $active;
/**
*
* @var array
*/
private $options = array();
/**
* @var array
*/
private $name;
/**
* @var float
*/
private $ordering;
/**
*
* @var int
*/
private $relation = 1;
const ONE_TO_ONE = 1;
const ONE_TO_MANY = 2;
/**
* @var CustomFieldsGroup
*/
private $customFieldGroup;
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
function getSlug()
{
return $this->slug;
}
/**
* Set label
*
* @param string $label
*
* @return CustomField
*/
public function setLabel($label)
{
$this->label = $label;
if ($this->slug === NULL) {
$this->slug = preg_replace('/[^A-Za-z0-9-]+/', '-', $label);
}
return $this;
}
function getOptions()
{
return $this->options;
}
/**
* Get label
*
* @return string
*/
public function getLabel()
{
return $this->label;
}
/**
* Set type
*
* @param string $type
*
* @return CustomField
*/
public function setType($type)
{
$this->type = $type;
return $this;
}
/**
* Get type
*
* @return string
*/
public function getType()
{
return $this->type;
}
function getRelation()
{
return $this->relation;
}
function setRelation($relation)
{
$this->relation = $relation;
return $this;
}
/**
* Set active
*
* @param boolean $active
*
* @return CustomField
*/
public function setActive($active)
{
$this->active = $active;
return $this;
}
/**
* Get active
*
* @return boolean
*/
public function getActive()
{
return $this->active;
}
/**
* Get customFieldGroup
*
* @return CustomFieldsGroup
*/
public function getCustomFieldsGroup()
{
return $this->customFieldGroup;
}
/**
* Set customFieldGroup
*
* @param \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup
*
* @return CustomField
*/
public function setCustomFieldsGroup(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup = null)
{
$this->customFieldGroup = $customFieldGroup;
return $this;
}
/**
* Set name
*
* @param array $name
*
* @return CustomField
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return array
*/
public function getName()
{
return $this->name;
}
/**
* Set order
*
* @param float $order
*
* @return CustomField
*/
public function setOrdering($order)
{
$this->ordering = $order;
return $this;
}
/**
* Get order
*
* @return float
*/
public function getOrdering()
{
return $this->ordering;
}
/**
* Set options
*
* @param array $options
*
* @return CustomField
*/
public function setOptions(array $options)
{
$this->options = $options;
return $this;
}
/**
* Set customFieldGroup
*
* @param \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup
*
* @return CustomField
*/
public function setCustomFieldGroup(\Chill\CustomFieldsBundle\Entity\CustomFieldsGroup $customFieldGroup = null)
{
$this->customFieldGroup = $customFieldGroup;
return $this;
}
/**
* Get customFieldGroup
*
* @return \Chill\CustomFieldsBundle\Entity\CustomFieldsGroup
*/
public function getCustomFieldGroup()
{
return $this->customFieldGroup;
}
}

View File

@@ -0,0 +1,146 @@
<?php
namespace Chill\CustomFieldsBundle\Entity;
/**
* CustomFieldGroup
*/
class CustomFieldsGroup
{
/**
* @var integer
*/
private $id;
/**
* @var array
*/
private $name;
/**
* @var string
*/
private $entity;
/**
* @var \Doctrine\Common\Collections\Collection
*/
private $customFields;
/**
* Constructor
*/
public function __construct()
{
$this->customFields = new \Doctrine\Common\Collections\ArrayCollection();
}
/**
* Add customField
*
* @param \Chill\CustomFieldsBundle\Entity\CustomField $customField
*
* @return CustomFieldsGroup
*/
public function addCustomField(\Chill\CustomFieldsBundle\Entity\CustomField $customField)
{
$this->customFields[] = $customField;
return $this;
}
/**
* Remove customField
*
* @param \Chill\CustomFieldsBundle\Entity\CustomField $customField
*/
public function removeCustomField(\Chill\CustomFieldsBundle\Entity\CustomField $customField)
{
$this->customFields->removeElement($customField);
}
/**
*
* @return \Doctrine\Common\Collections\Collection
*/
public function getCustomFields()
{
return $this->customFields;
}
/**
* Get id
*
* @return integer
*/
public function getId()
{
return $this->id;
}
/**
* Set name
*
* @param array $name
*
* @return CustomFieldsGroup
*/
public function setName($name)
{
$this->name = $name;
return $this;
}
/**
* Get name
*
* @return array
*/
public function getName($language = null)
{
//TODO set this in a service, PLUS twig function
if ($language) {
if (isset($this->name[$language])) {
return $this->name[$language];
} else {
foreach ($this->name as $name) {
if (!empty($name)) {
return $name;
}
}
}
return '';
} else {
return $this->name;
}
}
/**
* Set entity
*
* @param string $entity
*
* @return CustomFieldsGroup
*/
public function setEntity($entity)
{
$this->entity = $entity;
return $this;
}
/**
* Get entity
*
* @return string
*/
public function getEntity()
{
return $this->entity;
}
}