TinyUrlsController.php 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. App::uses('ToolsAppController', 'Tools.Controller');
  3. /*
  4. Apply this route (/Config/routes.php):
  5. Router::connect('/s/:id',
  6. array('plugin'=>'tools', 'controller'=>'tiny_urls', 'action'=>'go'),
  7. array('id'=>'[0-9a-zA-Z]+'));
  8. Result:
  9. /domain/s/ID
  10. */
  11. class TinyUrlsController extends ToolsAppController {
  12. //public $uses = array('Tools.TinyUrl');
  13. public function beforeFilter() {
  14. parent::beforeFilter();
  15. if (isset($this->Auth)) {
  16. $this->Auth->allow('go');
  17. }
  18. }
  19. /****************************************************************************************
  20. * ADMIN functions
  21. ****************************************************************************************/
  22. /**
  23. * main redirect function
  24. * 2011-07-11 ms
  25. */
  26. public function go() {
  27. if (empty($this->request->params['id'])) {
  28. throw new NotFoundException();
  29. }
  30. $entry = $this->TinyUrl->translate($this->request->params['id']);
  31. if (empty($entry)) {
  32. throw new NotFoundException();
  33. }
  34. //$message = $entry['TinyInt']['flash_message'];
  35. $url = $entry['TinyUrl']['target'];
  36. if (!empty($message)) {
  37. $type = !empty($entry['TinyUrl']['flash_type']) ? $entry['TinyUrl']['flash_type'] : 'success';
  38. $this->Common->flashMessage($message, $type);
  39. }
  40. $this->TinyUrl->up($entry['TinyUrl']['id'], array('field'=>'used', 'modify'=>true, 'timestampField'=>'last_used'));
  41. return $this->redirect($url, 301);
  42. }
  43. public function admin_index() {
  44. //TODO
  45. if ($this->Common->isPosted()) {
  46. $this->TinyUrl->set($this->request->data);
  47. if ($this->TinyUrl->validates()) {
  48. $id = $this->TinyUrl->generate($this->TinyUrl->data['TinyUrl']['url']);
  49. $this->Common->flashMessage('New Key: '.h($id), 'success');
  50. $url = $this->TinyUrl->urlByKey($id);
  51. $this->set(compact('url'));
  52. $this->request->data = array();
  53. }
  54. }
  55. $tinyUrls = $this->TinyUrl->find('count', array('conditions'=>array()));
  56. $this->set(compact('tinyUrls'));
  57. }
  58. public function admin_listing() {
  59. }
  60. public function admin_reset() {
  61. if (!$this->Common->isPosted()) {
  62. throw new MethodNotAllowedException();
  63. }
  64. $this->TinyUrl->truncate();
  65. $this->Common->flashMessage(__('Done'), 'success');
  66. return $this->Common->autoRedirect(array('action'=>'index'));
  67. }
  68. }