Skip to content

Commit

Permalink
Implementing fileupload functionality for task comments.
Browse files Browse the repository at this point in the history
  • Loading branch information
masood09 committed Jan 7, 2013
1 parent d77c153 commit 8074cfa
Show file tree
Hide file tree
Showing 5 changed files with 210 additions and 4 deletions.
18 changes: 17 additions & 1 deletion app/controllers/FilesController.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ protected function _getUniqueFileName($fileName, $dir)
return $fileName;
}

public function postAction($projectId=null)
public function postAction($projectId=null, $taskId = null)
{
if (!$this->request->isPost()) {
$this->response->redirect('project/index');
Expand Down Expand Up @@ -118,6 +118,16 @@ public function postAction($projectId=null)
return;
}

if (!is_null($taskId)) {
$task = Task::findFirst('id="' . $taskId . '"');

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

if ($this->request->hasFiles() == true) {
foreach ($this->request->getUploadedFiles() as $file) {
$projectDir = $this->UploadDir . $projectId . '/';
Expand All @@ -143,7 +153,13 @@ public function postAction($projectId=null)
$upload->size = $size;
$upload->user_id = $this->currentUser->id;
$upload->project_id = $projectId;

if ($task) {
$upload->task_id = $task->id;
}

$upload->uploaded_at = new Phalcon\Db\RawValue('now()');
$upload->uuid = $this->request->getPost('uuid');

if ($upload->save() == true) {
$temp = array();
Expand Down
10 changes: 10 additions & 0 deletions app/controllers/TaskController.php
Original file line number Diff line number Diff line change
Expand Up @@ -196,6 +196,7 @@ public function viewAction($id=null)
$this->view->setVar('task_user_time', $task_user_time);
$this->view->setVar('task_total_time', $task_total_time);
$this->view->setVar('extra_params', '/' . $id . '/');
$this->view->setVar('uuid', hash("sha256", date('Y-m-d H:m:i') . $id . $this->currentUser->id));
Phalcon\Tag::setTitle(($task->job_id) ? $task->job_id . ' - ' . $task->title : $task->title);
}

Expand Down Expand Up @@ -319,6 +320,7 @@ public function addcommentAction()
$user_id = $this->currentUser->id;
$message = $this->request->getPost('comment');
$created_at = new Phalcon\Db\RawValue('now()');
$uuid = $this->request->getPost('uuid');

$task = Task::findFirst('id="' . $task_id . '"');

Expand Down Expand Up @@ -355,6 +357,7 @@ public function addcommentAction()
$comment->task_id = $task_id;
$comment->comment = $message;
$comment->created_at = $created_at;
$comment->uuid = $uuid;

if ($comment->save() == true) {
$this->Email->sendCommentEmail($comment);
Expand All @@ -375,6 +378,13 @@ public function addcommentAction()
$taskUser->save();
}

$uploads = Upload::find('user_id = "' . $user_id . '" AND task_id = "' . $task_id . '" AND uuid = "' . $uuid . '"');

foreach($uploads AS $upload) {
$upload->comment_id = $comment->id;
$upload->save();
}

$this->response->redirect('task/view/' . $task_id);
$this->view->disable();
return;
Expand Down
11 changes: 11 additions & 0 deletions app/models/Comment.php
Original file line number Diff line number Diff line change
Expand Up @@ -14,4 +14,15 @@ public function initialize()
$this->belongsTo('user_id', 'User', 'id');
$this->belongsTo('task_id', 'Task', 'id');
}

public function getUploads()
{
$uploads = Upload::find('task_id = "' . $this->task_id . '" AND comment_id = "' . $this->id . '"');

if (count($uploads) > 0) {
return $uploads;
}

return array();
}
}
173 changes: 170 additions & 3 deletions app/views/task/view.phtml
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,42 @@
<div class="comment">
<?php echo Markdown($comment->comment); ?>
</div>

<?php $uploads = $comment->getUploads(); ?>

<?php if (count($uploads) > 0) { ?>
<table role="presentation" class="table fileUploads table-bordered">
<tbody data-toggle="modal-gallery" data-target="#modal-gallery">
<?php foreach($uploads AS $upload) { ?>
<?php $fileUrl = $this->url->get('uploads/' . $project->id . '/' . $upload->filename); ?>
<tr class="fileUpload">
<td class="preview">
<?php if (in_array($upload->type, array('image/jpeg', 'image/png', 'image/gif'))) { ?>
<a href="<?php echo $fileUrl; ?>" title="<?php echo $upload->filename; ?>" data-gallery="gallery" download="<?php echo $upload->filename; ?>">
<img src="<?php echo $fileUrl; ?>" width="128" alt="<?php echo $upload->filename; ?>">
</a>
<?php } else { ?>
<div class="<?php echo $upload->type; ?>"></div>
<?php } ?>
</td>
<td class="content">
<p>
<a href="<?php echo $fileUrl; ?>" title="<?php echo $upload->filename; ?>" data-gallery="gallery" download="<?php echo $fileUrl; ?>">
<?php echo $upload->filename; ?>
</a>
</p>
<p>
<?php echo $upload->size; ?>
</p>
<p>
Uploaded by <?php echo $upload->getUser()->full_name; ?> at <?php echo $upload->uploaded_at; ?>
</p>
</td>
</tr>
<?php } ?>
</tbody>
</table>
<?php } ?>
</td>
</tr>
<?php } ?>
Expand All @@ -132,11 +168,13 @@

<div class="content_block well">
<h5>Add new comment</h5>
<?php echo Tag::form(array("task/addcomment", 'class' => 'form-horizontal')); ?>
<?php echo Tag::form(array("task/addcomment", 'class' => 'form-horizontal', 'id' => 'fileupload', 'enctype' => 'multipart/form-data')); ?>
<textarea class="input-block-level" rows="6" name="comment"></textarea>
<input type="hidden" name="task_id" value="<?php echo $task->id; ?>">
<input type="hidden" name="uuid" value="<?php echo $uuid; ?>">

<br><br>

<br /><br />
<?php if ($currentUser->id == $task->assigned_to) { ?>
<label class="checkbox">
<input type="checkbox" name="task_complete" <?php echo ($task->status == 1) ? 'checked="checked"' : ''; ?>>
Expand All @@ -145,7 +183,19 @@
<br />
<?php } ?>

<input type="submit" class="btn btn-primary" value="Add comment">
<div class="row fileupload-buttonbar">
<div class="span7">
<!-- The fileinput-button span is used to style the file input field as button -->
<span class="btn btn-success fileinput-button">
<i class="icon-plus icon-white"></i>
<span>Add files...</span>
<input type="file" name="files[]" multiple>
</span>
</div>
</div>
<table role="presentation" class="table fileUploads"><tbody class="files" data-toggle="modal-gallery" data-target="#modal-gallery"></tbody></table>

<input type="submit" class="btn btn-primary" value="Add comment">
</form>
</div>
</div>
Expand Down Expand Up @@ -194,3 +244,120 @@
</div>
</div>
</div>

<script id="template-upload" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
<tr class="template-upload fade fileUpload">
<td class="preview">
<span class="fade"></span>
</td>

<td class="content">
<p>
{%=file.name%}
</p>

<p>
{%=o.formatFileSize(file.size)%}
</p>

{% if (!file.error) { %}
<div class="progress progress-success progress-striped active" role="progressbar" aria-valuemin="0" aria-valuemax="100" aria-valuenow="0"><div class="bar" style="width:0%;"></div></div>

<p>
<span class="start">
<button class="btn btn-primary">
<i class="icon-upload icon-white"></i>
<span>Start</span>
</button>
</span>&nbsp;&nbsp;
{% } else { %}
<p>
{% } %}

<span class="cancel">
<button class="btn btn-warning">
<i class="icon-ban-circle icon-white"></i>
<span>Cancel</span>
</button>
</span>
</p>
</td>
</tr>
{% } %}
</script>

<script id="template-download" type="text/x-tmpl">
{% for (var i=0, file; file=o.files[i]; i++) { %}
{% if (!file.error) { %}
<tr class="template-download fade fileUpload">
<td class="preview">
{% if (file.thumbnail_url) { %}
<a href="{%=file.url%}" title="{%=file.name%}" data-gallery="gallery" download="{%=file.name%}">
<img src="{%=file.thumbnail_url%}" width="128" alt="{%=file.name%}">
</a>
{% } else { %}
<div class="{%=file.type%}"></div>
{% } %}
</td>
<td class="content">
<p>
<a href="{%=file.url%}" title="{%=file.name%}" data-gallery="{%=file.thumbnail_url&&'gallery'%}" download="{%=file.name%}">{%=file.name%}</a>
</p>
<p>
{%=o.formatFileSize(file.size)%}
</p>
<p>
Uploaded by {%=file.uploaded_by%} at {%=file.uploaded_at%}
</p>

{% if (file.delete_url) { %}
<p>
<span class="delete">
<button class="btn btn-danger" data-type="{%=file.delete_type%}" data-url="{%=file.delete_url%}">
<i class="icon-trash icon-white"></i>
<span>Delete</span>
</button>
</span>
</p>
{% } %}
</td>
</tr>
{% } %}
{% } %}
</script>

<script type="text/javascript">
$('#fileupload').fileupload({
url: "<?php echo $this->url->get('files/post/' . $project->id . '/' . $task->id . '/'); ?>",
prependFiles : true,
previewMaxWidth: 128,
autoUpload : true
});
</script>

<div id="modal-gallery" class="modal modal-gallery hide fade" data-filter=":odd" tabindex="-1">
<div class="modal-header">
<a class="close" data-dismiss="modal">&times;</a>
<h3 class="modal-title"></h3>
</div>
<div class="modal-body"><div class="modal-image"></div></div>
<div class="modal-footer">
<a class="btn modal-download" target="_blank">
<i class="icon-download"></i>
<span>Download</span>
</a>
<a class="btn btn-success modal-play modal-slideshow" data-slideshow="5000">
<i class="icon-play icon-white"></i>
<span>Slideshow</span>
</a>
<a class="btn btn-info modal-prev">
<i class="icon-arrow-left icon-white"></i>
<span>Previous</span>
</a>
<a class="btn btn-primary modal-next">
<span>Next</span>
<i class="icon-arrow-right icon-white"></i>
</a>
</div>
</div>
2 changes: 2 additions & 0 deletions install/0.1.0.sql
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,7 @@ CREATE TABLE IF NOT EXISTS `comment` (
`task_id` int(10) unsigned NOT NULL,
`comment` TEXT NOT NULL,
`created_at` datetime NOT NULL,
`uuid` VARCHAR(255) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

Expand Down Expand Up @@ -128,5 +129,6 @@ CREATE TABLE IF NOT EXISTS `upload` (
`task_id` int(10) unsigned NULL,
`comment_id` int(10) unsigned NULL,
`uploaded_at` datetime NOT NULL,
`uuid` VARCHAR(255) NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8 AUTO_INCREMENT=1 ;

0 comments on commit 8074cfa

Please sign in to comment.