/
Paste.php
276 lines (245 loc) · 6.97 KB
/
Paste.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
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
<?php
/** Spindle_Model_Model */
require_once dirname(__FILE__) . '/Model.php';
/**
* Pastebin model
*
* @uses Spindle_Model_Model
* @package Spindle
* @subpackage Model
* @license New BSD {@link http://framework.zend.com/license/new-bsd}
* @version $Id: $
*/
class Spindle_Model_Paste extends Spindle_Model_Model
{
/**
* Table fields
* @var array
*/
protected $_fields;
/**
* Form representation/input filter of model data
*
* @var Form_Paste
*/
protected $_form;
/**
* @var Zend_Db_Table_Abstract
*/
protected $_table;
/**
* @var bool Whether or not to use a paginator for result sets
*/
protected $_usePaginator = false;
/**
* Constructor
*
* @param mixed $options
* @return void
*/
public function __construct($options = null)
{
$this->addHook('preAdd')
->addHook('postAdd');
if ($options instanceof Zend_Config) {
$options = $options->toArray();
}
if (is_array($options)) {
$this->setOptions($options);
}
}
/**
* Set flag indicating whether or not to use paginator
*
* @param bool $flag
* @return Spindle_Model_Paste
*/
public function setUsePaginator($flag)
{
$this->_usePaginator = (bool) $flag;
return $this;
}
/**
* Use a paginator?
*
* @return bool
*/
public function usePaginator()
{
return $this->_usePaginator;
}
/**
* Add a paste
*
* @param array|struct $data
* @return string
*/
public function add(array $data)
{
$this->callHook('preAdd', array($data));
$form = $this->getForm();
$belongTo = $form->getElementsBelongTo();
if (!empty($belongTo) && array_key_exists($belongTo, $data)) {
$data = $data[$belongTo];
}
if (!$form->isValid($data)) {
return false;
}
$values = $form->getValues();
if (!empty($belongTo)) {
$values = $values[$belongTo];
}
$id = $this->getTable()->insert($values);
$this->callHook('postAdd', array($id));
return $id;
}
/**
* Fetch a paste by id
*
* Returns boolean false on failure to find the paste; otherwise, a struct
* is returned.
*
* @param string $id
* @return struct|false
*/
public function get($id)
{
$table = $this->getTable();
$select = $table->select();
$select->where('id = ?', $id)
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
$row = $table->fetchRow($select);
if (null == $row) {
return false;
}
$data = $row->toArray();
if (!empty($data['parent'])) {
$parent = $this->get($data['parent']);
if (!$parent) {
$data['parent'] = null;
}
}
$data['children'] = $this->_getChildren($id);
return $data;
}
/**
* Get list of active pastes, ordered by creation date (desc)
*
* @param null|int|array If set to use paginator, an int value indicates page; array for specific criteria
* @return array|Zend_Paginator
*/
public function fetchActive($criteria = null)
{
$table = $this->getTable();
$adapter = $table->getAdapter();
$select = $adapter->select();
$select->from('paste', array('id', 'type', 'summary', 'user', 'created', 'expires'))
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
if ($this->usePaginator()) {
$page = (null === $criteria) ? 1 : (int) $criteria;
$paginator = new Zend_Paginator(
new Zend_Paginator_Adapter_DbSelect($select)
);
$paginator->setItemCountPerPage(15)
->setCurrentPageNumber($page);
return $paginator;
} elseif (is_array($criteria)) {
$this->_refineSelection($select, $criteria);
} else {
$select->order('created DESC');
}
return $adapter->fetchAll($select);
}
/**
* Fetch count of active pastes
*
* @return int
*/
public function fetchActiveCount()
{
$table = $this->getTable();
$adapter = $table->getAdapter();
$select = $adapter->select();
$select->from('paste', array('count' => 'COUNT(*)'))
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'))
->order('created DESC');
return $adapter->fetchOne($select);
}
/**
* Retrieve form/input filter
*
* @return Form_Paste
*/
public function getForm()
{
if (null === $this->_form) {
$this->_form = $this->getResourceLoader()->getForm('Paste');
}
return $this->_form;
}
/**
* Retrieve data provider
*
* @return Paste_Table
*/
public function getTable()
{
if (null === $this->_table) {
$this->_table = $this->getResourceLoader()->getDbtable('Paste');
}
return $this->_table;
}
/**
* Retrieve paste children
*
* @param string $id
* @return false|array
*/
protected function _getChildren($id)
{
$adapter = $this->getTable()->getAdapter();
$select = $adapter->select();
$select->from('paste', array('id'))
->where('parent = ?', $id)
->where('expires IS NULL OR expires = "" OR expires > ?', date('Y-m-d H:i:s'));
return $adapter->fetchCol($select);
}
/**
* Refine the active pastes selection based on criteria provided
*
* Allows setting a limit to the number of records returend
*
* @param Zend_Db_Select $select
* @param array $criteria
* @return void
*/
protected function _refineSelection(Zend_Db_Select $select, array $criteria)
{
if (array_key_exists('start', $criteria)
&& ($criteria['start'] == intval($criteria['start']))
) {
if (array_key_exists('count', $criteria)
&& ($criteria['count'] == intval($criteria['count']))
) {
$select->limit($criteria['count'], $criteria['start']);
}
}
$sorted = false;
if (array_key_exists('sort', $criteria)) {
$sort = $criteria['sort'];
$dir = 'ASC';
if ('-' == substr($sort, 0, 1)) {
$sort = substr($sort, 1);
$dir = 'DESC';
}
$fields = $this->getTable()->info('cols');
if (in_array($sort, $fields)) {
$select->order("$sort $dir");
$sorted = true;
}
}
if (!$sorted) {
$select->order('created DESC');
}
}
}