MaterialField::__construct()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
c 0
b 0
f 0
dl 0
loc 11
rs 9.4285
cc 1
eloc 8
nc 1
nop 2
1
<?php
2
/**
3
 * Created by PhpStorm.
4
 * User: VITALYIEGOROV
5
 * Date: 08.12.15
6
 * Time: 23:11
7
 */
8
namespace samsoncms\api\query;
9
10
use samson\activerecord\dbQuery;
11
use samsoncms\api\Material;
12
use samsoncms\api\Field;
13
use samsoncms\api\CMS;
14
use samsonframework\orm\Condition;
15
use samsonframework\orm\ArgumentInterface;
16
use samsonframework\orm\ConditionInterface;
17
18
/**
19
 * Material to additional fields relation query.
20
 * @package samsoncms\api
21
 */
22
class MaterialField extends Relational
23
{
24
    /**
25
     * MaterialField constructor
26
     * @param array $filteringIDs Collection of entity identifiers for filtering
27
     * @param string $identifier Entity identifier
28
     */
29
    public function __construct($filteringIDs = array(), $identifier = Material::class)
30
    {
31
        parent::__construct(
32
            $GLOBALS['__core']->getContainer()->get('query'),
33
            $identifier,
34
            Material::F_PRIMARY,
35
            Field::F_PRIMARY,
36
            CMS::MATERIAL_FIELD_RELATION_ENTITY,
37
            $filteringIDs
38
        );
39
    }
40
41
    /**
42
     * Get current entity identifiers collection by relation identifier ans its value.
43
     *
44
     * @param string $relationID Relation entity identifier
45
     * @param mixed|Condition $relationValue Relation entity value or relation condition
46
     * @param array $filteringIDs Collection of entity identifiers for filtering query
47
     * @param string $locale Locale for requests
48
     * @return array Collection of entity identifiers filtered by navigation identifier.
49
     */
50
    public function idsByRelationID($relationID, $relationValue = null, array $filteringIDs = array(), $locale = null)
51
    {
52
        $return = array();
53
54
        /** @var Field $fieldRecord We need to have field record */
55
        $fieldRecord = null;
56
        if (Field::byID($this->query, $relationID, $fieldRecord)) {
0 ignored issues
show
Deprecated Code introduced by
The method samsonframework\orm\Record::byID() has been deprecated with message: Record should not be queryable, query class ancestor must be used

This method has been deprecated. The supplier of the class has supplied an explanatory message.

The explanatory message should give you some clue as to whether and when the method will be removed from the class and what other method or class to use instead.

Loading history...
57
            // Get material identifiers by field
58
            $this->query->entity($this->relationIdentifier)
59
                ->where(\samsoncms\api\MaterialField::F_DELETION, 1)
60
                ->where($this->relationPrimary, $relationID);
61
62
            if ($relationValue instanceof ConditionInterface) {
63
                $this->query->whereCondition($relationValue);
64
            } else {
65
                $this->query->where($fieldRecord->valueFieldName(), $relationValue);
66
            }
67
68
            // Add material identifier filter if passed
69
            if (count($filteringIDs)) {
70
                $this->query->where($this->primaryField, $filteringIDs);
71
            }
72
73
            // If field is localized
74
            if ((int)$fieldRecord->local === 1) {
75
                // Add localization filter
76
                $this->query->where(\samsoncms\api\MaterialField::F_LOCALE, $locale);
77
            }
78
79
            // Perform database query and get only material identifiers collection
80
            $return = $this->query->fields($this->primaryField);
81
        }
82
83
        return $return;
84
    }
85
}
86