Skip to content
Snippets Groups Projects
Commit 6e77caf7 authored by damjan's avatar damjan
Browse files

[TASK] Coping Attribute into Type (separating Attribute to Type and Property)

parent 0bdafea7
No related branches found
No related tags found
No related merge requests found
<?php
namespace SGalinski\TypoScriptBackendBundle\Entity;
use Doctrine\Common\Collections\ArrayCollection;
use Doctrine\ORM\Mapping as ORM;
/**
* Type
*
* @ORM\Table(uniqueConstraints={
* @ORM\UniqueConstraint(name="property_in_type_unique", columns={"name", "parent_id", "is_property_of"}),
* @ORM\UniqueConstraint(name="type_unique", columns={"name", "is_type", "typo3_group", "is_property_of"}),
* @ORM\UniqueConstraint(name="url_name_unique", columns={"url_name", "is_type", "typo3_group", "is_property_of"})
* })
* @ORM\Entity(repositoryClass="SGalinski\TypoScriptBackendBundle\Entity\AttributeRepository")
*/
class Type {
/**
* Constant used in $typo3Group field.
*/
const NORMAL_GROUP = 1;
/**
* Constant used in $typo3Group field.
*/
const PAGE_GROUP = 2;
/**
* Constant used in $typo3Group field.
*/
const USER_GROUP = 3;
/**
* @var integer
*
* @ORM\Column(name="id", type="integer")
* @ORM\Id
* @ORM\GeneratedValue(strategy="AUTO")
*/
private $id;
/**
* @var string
*
* @ORM\Column(name="name", type="string", length=255)
*/
private $name;
/**
* @var string
*
* @ORM\Column(name="url_name", type="string", length=255, nullable=true)
*/
private $urlName;
/**
* @var string
*
* @ORM\Column(name="description", type="text", nullable=true)
*/
private $description;
/**
* @var int
*
* @ORM\Column(name="is_property_of", type="smallint", options={"default" = 0})
*/
private $isPropertyOf = 0;
/**
* @var string The TYPO3 version in which the attribute was introduced
* Example: "4.5" "6.1" "6.2"), NULL is default and NULL is interpreted as most early version.
*
* @ORM\Column(name="min_version", type="decimal", precision=4, scale=1, nullable=true)
*/
private $minVersion = NULL;
/**
* @var string Last TYPO3 version in which the attribute existed.
* A version after which the attribute was deprecated.
* Example: "4.5" "6.1" "6.2"), NULL is default and is interpreted as latest version.
*
* @ORM\Column(name="max_version", type="decimal", precision=4, scale=1, nullable=true)
*/
private $maxVersion = NULL;
/**
* @var integer 3 = USER_GROUP, 2 = PAGE_GROUP, 1 = NORMAL_GROUP
*
* @ORM\Column(name="typo3_group", type="smallint", options={"default" = 1})
*/
private $typo3Group = Attribute::NORMAL_GROUP;
/**
* @var Attribute
*
* @ORM\ManyToOne(targetEntity="Attribute")
* @ORM\JoinColumn(name="type_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $type;
/**
* @var Attribute
*
* @ORM\ManyToOne(targetEntity="Attribute", inversedBy="children")
* @ORM\JoinColumn(name="parent_id", referencedColumnName="id", onDelete="CASCADE")
*/
private $parent;
/**
* @var Category
*
* @ORM\ManyToOne(targetEntity="Category")
* @ORM\JoinColumn(name="category", referencedColumnName="id")
*/
private $category;
/**
* @var ArrayCollection
*
* @ORM\OneToMany(targetEntity="Attribute", mappedBy="parent")
*/
private $children;
/**
* @var string
*
* @ORM\Column(name="default_value", type="string", length=511, nullable=true)
*/
private $default;
/**
* @var boolean
*
* @ORM\Column(name="is_type", type="boolean")
*/
private $isType;
/**
* @var boolean
*
* @ORM\Column(name="deleted", type="boolean")
*/
private $deleted = FALSE;
/**
* Get id
*
* @return integer
*/
public function getId() {
return $this->id;
}
/**
* Set name
*
* @param string $name
* @return Attribute
*/
public function setName($name) {
$this->name = $name;
$this->urlName = preg_replace('/[^a-z0-9_-]/is', '_', $name);
return $this;
}
/**
* Get name
*
* @return string
*/
public function getName() {
return $this->name;
}
/**
* Get urlName
*
* @return string
*/
public function getUrlName() {
return $this->urlName;
}
/**
* Set urlName
*
* @param string $name
* @return Attribute
*/
public function setUrlName($name) {
$this->urlName = $name;
return $this;
}
/**
* Set description
*
* @param string $description
* @return Attribute
*/
public function setDescription($description) {
$this->description = $description;
return $this;
}
/**
* Get description
*
* @return string
*/
public function getDescription() {
return $this->description;
}
/**
* Set minVersion
*
* @param string $minVersion
* @return Attribute
*/
public function setMinVersion($minVersion) {
$this->minVersion = $minVersion;
return $this;
}
/**
* Get minVersion
*
* @return string
*/
public function getMinVersion() {
return $this->minVersion;
}
/**
* @return string
*/
public function getMaxVersion() {
return $this->maxVersion;
}
/**
* @param string $maxVersion
* @return Attribute
*/
public function setMaxVersion($maxVersion) {
$this->maxVersion = $maxVersion;
return $this;
}
/**
* @param int $isPropertyOf
* @return void
*/
public function setIsPropertyOf($isPropertyOf) {
$this->isPropertyOf = (int) $isPropertyOf;
}
/**
* @return int
*/
public function getIsPropertyOf() {
return $this->isPropertyOf;
}
/**
* Set typo3Group
*
* @param integer $typo3Group
* @return Attribute
*/
public function setTypo3Group($typo3Group) {
$this->typo3Group = $typo3Group;
return $this;
}
/**
* Get typo3Group
*
* @return integer
*/
public function getTypo3Group() {
return $this->typo3Group;
}
/**
* Set parent
*
* @param Attribute $parent
* @return Attribute
*/
public function setParent($parent) {
$this->parent = $parent;
// I know... would be better to split the table into type and attribute
// reason - unique constraints are not really working well with NULL values (parent_id instead of this)
// Note: this has also another problem, because for types the field is currently misused... in the long run the table must be splitted
$this->isPropertyOf = (int) $parent->getId();
return $this;
}
/**
* Get parent
*
* @return Attribute
*/
public function getParent() {
return $this->parent;
}
/**
* Set children
*
* @param ArrayCollection $children
* @return Attribute
*/
public function setChildren($children) {
$this->children = $children;
return $this;
}
/**
* Get children
*
* @return ArrayCollection
*/
public function getChildren() {
return $this->children;
}
/**
* Adds one child to the list.
*
* @param Attribute $child
*/
public function addChild(Attribute $child) {
$this->children[] = $child;
}
/**
* Set isType
*
* @param boolean $isType
* @return Attribute
*/
public function setIsType($isType) {
$this->isType = $isType;
return $this;
}
/**
* Get isType
*
* @return boolean
*/
public function getIsType() {
return $this->isType;
}
/**
* Set deleted
*
* @param boolean $deleted
* @return Attribute
*/
public function setDeleted($deleted) {
$this->deleted = $deleted;
return $this;
}
/**
* Get deleted
*
* @return boolean
*/
public function getDeleted() {
return $this->deleted;
}
/**
* @return Attribute
*/
public function getType() {
return $this->type;
}
/**
* @param Attribute $type
* @return Attribute
*/
public function setType($type) {
$this->type = $type;
return $this;
}
/**
* @return string
*/
public function getDefault() {
return $this->default;
}
/**
* @param string $default
* @return Attribute
*/
public function setDefault($default) {
$this->default = $default;
return $this;
}
/**
* @return Category
*/
public function getCategory() {
return $this->category;
}
/**
* @param Category $category
* @return Attribute
*/
public function setCategory($category) {
$this->category = $category;
return $this;
}
}
\ No newline at end of file
<?php
namespace SGalinski\TypoScriptBackendBundle\Entity;
use Doctrine\ORM\EntityRepository;
use Doctrine\ORM\Query;
use Doctrine\ORM\QueryBuilder;
/**
* TypeRepository
*
* This class was generated by the Doctrine ORM. Add your own custom
* repository methods below.
*/
class TypeRepository extends EntityRepository {
/**
* Persists attribute to database if it doesn't exist in database.
* If the attribute is already persisted (has id), it is updated (merged).
*
* @param Attribute $attribute
* @return Attribute
*/
public function persistAttribute(Attribute $attribute) {
if ($attribute->getId() !== NULL) {
$attribute = $this->_em->merge($attribute);
} else {
$this->_em->persist($attribute);
}
$this->_em->flush();
return $attribute;
}
/**
* Deletes Attribute.
*
* @param Attribute $attribute
*/
public function deleteAttribute(Attribute $attribute) {
$this->_em->remove($attribute);
$this->_em->flush();
}
/**
* Finds attributes by parameters (if set): $isType, $typo3Group, $typo3Version.
* If $typo3Version is set, filters attributes with $typo3Version in minVersion-maxVersion range.
* Returns persisted objects.
*
* @param boolean $isType
* @param integer $typo3Group
* @param string $typo3Version - if NULL, fetches all versions.
* @return array of persisted objects
*/
public function findByVersionInRange($isType = NULL, $typo3Group = Attribute::NORMAL_GROUP, $typo3Version = NULL) {
$query = $this->selectQueryByIsTypeGroupVersion($isType, $typo3Group, $typo3Version);
$attributes = $query->getResult();
return $attributes;
}
/**
* Fetches by id a record from attribute table.
* Doesn't fetch records referenced by foreign keys, but fetches those foreign keys with other record data.
* Example of returned value:
*
* {
* "id": 55,
* "name": "kr",
* "minVersion": "1.0",
* "maxVersion": "7.4",
* "typo3Group": 1,
* "isType": false,
* "deleted": false,
* "type_id": "14",
* "parent_id": "59"
* }
*
* @param $id
* @return array - associative array
*/
public function findByIdForeignKeysInsteadOfObjects($id) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->where('attribute.id = :id')
->setParameter('id', $id);
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray[0];
}
/**
* Fetches by name a type from attribute table.
* Doesn't fetch records referenced by foreign keys, but fetches those foreign keys with other record data.
* Example of returned value:
*
* {
* "id": 55,
* "name": "XXX",
* "minVersion": "1.0",
* "maxVersion": "7.4",
* "typo3Group": 1,
* "isType": true,
* "deleted": false,
* "parent_id": "59"
* }
*
* @param $urlName
* @return array - associative array
*/
public function findTypeByUrlNameForeignKeysInsteadOfObjects($urlName) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->where('attribute.urlName = :urlName')
->andWhere('attribute.isType = TRUE')
->andWhere('attribute.deleted = 0')
->setParameter('urlName', $urlName);
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray[0];
}
/**
* Returns children with parent_id = $id as en array.
*
* @param $id - parent_id
* @param string|NULL $orderBy
* @return array
*/
public function getChildrenByParentId($id, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->where('attribute.parent = :id')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('id', $id);
if ($orderBy) {
$queryBuilder->orderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray;
}
/**
* Returns properties with parent_id = $id as en array with names of property types.
* Returns only properties which are not marked as deleted.
*
* @param $id - parent_id
* @param string|NULL $orderBy
* @param int $typo3Group
* @param string $typo3Version
* @return array example: [{"property":{"id":1163,"name":"wrap","description":"Wraps the links.","minVersion":"1.0","maxVersion":"7.4",
* example: [{"property":{"id":1163,"name":"wrap","description":"Wraps the links.","minVersion":"1.0","maxVersion":"7.4",
* "typo3Group":1,"default":"\n","isType":false,"deleted":false,"type_id":"687","parent_id":"1164"},
* "type_name":"wrap","type_deleted":false}, ...]
*/
public function getPropertiesByParentIdWithTypeNames(
$id, $orderBy = NULL, $typo3Group = Attribute::NORMAL_GROUP, $typo3Version = NULL
) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->select(
'attribute AS property, property_type.name AS type_name, property_type.urlName AS type_url_name, property_type.deleted AS type_deleted'
)
->from('TypoScriptBackendBundle:Attribute', 'property_type')
->where('attribute.parent = :id AND property_type.id = attribute.type')
->andWhere(':typo3Group IS NULL OR attribute.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR attribute.minVersion IS NULL OR attribute.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR attribute.maxVersion IS NULL OR attribute.maxVersion >= :typo3Version')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version)
->setParameter('id', $id);
if ($orderBy) {
$queryBuilder->orderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray;
}
/**
* Lists all attributes by $version, $isType, $typo3Group parameters.
* Returns non persisted array of attributes which are not marked as deleted.
*
* @param $version
* @param $isType
* @param $typo3Group
* @param bool $namesOnly - If true returns names, urlNames and id only
* @param string|NULL $orderBy
* @return array - non persisted array of attributes
*/
public function listAttributes($version, $isType, $typo3Group, $namesOnly = FALSE, $orderBy = NULL) {
// TODO: "namesOnly" optimisation is dropped because the query "namesOnlySelectQueryByIsTypeGroupVersion"
// selects only attributes which have NOT NULL in category field.
// if ($namesOnly) {
// $query = $this->namesOnlySelectQueryByIsTypeGroupVersion($isType, $typo3Group, $version, $orderBy);
// } else {
$query = $this->selectQueryByIsTypeGroupVersion($isType, $typo3Group, $version, $orderBy);
// }
$query->setHint(Query::HINT_INCLUDE_META_COLUMNS, TRUE);
$attributeArray = $query->getArrayResult();
return $attributeArray;
}
/**
* Returns the query which lists all attributes by $version, $isType, $typo3Group parameters.
* The query returns only attributes which are not marked as deleted.
* All columns are selected.
*
* @param $isType
* @param $typo3Group
* @param $typo3Version
* @param string|NULL $orderBy
* @return Query
*/
protected function selectQueryByIsTypeGroupVersion($isType, $typo3Group, $typo3Version, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->select('attribute')
->leftJoin('attribute.category', 'categoryTable')
->where(':isType IS NULL OR attribute.isType = :isType')
->andWhere(':typo3Group IS NULL OR attribute.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR attribute.minVersion IS NULL OR attribute.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR attribute.maxVersion IS NULL OR attribute.maxVersion >= :typo3Version')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('isType', $isType)
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version);
if ($orderBy) {
$queryBuilder->addOrderBy('categoryTable.name', 'DESC')
->addOrderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
return $query;
}
/**
* Returns the query which lists all attributes by $version, $isType, $typo3Group parameters.
* The query returns only attributes which are not marked as deleted.
* Only id, urlName and name columns are selected.
*
* @param $isType
* @param $typo3Group
* @param $typo3Version
* @param string|NULL $orderBy
* @return Query
*/
protected function namesOnlySelectQueryByIsTypeGroupVersion($isType, $typo3Group, $typo3Version, $orderBy = NULL) {
/** @var QueryBuilder $queryBuilder */
$queryBuilder = $this->createQueryBuilder('attribute')
->select('attribute.id, attribute.urlName, attribute.name AS name, categoryTable.name AS category')
->from('TypoScriptBackendBundle:Category', 'categoryTable')
->where('attribute.category = categoryTable.id')
->andWhere(':isType IS NULL OR attribute.isType = :isType')
->andWhere(':typo3Group IS NULL OR attribute.typo3Group = :typo3Group')
->andWhere(':typo3Version IS NULL OR attribute.minVersion IS NULL OR attribute.minVersion <= :typo3Version')
->andWhere(':typo3Version IS NULL OR attribute.maxVersion IS NULL OR attribute.maxVersion >= :typo3Version')
->andWhere('attribute.deleted IS NULL OR attribute.deleted = FALSE')
->setParameter('isType', $isType)
->setParameter('typo3Group', $typo3Group)
->setParameter('typo3Version', $typo3Version);
if ($orderBy) {
$queryBuilder->addOrderBy('categoryTable.name', 'DESC')
->addOrderBy('attribute.' . $orderBy);
}
/** @var Query $query */
$query = $queryBuilder->getQuery();
return $query;
}
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment