QurlsController.php 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127
  1. <?php
  2. App::uses('ToolsAppController', 'Tools.Controller');
  3. /**
  4. * Qurls Controller
  5. *
  6. */
  7. class QurlsController extends ToolsAppController {
  8. public $paginate = [];
  9. public $components = ['Tools.Common'];
  10. public function beforeFilter() {
  11. parent::beforeFilter();
  12. if (isset($this->Auth)) {
  13. $this->Auth->allow('go');
  14. }
  15. }
  16. /**
  17. * Main login function
  18. */
  19. public function go($key) {
  20. $entry = $this->Qurl->translate($key);
  21. if (empty($entry)) {
  22. throw new NotFoundException();
  23. }
  24. //die(returns($entry));
  25. $note = $entry['Qurl']['note'];
  26. $url = $entry['Qurl']['url'];
  27. if ($note) {
  28. $this->Flash->message(nl2br($note), 'info');
  29. }
  30. $this->Qurl->markAsUsed($entry['Qurl']['id']);
  31. return $this->redirect($url);
  32. }
  33. /**
  34. * @return void
  35. */
  36. public function admin_index() {
  37. $this->Qurl->recursive = 0;
  38. $qurls = $this->paginate();
  39. $this->set(compact('qurls'));
  40. }
  41. /**
  42. * @return void
  43. */
  44. public function admin_view($id = null) {
  45. $this->Qurl->recursive = 0;
  46. if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id]]))) {
  47. $this->Flash->message(__('invalidRecord'), 'error');
  48. return $this->Common->autoRedirect(['action' => 'index']);
  49. }
  50. $this->set(compact('qurl'));
  51. }
  52. /**
  53. * @return void
  54. */
  55. public function admin_add($templateId = null) {
  56. if ($this->Common->isPosted()) {
  57. $this->Qurl->create();
  58. $this->request->data['Qurl']['key'] = '';
  59. if ($res = $this->Qurl->save($this->request->data)) {
  60. $var = $this->Qurl->urlByKey($res['Qurl']['key'], $res['Qurl']['title']);
  61. $this->Flash->message(__('Qurl: %s', $var), 'success');
  62. return $this->Common->postRedirect(['action' => 'index']);
  63. } else {
  64. $this->Flash->message(__('formContainsErrors'), 'error');
  65. }
  66. } else {
  67. $this->request->data['Qurl']['active'] = 1;
  68. if ($templateId && ($template = $this->Qurl->get($templateId))) {
  69. $this->request->data = $template;
  70. }
  71. }
  72. }
  73. /**
  74. * @return void
  75. */
  76. public function admin_edit($id = null) {
  77. if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id]]))) {
  78. $this->Flash->message(__('invalidRecord'), 'error');
  79. return $this->Common->autoRedirect(['action' => 'index']);
  80. }
  81. if ($this->Common->isPosted()) {
  82. if ($this->Qurl->save($this->request->data)) {
  83. $var = $this->request->data['Qurl']['key'];
  84. $this->Flash->message(__('record edit %s saved', h($var)), 'success');
  85. return $this->Common->postRedirect(['action' => 'index']);
  86. } else {
  87. $this->Flash->message(__('formContainsErrors'), 'error');
  88. }
  89. }
  90. if (empty($this->request->data)) {
  91. $this->request->data = $qurl;
  92. }
  93. }
  94. /**
  95. * @return void
  96. */
  97. public function admin_delete($id = null) {
  98. $this->request->allowMethod('post');
  99. if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id], 'fields' => ['id', 'key']]))) {
  100. $this->Flash->message(__('invalidRecord'), 'error');
  101. return $this->Common->autoRedirect(['action' => 'index']);
  102. }
  103. $var = $qurl['Qurl']['key'];
  104. if ($this->Qurl->delete($id)) {
  105. $this->Flash->message(__('record del %s done', h($var)), 'success');
  106. return $this->redirect(['action' => 'index']);
  107. }
  108. $this->Flash->message(__('record del %s not done exception', h($var)), 'error');
  109. return $this->Common->autoRedirect(['action' => 'index']);
  110. }
  111. }