forked from pimcore/pimcore
/
Resource.php
221 lines (174 loc) · 4.96 KB
/
Resource.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
<?php
/**
* Pimcore
*
* This source file is subject to the GNU General Public License version 3 (GPLv3)
* For the full copyright and license information, please view the LICENSE.md and gpl-3.0.txt
* files that are distributed with this source code.
*
* @category Pimcore
* @package Object
* @copyright Copyright (c) 2009-2015 pimcore GmbH (http://www.pimcore.org)
* @license http://www.pimcore.org/license GNU General Public License version 3 (GPLv3)
*/
namespace Pimcore\Model\Object\Listing;
use Pimcore\Model;
use Pimcore\Model\Object;
class Resource extends Model\Listing\Resource\AbstractResource {
/**
* get select query
*
* @return \Zend_Db_Select
* @throws \Exception
*/
public function getQuery()
{
// init
$select = $this->db->select();
// create base
$select->from(
[ 'objects' ]
, [
new \Zend_Db_Expr('SQL_CALC_FOUND_ROWS o_id')
, 'o_type'
]
);
// add joins
$this->addJoins( $select );
// add condition
$this->addConditions( $select );
// group by
$this->addGroupBy( $select );
// order
$this->addOrder( $select );
// limit
$this->addLimit( $select );
return $select;
}
/**
* Loads a list of objects for the specicifies parameters, returns an array of Object\AbstractObject elements
*
* @return array
*/
public function load() {
// load id's
$list = $this->loadIdList();
$objects = array();
foreach ($list as $o_id) {
if($object = Object::getById($o_id)) {
$objects[] = $object;
}
}
$this->model->setObjects($objects);
return $objects;
}
/**
* @return int
*/
public function getTotalCount()
{
$limit = $this->model->getLimit();
$hasLimit = !empty($limit);
$query = $this->getQuery();
if(!$hasLimit)
{
$query->limit(1);
}
$this->loadIdList();
if(!$hasLimit)
{
$query->reset( \Zend_Db_Select::LIMIT_COUNT );
}
return (int)$this->totalCount;
}
/**
* @return int
*/
public function getCount()
{
if (count($this->model->getObjects()) == 0)
{
$this->load();
}
return count($this->model->getObjects());
}
/**
* Loads a list of document ids for the specicifies parameters, returns an array of ids
*
* @return array
*/
public function loadIdList() {
$query = $this->getQuery(true);
$objectIds = $this->db->fetchCol( $query, $this->model->getConditionVariables() );
$this->totalCount = (int)$this->db->fetchOne( 'SELECT FOUND_ROWS()' );
return $objectIds;
}
/**
* @return string
*/
protected function getCondition() {
$condition = $this->model->getCondition();
$objectTypes = $this->model->getObjectTypes();
if(!empty($objectTypes)) {
if(!empty($condition)) {
$condition .= " AND ";
}
$condition .= " o_type IN ('" . implode("','", $objectTypes) . "')";
}
if ($condition) {
if (Object\AbstractObject::doHideUnpublished() && !$this->model->getUnpublished()) {
return " WHERE (" . $condition . ") AND o_published = 1";
}
return " WHERE " . $condition . " ";
}
else if (Object\AbstractObject::doHideUnpublished() && !$this->model->getUnpublished()) {
return " WHERE o_published = 1";
}
return "";
}
/**
* @param \Zend_DB_Select $select
*
* @return $this
*/
protected function addJoins(\Zend_DB_Select $select)
{
return $this;
}
/**
* @param \Zend_DB_Select $select
*
* @return $this
*/
protected function addConditions(\Zend_DB_Select $select)
{
$condition = $this->model->getCondition();
$objectTypes = $this->model->getObjectTypes();
if(!empty($objectTypes)) {
if(!empty($condition)) {
$condition .= " AND ";
}
$condition .= " o_type IN ('" . implode("','", $objectTypes) . "')";
}
if ($condition) {
if (Object\AbstractObject::doHideUnpublished() && !$this->model->getUnpublished()) {
$condition = "(" . $condition . ") AND o_published = 1";
}
}
else if (Object\AbstractObject::doHideUnpublished() && !$this->model->getUnpublished()) {
$condition = "o_published = 1";
}
if($condition)
{
$select->where( $condition );
}
return $this;
}
/**
* @return $this
*/
public function reset() {
$this->totalCount = 0;
return $this;
}
}