Skip to content

Commit

Permalink
Implementing project file upload functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
masood09 committed Jan 7, 2013
1 parent 0628e85 commit 54fa666
Show file tree
Hide file tree
Showing 21 changed files with 3,487 additions and 0 deletions.
2 changes: 2 additions & 0 deletions app/controllers/ControllerBase.php
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ protected function _getAcl()
'attendance' => array('savepost', 'index'),
'holiday' => array('savepost'),
'report' => array('index'),
'files' => array('get', 'post'),
);

// Private developer resources
Expand All @@ -49,6 +50,7 @@ protected function _getAcl()
'user' => array('logout', 'myaccount'),
'attendance' => array('savepost', 'index'),
'report' => array('index'),
'files' => array('get', 'post'),
);

foreach ($adminResources as $resource => $actions){
Expand Down
169 changes: 169 additions & 0 deletions app/controllers/FilesController.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
<?php

class FilesController extends ControllerBase
{
public function getAction($projectId=null)
{
if (is_null($projectId)) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

$project = Project::findFirst('id="' . $projectId . '"');

if (!$project) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

if (!$project->isInProject($this->currentUser)) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

$return = array();

$uploads = Upload::find('project_id = "' . $projectId . '"');

if (count($uploads) > 0) {
foreach($uploads AS $upload) {
$fileName = $upload->filename;
$fileUrl = 'uploads/' . $projectId . '/' . $fileName;

$temp = array();

$temp['name'] = $upload->filename;
$temp['size'] = (int)$upload->size;
$temp['type'] = $upload->type;
$temp['url'] = $this->url->get($fileUrl);

if (in_array($upload->type, array('image/jpeg', 'image/png', 'image/gif'))) {
$temp['thumbnail_url'] = $this->url->get($fileUrl);
}

$temp['uploaded_by'] = $upload->getUser()->full_name;
$temp['uploaded_at'] = $upload->uploaded_at;

if ($upload->user_id == $this->currentUser->id || $this->currentUser->id == 1) {
$temp['delete_url'] = $this->url->get('files/delete/');
$temp['delete_type'] = 'POST';
}

$return['files'][] = $temp;
}
}

echo json_encode($return);
$this->view->disable();
return;
}

protected function _upcountNameCallback($matches)
{
$index = isset($matches[1]) ? intval($matches[1]) + 1 : 1;
$ext = isset($matches[2]) ? $matches[2] : '';
return ' ('.$index.')'.$ext;
}

protected function _upcountName($fileName)
{
return preg_replace_callback(
'/(?:(?: \(([\d]+)\))?(\.[^.]+))?$/',
array($this, '_upcountNameCallback'),
$fileName,
1
);
}

protected function _getUniqueFileName($fileName, $dir)
{
while (is_file($dir . $fileName)) {
$fileName = $this->_upcountName($fileName);
}

return $fileName;
}

public function postAction($projectId=null)
{
if (!$this->request->isPost()) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

if (is_null($projectId)) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

$project = Project::findFirst('id="' . $projectId . '"');

if (!$project) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

if (!$project->isInProject($this->currentUser)) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

if ($this->request->hasFiles() == true) {
foreach ($this->request->getUploadedFiles() as $file) {
$projectDir = $this->UploadDir . $projectId . '/';

if (!is_dir($projectDir)) {
mkdir($projectDir);
}

$fileName = $this->_getUniqueFileName(current($file->getName()), $projectDir);
$filePath = $projectDir . $fileName;
$fileUrl = 'uploads/' . $projectId . '/' . $fileName;
$size = current($file->getSize());

move_uploaded_file(current($file->getTempName()), $filePath);

$finfo = new finfo;
$type = $finfo->file($filePath, FILEINFO_MIME_TYPE);

$upload = new Upload();
$upload->filename = $fileName;
$upload->filepath = $filePath;
$upload->type = $type;
$upload->size = $size;
$upload->user_id = $this->currentUser->id;
$upload->project_id = $projectId;
$upload->uploaded_at = new Phalcon\Db\RawValue('now()');

if ($upload->save() == true) {
$temp = array();
$temp['name'] = $upload->filename;
$temp['size'] = (int)$upload->size;
$temp['type'] = $upload->type;
$temp['url'] = $this->url->get($fileUrl);

if (in_array($upload->type, array('image/jpeg', 'image/png', 'image/gif'))) {
$temp['thumbnail_url'] = $this->url->get($fileUrl);
}

$temp['delete_url'] = $this->url->get('files/delete/');
$temp['delete_type'] = 'POST';

$return['files'][] = $temp;

echo json_encode($return);
}
}

$this->view->disable();
return;
}
}
}
29 changes: 29 additions & 0 deletions app/controllers/ProjectController.php
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,33 @@ public function notespostAction()
$this->view->disable();
return;
}

public function filesAction($id=null)
{
if (is_null($id)) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

$project = Project::findFirst('id="' . $id . '"');

if (!$project) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

if (!$project->isInProject($this->currentUser)) {
$this->response->redirect('project/index');
$this->view->disable();
return;
}

$this->view->setVar('project', $project);
$this->view->setVar('developers', User::getAllDevelopers(true));
$this->view->setVar('extra_params', '/' . $id . '/');

Phalcon\Tag::setTitle($project->name . ' | Files');
}
}
19 changes: 19 additions & 0 deletions app/models/Upload.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<?php

class Upload extends Phalcon\Mvc\Model
{
public function validation()
{
if ($this->validationHasFailed() == true) {
return false;
}
}

public function initialize()
{
$this->belongsTo('user_id', 'User', 'id');
$this->belongsTo('project_id', 'Project', 'id');
$this->belongsTo('task_id', 'Task', 'id');
$this->belongsTo('comment_id', 'Comment', 'id');
}
}
11 changes: 11 additions & 0 deletions app/views/index.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,24 @@
<?php echo Tag::stylesheetLink('css/bootstrap-responsive.css'); ?>
<?php echo Tag::stylesheetLink('css/jdpicker.css'); ?>
<?php echo Tag::stylesheetLink('css/style.css'); ?>
<?php echo Tag::stylesheetLink('css/bootstrap-image-gallery.css'); ?>
<?php echo Tag::stylesheetLink('css/jquery.fileupload-ui.css'); ?>

<!--[if lt IE 9]>
<script src="js/html5.js"></script>
<![endif]-->

<?php echo Tag::javascriptInclude('js/jquery-1.8.3.min.js'); ?>
<?php echo Tag::javascriptInclude('js/jquery.ui.widget.js'); ?>
<?php echo Tag::javascriptInclude('js/tmpl.min.js'); ?>
<?php echo Tag::javascriptInclude('js/load-image.min.js'); ?>
<?php echo Tag::javascriptInclude('js/canvas-to-blob.min.js'); ?>
<?php echo Tag::javascriptInclude('js/bootstrap.min.js'); ?>
<?php echo Tag::javascriptInclude('js/bootstrap-image-gallery.min.js'); ?>
<?php echo Tag::javascriptInclude('js/jquery.iframe-transport.js'); ?>
<?php echo Tag::javascriptInclude('js/jquery.fileupload.js'); ?>
<?php echo Tag::javascriptInclude('js/jquery.fileupload-fp.js'); ?>
<?php echo Tag::javascriptInclude('js/jquery.fileupload-ui.js'); ?>
<?php echo Tag::javascriptInclude('js/jquery.jdpicker.js'); ?>
</head>

Expand Down
3 changes: 3 additions & 0 deletions app/views/partials/projectTop.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,9 @@
<li <?php echo ($this->view->getActionName() == 'notes') ? 'class="active"' : ''; ?>>
<?php echo Tag::linkTo(array('project/notes/' . $project->id . '/', 'text' => 'Notes')); ?>
</li>
<li <?php echo ($this->view->getActionName() == 'files') ? 'class="active"' : ''; ?>>
<?php echo Tag::linkTo(array('project/files/' . $project->id . '/', 'text' => 'Files')); ?>
</li>
</ul>

<?php if ($currentUser->isAdmin()) { ?>
Expand Down
Loading

0 comments on commit 54fa666

Please sign in to comment.