Show the number of parts with this element and all its child elements in Admin pages

This commit is contained in:
Jan Böhmer 2022-09-21 13:20:57 +02:00
parent a9642b27a6
commit 32638777d9
4 changed files with 54 additions and 14 deletions

View file

@ -93,7 +93,7 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement
* @NoneOfItsChildren() * @NoneOfItsChildren()
* @Groups({"include_parents"}) * @Groups({"include_parents"})
*/ */
protected $parent; protected $parent = null;
/** @var string[] all names of all parent elements as a array of strings, /** @var string[] all names of all parent elements as a array of strings,
* the last array element is the name of the element itself * the last array element is the name of the element itself
@ -271,16 +271,17 @@ abstract class AbstractStructuralDBElement extends AttachmentContainingDBElement
*/ */
public function getSubelements(): iterable public function getSubelements(): iterable
{ {
return $this->children; return $this->children ?? new ArrayCollection();
} }
/** /**
* @see getSubelements()
* @return Collection<static>|iterable * @return Collection<static>|iterable
* @psalm-return Collection<int, static> * @psalm-return Collection<int, static>
*/ */
public function getChildren(): iterable public function getChildren(): iterable
{ {
return $this->children; return $this->getSubelements();
} }
public function isNotSelectable(): bool public function isNotSelectable(): bool

View file

@ -45,6 +45,23 @@ abstract class AbstractPartsContainingRepository extends StructuralDBElementRepo
*/ */
abstract public function getPartsCount(object $element): int; abstract public function getPartsCount(object $element): int;
/**
* Returns the count of the parts associated with this element and all its children.
* Please be aware that this function is pretty slow on large trees!
* @param AbstractPartsContainingDBElement $element
* @return int
*/
public function getPartsCountRecursive(AbstractPartsContainingDBElement $element): int
{
$count = $this->getPartsCount($element);
foreach ($element->getChildren() as $child) {
$count += $this->getPartsCountRecursive($child);
}
return $count;
}
protected function getPartsByField(object $element, array $order_by, string $field_name): array protected function getPartsByField(object $element, array $order_by, string $field_name): array
{ {
if (!$element instanceof AbstractPartsContainingDBElement) { if (!$element instanceof AbstractPartsContainingDBElement) {

View file

@ -1,15 +1,15 @@
{% import "helper.twig" as helper %} {% import "helper.twig" as helper %}
<div class="form-group row"> <div class="form-group row">
<label class="col-form-label col-md-3">{% trans %}id.label{% endtrans %}</label> <label class="col-form-label col-md-4">{% trans %}id.label{% endtrans %}</label>
<div class="col-md-9"> <div class="col-md-8">
<p class="form-control-plaintext">{% if entity.iD %}{{ entity.id }}{% else %}-{% endif %}</p> <p class="form-control-plaintext">{% if entity.iD %}{{ entity.id }}{% else %}-{% endif %}</p>
</div> </div>
</div> </div>
<div class="form-group row"> <div class="form-group row">
<label class="col-form-label col-md-3">{% trans %}createdAt{% endtrans %}</label> <label class="col-form-label col-md-4">{% trans %}createdAt{% endtrans %}</label>
<div class="col-md-9"> <div class="col-md-8">
<p class="form-control-plaintext"> <p class="form-control-plaintext">
{% if entity.id is not null and date(entity.addedDate) > date('1900/01/01') %} {% if entity.id is not null and date(entity.addedDate) > date('1900/01/01') %}
@ -22,8 +22,8 @@
</div> </div>
<div class="form-group row"> <div class="form-group row">
<label class="col-form-label col-md-3">{% trans %}lastModified{% endtrans %}</label> <label class="col-form-label col-md-4">{% trans %}lastModified{% endtrans %}</label>
<div class="col-md-9"> <div class="col-md-8">
<p class="form-control-plaintext"> <p class="form-control-plaintext">
{% if entity.id is not null and date(entity.lastModified) > date('1900/01/01') %} {% if entity.id is not null and date(entity.lastModified) > date('1900/01/01') %}
{{ helper.date_user_combination(entity, true, "long") }} {{ helper.date_user_combination(entity, true, "long") }}
@ -35,9 +35,9 @@
</div> </div>
<div class="form-group row"> <div class="form-group row">
<label class="col-form-label col-md-3">{% trans %}entity.info.parts_count{% endtrans %}</label> <label class="col-form-label col-md-4">{% trans %}entity.info.parts_count{% endtrans %}</label>
<div class="col-md-9"> <div class="col-md-8">
<p class="form-control-static"> <p class="form-control-plaintext">
{% if entity.id and partsContainingElement %} {% if entity.id and partsContainingElement %}
{{ repo.partsCount(entity) }} {{ repo.partsCount(entity) }}
{% else %} {% else %}
@ -45,4 +45,20 @@
{% endif %} {% endif %}
</p> </p>
</div> </div>
</div> </div>
{# Check if we really have a structural element #}
{% if entity.parent is defined %}
<div class="form-group row">
<label class="col-form-label col-md-4">{% trans %}entity.info.parts_count_recursive{% endtrans %}</label>
<div class="col-md-8">
<p class="form-control-plaintext">
{% if entity.id and partsContainingElement %}
{{ repo.partsCountRecursive(entity) }}
{% else %}
-
{% endif %}
</p>
</div>
</div>
{% endif %}

View file

@ -1092,7 +1092,7 @@ Sub elements will be moved upwards.]]></target>
</notes> </notes>
<segment> <segment>
<source>entity.info.parts_count</source> <source>entity.info.parts_count</source>
<target>Count of parts with this element</target> <target>Number of parts with this element</target>
</segment> </segment>
</unit> </unit>
<unit id="4FQqOUW" name="specifications.property"> <unit id="4FQqOUW" name="specifications.property">
@ -9771,5 +9771,11 @@ Element 3</target>
<target>Target element ID</target> <target>Target element ID</target>
</segment> </segment>
</unit> </unit>
<unit id="_vbPEFP" name="category.edit.partname_regex.placeholder">
<segment>
<source>category.edit.partname_regex.placeholder</source>
<target>Number of parts with this element or its subelements</target>
</segment>
</unit>
</file> </file>
</xliff> </xliff>