PageRenderTime 52ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/app/controllers/attachments_controller.php

http://croogo.googlecode.com/
PHP | 210 lines | 101 code | 21 blank | 88 comment | 20 complexity | b0e1bdebc3dd1a49d69ff223e2fa5f51 MD5 | raw file
Possible License(s): LGPL-2.1
  1. <?php
  2. /**
  3. * Attachments Controller
  4. *
  5. * This file will take care of file uploads (with rich text editor integration).
  6. *
  7. * PHP version 5
  8. *
  9. * @category Controller
  10. * @package Croogo
  11. * @version 1.0
  12. * @author Fahad Ibnay Heylaal <contact@fahad19.com>
  13. * @license http://www.opensource.org/licenses/mit-license.php The MIT License
  14. * @link http://www.croogo.org
  15. */
  16. class AttachmentsController extends AppController {
  17. /**
  18. * Controller name
  19. *
  20. * @var string
  21. * @access public
  22. */
  23. var $name = 'Attachments';
  24. /**
  25. * Models used by the Controller
  26. *
  27. * @var array
  28. * @access public
  29. */
  30. var $uses = array('Node');
  31. /**
  32. * Helpers used by the Controller
  33. *
  34. * @var array
  35. * @access public
  36. */
  37. var $helpers = array('Filemanager', 'Text', 'Image');
  38. /**
  39. * Node type
  40. *
  41. * If the Controller uses Node model,
  42. * this is, most of the time, the singular of the Controller name in lowercase.
  43. *
  44. * @var string
  45. * @access public
  46. */
  47. var $type = 'attachment';
  48. /**
  49. * Uploads directory
  50. *
  51. * relative to the webroot.
  52. *
  53. * @var string
  54. * @access public
  55. */
  56. var $uploadsDir = 'uploads';
  57. /**
  58. * Before executing controller actions
  59. *
  60. * @return void
  61. * @access public
  62. */
  63. function beforeFilter() {
  64. parent::beforeFilter();
  65. // Comment, Category, Tag not needed
  66. $this->Node->unbindModel(array('hasMany' => array('Comment'), 'hasAndBelongsToMany' => array('Category', 'Tag')));
  67. $this->Node->type = $this->type;
  68. $this->Node->Behaviors->attach('Tree', array('scope' => array('Node.type' => $this->type)));
  69. $this->set('type', $this->type);
  70. }
  71. /**
  72. * Admin index
  73. *
  74. * @return void
  75. * @access public
  76. */
  77. function admin_index() {
  78. $this->pageTitle = __('Attachments', true);
  79. $this->Node->recursive = 0;
  80. $this->paginate['Node']['order'] = 'Node.created DESC';
  81. $this->set('attachments', $this->paginate());
  82. }
  83. /**
  84. * Admin add
  85. *
  86. * @return void
  87. * @access public
  88. */
  89. function admin_add() {
  90. $this->pageTitle = __("Add Attachment", true);
  91. if (isset($this->params['named']['editor'])) {
  92. $this->layout = 'admin_full';
  93. }
  94. if (!empty($this->data)) {
  95. $file = $this->data['Node']['file'];
  96. unset($this->data['Node']['file']);
  97. // check if file with same path exists
  98. $destination = WWW_ROOT . $this->uploadsDir . DS . $file['name'];
  99. if (file_exists($destination)) {
  100. $newFileName = String::uuid() . '-' . $file['name'];
  101. $destination = WWW_ROOT . $this->uploadsDir . DS . $newFileName;
  102. } else {
  103. $newFileName = $file['name'];
  104. }
  105. // remove the extension for title
  106. if (explode('.', $file['name']) > 0) {
  107. $fileTitleE = explode('.', $file['name']);
  108. array_pop($fileTitleE);
  109. $fileTitle = implode('.', $fileTitleE);
  110. } else {
  111. $fileTitle = $file['name'];
  112. }
  113. $this->data['Node']['title'] = $fileTitle;
  114. $this->data['Node']['slug'] = $newFileName;
  115. $this->data['Node']['mime_type'] = $file['type'];
  116. //$this->data['Node']['guid'] = Router::url('/' . $this->uploadsDir . '/' . $newFileName, true);
  117. $this->data['Node']['path'] = '/' . $this->uploadsDir . '/' . $newFileName;
  118. $this->Node->create();
  119. if ($this->Node->save($this->data)) {
  120. // move the file
  121. move_uploaded_file($file['tmp_name'], $destination);
  122. $this->Session->setFlash(__('The Attachment has been saved', true));
  123. if (isset($this->params['named']['editor'])) {
  124. $this->redirect(array('action' => 'browse'));
  125. } else {
  126. $this->redirect(array('action'=>'index'));
  127. }
  128. } else {
  129. $this->Session->setFlash(__('The Attachment could not be saved. Please, try again.', true));
  130. }
  131. }
  132. }
  133. /**
  134. * Admin edit
  135. *
  136. * @param int $id
  137. * @return void
  138. * @access public
  139. */
  140. function admin_edit($id = null) {
  141. $this->pageTitle = __("Edit Attachment", true);
  142. if (!$id && empty($this->data)) {
  143. $this->Session->setFlash(__('Invalid Attachment', true));
  144. $this->redirect(array('action'=>'index'));
  145. }
  146. if (!empty($this->data)) {
  147. if ($this->Node->save($this->data)) {
  148. $this->Session->setFlash(__('The Attachment has been saved', true));
  149. $this->redirect(array('action'=>'index'));
  150. } else {
  151. $this->Session->setFlash(__('The Attachment could not be saved. Please, try again.', true));
  152. }
  153. }
  154. if (empty($this->data)) {
  155. $this->data = $this->Node->read(null, $id);
  156. }
  157. }
  158. /**
  159. * Admin delete
  160. *
  161. * @param int $id
  162. * @return void
  163. * @access public
  164. */
  165. function admin_delete($id = null) {
  166. if (!$id) {
  167. $this->Session->setFlash(__('Invalid id for Attachment', true));
  168. $this->redirect(array('action'=>'index'));
  169. }
  170. // get Node
  171. $attachment = $this->Node->find('first', array('conditions' => array('Node.id' => $id, 'Node.type' => $this->type)));
  172. if (isset($attachment['Node'])) {
  173. if ($this->Node->delete($id)) {
  174. // delete the file
  175. unlink(WWW_ROOT . $this->uploadsDir . DS . $attachment['Node']['slug']);
  176. $this->Session->setFlash(__('Attachment deleted', true));
  177. $this->redirect(array('action'=>'index'));
  178. }
  179. } else {
  180. $this->Session->setFlash(__('Invalid id for Attachment', true));
  181. $this->redirect(array('action'=>'index'));
  182. }
  183. }
  184. function admin_browse() {
  185. $this->layout = 'admin_full';
  186. $this->admin_index();
  187. }
  188. }
  189. ?>