Skip to content

pascal-martinez/z4m_storage

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

7 Commits
 
 
 
 
 
 
 
 
 
 

Repository files navigation

ZnetDK 4 Mobile module: Storage (z4m_storage)

The z4m_storage module simplifies the loading and storage of documents and photos in the application. It includes:

  • A Javascript API and view fragments to select one or more files on the user's disk, transmit them to the hosting and preview them once stored.
  • A PHP API to store and query the files uploaded by the user.
  • A view to consult the disk space consumed by the files uploaded to the hosting.
  • A view to delete the files uploaded to the hosting according to the chosen criteria (storage period, storage subdirectory, file extension and size).

FEATURES

  • Display of the disk space used by the App's database and by the stored documents and photos.

Disk space used view provided by the ZnetDK 4 Mobile 'z4m_storage' module

  • Purge of the documents and photos according to the specified period, subdirectory, file extension and file size.

Documents view provided by the ZnetDK 4 Mobile 'z4m_storage' module

CONCEPTS

File storage location in ZnetDK 4 Mobile

In a ZnetDK 4 Mobile application, documents and photos uploaded by users are generally stored within the documents directory (see Stored documents).

The z4m_storage module propose to store uploaded files in a dedicated subdirectory for each managed business object.
For example, uploaded files for the Customer business object could be stored in a subdirectory named customer\, the others uploaded for the Invoice business object could be stored in a subdirectory named invoice\.

When a file is uploaded through the z4m_storage module, the subdirectory can be specified to store the file in the appropriate subfolder.

File index stored in database

When a file is stored by the z4m_storage module, a record is saved in the SQL table z4m_documents. This record allows in particular to know the user at the origin of the file upload and to associate with it an identifier of the business object to which it is linked (for example 73 which is the identifier of the Customer for which the file was uploaded).

When a file is uploaded through the z4m_storage module, the business ID can be specified to link the file to the appropriate business identifier.

Photo reduction before upload

Before uploading a photo (jpeg or png file), the z4m_storage module reduces it automatically to the number of pixels specified by the PHP constant MOD_Z4M_STORAGE_MAX_PHOTO_WIDTH_IN_PIXELS.
This saves both network bandwidth during file transfer and disk space for storing it on the web hosting.

LICENCE

This module is published under the version 3 of GPL General Public Licence.

REQUIREMENTS

INSTALLATION

  1. Add a new subdirectory named z4m_storage within the ./engine/modules/ subdirectory of your ZnetDK 4 Mobile starter App,
  2. Copy module's code in the new ./engine/modules/z4m_storage/ subdirectory, or from your IDE, pull the code from this module's GitHub repository,
  3. Edit the App's menu.php located in the ./applications/default/app/ subfolder and include the menu.inc script to add menu item definition for the z4m_storage_stats and z4m_storage_documents views.
require ZNETDK_MOD_ROOT . '/z4m_storage/mod/menu.inc';
  1. Create a documents folder within the ./applications/default/ directory. This folder is the root directory where are stored the uploaded files.
  2. Reload the ZnetDK 4 Mobile starter App in the web browser to see the new Storage menu and the Disk space used and Documents submenus.

USERS GRANTED TO MODULE FEATURES

Once the Storage menu item is added to the application, you can restrict its access via a user profile.
For example:

  1. Create a user profile named Admin from the Authorizations | Profiles menu,
  2. Select for this new profile, the Disk space used and Documents submenu items,
  3. Finally for each allowed user, add them the Admin profile from the Authorizations | Users menu.

CONFIGURING FILE UPLOAD AND STORAGE

php.ini

The php directives below directly affect file uploads in the application and should be customized if necessary in the PHP configuration php.ini script.

config.php

Defines the following PHP constants to the config.php script of your Starter App to customize the default configuration of the Storage module:

ADD FILE UPLOAD TO YOUR APP

The z4m_storage module is shipped with two PHP view fragment upload_documents.php and upload_photos.php, that can be included in a view or a modal dialog to upload files.

Document upload: upload_documents.php view fragment

The upload_documents.php view fragment is PHP code for document upload (PDF documents and others). This PHP code displays:

  • an Upload documents... button to select the files to upload,
  • a table of the uploaded documents.

Upload documents view fragment provided by the ZnetDK 4 Mobile 'z4m_storage' module

To initialize the upload_documents.php view fragment, a Z4M_StorageDocumentUpload object is instantiated from the z4m_storage_upload-min.js JS module.

<div id="my-upload-container" class="w3-content">
<?php require 'z4m_storage/mod/view/fragment/upload_documents.php'; ?>
</div>
<script type="module">
    import { Z4M_StorageDocumentUpload } from './engine/modules/z4m_storage/public/js/class/z4m_storage_upload-min.js';
    const storageObj = new Z4M_StorageDocumentUpload('#my-upload-container');
    storageObj.setBusinessIdCallback(function(){
        return 72; // Identifier of the Invoice business object
    });
    // Subdirectory where documents are stored on the web server.
    storageObj.setStorageSubdirectory('invoice_docs');
    // Existing documents stored in the 'invoice_docs' subdirectory
    // for the business ID = 72 are displayed.
    storageObj.refresh();
</script>

Photo upload: upload_photos.php view fragment

The upload_photos.php view fragment is dedicated to photo upload. This PHP code displays:

  • an Upload photos... button to select the photos to upload,
  • The thumbnails of the uploaded photos.

Upload photos view fragment provided by the ZnetDK 4 Mobile 'z4m_storage' module

To initialize the upload_photos.php view fragment, a Z4M_StoragePhotoUpload object is instantiated from the z4m_storage_upload-min.js JS module.

<div id="my-upload-container" class="w3-content">
<?php require 'z4m_storage/mod/view/fragment/upload_photos.php'; ?>
</div>
<script type="module">
    import { Z4M_StoragePhotoUpload } from './engine/modules/z4m_storage/public/js/class/z4m_storage_upload-min.js';
    const storageObj = new Z4M_StoragePhotoUpload('#my-upload-container');
    storageObj.setBusinessIdCallback(function(){
        return 18; // Identifier of the Customer business object
    });
    // Subdirectory where photos are stored on the web server.
    storageObj.setStorageSubdirectory('customer_photos');
    // Existing photos stored in the 'customer_photos' subdirectory
    // for the business ID = 18 are displayed.
    storageObj.refresh();
</script>

ISSUES

SQL table

The z4m_documents SQL table is created automatically by the module when one of the module views is displayed for the first time.
If the MySQL user declared through the CFG_SQL_APPL_USR PHP constant does not have CREATE privilege, the module can't create the required SQL table. In this case, you can create the module's SQL table by importing in MySQL or phpMyAdmin the script z4m_storage.sql provided by the module.

File storage subdirectory

The subdirectories where are stored the uploaded files are not created automatically. When a subdirectory is specified for storing an uploaded file, the subdirectory must exist within the documents/ directory (see Stored documents).

CHANGE LOG

See CHANGELOG.md file.

CONTRIBUTING

Your contribution to the ZnetDK 4 Mobile project is welcome. Please refer to the CONTRIBUTING.md file.