forked from cassking/what-hood
-
Notifications
You must be signed in to change notification settings - Fork 0
/
UserPolygonController.php
322 lines (258 loc) · 10.5 KB
/
UserPolygonController.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
<?php
namespace Whathood\Controller;
use Zend\View\Model\ViewModel;
use Zend\View\Model\JsonModel;
use CrEOF\Spatial\PHP\Types\Geometry\Polygon;
use CrEOF\Spatial\PHP\Types\Geometry\Point;
use Whathood\Entity\Neighborhood;
use Whathood\Entity\UserPolygon;
use Whathood\Entity\Region;
use Whathood\Entity\WhathoodUser;
use Whathood\Model\EmailMessageBuilder;
use Whathood\Spatial\PHP\Types\Geometry\FeatureCollection;
use Whathood\Spatial\PHP\Types\Geometry\LineString;
use Whathood\View\Model\ErrorViewModel;
use Whathood\Collection;
/**
* Description of NeighborhoodController
*
* @author Jim Smiley twitter:@jimRsmiley
*/
class UserPolygonController extends BaseController
{
/**
* we want to page through the user polygons based on a center point
*
* @return \Zend\View\Model\ViewModel
* @throws \InvalidArgumentException
*/
public function pageCenterAction() {
$base_url = '/user-neighborhood/page-center';
$item_count_per_page = 1;
$x = $this->getUriParameter('x');
$y = $this->getUriParameter('y');
$pageNum = $this->getUriParameter('page');
if (empty($x) or empty($y))
throw new \InvalidArgumentException("must specify x and y");
$query = $this->m()->userPolygonMapper()->getPaginationQuery(
array(
'x' => $x,
'y' => $y
)
);
$uriParams = array(
'x' => $x,
'y' => $y
);
if( empty($pageNum) )
$pageNum = 1;
$paginator = new \Whathood\View\Paginator\UserPolygonPaginator(
new \Whathood\View\Paginator\UserPolygonPaginatorAdapter($query)
);
$paginator->setDefaultItemCountPerPage($item_count_per_page);
$paginator->setBaseUrl($base_url);
$paginator->setCurrentPageNumber($pageNum);
$paginator->setUriParams($uriParams);
$viewModel = $this->getViewModel( array('paginator' => $paginator,'x'=>$x,'y'=>$y));
$viewModel->setTemplate('/whathood/user-polygon/page-id.phtml');
return $viewModel;
}
/**
* we want to page through the user polygons based on a center point
*
* @return \Zend\View\Model\ViewModel
* @throws \InvalidArgumentException
*/
public function pageNeighborhoodAction() {
$base_url = '/user-neighborhood';
$item_count_per_page = 1;
$neighborhood_name = $this->getUriParameter('neighborhood');
$region_name = $this->getUriParameter('region');
$pageNum = $this->getUriParameter('page');
$this->logger()->info(
sprintf("Controller/UserPolygon/page-neighborhood neighborhood_name=%s region_name=%s",
$neighborhood_name,$region_name ));
if (empty($neighborhood_name) or empty($region_name))
throw new \InvalidArgumentException("must specify x and y");
try {
$neighborhood = $this->m()->neighborhoodMapper()->byName($neighborhood_name,$region_name);
}
catch(\Doctrine\ORM\NoResultException $e) {
throw new \Exception("no neighborhood found with name $neighborhood_name, region $region_name");
}
$query = $this->m()->userPolygonMapper()->createByNeighborhoodQuery($neighborhood);
$uriParams = array(
'region' => $region_name,
'neighborhood' => $neighborhood_name
);
if( empty($pageNum) )
$pageNum = 1;
// prep the paginator
$paginator = new \Whathood\View\Paginator\UserPolygonPaginator(
new \Whathood\View\Paginator\UserPolygonPaginatorAdapter($query)
);
$paginator->setDefaultItemCountPerPage($item_count_per_page);
$paginator->setBaseUrl($base_url);
$paginator->setCurrentPageNumber($pageNum);
$paginator->setUriParams($uriParams);
if (0 == $paginator->getTotalItemCount())
throw new \Exception("no user polygons returned by paginator query");
$viewModel = $this->getViewModel(
array('paginator' => $paginator)
);
$viewModel->setTemplate('/whathood/user-polygon/page-id.phtml');
return $viewModel;
}
/**
* we want to page through all user polygons 10 at a time
*
* @return \Zend\View\Model\ViewModel
* @throws \InvalidArgumentException
*/
public function pageListAction() {
$itemCountPerPage = 10;
$pageNum = $this->getUriParameter('page');
$query = $this->userPolygonMapper()->getPaginationQuery();
if(empty($pageNum))
$pageNum = 1;
$paginator = new \Whathood\Model\UserPolygonPaginator(
new \Whathood\Model\UserPolygonPaginatorAdapter($query)
);
$paginator->setBaseUrl('/user-neighborhood/page-list');
$paginator->setDefaultItemCountPerPage($itemCountPerPage);
$paginator->setCurrentPageNumber($pageNum);
$viewModel = $this->getViewModel( array(
'paginator' => $paginator,
));
return $viewModel;
}
public function byNeighborhoodNameAction() {
$regionName = $this->getUriParameter('region');
$neighborhoodName = $this->getUriParameter('neighborhood');
if( empty( $regionName ) )
return new ErrorViewModel( array( 'message' => 'region must be defined' ) );
if( empty( $neighborhoodName ) )
return new ErrorViewModel( array( 'message' => 'neighborhood must be defined' ) );
$neighborhoods = $this->neighborhoodMapper()
->getNeighborhoodByName( $neighborhoodName, $regionName );
$viewModel = new ViewModel( array(
'neighborhoods' => $neighborhoods,
'neighborhoodName' => $neighborhoodName,
'regionName' => $regionName ) );
$viewModel->setTemplate('whathood/user-polygon/show-many.phtml');
return $viewModel;
}
public function byUserIdAction() {
$userId = $this->getUriParameter('i');
$format = $this->getUriParameter('format');
if( empty($userId) ) {
return new ErrorViewModel( "user_id must be defined" );
}
if( 'json' === $format ) {
$userPolygonJson = $this->userPolygonMapper()->userPolygonsByUserIdAsGeoJson( $userId );
}
else {
return new ViewModel();
}
}
public function addAction() {
return new ViewModel();
}
public function addPostAction() {
if (!$this->getRequest()->isPost())
throw new \Exception("addPostAction expects a POST");
$neighborhood_name = $this->getRequest()->getPost('neighborhood_name');
$region_name = $this->getRequest()->getPost('region_name');
$polygon_array = $this->getRequest()->getPost('polygon_json');
if (empty($polygon_array))
throw new \InvalidArgumentException("polygon_json may not be empty");
if (empty($neighborhood_name))
throw new \InvalidArgumentException("neighborhood_name may not be empty");
$polygon = \Whathood\Polygon::buildPolygonFromGeoJsonArray($polygon_array,$srid=4326);
$whathoodUser = $this->getWhathoodUser();
$userPolygon = new UserPolygon( array(
'neighborhood' => new Neighborhood(array(
'name' => $neighborhood_name )
),
'polygon' => $polygon,
'region' => new Region( array(
'name' => $region_name )
),
'whathoodUser' => $whathoodUser ));
$this->m()->userPolygonMapper()->save( $userPolygon );
$this->logger()->info(
sprintf("saved user-polygon id=%s neighborhood=%s(%s) region=%s ip-address=%s",
$userPolygon->getId(),
$userPolygon->getNeighborhood()->getName(),
$userPolygon->getNeighborhood()->getId(),
$userPolygon->getRegion()->getName(),
$whathoodUser->getIpAddress()
)
);
$this->messageQueue()->push(
'Whathood\Job\NeighborhoodBorderBuilderJob',
array(
'neighborhood_id' => $userPolygon->getNeighborhood()->getId()
)
);
$this->pushEmailJob(
\Whathood\View\MailMessageBuilder::buildNewUserPolygon($userPolygon)
);
return new JsonModel( array(
'status' => 'success',
'user_polygon_id' => $userPolygon->getId() )
);
}
public function byIdAction() {
$format = $this->getUriParameter('format');
$id = $this->getUriParameter('user_polygon_id');
if( empty($id) ) {
throw new \InvalidArgumentException('id may not be empty');
}
$viewModel = $this->getViewModel( array(
'user_polygon_id' => $id ));
$viewModel->setTemplate(
'/whathood/user-polygon/view.phtml');
return $viewModel;
}
public function byUserNameAction() {
$userName = $this->getUriParameter('whathood_user_name');
if( empty( $userName ) )
throw new \InvalidArgumentException('user id may not be null');
$neighborhoods = $this->userPolygonMapper()->byUserName($userName);
if( empty( $neighborhoods ) )
return new ErrorViewModel( array(
'message' => 'You do not have any neighborhoods'
));
$viewModel = new ViewModel( array('neighborhoods' => $neighborhoods ) );
$viewModel->setTemplate('whathood/neighborhood/list.phtml');
return $viewModel;
}
public function deleteAction() {
$user = $this->getLoggedInWhathoodUser();
$neighborhoodId = $this->getUriParameter('id');
$confirmedDelete = $this->getUriParameter('confirmed');
/*
* have user confirm delete
*/
if( empty($confirmedDelete) || $confirmedDelete !== 'yes' ) {
$viewModel = new ViewModel( array(
'neighborhoodId' => $neighborhoodId
));
$viewModel->setTemplate('whathood/neighborhood/neighborhood_confirm_delete.phtml');
return $viewModel;
}
/*
* make sure this neighborhood belongs to the user
*/
$neighborhood = $this->neighborhoodMapper()->byId($neighborhoodId);
$neighborhood->setDeleted(true);
$this->neighborhoodMapper()->update( $neighborhood );
}
public function listAction() {}
public function consoledefaultAction() {
$collection = new Collection($this->userPolygonMapper()->fetchAll());
print $collection->__toString();
}
}
?>