📜 ⬆️ ⬇️

SDK: creating an application in 3 steps



Good day.

Last week we announced the opening of the SDK and it was the turn to describe the process of creating an application.
')
Applications for Finobox can be very diverse, such as the preparation of contracts and documents, estimates and reports, financial data analysis and graphing, and so on.

Any php developer using the SDK can create his own application and place it in Finobox, making a profit from the sales of this application to users.

In this post we describe the creation of an application for importing financial data from a CSV file.


1. Preparations


From the developer center we download the latest version of the SDK and unpack it on our web server. Everything should work out of the box, no ways and configurations should be configured.

All applications are located in the Store folder. So far here we will find applications for an example and preparation - MyNewApplication . Based on the workpiece and we will create the application in this article.

The folder name must match the file name and class name of the application. Let's call the application CSVImport . Rename the file, folder and class described in the file.

The file structure will be as follows:


Edit the description.xml file:
< application >
< name > CSV </ name >
< descr > CSV </ descr >
</ application >



Let's see what happened:


SDK Help: Application Principles

2. Create an interface


So, our application should receive a file from the user and overtake all the detected records in payments of the current panel. For this task, a single window is enough in the interface, where there will be file input and a launch button.

But we will go the other way and make the application “two-way” - in the second step we will show the user how the application understands the downloaded data before starting to grind them into payments.

So, in the application class CSVImport there will be three methods: main and step2 for building two windows and import for performing actions on importing data.

<?php
class CSVImport extends Application{
function main(){
// HTML
$ this ->setResultType(self::$RESULT_HTML);
}
function step2(){
// HTML
$ this ->setResultType(self::$RESULT_HTML);
}
function import(){
//
$ this ->setResultType(self::$RESULT_CLOSE_N_RELOAD);
}
}
?>


2.1 File Download Window

To build the form interface, use the form designer.
SDK Help: Form Designer
function main(){
$form = $ this ->startUIForm();
//
$form->add(
FormElement::factory( 'FileInput' , 'csvfile' )
->setLabel( ' CSV ' )
);
$form->add(
FormElement::factory( 'StaticInput' )
->addAttribute( 'class="notice"' )
->setValue( ' " " ' )
);
// step2 ,
$ this ->setResultType(self::$RESULT_HTML);
return $ this ->finalizeUIForm($form, 'step2' , ' ' )->getHtml();
}

View of the application in the first step:

2.2 Format confirmation window

File uploaded (or not loaded :)). Check it and display the first 10 entries in the table. To build the code of this window we will use the template mechanism. The template is put in the application folder under the name csvtable.php. Also, give “transit” the path to the downloaded file.

SDK Help: Templates
function step2(){
$file = $_FILES[ 'csvfile' ];
/**
*
*/
if ($file[ 'error' ] != 0)
throw new Exception( ' ' );
if ( ($file[ 'type' ] != 'text/plain' ) && ($file[ 'type' ] != 'text/csv' ))
throw new Exception( ' ' );
/**
* 10
*/
$limit = 10;$strings = array();
$fhandle = @fopen($file[ 'tmp_name' ], 'r' );
if ($fhandle === false ) throw new Exception( ' ' );
while (!feof($fhandle)){
$strings[] = fgets($fhandle);
if (--$limit < 0) break ;
}
fclose($fhandle);
$ this ->setResultType(self::$RESULT_HTML);
//
return $ this ->renderTemplate( 'csvtable.php' , array( 'csv' => $strings, 'filename' => $file[ 'tmp_name' ]));
}

View of the application in the second step:

2.3 Data Import

After confirming the format, it remains only to go through the file and create new payments. For the experiments, take the export CSV-file of the Chatter service. The format is:

1 field (zero index) - category of payment. In the concept of a finbox, this is a label.
3 - date of payment
4 - payment name, comment
7 - amount

SDK Help: Downloading Files and Making Payments
function import(){
$fhandle = @fopen($_POST[ 'csvfile' ], 'r' );
if ($fhandle === false ) throw new Exception( ' ' );
while (!feof($fhandle)){
$row = iconv( 'CP1251' , 'UTF-8' , fgets($fhandle));
$row = str_replace( '"' , '' , $row);
$entry = explode( ';' , $row);
if (count($entry) > 1){
$payment = new Entry();
$payment->name = $entry[3];
// , ,
$payment->currency_id = $ this ->userPreferences->defaultCurrency;
$payment-> value = $entry[6];
$payment->paiddate = $entry[2];
$payment->tags = $entry[0];
//
$ this ->entriesData->insert($payment);
}
}
fclose($fhandle);
//
unlink($_POST[ 'csvfile' ]);
//
$ this ->setResultType(self::$RESULT_CLOSE_N_RELOAD);
return ;
}


Note: in the SDK mode, the functions of updating and adding payments do not change the data file, that is, after the data has been imported in the test database, they will no longer be. In the work service, everything works in a working way.

3. Sending the app to the store



The application is ready, it remains only to tie a ribbon, provide an icon, set a price and send it to the developer’s center for review.


Finished application for importing such files into Finobox.ru can be connected here.

* Source Code Highlighter .

Source: https://habr.com/ru/post/95924/


All Articles