PageRenderTime 36ms CodeModel.GetById 12ms RepoModel.GetById 1ms app.codeStats 0ms

/FileManager/Controller/AttachmentsController.php

https://github.com/kareypowell/croogo
PHP | 206 lines | 110 code | 26 blank | 70 comment | 22 complexity | 925f296570c384864f4e71c715970bae MD5 | raw file
  1. <?php
  2. App::uses('FileManagerAppController', 'FileManager.Controller');
  3. /**
  4. * Attachments Controller
  5. *
  6. * This file will take care of file uploads (with rich text editor integration).
  7. *
  8. * @category FileManager.Controller
  9. * @package Croogo.FileManager.Controller
  10. * @version 1.0
  11. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  12. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  13. * @link http://www.croogo.org
  14. */
  15. class AttachmentsController extends FileManagerAppController {
  16. /**
  17. * Models used by the Controller
  18. *
  19. * @var array
  20. * @access public
  21. */
  22. public $uses = array('FileManager.Attachment');
  23. /**
  24. * Helpers used by the Controller
  25. *
  26. * @var array
  27. * @access public
  28. */
  29. public $helpers = array('FileManager.FileManager', 'Text', 'Croogo.Image');
  30. /**
  31. * Provides backwards compatibility access to the deprecated properties
  32. */
  33. public function __get($name) {
  34. switch ($name) {
  35. case 'type':
  36. case 'uploadsDir':
  37. return $this->Attachment->{$name};
  38. break;
  39. default:
  40. return parent::__get($name);
  41. }
  42. }
  43. /**
  44. * Provides backwards compatibility access for settings values to deprecated
  45. * properties
  46. */
  47. public function __set($name, $val) {
  48. switch ($name) {
  49. case 'type':
  50. case 'uploadsDir':
  51. return $this->Attachment->{$name} = $val;
  52. break;
  53. default:
  54. return parent::__set($name, $val);
  55. }
  56. }
  57. /**
  58. * Before executing controller actions
  59. *
  60. * @return void
  61. * @access public
  62. */
  63. public function beforeFilter() {
  64. parent::beforeFilter();
  65. // Comment, Category, Tag not needed
  66. $this->Attachment->unbindModel(array(
  67. 'hasMany' => array('Comment'),
  68. 'hasAndBelongsToMany' => array('Category', 'Tag'))
  69. );
  70. $this->Attachment->type = $this->type;
  71. $this->Attachment->Behaviors->attach('Tree', array(
  72. 'scope' => array(
  73. $this->Attachment->alias . '.type' => $this->type,
  74. )
  75. ));
  76. $this->set('type', $this->Attachment->type);
  77. if ($this->action == 'admin_add') {
  78. $this->Security->csrfCheck = false;
  79. }
  80. }
  81. /**
  82. * Admin index
  83. *
  84. * @return void
  85. * @access public
  86. */
  87. public function admin_index() {
  88. $this->set('title_for_layout', __d('croogo', 'Attachments'));
  89. $this->Attachment->recursive = 0;
  90. $this->paginate['Attachment']['order'] = 'Attachment.created DESC';
  91. $this->set('attachments', $this->paginate());
  92. }
  93. /**
  94. * Admin add
  95. *
  96. * @return void
  97. * @access public
  98. */
  99. public function admin_add() {
  100. $this->set('title_for_layout', __d('croogo', 'Add Attachment'));
  101. if (isset($this->request->params['named']['editor'])) {
  102. $this->layout = 'admin_popup';
  103. }
  104. if ($this->request->is('post') || !empty($this->request->data)) {
  105. if (empty($this->data['Attachment'])) {
  106. $this->Attachment->invalidate('file', __d('croogo', 'Upload failed. Please ensure size does not exceed the server limit.'));
  107. return;
  108. }
  109. $this->Attachment->create();
  110. if ($this->Attachment->save($this->request->data)) {
  111. $this->Session->setFlash(__d('croogo', 'The Attachment has been saved'), 'default', array('class' => 'success'));
  112. if (isset($this->request->params['named']['editor'])) {
  113. return $this->redirect(array('action' => 'browse'));
  114. } else {
  115. return $this->redirect(array('action' => 'index'));
  116. }
  117. } else {
  118. $this->Session->setFlash(__d('croogo', 'The Attachment could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  119. }
  120. }
  121. }
  122. /**
  123. * Admin edit
  124. *
  125. * @param int $id
  126. * @return void
  127. * @access public
  128. */
  129. public function admin_edit($id = null) {
  130. $this->set('title_for_layout', __d('croogo', 'Edit Attachment'));
  131. if (isset($this->request->params['named']['editor'])) {
  132. $this->layout = 'admin_popup';
  133. }
  134. if (!$id && empty($this->request->data)) {
  135. $this->Session->setFlash(__d('croogo', 'Invalid Attachment'), 'default', array('class' => 'error'));
  136. return $this->redirect(array('action' => 'index'));
  137. }
  138. if (!empty($this->request->data)) {
  139. if ($this->Attachment->save($this->request->data)) {
  140. $this->Session->setFlash(__d('croogo', 'The Attachment has been saved'), 'default', array('class' => 'success'));
  141. return $this->Croogo->redirect(array('action' => 'edit', $this->Attachment->id));
  142. } else {
  143. $this->Session->setFlash(__d('croogo', 'The Attachment could not be saved. Please, try again.'), 'default', array('class' => 'error'));
  144. }
  145. }
  146. if (empty($this->request->data)) {
  147. $this->request->data = $this->Attachment->read(null, $id);
  148. }
  149. }
  150. /**
  151. * Admin delete
  152. *
  153. * @param int $id
  154. * @return void
  155. * @access public
  156. */
  157. public function admin_delete($id = null) {
  158. if (!$id) {
  159. $this->Session->setFlash(__d('croogo', 'Invalid id for Attachment'), 'default', array('class' => 'error'));
  160. return $this->redirect(array('action' => 'index'));
  161. }
  162. if ($this->Attachment->delete($id)) {
  163. $this->Session->setFlash(__d('croogo', 'Attachment deleted'), 'default', array('class' => 'success'));
  164. return $this->redirect(array('action' => 'index'));
  165. } else {
  166. $this->Session->setFlash(__d('croogo', 'Invalid id for Attachment'), 'default', array('class' => 'error'));
  167. return $this->redirect(array('action' => 'index'));
  168. }
  169. }
  170. /**
  171. * Admin browse
  172. *
  173. * @return void
  174. * @access public
  175. */
  176. public function admin_browse() {
  177. $this->layout = 'admin_popup';
  178. $this->admin_index();
  179. }
  180. }