Creating a CSV Upload Page

From IHRIS Wiki
Revision as of 15:06, 27 February 2014 by Lduncan (talk | contribs) (Created page with "If you need to allow users to upload data into your site and import that directly into various forms you can extend I2CE_PageFormCSV page class. The CSV file can optionally incl...")
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)

If you need to allow users to upload data into your site and import that directly into various forms you can extend I2CE_PageFormCSV page class. The CSV file can optionally include headers so that the order of the columns doesn't have to be set. This example will entail adding some data as a child form for a person in iHRIS Manage, however this many not be required for your use case so you won't need to include the pieces for this parent form. For this example we'll configure a page that will allow you to upload education details for a particular person.

Configuring Your Module

First you need to configure a new page in your module. You can just use a standard module header for this and require the Forms module (although this is probably already enabled by default) as well as PersonEducation since that's the form we'll be importing.

<source lang="xml"> <?xml version="1.0"?> <!DOCTYPE I2CEConfiguration SYSTEM "I2CE_Configuration.dtd"> <I2CEConfiguration name="FormCSVUpload">

 <metadata>
   <displayName>FormCSVUpload</displayName>
   <category>Tutorial</category>
   <description>Tutorial for uploading CSV data into iHRIS.  This example will load education details to a person.</description>
   <creator>IntraHealth Informatics</creator>
   <email>hris@capacityplus.org</email>
   <link>https://launchpad.net/ihris-manage</link>
   <version>4.2.0</version>
   <path name="classes">
     <value>./lib</value>
   </path>
   <path name="templates">
     <value>./templates</value>
   </path>
   <requirement name="forms">
     <atLeast version="4.2" />
   </requirement>
   <requirement name="PersonEducation">
     <atLeast version="4.2" />
   </requirement>
   <priority>200</priority>
 </metadata>
 <configurationGroup name="FormCSVUpload" path="/I2CE">
   <displayName>FormCSVUpload</displayName>
 </configurationGroup>

</I2CEConfiguration> </source>

Now we can add in the configuration for the new page we're creating. This should go in the main configurationGroup of your module.

<source lang="xml">

   <configurationGroup name="page">
     <configurationGroup name="upload_education">
       <displayName>Upload page</displayName>
       <description>The page 'upload_education' which is used to add education details to a person from a CSV file upload.</description>
       <configuration name="class" values="single">
         <value>iHRIS_PageFormUpload_Education</value>
       </configuration>
       <configuration name="style" values="single">
         <value>shell</value>
       </configuration>
       <configurationGroup name="args">
         <configuration name="title" values="single" locale="en_US">
           <value>Upload Education</value>
         </configuration>
         <configuration name="defaultHTMLFile" values="single">
           <value>upload_education.html</value>
         </configuration>
         <configuration name="tasks" values="many">
           <value>person_can_edit_child_form_education</value>
         </configuration>
         <configuration name="action_permission" type="delimited">
           <value>view:task(person_can_view_child_form_education)</value>
           <value>edit:task(person_can_edit_child_form_education)</value>
         </configuration>
       </configurationGroup>
     </configurationGroup> 
   </configurationGroup> 

</source>

Creating Your Template

Now we need to create the template file mentioned in the module: upload_education.html. This will include the file upload as well as a checkbox to mark if the file has headers or not. This example will required that the headers are set. It will also include some additional display of the person form and links back to the default view page.

<source lang="html5">

Upload Education for: ,

 <form method="POST" enctype="multipart/form-data">
   
Upload Education Details

<label for="education">Select CSV file to upload</label> *
<input type="file" id="education" name="education" />

Has Headers?
<input type="checkbox" id="has_header:education" name="has_header:education" value="1" checked="checked" /> <label for="has_header:education">This file has headers.</label>

 </form>

</source>

Extending the CSV Upload Class

Next you need to create a new page class as named in the module: iHRIS_PageFormUpload_Education.php.

You should include comments as necessary, but those won't be included in this tutorial to save space.

<source lang="php">

</source>