Skip to content

Commit cf1521b

Browse files
authored
Merge pull request #10 from TAMULib/issue-7
Issue 7
2 parents cb4b404 + 11e2d68 commit cf1521b

File tree

20 files changed

+763
-42
lines changed

20 files changed

+763
-42
lines changed
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
<?php
2+
namespace App\Classes\Controllers;
3+
use App\Classes\Data as AppClasses;
4+
use Core\Classes as Core;
5+
6+
class FilesController extends Core\AbstractController {
7+
private $fileManager;
8+
9+
public function configure() {
10+
$this->fileManager = $this->getSite()->getHelper("FileManager");
11+
}
12+
13+
protected function upload() {
14+
$data = $this->getSite()->getSanitizedInputData();
15+
if ($data['newFile']) {
16+
try {
17+
$fileName = $this->fileManager->processBase64File($data['newFile'],$data['fileGloss']);
18+
19+
$fileType = '';
20+
$temp = explode('.',$data['gloss']);
21+
if (count($temp) > 1) {
22+
$fileType = array_pop($temp);
23+
}
24+
} catch (\RuntimeException $e) {
25+
$this->getLogger()->error($e->getMessage());
26+
http_response_code(500);
27+
}
28+
}
29+
}
30+
31+
protected function download() {
32+
$data = $this->getSite()->getSanitizedInputData();
33+
if ($data['fileName']) {
34+
$this->fileManager->getDownloadableFileByFileName($data['fileName']);
35+
}
36+
}
37+
38+
protected function remove() {
39+
$data = $this->getSite()->getSanitizedInputData();
40+
if ($data['fileName']) {
41+
try {
42+
$this->fileManager->removeFileByFileName($data['fileName']);
43+
} catch (\RuntimeException $e) {
44+
$this->getLogger()->error($e->getMessage());
45+
$this->getSite()->addSystemError('There was an error reamoving this file: '.$data['fileName']);
46+
}
47+
}
48+
}
49+
50+
protected function loadDefault() {
51+
$this->getPage()->setSubTitle('Files');
52+
try {
53+
$files = $this->fileManager->getDirectoryFiles();
54+
$this->getSite()->getViewRenderer()->registerViewVariable("scanned_directory",$this->fileManager->getBaseFilePath());
55+
$this->getSite()->getViewRenderer()->registerViewVariable("files",$files);
56+
} catch (\RuntimeException $e) {
57+
$this->getLogger()->warn($e->getMessage());
58+
$this->getSite()->addSystemError('There was an error reading the contents of the upload directory');
59+
}
60+
$this->setViewName('files');
61+
}
62+
}

App/Classes/Controllers/WidgetsController.php

Lines changed: 73 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,85 @@ protected function partsRemove() {
8484
}
8585
}
8686

87+
protected function attachments() {
88+
$data = $this->getSite()->getSanitizedInputData();
89+
$this->getPage()->setSubTitle('Attachments');
90+
91+
if ($data['widgetid']) {
92+
$widget = $this->widgetsRepo->getById($data['widgetid']);
93+
if (!$data['pageContext']) {
94+
$data['pageContext'] = null;
95+
}
96+
$this->getSite()->getViewRenderer()->registerViewVariable('pageContext', $data['pageContext']);
97+
$this->getSite()->getViewRenderer()->registerViewVariable('attachments',$this->getSite()->getDataRepository("Files")->getByRelatedIdAndType($widget['id'],1));
98+
$this->getSite()->getViewRenderer()->registerViewVariable('widget', $widget);
99+
$this->setViewName('widgets.attachments');
100+
}
101+
}
102+
103+
protected function attachmentsDownload() {
104+
$data = $this->getSite()->getSanitizedInputData();
105+
if ($data['attachmentid'] && ($file = $this->getSite()->getDataRepository("Files")->getById($data['attachmentid']))) {
106+
try {
107+
$this->getSite()->getHelper("FileManager")->getDownloadableFile($file);
108+
} catch (\RuntimeException $e) {
109+
$this->getLogger()->warn($e->getMessage());
110+
$this->getSite()->addSystemError('Could not find the file requested for download');
111+
$this->loadDefault();
112+
}
113+
}
114+
}
115+
116+
protected function attachmentsAdd() {
117+
$data = $this->getSite()->getSanitizedInputData();
118+
if ($data['newFile'] && $data['widgetid']) {
119+
$widget = $this->widgetsRepo->getById($data['widgetid']);
120+
$fileManager = $this->getSite()->getHelper("FileManager");
121+
$filesRepo = $this->getSite()->getDataRepository("Files");
122+
123+
try {
124+
$filePath = 'attachments';
125+
$fileName = $fileManager->processBase64File($data['newFile'],null,$filePath);
126+
127+
$fileType = '';
128+
$temp = explode('.',$data['fileGloss']);
129+
if (count($temp) > 1) {
130+
$fileType = array_pop($temp);
131+
}
132+
133+
//add file to files repo
134+
$filesRepo->add(array("name"=>$fileName,"path"=>$filePath,"file_type"=>$fileType,"gloss"=>$data['fileGloss'],"userid"=>$this->getSite()->getGlobalUser()->getProfileValue("id"),"typeid"=>1,"relatedid"=>$data['widgetid']));
135+
} catch (\RuntimeException $e) {
136+
$this->getLogger()->error($e->getMessage());
137+
http_response_code(500);
138+
}
139+
}
140+
}
141+
142+
protected function attachmentsRemove() {
143+
$data = $this->getSite()->getSanitizedInputData();
144+
if ($data['attachmentid']) {
145+
$fileManager = $this->getSite()->getHelper("FileManager");
146+
$filesRepo = $this->getSite()->getDataRepository("Files");
147+
if ($removableFile = $filesRepo->getById($data['attachmentid'])) {
148+
try {
149+
$fileManager->removeFile($removableFile);
150+
$filesRepo->removeById($data['attachmentid']);
151+
} catch (\RuntimeException $e) {
152+
$this->getLogger()->warn($e->getMessage());
153+
$this->getSite()->addSystemError('Error removing the attachment');
154+
}
155+
}
156+
}
157+
}
158+
87159
protected function search() {
88160
$data = $this->getSite()->getSanitizedInputData();
89161
if (isset($data['term'])) {
90162
$this->getSite()->getViewRenderer()->registerViewVariable("widgets",$this->widgetsRepo->search($data['term']));
91163
$this->setViewName("widgets.list");
92164
} else {
93-
$site->addSystemError('There was an error with the search');
165+
$this->getSite()->addSystemError('There was an error with the search');
94166
}
95167
}
96168

App/Classes/Data/DatabaseFile.php

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
<?php
2+
namespace App\Classes\Data;
3+
use Core\Classes\Data as CoreData;
4+
5+
/**
6+
* Represents a file entry from the database
7+
*
8+
* @author Jason Savell <jsavell@library.tamu.edu>
9+
*/
10+
class DatabaseFile extends CoreData\SimpleFile {
11+
private $id;
12+
private $uploadDate;
13+
private $uploaderData;
14+
private $typeId;
15+
private $relatedId;
16+
17+
public function __construct($fileName,$filePath,$fileType=null,$gloss=null,$id,$uploadDate,$uploaderData,$typeId,$relatedId) {
18+
parent::__construct($fileName,$filePath,$fileType,$gloss);
19+
$this->setId($id);
20+
$this->setUploadDate($uploadDate);
21+
$this->setUploaderData($uploaderData);
22+
$this->setTypeId($typeId);
23+
$this->setRelatedId($relatedId);
24+
}
25+
26+
protected function setId($id) {
27+
$this->id = $id;
28+
}
29+
30+
protected function setUploadDate($uploadDate) {
31+
$this->uploadDate = $uploadDate;
32+
}
33+
34+
protected function setUploaderData($uploaderData) {
35+
$this->uploaderData = $uploaderData;
36+
}
37+
38+
protected function setTypeId($typeId) {
39+
$this->typeId = $typeId;
40+
}
41+
42+
protected function setRelatedId($relatedId) {
43+
$this->relatedId = $relatedId;
44+
}
45+
46+
public function getId() {
47+
return $this->id;
48+
}
49+
50+
public function getUploadDate() {
51+
return $this->uploadDate;
52+
}
53+
54+
public function getUploaderData() {
55+
return $this->uploaderData;
56+
}
57+
58+
public function getTypeId() {
59+
return $this->typeId;
60+
}
61+
62+
public function getRelatedId() {
63+
return $this->relatedId;
64+
}
65+
66+
public function getFullPath() {
67+
$fullPath = $this->getFilePath();
68+
if ($fullPath) {
69+
$fullPath .= '/';
70+
}
71+
return $fullPath.$this->getFileName();
72+
}
73+
}

App/Classes/Data/Files.php

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
<?php
2+
namespace App\Classes\Data;
3+
use Core\Classes\Data as CoreData;
4+
use App\Classes\Helpers as Helpers;
5+
6+
/**
7+
* Repo for managing Files
8+
*
9+
* @author Jason Savell <jsavell@library.tamu.edu>
10+
*/
11+
12+
class Files extends CoreData\AbstractDataBaseRepository {
13+
private $fileManager;
14+
15+
public function __construct() {
16+
parent::__construct('files','id','uploaded');
17+
}
18+
19+
private function getBaseSql() {
20+
return "SELECT f.*,ft.name AS type_name,u.username,u.name_first,u.name_last FROM {$this->primaryTable} f
21+
LEFT JOIN files_types ft ON f.id=f.typeid
22+
LEFT JOIN users u ON f.userid=u.id";
23+
}
24+
25+
private function getOrderedQueryResults($sql,$bindparams=null) {
26+
if ($this->defaultOrderBy) {
27+
$sql .= " ORDER BY {$this->defaultOrderBy}";
28+
}
29+
30+
$fileRows = $this->queryWithIndex($sql,$this->primaryKey,null,$bindparams);
31+
if ($fileRows) {
32+
$fileRows = array_map(array($this,'processUserData'),$fileRows);
33+
return $this->getFileInstances($fileRows);
34+
}
35+
return $fileRows;
36+
}
37+
38+
private function processUserData($fileData) {
39+
$userKeys = array('userid','username','name_last','name_first');
40+
$userData = array();
41+
foreach ($userKeys as $key) {
42+
$userData[$key] = $fileData[$key];
43+
unset($fileData[$key]);
44+
}
45+
$fileData['userData'] = $userData;
46+
return $fileData;
47+
}
48+
49+
public function get() {
50+
return $this->getOrderedQueryResults($this->getBaseSql());
51+
}
52+
53+
public function getById($id) {
54+
$sql = $this->getBaseSql()." WHERE f.{$this->primaryKey}=:id";
55+
$temp = $this->executeQuery($sql,array(":id"=>$id));
56+
if (!$temp[0]) {
57+
return false;
58+
}
59+
return $this->getFileInstance($this->processUserData($temp[0]));
60+
}
61+
62+
public function getByRelatedIdAndType($relatedId,$typeId) {
63+
return $this->getOrderedQueryResults($this->getBaseSql()." WHERE f.typeid=:typeid AND f.relatedid=:relatedid",array(":relatedid"=>$relatedId,":typeid"=>$typeId));
64+
65+
}
66+
67+
protected function getFileManager() {
68+
if (!$this->fileManager) {
69+
$this->fileManager = $this->getSite()->getHelper("FileManager");
70+
}
71+
return $this->fileManager;
72+
}
73+
74+
public function getUploadPath() {
75+
return $this->getFileManager()->getBaseFilePath();
76+
}
77+
78+
private function getFileInstance($data) {
79+
return new DatabaseFile($data['name'],$data['path'],$data['file_type'],$data['gloss'],$data['id'],$data['uploaded'],$data['userData'],$data['typeid'],$data['type_name'],$data['relatedid']);
80+
}
81+
82+
private function getFileInstances($fileRows) {
83+
$files = array();
84+
foreach ($fileRows as $fileRow) {
85+
$files[] = $this->getFileInstance($fileRow);
86+
}
87+
return $files;
88+
}
89+
}

App/Config/config.pages.php

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
$sitePages = array(
2424
"widgets" => new CoreClasses\CoreSitePage("widgets","widgets",SECURITY_USER),
2525
"DynamicRepo" => new CoreClasses\CoreSitePage("dynamic repo","dynamic-repo",SECURITY_USER),
26+
"files" => new CoreClasses\CoreSitePage("File Manager","files",SECURITY_USER),
2627
"users" => new CoreClasses\CoreSitePage("users","users",SECURITY_ADMIN));
2728

2829
/* If you'd like to use the app level SitePage, use the following $sitePages array, instead, and uncomment the AppClasses namespace alias at the top of this file.

App/Config/config_sample.php

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,10 @@
109109
define('SECURITY_USER',0);
110110
define('SECURITY_ADMIN',1);
111111

112+
//Sets where user uploaded files will be placed
113+
//Core\Classes\Helpers\FileManager is dependent on this configuration
114+
define('UPLOAD_PATH',PATH_ROOT.'uploads/');
115+
112116
?>
113117

114118

App/Lib/functions.php

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,50 @@
66
*/
77

88
require_once PATH_CORE_LIB."functions.php";
9+
10+
function buildUploadForm($baseUrl,$modalContext=null,$action='upload',$subaction=null,$hiddenFields=null) {
11+
$html = '<form class="do-upload form-inline" name="upload" method="POST" action="'.$baseUrl.'">';
12+
if ($modalContext) {
13+
$html .= '<input type="hidden" name="modal_context" value="'.$modalContext.'" />';
14+
}
15+
$html .= ' <input type="hidden" name="action" value="'.$action.'" />';
16+
if ($subaction) {
17+
$html .= '<input type="hidden" name="subaction" value="'.$subaction.'" />';
18+
}
19+
if ($hiddenFields) {
20+
foreach ($hiddenFields as $field) {
21+
$html .= '<input type="hidden" name="'.$field['name'].'" value="'.$field['value'].'" />';
22+
}
23+
}
24+
25+
$html .= ' <input class="do-file-gloss" type="hidden" name="fileGloss" value="" />
26+
<div class="do-file-preview"></div>
27+
<input class="inline-block" type="file" name="file_input" />
28+
<input class="btn btn-default" type="submit" name="submitupload" value="Upload File" />
29+
</form>';
30+
return $html;
31+
}
32+
33+
function buildHTMLUploadForm($baseUrl,$modalContext=null,$action='upload',$subaction=null,$hiddenFields=null) {
34+
$html = '<form class="do-upload vertical-spacer-bottom " name="upload" method="POST" action="'.$baseUrl.'">';
35+
if ($modalContext) {
36+
$html .= '<input type="hidden" name="modal_context" value="'.$modalContext.'" />';
37+
}
38+
$html .= ' <input type="hidden" name="action" value="'.$action.'" />';
39+
if ($subaction) {
40+
$html .= '<input type="hidden" name="subaction" value="'.$subaction.'" />';
41+
}
42+
if ($hiddenFields) {
43+
foreach ($hiddenFields as $field) {
44+
$html .= '<input type="hidden" name="'.$field['name'].'" value="'.$field['value'].'" />';
45+
}
46+
}
47+
48+
$html .= ' <input class="do-file-gloss" type="hidden" name="fileGloss" value="" />
49+
<div class="do-file-preview"></div>
50+
<input class="inline-block" type="file" name="file_input" />
51+
<input class="inline-block small" type="submit" name="submitupload" value="Upload File" />
52+
</form>';
53+
return $html;
54+
}
955
?>

0 commit comments

Comments
 (0)