Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <?php
- App::uses('AppController', 'Controller');
- /**
- * NewsAndMedia Controller
- *
- * @property NewsAndMedia $NewsAndMedia
- * @property PaginatorComponent $Paginator
- */
- class NewsAndMediaController extends AppController {
- /**
- * Components
- *
- * @var array
- */
- public $components = array('Paginator');
- /**
- * index method
- *
- * @return void
- */
- public function index() {
- $this->NewsAndMedia->recursive = 0;
- $this->set('newsAndMedia', $this->Paginator->paginate());
- }
- /**
- * view method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function view($id = null) {
- if (!$this->NewsAndMedia->exists($id)) {
- throw new NotFoundException(__('Invalid news and media'));
- }
- $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
- $this->set('newsAndMedia', $this->NewsAndMedia->find('first', $options));
- }
- /**
- * add method
- *
- * @return void
- */
- public function add() {
- if ($this->request->is('post')) {
- $this->NewsAndMedia->create();
- if ($this->uploadFile() && $this->NewsAndMedia->save($this->request->data)) {
- return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
- }
- }
- }
- public function uploadFile() {
- $file = $this->data['NewsAndMedia']['pdf'];
- if ($file['error'] === UPLOAD_ERR_OK) {
- if (move_uploaded_file($file['tmp_name'], APP.'uploads'.DS.$file['name'])) {
- unset($this->request->data['NewsAndMedia']['pdf']);
- $this->request->data['NewsAndMedia']['pdf'] = $file['name'];
- return true;
- }
- }
- return false;
- }
- /**
- * edit method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function edit($id = null) {
- if (!$this->NewsAndMedia->exists($id)) {
- throw new NotFoundException(__('Invalid news and media'));
- }
- if ($this->request->is(array('post', 'put'))) {
- if ($this->NewsAndMedia->save($this->request->data)) {
- return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
- }
- } else {
- $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
- $this->request->data = $this->NewsAndMedia->find('first', $options);
- }
- }
- /**
- * delete method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function delete($id = null) {
- $this->NewsAndMedia->id = $id;
- if (!$this->NewsAndMedia->exists()) {
- throw new NotFoundException(__('Invalid news and media'));
- }
- $this->request->allowMethod('post', 'delete');
- $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
- $item = $this->NewsAndMedia->find('first', $options);
- unlink(APP.'uploads'.DS.$item['NewsAndMedia']['pdf']);
- if ($this->NewsAndMedia->delete()) {
- return $this->flash(__('The news and media has been deleted.'), array('action' => 'index'));
- } else {
- return $this->flash(__('The news and media could not be deleted. Please, try again.'), array('action' => 'index'));
- }
- }
- /**
- * admin_index method
- *
- * @return void
- */
- public function admin_index() {
- $this->NewsAndMedia->recursive = 0;
- $this->set('newsAndMedia', $this->Paginator->paginate());
- }
- /**
- * admin_view method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function admin_view($id = null) {
- if (!$this->NewsAndMedia->exists($id)) {
- throw new NotFoundException(__('Invalid news and media'));
- }
- $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
- $this->set('newsAndMedia', $this->NewsAndMedia->find('first', $options));
- }
- /**
- * admin_add method
- *
- * @return void
- */
- public function admin_add() {
- if ($this->request->is('post')) {
- $this->NewsAndMedia->create();
- if ($this->NewsAndMedia->save($this->request->data)) {
- return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
- }
- }
- }
- /**
- * admin_edit method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function admin_edit($id = null) {
- if (!$this->NewsAndMedia->exists($id)) {
- throw new NotFoundException(__('Invalid news and media'));
- }
- if ($this->request->is(array('post', 'put'))) {
- if ($this->NewsAndMedia->save($this->request->data)) {
- return $this->flash(__('The news and media has been saved.'), array('action' => 'index'));
- }
- } else {
- $options = array('conditions' => array('NewsAndMedia.' . $this->NewsAndMedia->primaryKey => $id));
- $this->request->data = $this->NewsAndMedia->find('first', $options);
- }
- }
- /**
- * admin_delete method
- *
- * @throws NotFoundException
- * @param string $id
- * @return void
- */
- public function admin_delete($id = null) {
- $this->NewsAndMedia->id = $id;
- if (!$this->NewsAndMedia->exists()) {
- throw new NotFoundException(__('Invalid news and media'));
- }
- $this->request->allowMethod('post', 'delete');
- if ($this->NewsAndMedia->delete()) {
- return $this->flash(__('The news and media has been deleted.'), array('action' => 'index'));
- } else {
- return $this->flash(__('The news and media could not be deleted. Please, try again.'), array('action' => 'index'));
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement