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

Document management [Data Management and Document Sharing]

In this section we will create a new document management interface. The document management interface will allow userto upload documents, manage uploads, and share uploaded documents with other users. The user interface will also allow userto manage sharing, and add/remove shares.

In this section, we will focus on providing users with options to create file uploads and manage those uploads. We will be using the filesystem tstore the uploaded file and the relative path of the uploaded file will be stored in the database mapped to the user who uploaded the file.

Some of the importanZend Framework components used in file uploads are:

        File upload form element (Zend\Form\Element\File): The File upload element is used in the upload form to display a file input box. This element is an equivalent of the <input type='file'../> style element in HTML used for allowing userto upload files. The file input elemencan be rendered by setting 'type' => 'file' in the form definition.

     File transfer adapter (Zend\File\Transfer\Adapter\Http): The file transfer adapter handle file uploads upon form submission. The setDestination() method in the file transfer adapter allows the user to set a destination and receive the file in that destination. The receive() method is used to initiate the transfer.
In this task, we will be creating a new document upload form; file uploads will be stored in the filesystem, and the information regarding the file upload will be stored in the database in a table named uploads. The file uploads arstored in a folder location defined in the module configuration. Perform the following stepto do so:

1.       Our first step will be to define a location where files can be uploaded in the module's configuration (config/module.config.php):
<?php
return array(
// Other configurations
// ..
// ..
// MODULE CONFIGURATIONS 'module_config' => array(

'upload_location' =>   DIR  
),
);

. '/../data/uploads',


2.       Next, we need to create a table which will store the upload information:
CREATE TABLE IF NOT EXISTS uploads (
id INT NOT NULL AUTO_INCREMENT PRIMARY KEY , filename VARCHAR( 255 ) NOT NULL ,
label VARCHAR( 255 ) NOT NULL , user_id INT NOT NULL,
UNIQUE KEY (filename)
);

3.       Create the Upload and UploadTable classes for interacting with the uploads table. Add default methods such as saveUpload()fetchAll()getUpload(), and deleteUpload(). Also, add a method toget uploads made by a specific user getUploadsByUserId($userId):
public function getUploadsByUserId($userId)
{
$userId  = (int) $userId;
$rowset = $this->tableGateway->select( array('user_id' => $userId));
return $rowset;
}

4.       Create an UploadManagerController controller for managing file uploads. Add
indexAction() to display the list of uploads done by the user:
$uploadTable = $this->getServiceLocator()
->get('UploadTable');
$userTable = $this->getServiceLocator()
->get('UserTable');
// Get User Info from Session
$userEmail = $this->getAuthService()
->getStorage()->read();
$user = $userTable->getUserByEmail($userEmail);

$viewModel  = new ViewModel( array(
'myUploads' => $uploadTable->getUploadsByUserId($user->id),
));
return $viewModel;


5.       Create an upload form with a file input as described in the following code snippet:
$this->add(array(
'name' => 'fileupload', 'attributes' => array(
'type'  => 'file',
),
'options' => array(
'label' => 'File Upload',
),
));


6.       Create viewfor the file upload form, and the index action. Nowe have all the necessary elements to handle a file upload. We need tread the configuration for the file upload path and use the Zend HTTP filetransfer adapter treceive the file
in the configuration location. The get('config') method on the service locator is used tretrieve the configuration. The following code is used tread the file upload location from the configuration:
public function getFileUploadLocation()
{
// Fetch Configuration from Module Config
$config  = $this->getServiceLocator()->get('config'); return $config['module_config']['upload_location'];
}


7.       The lasstep is to handle the file upload process. There are two actions that need to happen once the form is successfully submitted:
1.      The uploaded file has to be moved to the file upload locations.
2.      An entry needs to be added describing the upload in the 'uploads' table
using the following code:
$uploadFile = $this->params()->fromFiles('fileupload');
$form->setData($request->getPost());

if ($form->isValid()) {
// Fetch Configuration from Module Config
$uploadPath    = $this->getFileUploadLocation();

// Save Uploaded file
$adapter = new \Zend\File\Transfer\Adapter\Http();
$adapter->setDestination($uploadPath);
if ($adapter->receive($uploadFile['name'])) {
// File upload sucessfull
$exchange_data = array();
$exchange_data['label'] = $request->getPost()
->get('label');
$exchange_data['filename'] = $uploadFile['name'];
$exchange_data['user_id'] = $user->id;

$upload->exchangeArray($exchange_data);
$uploadTable = $this->getServiceLocator()
->get('UploadTable');
$uploadTable->saveUpload($upload);

return $this->redirect()
->toRoute('users/upload-manager' , array('action' =>                   'index'
));
}
}

8.       Add a child route (upload manger) for the UploadManager controller and the controller to the invokables list.
9.       Open the web browser and test the upload form.
The final form will look like the following screenshot:


What just happened?
We have now created a file upload process, which allows users to upload files into the application and view the files that are uploaded. We have used Zend Framework's file upload handling components to handle a file upload. In our next section, we will set up a file sharing mechanism such that the documents can be shared with different users. Before we move on to implement file sharing, please complete the following task.

Your nextask will be to add a Deletoption that allows userto delete uploaded files as shown in the following screenshot. Also, ensure that the file is removed from the filesystem when the delete action is triggered.


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

Đăng nhận xét