PageRenderTime 28ms CodeModel.GetById 7ms RepoModel.GetById 0ms app.codeStats 0ms

/modules/document/classes/controller/document.php

https://github.com/kodeplay/kodelearn
PHP | 272 lines | 196 code | 75 blank | 1 comment | 17 complexity | e4882db04716494c5c49d31a18d508f7 MD5 | raw file
  1. <?php defined('SYSPATH') or die('No direct script access.');
  2. class Controller_Document extends Controller_Base {
  3. private $_errors = array();
  4. public function action_index() {
  5. $course_id = Session::instance()->get('course_id');
  6. $course = ORM::factory('course', $course_id);
  7. $role = Auth::instance()->get_user()->role();
  8. $criteria = array(
  9. 'course' => $course,
  10. 'role' => $role,
  11. 'filter_title' => $this->request->param('filter_title'),
  12. 'filter_by' => $this->request->param('filter_by')
  13. );
  14. $documents = Model_Document::documents($criteria); //ORM::factory('document')->find_all();
  15. $url = ('document/index');
  16. $filter = array(
  17. 'text' => '',
  18. 'select' => ''
  19. );
  20. if ($this->request->param('filter_title')) {
  21. $url .= '/filter_title/'.$this->request->param('filter_title');
  22. $filter = array(
  23. 'text' => $this->request->param('filter_title'),
  24. 'select' => 'filter_title'
  25. );
  26. }
  27. if ($this->request->param('filter_by')) {
  28. $url .= '/filter_by/'.$this->request->param('filter_by');
  29. $filter = array(
  30. 'text' => $this->request->param('filter_by'),
  31. 'select' => 'filter_by'
  32. );
  33. }
  34. $filter['url'] = URL::site('document/index');
  35. $view = View::factory('document/list')
  36. ->set('page_title', 'Documents Manager')
  37. ->bind('documents', $documents)
  38. ->bind('filter', $filter);
  39. $this->content = $view;
  40. Breadcrumbs::add(array(
  41. 'Courses', Url::site('course')
  42. ));
  43. Breadcrumbs::add(array(
  44. 'Documents', Url::site('document')
  45. ));
  46. }
  47. public function action_download(){
  48. $id = $this->request->param('id');
  49. $document = ORM::factory('document', $id);
  50. $path = UPLOAD_PATH;
  51. $filename = $path . $document->name;
  52. if((!file_exists($filename)) || (file_exists($filename) && is_dir($filename))){
  53. Request::current()->redirect('error/not_found');
  54. }
  55. if(!$document->is_allowed()){
  56. Request::current()->redirect('error/access_denied');
  57. }
  58. $download_name = str_replace(substr(basename($filename),0,13),'',basename($filename)); //to remove the uniqid prepended to the filename
  59. header("Pragma: public");
  60. header("Expires: 0");
  61. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  62. header("Cache-Control: public");
  63. header("Content-Description: File Transfer");
  64. header("Content-Type: " . File::mime($filename));
  65. header("Content-Disposition: attachment; filename=" . $download_name);
  66. header("Content-Transfer-Encoding: binary");
  67. readfile($filename);
  68. exit;
  69. }
  70. public function action_upload(){
  71. $submitted = FALSE;
  72. if ($this->request->method() === 'POST' && $this->request->post()) {
  73. if (Arr::get($this->request->post(), 'save') !== null) {
  74. $submitted = TRUE;
  75. $document = ORM::factory('document');
  76. $validator = $document->validator(array_merge($this->request->post(), $_FILES));
  77. $validator->bind(':files', $_FILES['name']);
  78. if($validator->check()){
  79. $filename = Upload::save($_FILES['name'], NULL, UPLOAD_PATH);
  80. $document = ORM::factory('document');
  81. $document->values($this->request->post());
  82. $document->name = basename($filename);
  83. $document->time = time();
  84. $document->save();
  85. $document->add('courses', $this->request->post('course_id'));
  86. $document->add('roles', $this->request->post('role'));
  87. foreach($this->request->post('course_id') as $course_id){
  88. $feed = new Feed_Document();
  89. $feed->set_action('add');
  90. $feed->set_course_id($course_id);
  91. $feed->set_respective_id($document->id);
  92. $feed->set_actor_id(Auth::instance()->get_user()->id);
  93. $stream_data = array(
  94. 'course_id' => $course_id,
  95. 'role_id' => $this->request->post('role'),
  96. );
  97. $feed->streams($stream_data);
  98. $feed->save();
  99. }
  100. Request::current()->redirect('document');
  101. } else {
  102. $this->_errors = $validator->errors('document');
  103. }
  104. }
  105. }
  106. $courses = Model_Course::courses()->as_array('id', 'name');
  107. $course_id = Session::instance()->get('course_id');
  108. //remove the current course from the list
  109. unset($courses[$course_id]);
  110. $form = new Stickyform('document/upload', array('enctype'=>"multipart/form-data"), ($submitted ? $this->_errors : array()));
  111. $form->default_data = array(
  112. 'title' => '',
  113. 'user_id' => Auth::instance()->get_user()->id,
  114. 'course_id' => 0,
  115. 'role' => 0
  116. );
  117. $form->posted_data = $submitted ? $this->request->post() : array();
  118. $form->append('Title', 'title', 'text');
  119. $form->append('Access To', 'role', 'text');
  120. $form->append('User', 'user_id', 'hidden');
  121. $form->append('File', 'name', 'file');
  122. $form->append('Also add to', 'course_id', 'select', array('options' => $courses, 'attributes' => array('multiple' => 'multiple', 'name' => 'course_id[]')));
  123. $form->append('Upload', 'save', 'submit', array('attributes' => array('class' => 'button')));
  124. $form->process();
  125. $course = ORM::factory('course', $course_id);
  126. $roles = ORM::factory('role')->find_all()->as_array('id', 'name');
  127. $view = View::factory('document/form')
  128. ->bind('form', $form)
  129. ->bind('course', $course)
  130. ->bind('roles', $roles);
  131. Breadcrumbs::add(array(
  132. 'Courses', Url::site('course')
  133. ));
  134. Breadcrumbs::add(array(
  135. 'Documents', Url::site('document')
  136. ));
  137. Breadcrumbs::add(array(
  138. 'Upload', Url::site('document/upload')
  139. ));
  140. $this->content = $view;
  141. }
  142. public function action_delete() {
  143. $id = $this->request->param('id');
  144. if(Acl::instance()->is_allowed('document_delete')){
  145. ORM::factory('document', $id)->delete();
  146. $json = array(
  147. 'success' => 1,
  148. 'msg' => array('Document is deleted successfully')
  149. );
  150. } else {
  151. $json = array(
  152. 'success' => 0,
  153. 'reason' => 'access_denied'
  154. );
  155. }
  156. echo json_encode($json);
  157. exit;
  158. }
  159. public function action_edit() {
  160. if($this->request->method() === 'POST' && $this->request->post()){
  161. $document = ORM::factory('document', $this->request->post('document_id'));
  162. $validator = $document->validator($this->request->post(), FALSE);
  163. if ($validator->check()) {
  164. $document->title = $this->request->post('title');
  165. $document->remove('roles');
  166. $document->add('roles', $this->request->post('role'));
  167. $document->save();
  168. $json = array(
  169. 'success' => 1,
  170. 'msg' => array('Document is edited successfully')
  171. );
  172. } else {
  173. $json = array(
  174. 'success' => 0,
  175. 'errors' => array_values($validator->errors('document'))
  176. );
  177. }
  178. echo json_encode($json);
  179. exit;
  180. }
  181. $id = $this->request->param('id');
  182. $document = ORM::factory('document', $id);
  183. $title = $document->title;
  184. $roles_access = $document->roles->find_all()->as_array(Null, 'id');
  185. $roles = ORM::factory('role')->find_all()->as_array('id', 'name');
  186. $view = View::factory('document/edit')
  187. ->bind('title', $title)
  188. ->bind('roles', $roles)
  189. ->bind('id', $id)
  190. ->bind('roles_access', $roles_access);
  191. echo json_encode(array(
  192. 'success' => 1,
  193. 'html' => $view->render(),
  194. ));
  195. exit;
  196. }
  197. }