forked from anahitasocial/anahita
/
abstract.php
350 lines (295 loc) · 10.1 KB
/
abstract.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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
/**
* LICENSE: ##LICENSE##
*
* @category Anahita
* @package Com_Actors
* @subpackage Controller
* @author Arash Sanieyan <ash@anahitapolis.com>
* @author Rastin Mehr <rastin@anahitapolis.com>
* @copyright 2008 - 2010 rmdStudio Inc./Peerglobe Technology Inc
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @version SVN: $Id: resource.php 11985 2012-01-12 10:53:20Z asanieyan $
* @link http://www.anahitapolis.com
*/
/**
* Abstract Actor Controller
*
* @category Anahita
* @package Com_Actors
* @subpackage Controller
* @author Arash Sanieyan <ash@anahitapolis.com>
* @author Rastin Mehr <rastin@anahitapolis.com>
* @license GNU GPLv3 <http://www.gnu.org/licenses/gpl-3.0.html>
* @link http://www.anahitapolis.com
*/
abstract class ComActorsControllerAbstract extends ComBaseControllerService
{
/**
* The max upload limit
*
* @var int
*/
protected $_max_upload_limit;
/**
* Constructor.
*
* @param KConfig $config An optional KConfig object with configuration options.
*
* @return void
*/
public function __construct(KConfig $config)
{
parent::__construct($config);
$this->registerCallback(array('after.delete','after.add'), array($this, 'redirect'));
//set filter state
$this->getState()->insert('filter');
$this->_max_upload_limit = $config->max_upload_limit;
}
/**
* Initializes the default configuration for the object
*
* Called from {@link __construct()} as a first step of object instantiation.
*
* @param KConfig $config An optional KConfig object with configuration options.
*
* @return void
*/
protected function _initialize(KConfig $config)
{
$config->append(array(
'max_upload_limit' => ini_get('upload_max_filesize')
));
parent::_initialize($config);
$config->append(array(
'behaviors' => to_hash(array(
'com://site/search.controller.behavior.searchable',
'com://site/stories.controller.behavior.publisher',
'com://site/notifications.controller.behavior.notifier',
'followable',
'administrable',
'ownable',
'privatable',
'enablable',
'subscribable',
'com://site/hashtags.controller.behavior.hashtagable',
'coverable'
))
));
JFactory::getLanguage()->load('com_actors');
}
/**
* Browse Action
*
* @param KCommandContext $context Context parameter
*
* @return AnDomainEntitysetDefault
*/
protected function _actionBrowse(KCommandContext $context)
{
$context->append(array(
'query' => $this->getRepository()->getQuery()
));
$query = $context->query;
if($this->q)
$query->keyword($this->getService('anahita:filter.term')->sanitize($this->q));
if($this->ids)
{
$ids = KConfig::unbox($this->ids);
$query->id($ids);
}
else
{
$query->limit($this->limit, $this->start);
}
$entities = $query->toEntitySet();
if($this->isOwnable() && $this->actor)
{
$this->_state->append(array(
'filter' => 'following'
));
if($this->filter == 'administering' && $this->getRepository()->hasBehavior('administrable'))
{
$entities->where('administrators.id', 'IN', array($this->actor->id));
}
elseif($this->actor->isFollowable())
{
$entities->where('followers.id','IN', array($this->actor->id));
}
}
$entities->order('created_on', 'desc');
return $this->setList($entities)->getList();
}
/**
* Add an actor
*
* @param KCommandContext $context Context parameter
*
* @return AnDomainEntityAbstract
*/
protected function _actionAdd(KCommandContext $context)
{
$entity = parent::_actionAdd($context);
if ($entity->isPortraitable() && KRequest::has('files.portrait'))
{
$file = KRequest::get('files.portrait', 'raw');
if( $this->bellowSizeLimit( $file ) && $file['error'] == 0 )
{
$entity->setPortrait(array('url'=>$file['tmp_name'], 'mimetype'=>$file['type']));
}
}
return $entity;
}
/**
* Edit's an actor data
*
* @param KCommandContext $context Context parameter
*
* @return AnDomainEntityAbstract
*/
protected function _actionEdit(KCommandContext $context)
{
$entity = parent::_actionEdit($context);
if ($entity->isPortraitable() && KRequest::has('files.portrait'))
{
$file = KRequest::get('files.portrait', 'raw');
if( $this->bellowSizeLimit( $file ) && $file['error'] == 0 )
{
$this->getItem()->setPortrait(array('url'=>$file['tmp_name'], 'mimetype'=>$file['type']));
$story = $this->createStory(array(
'name' => 'avatar_edit',
'owner' => $entity,
'target' => $entity
));
}
else
{
$this->getItem()->removePortraitImage();
}
}
if($entity->save($context))
{
dispatch_plugin('profile.onSave', array('actor'=>$entity, 'data'=>$context->data));
}
return $entity;
}
/**
* Set the default Actor View
*
* @param KCommandContext $context Context parameter
*
* @return ComActorsControllerDefault
*/
public function setView($view)
{
parent::setView($view);
if(!$this->_view instanceof ComBaseViewAbstract)
{
$name = KInflector::isPlural($this->view) ? 'actors' : 'actor';
$defaults[] = 'ComActorsView'.ucfirst($view).ucfirst($this->_view->name);
$defaults[] = 'ComActorsView'.ucfirst($name).ucfirst($this->_view->name);
register_default(array('identifier'=>$this->_view, 'default'=>$defaults));
}
return $this;
}
/**
* Deletes an actor and all of the necessary cleanup. It also dispatches all the apps to
* clean up after the deleted actor
*
* @param KCommandContext $context Context parameter
*
* @return AnDomainEntityAbstract
*/
protected function _actionDelete(KCommandContext $context)
{
$this->getService('repos://site/components')->fetchSet()
->registerEventDispatcher($this->getService('anahita:event.dispatcher'));
$result = parent::_actionDelete($context);
$this->getService('anahita:event.dispatcher')->dispatchEvent('onDeleteActor', array('actor_id'=>$this->getItem()));
return $result;
}
/**
* Get a toolbar by identifier
*
* @return KControllerToolbarAbstract
*/
public function getToolbar($toolbar, $config = array())
{
if(is_string($toolbar))
{
//if actorbar or menu alawys default to the base
if (in_array($toolbar, array('actorbar')))
{
$identifier = clone $this->getIdentifier();
$identifier->path = array('controller','toolbar');
$identifier->name = $toolbar;
register_default(array('identifier'=>$identifier, 'default'=>'ComActorsControllerToolbar'.ucfirst($toolbar)));
$toolbar = $identifier;
}
}
return parent::getToolbar($toolbar, $config);
}
/**
* Overwrite the setPrivacy action in privatable behavior
*
* @param KCommandContext $context Context parameter
*
* @return void
*
* @see ComActorsDomainBehaviorPrivatable
*/
protected function _actionSetPrivacy(KCommandContext $context)
{
if($this->hasBehavior('privatable'))
{
$this->getBehavior('privatable')->execute('action.setprivacy', $context);
}
$data = $context->data;
if($data->access != 'followers')
{
$data->allowFollowRequest = false;
}
$this->getItem()->allowFollowRequest = (bool) $data->allowFollowRequest;
}
/**
* Set the necessary redirect
*
* @param KCommandContext $context
*
* @return void
*/
public function redirect(KCommandContext $context)
{
$url = null;
if( $context->action == 'delete' )
{
$url = 'option=com_'.$this->getIdentifier()->package.'&view='.KInflector::pluralize($this->getIdentifier()->name);
}
elseif( $context->action == 'add' )
{
$url = $context->result->getURL().'&get=settings';
}
if($url)
{
$context->response->setRedirect(JRoute::_($url));
}
}
/**
* Checks to see whether the uploaded file exceeds the allowed limit
*
* @param posted file request
*
* @return boolean
*/
public function bellowSizeLimit( $file ) {
$content = @file_get_contents($file['tmp_name']);
$filesize = strlen($content);
$uploadlimit = $this->_max_upload_limit * 1024 * 1024;
if($filesize > $uploadlimit)
{
throw new LibBaseControllerExceptionBadRequest('Exceed maximum size');
return false;
}
return true;
}
}