Thứ Năm, 24 tháng 4, 2014

Managing file sharing [Data Management and Document Sharing]

Now that we have a fully functional document management section, our next task is to extend this document management system to support file sharing with other users. The most importanpart of implementing a file sharingmechanism is to store the information about upload sharing; we do this by linking documents with user IDs in a table called upload_sharing.
For implementing file sharingwe will need to create a netable called upload_sharing and store all sharing-related information in thatable. The following steps will explain how this is implemented in our application:

1.       Create a netable called upload_sharing; this table will hold the relationship about uploads shared with users:
CREATE TABLE IF NOT EXISTS uploads_sharing (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , upload_id INT NOT NULL ,
user_id INT NOT NULL,
UNIQUE KEY (upload_id, user_id)
);

2.       In the module definition Module.php, add a simple TableGateway object for the
uploads_sharing table:
'UploadSharingTableGateway' => function ($sm) {
$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
return new TableGateway('uploads_sharing', $dbAdapter);
},

3.       Modify the constructor of the UploadTable class ttake in an additional parameter of the upload sharing TableGateway object:
public function   construct(TableGateway $tableGateway, TableGateway $uploadSharingTableGateway)
{
$this->tableGateway = $tableGateway;
$this->uploadSharingTableGateway = $uploadSharingTableGateway;
}

4.       Modify the module configuration (Module.phpfor the UploadTable factorto support UploadSharingTableGateway:
'UploadTable' =>   function($sm) {
$tableGateway = $sm->get('UploadTableGateway');


$uploadSharingTableGateway = $sm->get('UploadSharingTableGatew ay');
$table = new UploadTable($tableGateway,
$uploadSharingTableGateway); return $table;
},

5.       Modify the UploadTable class to support the following file sharing functions:
        addSharing(): Adds a new sharing permission for the given upload with the user
        removeSharing()Removes the sharing permission for the specific upload/user combination
        getSharedUsers(): Gets the list of userfor which the upload is shared
        getSharedUploadsForUserId(): Gets the list of uploads that are shared for that user

This can be done using the following code:
public function addSharing($uploadId, $userId)
{
$data = array(
'upload_id' => (int)$uploadId, 'user_id'       => (int)$userId,
);
$this->uploadSharingTableGateway->insert($data);
}

public function removeSharing($uploadId, $userId)
{
$data = array(
'upload_id' => (int)$uploadId, 'user_id'       => (int)$userId,
);
$this->uploadSharingTableGateway->delete($data);
}

public function getSharedUsers($uploadId)
{
$uploadId  = (int) $uploadId;
$rowset = $this->uploadSharingTableGateway->select( array('upload_id' => $uploadId));
return $rowset;


}

public function getSharedUploadsForUserId($userId)
{
$userId  = (int) $userId;

$rowset = $this->uploadSharingTableGateway->select( function (Select $select) use ($userId){
$select->columns(array())
->where(array('uploads_sharing.user_id'=>$userId))
->join('uploads', 'uploads_sharing.upload_id = uploads.id');
});

return $rowset;
}

The Manage Documents section lists all uploads for a specific user and also lists uploads shared by others with the user:


6.       Modify the edit upload form to display the list of users the upload is shared with;
this can be achieved by passing the upload ID to the getSharedUsers()method of
the UploadTable object.


7.       Add a new section in the edit upload form which allows the addition of new shares; this is achieved by displaying the list of all users in the system in a drop-down list. When the user clicks on Add Share, a newrecord is added
to the upload_sharing table:
$userTable = $this->getServiceLocator()
->get('UserTable');
$uploadTable = $this->getServiceLocator()
->get('UploadTable');
$form = $this->getServiceLocator()->get('UploadForm');
$request = $this->getRequest(); if ($request->isPost()) {
$userId = $request->getPost()->get('user_id');
$uploadId = $request->getPost()->get('upload_id');
$uploadTable->addSharing($uploadId, $userId);
}

The following screenshot shows the Upload Sharing page with a drop-down listo add shares:


8.       The last section of the file sharing implementation is to allow an option for userto download shared files. This is provided by the fileDownloadAction() function defined in our file sharing application:
public function fileDownloadAction()
{
$uploadId = $this->params()->fromRoute('id');
$uploadTable = $this->getServiceLocator()
->get('UploadTable');


$upload = $uploadTable->getUpload($uploadId);

// Fetch Configuration from Module Config
$uploadPath    = $this->getFileUploadLocation();
$file = file_get_contents($uploadPath ."/" . $upload->filename);

// Directly return the Response
$response = $this->getEvent()->getResponse();
$response->getHeaders()->addHeaders(array( 'Content-Type' => 'application/octet-stream', 'Content-Disposition' => 'attachment;filename="'
.$upload->filename . '"',

));
$response->setContent($file);

return $response;
}
9.       Now we have a fully functional file sharing system in place. Test the file sharing system; start by sharing the file with different users, and log in and out as different users.
The final form should look like the following screenshot:


What just happened?
You created a table thacan store user and upload relationships; you modified the UploadTable class to support additional sharing functions. You created controllers and viewto enable file sharing, and finally you provided the ability for the user to download the shared file using a file download script. With this, you have successfully implemented the file sharing system, where usercan now upload, edit, and share documents within the system.
Q1. In TableGateway, which function is used to determine the last inserted record ID?

1.       getLastId()
2.       getLastInsertId()
3.       get('last_insert_id')
4.       getLastInsertValue()

Q2. Which method can be used to disable layouts in a view model?

1.       $viewModel->setNoLayouts(true)
2.       $ viewModel->Layouts(false)
3.       $viewModel->setTerminal(true)
4.       $viewModel->setLayouts(false)


Summary


In this chapterwe have discussed several topics in the context of data and file management. First, we elaborated on the usage of the TableGateway database pattern. We then implemented a simple file upload service by making use of Zend Framework's file transfer components. Finallywe implemented a simple file sharing service by utilizing both Zend Framework's file transfer components and the TableGateway pattern. In the next chapter,we will be working closely on the frontend, especially with JavaScript and AJAX calls.

Không có nhận xét nào:

Đăng nhận xét