Advertisement
Guest User

Untitled

a guest
Jun 30th, 2014
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 5.29 KB | None | 0 0
  1. <?php
  2. App::uses('AppController', 'Controller');
  3. /**
  4. * NewsAndMedia Controller
  5. *
  6. * @property NewsAndMedia $NewsAndMedia
  7. * @property PaginatorComponent $Paginator
  8. */
  9. class NewsAndMediaController extends AppController {
  10.  
  11. /**
  12. * Components
  13. *
  14. * @var array
  15. */
  16. public $components = array('Paginator');
  17.  
  18. /**
  19. * index method
  20. *
  21. * @return void
  22. */
  23. public function index() {
  24. $this->NewsAndMedia->recursive = 0;
  25. $this->set('newsAndMedia', $this->Paginator->paginate());
  26. }
  27.  
  28. /**
  29. * view method
  30. *
  31. * @throws NotFoundException
  32. * @param string $id
  33. * @return void
  34. */
  35. public function view($id = null) {
  36. if (!$this->NewsAndMedia->exists($id)) {
  37. throw new NotFoundException(__('Invalid news and media'));
  38. }
  39. $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
  40. $this->set('newsAndMedia', $this->NewsAndMedia->find('first', $options));
  41. }
  42.  
  43. /**
  44. * add method
  45. *
  46. * @return void
  47. */
  48. public function add() {
  49. if ($this->request->is('post')) {
  50. $this->NewsAndMedia->create();
  51. if ($this->uploadFile() && $this->NewsAndMedia->save($this->request->data)) {
  52. return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
  53. }
  54. }
  55. }
  56.  
  57. public function uploadFile() {
  58. $file = $this->data['NewsAndMedia']['pdf'];
  59. if ($file['error'] === UPLOAD_ERR_OK) {
  60. if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$file['name'])) {
  61. unset($this->request->data['NewsAndMedia']['pdf']);
  62. $this->request->data['NewsAndMedia']['pdf'] = $file['name'];
  63. return true;
  64. }
  65. }
  66. return false;
  67. }
  68.  
  69. /**
  70. * edit method
  71. *
  72. * @throws NotFoundException
  73. * @param string $id
  74. * @return void
  75. */
  76. public function edit($id = null) {
  77. if (!$this->NewsAndMedia->exists($id)) {
  78. throw new NotFoundException(__('Invalid news and media'));
  79. }
  80. if ($this->request->is(array('post', 'put'))) {
  81. if ($this->NewsAndMedia->save($this->request->data)) {
  82. return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
  83. }
  84. } else {
  85. $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
  86. $this->request->data = $this->NewsAndMedia->find('first', $options);
  87. }
  88. }
  89.  
  90. /**
  91. * delete method
  92. *
  93. * @throws NotFoundException
  94. * @param string $id
  95. * @return void
  96. */
  97. public function delete($id = null) {
  98. $this->NewsAndMedia->id = $id;
  99. if (!$this->NewsAndMedia->exists()) {
  100. throw new NotFoundException(__('Invalid news and media'));
  101. }
  102. $this->request->allowMethod('post', 'delete');
  103. $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
  104. $item = $this->NewsAndMedia->find('first', $options);
  105. unlink(APP.'uploads'.DS.$item['NewsAndMedia']['pdf']);
  106. if ($this->NewsAndMedia->delete()) {
  107. return $this->flash(__('The news and media has been deleted.'), array('action' => 'index'));
  108. } else {
  109. return $this->flash(__('The news and media could not be deleted. Please, try again.'), array('action' => 'index'));
  110. }
  111. }
  112.  
  113. /**
  114. * admin_index method
  115. *
  116. * @return void
  117. */
  118. public function admin_index() {
  119. $this->NewsAndMedia->recursive = 0;
  120. $this->set('newsAndMedia', $this->Paginator->paginate());
  121. }
  122.  
  123. /**
  124. * admin_view method
  125. *
  126. * @throws NotFoundException
  127. * @param string $id
  128. * @return void
  129. */
  130. public function admin_view($id = null) {
  131. if (!$this->NewsAndMedia->exists($id)) {
  132. throw new NotFoundException(__('Invalid news and media'));
  133. }
  134. $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
  135. $this->set('newsAndMedia', $this->NewsAndMedia->find('first', $options));
  136. }
  137.  
  138. /**
  139. * admin_add method
  140. *
  141. * @return void
  142. */
  143. public function admin_add() {
  144. if ($this->request->is('post')) {
  145. $this->NewsAndMedia->create();
  146. if ($this->NewsAndMedia->save($this->request->data)) {
  147. return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
  148. }
  149. }
  150. }
  151.  
  152. /**
  153. * admin_edit method
  154. *
  155. * @throws NotFoundException
  156. * @param string $id
  157. * @return void
  158. */
  159. public function admin_edit($id = null) {
  160. if (!$this->NewsAndMedia->exists($id)) {
  161. throw new NotFoundException(__('Invalid news and media'));
  162. }
  163. if ($this->request->is(array('post', 'put'))) {
  164. if ($this->NewsAndMedia->save($this->request->data)) {
  165. return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
  166. }
  167. } else {
  168. $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
  169. $this->request->data = $this->NewsAndMedia->find('first', $options);
  170. }
  171. }
  172.  
  173. /**
  174. * admin_delete method
  175. *
  176. * @throws NotFoundException
  177. * @param string $id
  178. * @return void
  179. */
  180. public function admin_delete($id = null) {
  181. $this->NewsAndMedia->id = $id;
  182. if (!$this->NewsAndMedia->exists()) {
  183. throw new NotFoundException(__('Invalid news and media'));
  184. }
  185. $this->request->allowMethod('post', 'delete');
  186. if ($this->NewsAndMedia->delete()) {
  187. return $this->flash(__('The news and media has been deleted.'), array('action' => 'index'));
  188. } else {
  189. return $this->flash(__('The news and media could not be deleted. Please, try again.'), array('action' => 'index'));
  190. }
  191. }
  192. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement