/**
* Retrieves all elements that have a specific tag or one of its child tags assigned
*
* @param Tag $tag The tag to search for
* @param string $type The type of elements to search for: 'document', 'asset' or 'object'
* @param array $subtypes Filter by subtypes, eg. page, object, email, folder etc.
* @param array $classNames For objects only: filter by classnames
* @param bool $considerChildTags Look for elements having one of $tag's children assigned
*
* @return array
*/
public function getElementsForTag(Tag $tag, $type, array $subtypes = [], array $classNames = [], $considerChildTags = false)
{
$elements = [];
$map = ['document' => ['documents', 'id', 'type', '\\Pimcore\\Model\\Document'], 'asset' => ['assets', 'id', 'type', '\\Pimcore\\Model\\Asset'], 'object' => ['objects', 'o_id', 'o_type', '\\Pimcore\\Model\\Object\\AbstractObject']];
$select = $this->db->select()->from('tags_assignment', [])->where('tags_assignment.ctype = ?', $type);
if (true === $considerChildTags) {
$select->joinInner('tags', 'tags.id = tags_assignment.tagid', ['tags_id' => 'id']);
$select->where('(' . $this->db->quoteInto('tags_assignment.tagid = ?', $tag->getId()) . ' OR ' . $this->db->quoteInto('tags.idPath LIKE ?', $tag->getFullIdPath() . "%") . ')');
} else {
$select->where('tags_assignment.tagid = ?', $tag->getId());
}
$select->joinInner(['el' => $map[$type][0]], 'tags_assignment.cId = el.' . $map[$type][1], ['el_id' => $map[$type][1]]);
if (!empty($subtypes)) {
$select->where($map[$type][2] . ' IN (?)', $subtypes);
}
if ('object' === $type && !empty($classNames)) {
$select->where('o_className IN (?)', $classNames);
}
$res = $this->db->query($select);
while ($row = $res->fetch()) {
$el = $map[$type][3]::getById($row['el_id']);
if ($el) {
$elements[] = $el;
}
}
return $elements;
}