PageRenderTime 43ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/bqregsys/app/controllers/registrations_controller.php

https://bitbucket.org/dvcolgan/bqregsys
PHP | 191 lines | 157 code | 21 blank | 13 comment | 33 complexity | 53a064c22c72362b1e12e34a49e3646b MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. class RegistrationsController extends AppController {
  3. var $name = 'Registrations';
  4. var $helpers = array('Html', 'Form', 'Javascript');
  5. var $components = array('Email');
  6. function beforeFilter() {
  7. $this->Auth->allow('add');
  8. $this->Auth->allow('resume');
  9. $this->Auth->allow('edit');
  10. $this->Auth->allow('thank_you');
  11. }
  12. function resume() {
  13. $this->Session->destroy();
  14. if (!empty($this->data)) {
  15. $registration = $this->Registration->find('first', array('conditions'=>array('Registration.email'=>$this->data['email'], 'Registration.resumption_key'=>$this->data['resumption_key'])));
  16. if (!empty($registration)) {
  17. $this->Session->write('registration_id', $registration['Registration']['id']);
  18. $this->Session->write('resumption_key', $registration['Registration']['resumption_key']);
  19. $this->redirect(array('action'=>'edit', $registration['Registration']['id']));
  20. }
  21. else {
  22. $this->Session->setFlash('Email or resumption key invalid.');
  23. }
  24. }
  25. }
  26. function index() {
  27. $this->Registration->recursive = 1;
  28. $this->set('registrations', $this->paginate());
  29. }
  30. function view($id = null) {
  31. if (!$id) {
  32. $this->Session->setFlash(__('Invalid Registration.', true));
  33. $this->redirect(array('action'=>'index'));
  34. }
  35. $this->set('registration', $this->Registration->read(null, $id));
  36. }
  37. /*function add() {
  38. if (!empty($this->data)) {
  39. $this->Registration->create();
  40. if ($this->Registration->save($this->data)) {
  41. $this->Session->setFlash(__('The Registration has been saved', true));
  42. $this->redirect(array('action'=>'index'));
  43. } else {
  44. $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
  45. }
  46. }
  47. $tournaments = $this->Registration->Tournament->find('list');
  48. $this->set(compact('tournaments'));
  49. }*/
  50. function add() {
  51. if(!empty($this->data)) {
  52. $this->data['Registration']['resumption_key'] = substr(md5(uniqid(rand(), true)), 0, 12);
  53. if ($this->Registration->saveAll($this->data, array('validate'=>'first'))) {
  54. $this->Session->setFlash(__('The Registration has been saved', true));
  55. $this->Session->write('resumption_key', $this->data['Registration']['resumption_key']);
  56. $this->redirect(array('action'=>'thank_you'));
  57. } else {
  58. $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
  59. }
  60. }
  61. $tournaments = $this->Registration->Tournament->find('list');
  62. $this->set(compact('tournaments'));
  63. }
  64. function edit($id = null) {
  65. if (!($this->Session->read('registration_id') == $id)) {
  66. if (!$this->Session->check('Auth.User.id')) {
  67. $this->redirect(array('action'=>'resume'));
  68. }
  69. }
  70. if (!$id && empty($this->data)) {
  71. $this->Session->setFlash(__('Invalid Registration', true));
  72. $this->redirect(array('action'=>'index'));
  73. }
  74. if (!empty($this->data)) {
  75. $registration = $this->Registration->findById($id);
  76. $this->data['Registration']['resumption_key'] = $registration['Registration']['resumption_key'];
  77. $this->Registration->deleteAll(array('Registration.id' => $id));
  78. if ($this->Registration->saveAll($this->data, array('validate'=>'first'))) {
  79. if ($this->Session->check('Auth.User.id')) {
  80. $this->redirect(array('action'=>'index'));
  81. }
  82. else {
  83. $registration = $this->Registration->findById($id);
  84. $this->redirect(array('action'=>'thank_you'));
  85. }
  86. } else {
  87. $this->Session->setFlash('The Registration could not be saved. Please, try again.');
  88. $this->Session->destroy();
  89. }
  90. }
  91. if (empty($this->data)) {
  92. $this->data = $this->Registration->findById($id);
  93. $this->set('registration',$this->data);
  94. $this->set('registration_id', $id);
  95. }
  96. $tournaments = $this->Registration->Tournament->find('list');
  97. $this->set(compact('tournaments'));
  98. }
  99. function thank_you() {
  100. if (!$this->Session->check('resumption_key')) {
  101. $this->redirect(array('action'=>'add'));
  102. }
  103. $this->set('resumption_key', $this->Session->read('resumption_key'));
  104. $this->Session->destroy();
  105. }
  106. function delete($id = null) {
  107. if (!$id) {
  108. $this->Session->setFlash(__('Invalid id for Registration', true));
  109. $this->redirect(array('action'=>'index'));
  110. }
  111. if ($this->Registration->del($id)) {
  112. $this->Session->setFlash(__('Registration deleted', true));
  113. $this->redirect(array('action'=>'index'));
  114. }
  115. }
  116. function admin_index() {
  117. $this->Registration->recursive = 0;
  118. $this->set('registrations', $this->paginate());
  119. }
  120. function admin_view($id = null) {
  121. if (!$id) {
  122. $this->Session->setFlash(__('Invalid Registration.', true));
  123. $this->redirect(array('action'=>'index'));
  124. }
  125. $this->set('registration', $this->Registration->read(null, $id));
  126. }
  127. function admin_add() {
  128. if (!empty($this->data)) {
  129. $this->Registration->create();
  130. if ($this->Registration->save($this->data)) {
  131. $this->Session->setFlash(__('The Registration has been saved', true));
  132. $this->redirect(array('action'=>'index'));
  133. } else {
  134. $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
  135. }
  136. }
  137. $tournaments = $this->Registration->Tournament->find('list');
  138. $this->set(compact('tournaments'));
  139. }
  140. function admin_edit($id = null) {
  141. if (!$id && empty($this->data)) {
  142. $this->Session->setFlash(__('Invalid Registration', true));
  143. $this->redirect(array('action'=>'index'));
  144. }
  145. if (!empty($this->data)) {
  146. if ($this->Registration->save($this->data)) {
  147. $this->Session->setFlash(__('The Registration has been saved', true));
  148. $this->redirect(array('action'=>'index'));
  149. } else {
  150. $this->Session->setFlash(__('The Registration could not be saved. Please, try again.', true));
  151. }
  152. }
  153. if (empty($this->data)) {
  154. $this->data = $this->Registration->read(null, $id);
  155. }
  156. $tournaments = $this->Registration->Tournament->find('list');
  157. $this->set(compact('tournaments'));
  158. }
  159. function admin_delete($id = null) {
  160. if (!$id) {
  161. $this->Session->setFlash(__('Invalid id for Registration', true));
  162. $this->redirect(array('action'=>'index'));
  163. }
  164. if ($this->Registration->del($id)) {
  165. $this->Session->setFlash(__('Registration deleted', true));
  166. $this->redirect(array('action'=>'index'));
  167. }
  168. }
  169. }
  170. ?>