Duplicate code is one of the most pungent code smells. A rule that is often used is to re-structure code once it is duplicated in three or more places.
Common duplication problems, and corresponding solutions are:
Complex classes like PostVoteController often do a lot of different things. To break such a class down, we need to identify a cohesive component within that class. A common approach to find such a component is to look for fields/methods that share the same prefixes, or suffixes. You can also have a look at the cohesion graph to spot any un-connected, or weakly-connected components.
Once you have determined the fields that belong together, you can apply the Extract Class refactoring. If the component makes sense as a sub-class, Extract Subclass is also a candidate, and is often faster.
While breaking up the class, it is a good idea to analyze how other classes use PostVoteController, and based on these observations, apply Extract Interface, too.
| 1 | <?php |
||
| 7 | class PostVoteController extends GameController |
||
| 8 | { |
||
| 9 | /** |
||
| 10 | * @var gameService |
||
| 11 | */ |
||
| 12 | protected $gameService; |
||
| 13 | |||
| 14 | /** |
||
| 15 | * --DONE-- 1. try to change the Game Id (on le redirige vers la home du jeu) |
||
| 16 | * --DONE-- 2. try to modify questions (the form is recreated and verified in the controller) |
||
| 17 | * --DONE-- 3. don't answer to questions (form is checked controller side) |
||
| 18 | * 4. try to game the chrono |
||
| 19 | * 5. try to play again |
||
| 20 | * 6. try to change answers |
||
| 21 | * --DONE-- 7. essaie de répondre sans être inscrit (on le redirige vers la home du jeu) |
||
| 22 | */ |
||
| 23 | public function playAction() |
||
| 24 | { |
||
| 25 | $sg = $this->getGameService(); |
||
| 26 | |||
| 27 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 28 | |||
| 29 | |||
| 30 | $game = $sg->checkGame($identifier); |
||
| 31 | if (! $game || $game->isClosed()) { |
||
| 32 | return $this->notFoundAction(); |
||
| 33 | } |
||
| 34 | |||
| 35 | $redirectFb = $this->checkFbRegistration($this->zfcUserAuthentication()->getIdentity(), $game); |
||
|
|
|||
| 36 | if ($redirectFb) { |
||
| 37 | return $redirectFb; |
||
| 38 | } |
||
| 39 | |||
| 40 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 41 | |||
| 42 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 43 | $redirect = urlencode( |
||
| 44 | $this->frontendUrl()->fromRoute( |
||
| 45 | $game->getClassType() . '/play', |
||
| 46 | array('id' => $game->getIdentifier(), ), |
||
| 47 | array('force_canonical' => true) |
||
| 48 | ) |
||
| 49 | ); |
||
| 50 | |||
| 51 | return $this->redirect()->toUrl( |
||
| 52 | $this->frontendUrl()->fromRoute( |
||
| 53 | 'zfcuser/register', |
||
| 54 | array() |
||
| 55 | ) . '?redirect='.$redirect |
||
| 56 | ); |
||
| 57 | } |
||
| 58 | |||
| 59 | $entry = $sg->play($game, $user); |
||
| 60 | |||
| 61 | if (!$entry) { |
||
| 62 | $lastEntry = $sg->findLastInactiveEntry($game, $user); |
||
| 63 | View Code Duplication | if ($lastEntry === null) { |
|
| 64 | return $this->redirect()->toUrl( |
||
| 65 | $this->frontendUrl()->fromRoute( |
||
| 66 | 'postvote', |
||
| 67 | array( |
||
| 68 | 'id' => $identifier, |
||
| 69 | |||
| 70 | ) |
||
| 71 | ) |
||
| 72 | ); |
||
| 73 | } |
||
| 74 | |||
| 75 | $lastEntryId = $lastEntry->getId(); |
||
| 76 | $lastPost = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntryId)); |
||
| 77 | $postId = $lastPost->getId(); |
||
| 78 | if ($lastPost->getStatus() == 2) { |
||
| 79 | // the user has already taken part of this game and the participation limit has been reached |
||
| 80 | $this->flashMessenger()->addMessage( |
||
| 81 | $this->getServiceLocator()->get('translator')->translate('You have already a Post') |
||
| 82 | ); |
||
| 83 | |||
| 84 | return $this->redirect()->toUrl( |
||
| 85 | $this->frontendUrl()->fromRoute( |
||
| 86 | 'postvote/post', |
||
| 87 | array( |
||
| 88 | 'id' => $identifier, |
||
| 89 | 'post' => $postId, |
||
| 90 | |||
| 91 | ) |
||
| 92 | ) |
||
| 93 | ); |
||
| 94 | } else { |
||
| 95 | $this->flashMessenger()->addMessage( |
||
| 96 | $this->getServiceLocator()->get('translator')->translate('Your Post is waiting for validation') |
||
| 97 | ); |
||
| 98 | |||
| 99 | return $this->redirect()->toUrl( |
||
| 100 | $this->frontendUrl()->fromRoute( |
||
| 101 | 'postvote/post', |
||
| 102 | array( |
||
| 103 | 'id' => $identifier, |
||
| 104 | 'post' => $postId, |
||
| 105 | |||
| 106 | ) |
||
| 107 | ) |
||
| 108 | ); |
||
| 109 | } |
||
| 110 | } |
||
| 111 | |||
| 112 | View Code Duplication | if (! $game->getForm()) { |
|
| 113 | return $this->redirect()->toUrl( |
||
| 114 | $this->frontendUrl()->fromRoute( |
||
| 115 | 'postvote', |
||
| 116 | array( |
||
| 117 | 'id' => $identifier, |
||
| 118 | |||
| 119 | ) |
||
| 120 | ) |
||
| 121 | ); |
||
| 122 | } |
||
| 123 | |||
| 124 | $form = $sg->createFormFromJson($game->getForm()->getForm(), 'postvoteForm'); |
||
| 125 | |||
| 126 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 127 | $post = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
| 128 | if ($post) { |
||
| 129 | foreach ($post->getPostElements() as $element) { |
||
| 130 | try { |
||
| 131 | $form->get($element->getName())->setValue($element->getValue()); |
||
| 132 | |||
| 133 | $elementType = $form->get($element->getName())->getAttribute('type'); |
||
| 134 | if ($elementType == 'file' && $element->getValue() != '') { |
||
| 135 | $filter = $form->getInputFilter(); |
||
| 136 | $elementInput = $filter->get($element->getName()); |
||
| 137 | $elementInput->setRequired(false); |
||
| 138 | $form->get($element->getName())->setAttribute('required', false); |
||
| 139 | } |
||
| 140 | } catch (\Zend\Form\Exception\InvalidElementException $e) { |
||
| 141 | } |
||
| 142 | } |
||
| 143 | } |
||
| 144 | |||
| 145 | $viewModel = $this->buildView($game); |
||
| 146 | |||
| 147 | if ($this->getRequest()->isPost()) { |
||
| 148 | // POST Request: Process form |
||
| 149 | $data = array_merge_recursive( |
||
| 150 | $this->getRequest()->getPost()->toArray(), |
||
| 151 | $this->getRequest()->getFiles()->toArray() |
||
| 152 | ); |
||
| 153 | |||
| 154 | $form->setData($data); |
||
| 155 | |||
| 156 | if ($form->isValid()) { |
||
| 157 | $data = $form->getData(); |
||
| 158 | $post = $this->getGameService()->createPost($data, $game, $user, $form); |
||
| 159 | |||
| 160 | if ($post && !empty($game->nextStep('play'))) { |
||
| 161 | // determine the route where the user should go |
||
| 162 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
| 163 | 'postvote/'.$game->nextStep('play'), |
||
| 164 | array( |
||
| 165 | 'id' => $game->getIdentifier(), |
||
| 166 | |||
| 167 | ) |
||
| 168 | ); |
||
| 169 | |||
| 170 | return $this->redirect()->toUrl($redirectUrl); |
||
| 171 | } |
||
| 172 | } else { |
||
| 173 | $messages = $form->getMessages(); |
||
| 174 | $viewModel = $this->buildView($game); |
||
| 175 | $viewModel->setVariables(array( |
||
| 176 | 'success' => false, |
||
| 177 | 'message' => implode(',', $messages['title']), |
||
| 178 | )); |
||
| 179 | } |
||
| 180 | } |
||
| 181 | |||
| 182 | $viewModel->setVariables(array( |
||
| 183 | 'playerData' => $entry->getPlayerData(), |
||
| 184 | 'form' => $form, |
||
| 185 | 'post' => $post, |
||
| 186 | )); |
||
| 187 | |||
| 188 | return $viewModel; |
||
| 189 | } |
||
| 190 | |||
| 191 | public function previewAction() |
||
| 192 | { |
||
| 193 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 194 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 195 | $sg = $this->getGameService(); |
||
| 196 | |||
| 197 | $game = $sg->checkGame($identifier); |
||
| 198 | if (! $game) { |
||
| 199 | return $this->notFoundAction(); |
||
| 200 | } |
||
| 201 | |||
| 202 | $entry = $sg->findLastActiveEntry($game, $user); |
||
| 203 | |||
| 204 | if (!$entry) { |
||
| 205 | // the user has already taken part of this game and the participation limit has been reached |
||
| 206 | return $this->redirect()->toUrl( |
||
| 207 | $this->frontendUrl()->fromRoute( |
||
| 208 | 'postvote/'.$game->nextStep('preview'), |
||
| 209 | array( |
||
| 210 | 'id' => $identifier, |
||
| 211 | |||
| 212 | ) |
||
| 213 | ) |
||
| 214 | ); |
||
| 215 | } |
||
| 216 | |||
| 217 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 218 | $post = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $entry, 'status' => 0)); |
||
| 219 | |||
| 220 | if (! $post) { |
||
| 221 | return $this->redirect()->toUrl( |
||
| 222 | $this->frontendUrl()->fromRoute( |
||
| 223 | 'postvote', |
||
| 224 | array( |
||
| 225 | 'id' => $identifier, |
||
| 226 | |||
| 227 | ) |
||
| 228 | ) |
||
| 229 | ); |
||
| 230 | } |
||
| 231 | |||
| 232 | if ($this->getRequest()->isPost()) { |
||
| 233 | $post = $this->getGameService()->confirmPost($game, $user); |
||
| 234 | |||
| 235 | if ($post) { |
||
| 236 | if (!($step = $game->nextStep('play'))) { |
||
| 237 | $step = 'result'; |
||
| 238 | } |
||
| 239 | $redirectUrl = $this->frontendUrl()->fromRoute( |
||
| 240 | 'postvote/'.$step, |
||
| 241 | array( |
||
| 242 | 'id' => $game->getIdentifier(), |
||
| 243 | |||
| 244 | ) |
||
| 245 | ); |
||
| 246 | |||
| 247 | return $this->redirect()->toUrl($redirectUrl); |
||
| 248 | } |
||
| 249 | } |
||
| 250 | |||
| 251 | $viewModel = $this->buildView($game); |
||
| 252 | $viewModel->setVariables(array('post' => $post)); |
||
| 253 | |||
| 254 | return $viewModel; |
||
| 255 | } |
||
| 256 | |||
| 257 | /** |
||
| 258 | * View the Post page |
||
| 259 | * @return multitype:|\Zend\Http\Response|\Zend\View\Model\ViewModel |
||
| 260 | */ |
||
| 261 | public function postAction() |
||
| 262 | { |
||
| 263 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 264 | $postId = $this->getEvent()->getRouteMatch()->getParam('post'); |
||
| 265 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 266 | $sg = $this->getGameService(); |
||
| 267 | $voted = false; |
||
| 268 | |||
| 269 | $statusMail = false; |
||
| 270 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
| 271 | $to = ''; |
||
| 272 | $skinUrl = $sg->getServiceManager()->get('ViewRenderer')->url( |
||
| 273 | 'frontend', |
||
| 274 | array( |
||
| 275 | |||
| 276 | ), |
||
| 277 | array('force_canonical' => true) |
||
| 278 | ); |
||
| 279 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
| 280 | if (isset($config['moderation']['email'])) { |
||
| 281 | $to = $config['moderation']['email']; |
||
| 282 | } |
||
| 283 | |||
| 284 | $game = $sg->checkGame($identifier, false); |
||
| 285 | |||
| 286 | if (! $postId) { |
||
| 287 | return $this->notFoundAction(); |
||
| 288 | } |
||
| 289 | |||
| 290 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 291 | $post = $sg->getPostVotePostMapper()->findById($postId); |
||
| 292 | |||
| 293 | View Code Duplication | if (! $post || $post->getStatus() === 9) { |
|
| 294 | return $this->redirect()->toUrl( |
||
| 295 | $this->frontendUrl()->fromRoute( |
||
| 296 | 'postvote', |
||
| 297 | array( |
||
| 298 | 'id' => $identifier, |
||
| 299 | |||
| 300 | ) |
||
| 301 | ) |
||
| 302 | ); |
||
| 303 | } |
||
| 304 | |||
| 305 | $formModeration = new Form(); |
||
| 306 | $formModeration->setAttribute('method', 'post'); |
||
| 307 | |||
| 308 | $formModeration->add(array( |
||
| 309 | 'name' => 'moderation', |
||
| 310 | 'attributes' => array( |
||
| 311 | 'type' => 'hidden', |
||
| 312 | 'value' => '1' |
||
| 313 | ), |
||
| 314 | )); |
||
| 315 | |||
| 316 | $form = new \PlaygroundGame\Form\Frontend\PostVoteVote( |
||
| 317 | $this->frontendUrl()->fromRoute( |
||
| 318 | 'postvote/post/captcha', |
||
| 319 | array( |
||
| 320 | 'id' => $identifier, |
||
| 321 | 'post' => $postId, |
||
| 322 | |||
| 323 | ) |
||
| 324 | ) |
||
| 325 | ); |
||
| 326 | |||
| 327 | if ($user) { |
||
| 328 | $form->remove('captcha'); |
||
| 329 | } |
||
| 330 | |||
| 331 | $alreadyVoted = ''; |
||
| 332 | $reportId = ''; |
||
| 333 | |||
| 334 | $request = $this->getRequest(); |
||
| 335 | if ($request->isPost()) { |
||
| 336 | $data = $request->getPost()->toArray(); |
||
| 337 | if (isset($data['moderation'])) { |
||
| 338 | $formModeration->setData($data); |
||
| 339 | if ($formModeration->isValid()) { |
||
| 340 | $from = $to; |
||
| 341 | $subject= 'Moderation Post and Vote'; |
||
| 342 | $result = $mailService->createHtmlMessage( |
||
| 343 | $from, |
||
| 344 | $to, |
||
| 345 | $subject, |
||
| 346 | 'playground-game/email/moderation', |
||
| 347 | array('data' => $data, 'skinUrl' => $skinUrl) |
||
| 348 | ); |
||
| 349 | $mailService->send($result); |
||
| 350 | if ($result) { |
||
| 351 | $statusMail = true; |
||
| 352 | $reportId = $data['reportid']; |
||
| 353 | } |
||
| 354 | } |
||
| 355 | } else { |
||
| 356 | $form->setData($request->getPost()); |
||
| 357 | if ($form->isValid()) { |
||
| 358 | if ($sg->addVote($user, $this->getRequest()->getServer('REMOTE_ADDR'), $post)) { |
||
| 359 | $voted = true; |
||
| 360 | } else { |
||
| 361 | $alreadyVoted = 'Vous avez déjà voté!'; |
||
| 362 | } |
||
| 363 | } |
||
| 364 | } |
||
| 365 | } |
||
| 366 | |||
| 367 | $viewModel = $this->buildView($game); |
||
| 368 | $viewModel->setVariables( |
||
| 369 | array( |
||
| 370 | 'post' => $post, |
||
| 371 | 'voted' => $voted, |
||
| 372 | 'form' => $form, |
||
| 373 | 'formModeration' => $formModeration, |
||
| 374 | 'statusMail' => $statusMail, |
||
| 375 | 'alreadyVoted' => $alreadyVoted, |
||
| 376 | 'reportId' => $reportId, |
||
| 377 | ) |
||
| 378 | ); |
||
| 379 | |||
| 380 | return $viewModel; |
||
| 381 | } |
||
| 382 | |||
| 383 | /** |
||
| 384 | * |
||
| 385 | */ |
||
| 386 | public function resultAction() |
||
| 387 | { |
||
| 388 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 389 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 390 | |||
| 391 | |||
| 392 | $statusMail = null; |
||
| 393 | |||
| 394 | if (!$identifier) { |
||
| 395 | return $this->notFoundAction(); |
||
| 396 | } |
||
| 397 | |||
| 398 | $postVoteMapper = $this->getGameService()->getPostVoteMapper(); |
||
| 399 | $game = $postVoteMapper->findByIdentifier($identifier); |
||
| 400 | |||
| 401 | if (!$game || $game->isClosed()) { |
||
| 402 | return $this->notFoundAction(); |
||
| 403 | } |
||
| 404 | |||
| 405 | // Has the user finished the game ? |
||
| 406 | $lastEntry = $this->getGameService()->findLastInactiveEntry($game, $user); |
||
| 407 | |||
| 408 | View Code Duplication | if ($lastEntry === null) { |
|
| 409 | return $this->redirect()->toUrl( |
||
| 410 | $this->frontendUrl()->fromRoute( |
||
| 411 | 'postvote', |
||
| 412 | array( |
||
| 413 | 'id' => $identifier, |
||
| 414 | |||
| 415 | ) |
||
| 416 | ) |
||
| 417 | ); |
||
| 418 | } |
||
| 419 | |||
| 420 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 421 | $redirect = urlencode( |
||
| 422 | $this->frontendUrl()->fromRoute( |
||
| 423 | 'postvote/result', |
||
| 424 | array( |
||
| 425 | 'id' => $game->getIdentifier(), |
||
| 426 | |||
| 427 | ) |
||
| 428 | ) |
||
| 429 | ); |
||
| 430 | return $this->redirect()->toUrl( |
||
| 431 | $this->frontendUrl()->fromRoute( |
||
| 432 | 'zfcuser/register', |
||
| 433 | array() |
||
| 434 | ) . '?redirect='.$redirect |
||
| 435 | ); |
||
| 436 | } |
||
| 437 | |||
| 438 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 439 | $form->setAttribute('method', 'post'); |
||
| 440 | |||
| 441 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
| 442 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 443 | $form->setData($data); |
||
| 444 | if ($form->isValid()) { |
||
| 445 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
| 446 | if ($result) { |
||
| 447 | $statusMail = true; |
||
| 448 | } |
||
| 449 | } |
||
| 450 | } |
||
| 451 | |||
| 452 | // Je recherche le post associé à entry + status == 0. Si non trouvé, je redirige vers home du jeu. |
||
| 453 | $post = $this->getGameService()->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
| 454 | |||
| 455 | if (! $post) { |
||
| 456 | return $this->redirect()->toUrl( |
||
| 457 | $this->frontendUrl()->fromRoute( |
||
| 458 | 'postvote', |
||
| 459 | array( |
||
| 460 | 'id' => $identifier, |
||
| 461 | |||
| 462 | ) |
||
| 463 | ) |
||
| 464 | ); |
||
| 465 | } |
||
| 466 | |||
| 467 | $viewModel = $this->buildView($game); |
||
| 468 | |||
| 469 | $viewModel->setVariables(array( |
||
| 470 | 'statusMail' => $statusMail, |
||
| 471 | 'post' => $post, |
||
| 472 | 'form' => $form, |
||
| 473 | )); |
||
| 474 | |||
| 475 | return $viewModel; |
||
| 476 | } |
||
| 477 | |||
| 478 | /** |
||
| 479 | * Example of AJAX File Upload with Session Progress and partial validation. |
||
| 480 | * It's now possible to send a base64 image in this case the call is the form : |
||
| 481 | * this._ajax( |
||
| 482 | * { |
||
| 483 | * url: url.dataset.url, |
||
| 484 | * method: 'post', |
||
| 485 | * body: 'photo=' + image |
||
| 486 | * }, |
||
| 487 | * |
||
| 488 | * @return \Zend\Stdlib\ResponseInterface |
||
| 489 | */ |
||
| 490 | public function ajaxuploadAction() |
||
| 541 | |||
| 542 | public function ajaxdeleteAction() |
||
| 580 | |||
| 581 | public function listAction() |
||
| 582 | { |
||
| 583 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 584 | $filter = $this->getEvent()->getRouteMatch()->getParam('filter'); |
||
| 585 | $search = $this->params()->fromQuery('name'); |
||
| 586 | $sg = $this->getGameService(); |
||
| 587 | $postId = $this->params()->fromQuery('id'); |
||
| 588 | |||
| 589 | $statusMail = false; |
||
| 590 | $mailService = $this->getServiceLocator()->get('playgroundgame_message'); |
||
| 591 | $to = ''; |
||
| 592 | $skinUrl = $sg->getServiceManager()->get('ViewRenderer')->url( |
||
| 593 | 'frontend', |
||
| 594 | array(), |
||
| 595 | array('force_canonical' => true) |
||
| 596 | ); |
||
| 597 | $config = $this->getGameService()->getServiceManager()->get('config'); |
||
| 598 | if (isset($config['moderation']['email'])) { |
||
| 599 | $to = $config['moderation']['email']; |
||
| 600 | } |
||
| 601 | |||
| 602 | $request = $this->getRequest(); |
||
| 603 | |||
| 604 | $game = $sg->checkGame($identifier, false); |
||
| 605 | |||
| 606 | // Je recherche les posts validés associés à ce jeu |
||
| 607 | $posts = $sg->findArrayOfValidatedPosts($game, $filter, $search); |
||
| 608 | |||
| 609 | if (is_array($posts)) { |
||
| 610 | $paginator = new \Zend\Paginator\Paginator(new \Zend\Paginator\Adapter\ArrayAdapter($posts)); |
||
| 611 | $paginator->setItemCountPerPage($game->getPostDisplayNumber()); |
||
| 612 | $paginator->setCurrentPageNumber($this->getEvent()->getRouteMatch()->getParam('p')); |
||
| 613 | } else { |
||
| 614 | $paginator = $posts; |
||
| 615 | } |
||
| 616 | |||
| 617 | $form = new Form(); |
||
| 618 | $form->setAttribute('method', 'post'); |
||
| 619 | |||
| 620 | $form->add(array( |
||
| 621 | 'name' => 'moderation', |
||
| 622 | 'attributes' => array( |
||
| 623 | 'type' => 'hidden', |
||
| 624 | 'value' => '1' |
||
| 625 | ), |
||
| 626 | )); |
||
| 627 | |||
| 628 | $reportId =''; |
||
| 629 | |||
| 630 | if ($request->isPost()) { |
||
| 631 | $data = $request->getPost()->toArray(); |
||
| 632 | |||
| 633 | if (isset($data['moderation'])) { |
||
| 634 | $from = $to; |
||
| 635 | $subject= 'Moderation Post and Vote'; |
||
| 636 | $result = $mailService->createHtmlMessage( |
||
| 637 | $from, |
||
| 638 | $to, |
||
| 639 | $subject, |
||
| 640 | 'playground-game/email/moderation', |
||
| 641 | array('data' => $data, 'skinUrl' => $skinUrl) |
||
| 642 | ); |
||
| 643 | $mailService->send($result); |
||
| 644 | if ($result) { |
||
| 645 | $statusMail = true; |
||
| 646 | $reportId = $data['reportid']; |
||
| 647 | } |
||
| 648 | } |
||
| 649 | } |
||
| 650 | |||
| 651 | $viewModel = $this->buildView($game); |
||
| 652 | |||
| 653 | if ($postId) { |
||
| 654 | $postTarget = $sg->getPostVotePostMapper()->findById($postId); |
||
| 655 | if ($postTarget) { |
||
| 656 | View Code Duplication | foreach ($postTarget->getPostElements() as $element) { |
|
| 657 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
| 658 | '', |
||
| 659 | array(), |
||
| 660 | array('force_canonical' => true), |
||
| 661 | false |
||
| 662 | ) . $element->getValue(); |
||
| 663 | break; |
||
| 664 | } |
||
| 665 | |||
| 666 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
| 667 | |||
| 668 | // Without bit.ly shortener |
||
| 669 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 670 | 'postvote/list', |
||
| 671 | array( |
||
| 672 | 'id' => $game->getIdentifier(), |
||
| 673 | 'filter' => 'date', |
||
| 674 | |||
| 675 | ), |
||
| 676 | array('force_canonical' => true) |
||
| 677 | ).'?id='.$postTarget->getId().'&key='.$secretKey; |
||
| 678 | // With core shortener helper |
||
| 679 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
| 680 | |||
| 681 | $this->getViewHelper('HeadMeta')->setProperty('og:image', $fbShareImage); |
||
| 682 | |||
| 683 | $this->getViewHelper('HeadMeta')->setProperty('twitter:card', "photo"); |
||
| 684 | $this->getViewHelper('HeadMeta')->setProperty('twitter:site', "@alfie_selfie"); |
||
| 685 | $this->getViewHelper('HeadMeta')->setProperty('twitter:title', $game->getTwShareMessage()); |
||
| 686 | $this->getViewHelper('HeadMeta')->setProperty('twitter:description', ""); |
||
| 687 | $this->getViewHelper('HeadMeta')->setProperty('twitter:image', $fbShareImage); |
||
| 688 | $this->getViewHelper('HeadMeta')->setProperty('twitter:url', $socialLinkUrl); |
||
| 689 | } |
||
| 690 | } |
||
| 691 | |||
| 692 | $viewModel->setVariables(array( |
||
| 693 | 'posts' => $paginator, |
||
| 694 | 'form' => $form, |
||
| 695 | 'statusMail' => $statusMail, |
||
| 696 | 'reportId' => $reportId, |
||
| 697 | 'filter' => $filter, |
||
| 698 | 'search' => $search, |
||
| 699 | )); |
||
| 700 | |||
| 701 | return $viewModel; |
||
| 702 | } |
||
| 703 | |||
| 704 | public function ajaxVoteAction() |
||
| 746 | |||
| 747 | public function captchaAction() |
||
| 771 | |||
| 772 | public function shareAction() |
||
| 773 | { |
||
| 774 | $identifier = $this->getEvent()->getRouteMatch()->getParam('id'); |
||
| 775 | $user = $this->zfcUserAuthentication()->getIdentity(); |
||
| 776 | $sg = $this->getGameService(); |
||
| 777 | |||
| 778 | $statusMail = null; |
||
| 779 | |||
| 780 | if (!$identifier) { |
||
| 781 | return $this->notFoundAction(); |
||
| 782 | } |
||
| 783 | |||
| 784 | $gameMapper = $this->getGameService()->getGameMapper(); |
||
| 785 | $game = $gameMapper->findByIdentifier($identifier); |
||
| 786 | |||
| 787 | if (!$game || $game->isClosed()) { |
||
| 788 | return $this->notFoundAction(); |
||
| 789 | } |
||
| 790 | |||
| 791 | // Has the user finished the game ? |
||
| 792 | $lastEntry = $this->getGameService()->findLastInactiveEntry($game, $user); |
||
| 793 | |||
| 794 | View Code Duplication | if ($lastEntry === null) { |
|
| 795 | return $this->redirect()->toUrl( |
||
| 796 | $this->frontendUrl()->fromRoute( |
||
| 797 | 'postvote', |
||
| 798 | array( |
||
| 799 | 'id' => $identifier, |
||
| 800 | |||
| 801 | ) |
||
| 802 | ) |
||
| 803 | ); |
||
| 804 | } |
||
| 805 | |||
| 806 | $post = $sg->getPostVotePostMapper()->findOneBy(array('entry' => $lastEntry)); |
||
| 807 | |||
| 808 | $secretKey = strtoupper(substr(sha1(uniqid('pg_', true).'####'.time()), 0, 15)); |
||
| 809 | $socialLinkUrl = $this->frontendUrl()->fromRoute( |
||
| 810 | 'postvote/post', |
||
| 811 | array( |
||
| 812 | 'id' => $identifier, |
||
| 813 | 'post' => $post->getId(), |
||
| 814 | |||
| 815 | ), |
||
| 816 | array('force_canonical' => true) |
||
| 817 | ).'?key='.$secretKey; |
||
| 818 | // With core shortener helper |
||
| 819 | $socialLinkUrl = $this->shortenUrl()->shortenUrl($socialLinkUrl); |
||
| 820 | |||
| 821 | View Code Duplication | if (!$user && !$game->getAnonymousAllowed()) { |
|
| 822 | $redirect = urlencode( |
||
| 823 | $this->frontendUrl()->fromRoute( |
||
| 824 | 'postvote/result', |
||
| 825 | array( |
||
| 826 | 'id' => $game->getIdentifier(), |
||
| 827 | |||
| 828 | ) |
||
| 829 | ) |
||
| 830 | ); |
||
| 831 | return $this->redirect()->toUrl( |
||
| 832 | $this->frontendUrl()->fromRoute( |
||
| 833 | 'zfcuser/register', |
||
| 834 | array() |
||
| 835 | ) . '?redirect='.$redirect |
||
| 836 | ); |
||
| 837 | } |
||
| 838 | |||
| 839 | $form = $this->getServiceLocator()->get('playgroundgame_sharemail_form'); |
||
| 840 | $form->setAttribute('method', 'post'); |
||
| 841 | |||
| 842 | View Code Duplication | if ($this->getRequest()->isPost()) { |
|
| 843 | $data = $this->getRequest()->getPost()->toArray(); |
||
| 844 | $form->setData($data); |
||
| 845 | if ($form->isValid()) { |
||
| 846 | $result = $this->getGameService()->sendShareMail($data, $game, $user, $lastEntry); |
||
| 847 | if ($result) { |
||
| 848 | $statusMail = true; |
||
| 849 | } |
||
| 850 | } |
||
| 851 | } |
||
| 852 | |||
| 853 | $viewModel = $this->buildView($game); |
||
| 854 | |||
| 855 | View Code Duplication | foreach ($post->getPostElements() as $element) { |
|
| 856 | $fbShareImage = $this->frontendUrl()->fromRoute( |
||
| 857 | '', |
||
| 858 | array(), |
||
| 859 | array('force_canonical' => true), |
||
| 860 | false |
||
| 861 | ) . $element->getValue(); |
||
| 862 | break; |
||
| 863 | } |
||
| 864 | |||
| 865 | $this->getViewHelper('HeadMeta')->setProperty('og:image', $fbShareImage); |
||
| 866 | |||
| 867 | $this->getViewHelper('HeadMeta')->setProperty('twitter:card', "photo"); |
||
| 868 | $this->getViewHelper('HeadMeta')->setProperty('twitter:site', "@playground"); |
||
| 869 | $this->getViewHelper('HeadMeta')->setProperty('twitter:title', $game->getTwShareMessage()); |
||
| 870 | $this->getViewHelper('HeadMeta')->setProperty('twitter:description', ""); |
||
| 871 | $this->getViewHelper('HeadMeta')->setProperty('twitter:image', $fbShareImage); |
||
| 872 | $this->getViewHelper('HeadMeta')->setProperty('twitter:url', $socialLinkUrl); |
||
| 873 | |||
| 874 | $viewModel->setVariables(array( |
||
| 875 | 'statusMail' => $statusMail, |
||
| 876 | 'form' => $form, |
||
| 877 | 'socialLinkUrl' => $socialLinkUrl, |
||
| 878 | 'post' => $post |
||
| 879 | )); |
||
| 880 | |||
| 881 | return $viewModel; |
||
| 882 | } |
||
| 883 | |||
| 884 | public function getGameService() |
||
| 892 | } |
||
| 893 |
If you implement
__calland you know which methods are available, you can improve IDE auto-completion and static analysis by adding a @method annotation to the class.This is often the case, when
__callis implemented by a parent class and only the child class knows which methods exist: