Completed
Push — master ( 8cf2f6...851d6b )
by Vitaly
08:36 queued 02:40
created

Collection::__construct()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 6
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 1
Metric Value
c 2
b 0
f 1
dl 0
loc 6
rs 9.4285
cc 1
eloc 3
nc 1
nop 3
1
<?php
2
/**
3
 * Created by Vitaly Iegorov <[email protected]>.
4
 * on 11.02.16 at 14:15
5
 */
6
namespace samsoncms\api\renderable;
7
8
use samsoncms\api\Entity;
9
use samsonframework\core\ViewInterface;
10
use samsonframework\orm\QueryInterface;
11
12
/**
13
 * Renderable fields table.
14
 * Class should be used to simplify SamsonCMS generated entities outputting.
15
 *
16
 * @see \samsoncms\api\FieldsTable This class is just a wrapper with rendering
17
 *
18
 * @package samsoncms\api\renderable
19
 */
20
class Collection extends \samsoncms\api\query\Entity
21
{
22
    /** Name of the items variable in index view */
23
    const ITEMS_VIEW_VARIABLE = 'items';
24
25
    /** Name of the item prefix for variable in item view */
26
    const ITEM_VIEW_VARIABLE = 'item';
27
28
    /** @var string Block view file */
29
    protected $indexView = 'www/index';
30
31
    /** @var string Item view file */
32
    protected $itemView = 'www/item';
33
34
    /** @var string Empty view file */
35
    protected $emptyView = 'www/empty';
36
37
    /** @var ViewInterface View render object */
38
    protected $renderer;
39
40
    /** @var int Count of entities on one page */
41
    protected $pageSize;
42
43
    /** @var int Current page number */
44
    protected $pageNumber;
45
46
    /**
47
     * Collection constructor.
48
     *
49
     * @param QueryInterface $query Instance for querying database
50
     * @param ViewInterface  $renderer Instance for rendering views
51
     * @param string|null    $locale Localization language
52
     */
53
    public function __construct(QueryInterface $query, ViewInterface $renderer, $locale = null)
54
    {
55
        $this->renderer = $renderer;
56
57
        parent::__construct($query, $locale);
58
    }
59
60
    /**
61
     * Set index view path.
62
     * @param string $indexView Index view path
63
     * @return $this Chaining
64
     */
65
    public function indexView($indexView)
66
    {
67
        $this->indexView = $indexView;
68
        return $this;
69
    }
70
71
    /**
72
     * Set item view path.
73
     * @param string $itemView Item view path
74
     * @return $this Chaining
75
     */
76
    public function itemView($itemView)
77
    {
78
        $this->itemView = $itemView;
79
80
        return $this;
81
    }
82
83
    /**
84
     * Set empty view path.
85
     * @param string $emptyView Empty view path
86
     * @return $this Chaining
87
     */
88
    public function emptyView($emptyView)
89
    {
90
        $this->emptyView = $emptyView;
91
        return $this;
92
    }
93
94
    /**
95
     * Render Entity collection item.
96
     *
97
     * @param Entity $item SamsonCMS entity for rendering
98
     *
99
     * @return string Rendered HTML
100
     */
101
    public function renderItem(Entity $item)
102
    {
103
        return $this->renderer
104
            ->view($this->itemView)
105
            ->set(self::ITEM_VIEW_VARIABLE, $item)
106
            ->output();
107
    }
108
109
    /**
110
     * Render empty collection item.
111
     *
112
     * @return string Rendered HTML
113
     */
114
    public function renderEmpty()
115
    {
116
        return $this->renderer->view($this->emptyView)->output();
117
    }
118
119
    /**
120
     * Render Entity collection index.
121
     *
122
     * @param string $items Collection of rendered items
123
     *
124
     * @return string Rendered HTML
125
     */
126
    public function renderIndex($items)
127
    {
128
        return $this->renderer->view($this->indexView)
129
            ->set(self::ITEMS_VIEW_VARIABLE, $items)
130
            ->output();
131
    }
132
133
    /**
134
     * Set pagination for SamsonCMS query.
135
     *
136
     * @param int $pageNumber Result page number
137
     * @param int|null $pageSize Results page size
138
     * @return $this Chaining
139
     */
140
    public function pager($pageNumber, $pageSize = null)
141
    {
142
        $this->pageNumber = $pageNumber;
143
        $this->pageSize = null !== $pageSize ? $pageSize : $this->pageSize;
144
145
        return $this;
146
    }
147
148
    /** @return string Rendered HTML for fields table */
149
    public function render()
150
    {
151
        // Perform SamsonCMS query
152
        $collection = $this->find($this->pageNumber, $this->pageSize);
153
154
        $html = '';
155
        if (count($collection)) {
156
            // Render each entity view in collection
157
            foreach ($collection as $row) {
158
                $html .= $this->renderItem($row);
159
            }
160
161
            // Render collection main view with items
162
            $html = $this->renderIndex($html);
163
        } else { // Render empty entity view
164
            $html .= $this->renderEmpty();
165
        }
166
167
        return $html;
168
    }
169
}
170