QurlsController.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125
  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->info(nl2br($note));
  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. $qurls = $this->paginate();
  38. $this->set(compact('qurls'));
  39. }
  40. /**
  41. * @return void
  42. */
  43. public function admin_view($id = null) {
  44. if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id]]))) {
  45. $this->Flash->error(__('invalidRecord'));
  46. return $this->Common->autoRedirect(['action' => 'index']);
  47. }
  48. $this->set(compact('qurl'));
  49. }
  50. /**
  51. * @return void
  52. */
  53. public function admin_add($templateId = null) {
  54. if ($this->Common->isPosted()) {
  55. $this->Qurl->create();
  56. $this->request->data['Qurl']['key'] = '';
  57. if ($res = $this->Qurl->save($this->request->data)) {
  58. $var = $this->Qurl->urlByKey($res['Qurl']['key'], $res['Qurl']['title']);
  59. $this->Flash->success(__('Qurl: %s', $var));
  60. return $this->Common->postRedirect(['action' => 'index']);
  61. } else {
  62. $this->Flash->error(__('formContainsErrors'));
  63. }
  64. } else {
  65. $this->request->data['Qurl']['active'] = 1;
  66. if ($templateId && ($template = $this->Qurl->get($templateId))) {
  67. $this->request->data = $template;
  68. }
  69. }
  70. }
  71. /**
  72. * @return void
  73. */
  74. public function admin_edit($id = null) {
  75. if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id]]))) {
  76. $this->Flash->error(__('invalidRecord'));
  77. return $this->Common->autoRedirect(['action' => 'index']);
  78. }
  79. if ($this->Common->isPosted()) {
  80. if ($this->Qurl->save($this->request->data)) {
  81. $var = $this->request->data['Qurl']['key'];
  82. $this->Flash->success(__('record edit %s saved', h($var)));
  83. return $this->Common->postRedirect(['action' => 'index']);
  84. } else {
  85. $this->Flash->error(__('formContainsErrors'));
  86. }
  87. }
  88. if (empty($this->request->data)) {
  89. $this->request->data = $qurl;
  90. }
  91. }
  92. /**
  93. * @return void
  94. */
  95. public function admin_delete($id = null) {
  96. $this->request->allowMethod('post');
  97. if (empty($id) || !($qurl = $this->Qurl->find('first', ['conditions' => ['Qurl.id' => $id], 'fields' => ['id', 'key']]))) {
  98. $this->Flash->error(__('invalidRecord'));
  99. return $this->Common->autoRedirect(['action' => 'index']);
  100. }
  101. $var = $qurl['Qurl']['key'];
  102. if ($this->Qurl->delete($id)) {
  103. $this->Flash->success(__('record del %s done', h($var)));
  104. return $this->redirect(['action' => 'index']);
  105. }
  106. $this->Flash->error(__('record del %s not done exception', h($var)));
  107. return $this->Common->autoRedirect(['action' => 'index']);
  108. }
  109. }