fmsystem-commits
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Fmsystem-commits] [11820] property: add identificator to import files i


From: Sigurd Nes
Subject: [Fmsystem-commits] [11820] property: add identificator to import files in order to avoid import to wrong target
Date: Fri, 14 Mar 2014 11:51:30 +0000

Revision: 11820
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=11820
Author:   sigurdne
Date:     2014-03-14 11:51:30 +0000 (Fri, 14 Mar 2014)
Log Message:
-----------
property: add identificator to import files in order to avoid import to wrong 
target

Modified Paths:
--------------
    trunk/property/inc/class.bocommon.inc.php
    trunk/property/inc/class.uiimport.inc.php
    trunk/property/inc/import/default/cvs_import_fiktiv_faktura_nlsh

Modified: trunk/property/inc/class.bocommon.inc.php
===================================================================
--- trunk/property/inc/class.bocommon.inc.php   2014-03-14 09:57:16 UTC (rev 
11819)
+++ trunk/property/inc/class.bocommon.inc.php   2014-03-14 11:51:30 UTC (rev 
11820)
@@ -1520,8 +1520,10 @@
                 * @param array $name array containing keys in $list
                 * @param array $descr array containing Names for the heading 
of the output for the coresponding keys in $list
                 * @param array $input_type array containing information 
whether fields are to be suppressed from the output
+                * @param array $identificator array containing 1.row for 
identification purposes in case of data import.
+                * @param string $filename
                 */
-               function download($list,$name,$descr,$input_type=array())
+               function 
download($list,$name,$descr,$input_type=array(),$identificator = 
array(),$filename = '')
                {
                        set_time_limit(500);
                        $GLOBALS['phpgw_info']['flags']['noheader'] = true;
@@ -1533,13 +1535,13 @@
                        switch ($export_format)
                        {
                                case 'csv':
-                                       
$this->csv_out($list,$name,$descr,$input_type);
+                                       
$this->csv_out($list,$name,$descr,$input_type,$identificator,$filename);
                                        break;
                                case 'excel':
-                                       
$this->excel_out($list,$name,$descr,$input_type);
+                                       
$this->excel_out($list,$name,$descr,$input_type,$identificator,$filename);
                                        break;
                                case 'ods':
-                                       
$this->ods_out($list,$name,$descr,$input_type);
+                                       
$this->ods_out($list,$name,$descr,$input_type,$identificator,$filename);
                                        break;
                        }
                }
@@ -1551,13 +1553,22 @@
                 * @param array $name array containing keys in $list
                 * @param array $descr array containing Names for the heading 
of the output for the coresponding keys in $list
                 * @param array $input_type array containing information 
whether fields are to be suppressed from the output
+                * @param array $identificator array containing 1.row for 
identification purposes in case of data import.
+                * @param string $filename
                 */
-               function excel_out($list,$name,$descr,$input_type=array())
+               function 
excel_out($list,$name,$descr,$input_type=array(),$identificator=array(),$filename
 = '')
                {
                        phpgw::import_class('phpgwapi.phpexcel');
+                       if($filename)
+                       {
+                               $filename_arr = explode('.', str_replace(' 
','_',basename($filename)));
+                               $filename = $filename_arr[0].'.xlsx';
+                       }
+                       else
+                       {
+                               $filename= str_replace(' 
','_',$GLOBALS['phpgw_info']['user']['account_lid']).'.xlsx';                   
       
+                       }
 
-                       $filename= str_replace(' 
','_',$GLOBALS['phpgw_info']['user']['account_lid']).'.xlsx';
-
                        $browser = CreateObject('phpgwapi.browser');
 //                     
$browser->content_header($filename,'application/vnd.ms-excel');
                        
$browser->content_header($filename,'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet');
@@ -1579,6 +1590,22 @@
                        // Set active sheet index to the first sheet, so Excel 
opens this as the first sheet
                        $objPHPExcel->setActiveSheetIndex(0);
 
+                       if($identificator)
+                       {
+                               $_first_row = 2;
+                               $i = 0;
+                               foreach($identificator as $key => $value)
+                               {
+                                       
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($i, 1, $key);
+                                       $i++;
+                                       
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($i, 1, $value);
+                                       $i++;
+                               }
+                       }
+                       else
+                       {
+                               $_first_row = 1;
+                       }
                        $count_uicols_name=count($name);
 
                        $text_format = array();
@@ -1593,7 +1620,7 @@
                                                
//$objPHPExcel->getActiveSheet()->getStyle('B3:B7')->applyFromArray($styleArray);
 
                                        }
-                                       
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($m, 1, 
$descr[$k]);
+                                       
$objPHPExcel->setActiveSheetIndex(0)->setCellValueByColumnAndRow($m, 
$_first_row, $descr[$k]);
                                        $m++;
                                }
                        }
@@ -1615,7 +1642,7 @@
                                        $j++;
                                }
 
-                               $line = 1;
+                               $line = $_first_row;
                                $col = 'A';
 
                                foreach($content as $row)
@@ -1713,9 +1740,18 @@
                 * @param array $descr array containing Names for the heading 
of the output for the coresponding keys in $list
                 * @param array $input_type array containing information 
whether fields are to be suppressed from the output
                 */
-               function csv_out($list, $name, $descr, $input_type = array() )
+               function csv_out($list, $name, $descr, $input_type = array(), 
$identificator=array(),$filename = '')
                {
-                       $filename= str_replace(' 
','_',$GLOBALS['phpgw_info']['user']['account_lid']).'.csv';
+                       if($filename)
+                       {
+                               $filename_arr = explode('.', str_replace(' 
','_',basename($filename)));
+                               $filename = $filename_arr[0].'.csv';
+                       }
+                       else
+                       {
+                               $filename= str_replace(' 
','_',$GLOBALS['phpgw_info']['user']['account_lid']).'.csv';
+                       }
+
                        $browser = CreateObject('phpgwapi.browser');
                        $browser->content_header($filename, 'application/csv');
 
@@ -1724,6 +1760,17 @@
                                die('Unable to write to "php://output" - pleace 
notify the Administrator');
                        }
 
+                       if($identificator)
+                       {
+                               $_identificator = array();
+                               foreach($identificator as $key => $value)
+                               {
+                                       $_identificator[] =  $key;
+                                       $_identificator[] =  $value;
+                               }
+                               fputcsv($fp, $_identificator, ';');
+                       }
+
                        $count_uicols_name=count($name);
 
                        $header = array();
@@ -1763,9 +1810,18 @@
                 * @param array $descr array containing Names for the heading 
of the output for the coresponding keys in $list
                 * @param array $input_type array containing information 
whether fields are to be suppressed from the output
                 */
-               function ods_out($list, $name, $descr, $input_type = array() )
+               function ods_out($list, $name, $descr, $input_type = array(), 
$identificator=array(),$filename = '')
                {
-                       $filename= str_replace(' 
','_',$GLOBALS['phpgw_info']['user']['account_lid']).'.ods';
+                       if($filename)
+                       {
+                               $filename_arr = explode('.', str_replace(' 
','_',basename($filename)));
+                               $filename = $filename_arr[0].'.ods';
+                       }
+                       else
+                       {
+                               $filename= str_replace(' 
','_',$GLOBALS['phpgw_info']['user']['account_lid']).'.ods';
+                       }
+
                        $browser = CreateObject('phpgwapi.browser');
                        $browser->content_header($filename, 'application/ods');
 
@@ -1774,12 +1830,29 @@
                        $ods = createObject('property.ods');
                        $object = $ods->newOds(); //create a new ods file
 
+                       if($identificator)
+                       {
+                               $_first_row = 1;
+                               $i = 0;
+                               foreach($identificator as $key => $value)
+                               {
+                                       $object->addCell(1, 0, $i, $key, 
'string');
+                                       $i++;
+                                       $object->addCell(1, 0, $i, $value, 
'string');
+                                       $i++;
+                               }
+                       }
+                       else
+                       {
+                               $_first_row = 0;
+                       }
+
                        $m=0;
                        for ($k=0;$k<$count_uicols_name;$k++)
                        {
                                if(!isset($input_type[$k]) || 
$input_type[$k]!='hidden')
                                {
-                                       $object->addCell(1, 0, $m, $descr[$k], 
'string');
+                                       $object->addCell(1, $_first_row, $m, 
$descr[$k], 'string');
                                        $m++;
                                }
                        }
@@ -1801,14 +1874,14 @@
                                        $j++;
                                }
 
-                               $line = 0;
+                               $line = $_first_row +1;
                                foreach($content as $row)
                                {
-                                       $line++;
                                        for ($i=0; $i<count($row); $i++)
                                        {
                                                $object->addCell(1, $line, $i, 
$row[$i], 'string');
                                        }
+                                       $line++;
                                }
                        }
 

Modified: trunk/property/inc/class.uiimport.inc.php
===================================================================
--- trunk/property/inc/class.uiimport.inc.php   2014-03-14 09:57:16 UTC (rev 
11819)
+++ trunk/property/inc/class.uiimport.inc.php   2014-03-14 11:51:30 UTC (rev 
11820)
@@ -26,6 +26,7 @@
                protected $fields = array();
                protected $table;
                protected $debug;
+               protected $identificator;
 
                // Label on the import button. Changes as we step through the 
import process.
                protected $import_button_label;
@@ -90,6 +91,7 @@
                        $this->download_template_button_label = 'Download 
template';
 
                        $check_method = 0;
+                       $get_identificator = false;
                        if($this->conv_type = phpgw::get_var('conv_type'))
                        {
                                $check_method ++;
@@ -97,11 +99,13 @@
                        if ($location_id = phpgw::get_var('location_id', 'int'))
                        {
                                $check_method ++;
+                               $get_identificator = true;
                        }
                                
                        if($table = phpgw::get_var('table'))
                        {
                                $check_method ++;
+                               $get_identificator = true;
                        }
 
                        if($check_method > 1)
@@ -190,12 +194,12 @@
                                                case 
'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet':
                                                case 
'application/vnd.oasis.opendocument.spreadsheet':
                                                case 'application/vnd.ms-excel':
-                                                       $this->csvdata = 
$this->getexceldata($file['name']);
+                                                       $this->csvdata = 
$this->getexceldata($file['name'],$get_identificator);
                                                        $valid_type = true;
                                                        break;
                                                case 'text/csv':
                                                case 
'text/comma-separated-values':
-                                                       $this->csvdata = 
$this->getcsvdata($file['name']);
+                                                       $this->csvdata = 
$this->getcsvdata($file['name'],$get_identificator);
                                                        $valid_type = true;
                                                        break;
                                                default:
@@ -389,6 +393,7 @@
 
                protected function get_template($location_id = 0)
                {
+                       $_identificator = array();
                        $data = array();
                        $_fields = array();
                        if(!$location_id && $this->table)
@@ -422,6 +427,14 @@
                        }
                        else if($location_id)
                        {
+                               $system_location = 
$GLOBALS['phpgw']->locations->get_name($location_id);
+                               $_identificator = array
+                               (
+                                       'identificator' => 
"location::{$system_location['appname']}::{$system_location['location']}"
+                               );
+                               
+                               $filename = 
"fm_entity_{$category['entity_id']}_{$category['id']}";
+
                                $entity_id = $category['entity_id'];
                                $cat_id = $category['id'];
 
@@ -490,9 +503,40 @@
                                        }
                                }
                        }
+                       if(!$_identificator && $this->table)
+                       {
+                               $_identificator = array
+                               (
+                                       'identificator' => 
"table::{$this->table}"
+                               );
+                               $filename = $this->table;
 
-                       $fields = array_keys($_fields);
+                       }
+                       else if(!$_identificator && $this->conv_type)
+                       {
+                               $_identificator = array
+                               (
+                                       'identificator' => 
"conversion::{$this->conv_type}"
+                               );
 
+                               if ( preg_match('/\.\./', $this->conv_type) )
+                               {
+                                               throw new Exception("Not a 
valid file: {$this->conv_type}");
+                               }
+
+                               $file = PHPGW_SERVER_ROOT . 
"/property/inc/import/{$GLOBALS['phpgw_info']['user']['domain']}/{$this->conv_type}";
+
+                               if ( is_file($file) )
+                               {
+                                       require_once $file;
+                               }
+                               $_import_conversion = new 
import_conversion(0,false,true);
+                               $fields = $_import_conversion->fields;
+                               $filename = 
$_import_conversion->filename_template;
+                       }
+
+                       $fields = $fields ? $fields : array_keys($_fields);
+
                        if(phpgw::get_var('debug', 'bool'))
                        {
                                _debug_array($fields);
@@ -500,13 +544,46 @@
                        else
                        {
                                $bocommon = CreateObject('property.bocommon');
-                               $bocommon->download($data, $fields, $fields);
+                               $bocommon->download($data, $fields, 
$fields,array(),$_identificator,$filename);
                                $GLOBALS['phpgw']->common->phpgw_exit();
                        }
                }
 
                protected function import_data()
                {
+                       if(!$this->identificator)
+                       {
+                               throw new Exception("Missing identificator in 
dataset");
+                       }
+                       $identificator_arr = explode("::", 
$this->identificator);
+                       switch($identificator_arr[0])
+                       {
+                               case 'location':
+                                       
if(!$GLOBALS['phpgw']->locations->get_id($identificator_arr[1], 
$identificator_arr[2]))
+                                       {
+                                               throw new Exception("No valid 
location: {$identificator_arr[2]}");
+                                       }
+                                       break;
+                               case 'table':
+                                       if($this->table && 
$identificator_arr[1] != $this->table)
+                                       {
+                                               throw new Exception("Not the 
intended target? got: {$identificator_arr[1]} , expected: {$this->table}");
+                                       }
+                                       break;
+                               case 'conversion':
+                                       if($this->conv_type && 
$identificator_arr[1] != $this->conv_type)
+                                       {
+                                               throw new Exception("Not the 
intended target? got: {$identificator_arr[1]} , expected: {$this->conv_type}");
+                                       }
+                                       break;
+                               default:
+                                       throw new Exception("No valid 
location");
+
+                       }
+                       _debug_array($this->csvdata);
+_debug_array($this->fields);
+                       _debug_array($this->identificator);die();
+
                        $metadata = array();
                        if($this->table && $this->fields)
                        {
@@ -598,17 +675,20 @@
                }
 
 
-               protected function getcsvdata($path, $skipfirstline = true)
+               protected function getcsvdata($path, $get_identificator = true)
                {
                        // Open the csv file
                        $handle = fopen($path, "r");
 
-                       if ($skipfirstline)
+                       if($get_identificator)
                        {
-                               // Read the first line to get the headers out 
of the way
-                               $this->fields = $this->getcsv($handle);
+                               $_identificator_arr = $this->getcsv($handle);
+                               $this->identificator = $_identificator_arr[1];
                        }
 
+                       // Read the first line to get the headers out of the way
+                       $this->fields = $this->getcsv($handle);
+
                        $result = array();
 
                        while(($data = $this->getcsv($handle)) !== false)
@@ -625,7 +705,7 @@
                }
 
 
-               protected function getexceldata($path, $skipfirstline = true)
+               protected function getexceldata($path, $get_identificator = 
false)
                {
                        phpgw::import_class('phpgwapi.phpexcel');
 
@@ -640,12 +720,20 @@
 
                        $rows = 
$objPHPExcel->getActiveSheet()->getHighestDataRow();
 
-                       $start = $skipfirstline ? 2 : 1; // Read the first line 
to get the headers out of the way
+                       $start = $get_identificator ? 3 : 1; // Read the first 
line to get the headers out of the way
 
-                       if ($skipfirstline)
+                       if($get_identificator)
                        {
+                               $this->identificator = 
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow(1,1)->getCalculatedValue();
                                for ($j=0; $j < $highestColumnIndex; $j++ )
                                {
+                                       $this->fields[] = 
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow($j,2)->getCalculatedValue();
+                               }
+                       }
+                       else
+                       {
+                               for ($j=0; $j < $highestColumnIndex; $j++ )
+                               {
                                        $this->fields[] = 
$objPHPExcel->getActiveSheet()->getCellByColumnAndRow($j,1)->getCalculatedValue();
                                }
                        }

Modified: trunk/property/inc/import/default/cvs_import_fiktiv_faktura_nlsh
===================================================================
--- trunk/property/inc/import/default/cvs_import_fiktiv_faktura_nlsh    
2014-03-14 09:57:16 UTC (rev 11819)
+++ trunk/property/inc/import/default/cvs_import_fiktiv_faktura_nlsh    
2014-03-14 11:51:30 UTC (rev 11820)
@@ -10,9 +10,19 @@
                protected $year;
                protected $month;
                protected $date;
+               public $fields = array('Hovedgruppering', 'Art', 'Hjelpetekst', 
'Koststed', 'Beløp', 'Best.nr');
+               public $filename_template;
 
-               public function __construct($location_id,$debug = false)
+               public function __construct($location_id,$debug = false, 
$get_template = false)
                {
+                       /*
+                        * The purpose is to get to the fields definition
+                        */
+                       if($get_template)
+                       {
+                               $this->filename_template = "DTDRIFT rapport til 
Portico " . date(Ym);// 201401"
+                               return;
+                       }
                        set_time_limit(10000); //Set the time limit for this 
request
                        $this->account          = 
(int)$GLOBALS['phpgw_info']['user']['account_id'];
                        $this->db           = & $GLOBALS['phpgw']->db;
@@ -48,7 +58,6 @@
 
                public function add($data)
                {
-
 /*
     [0] => Hovedgruppering
     [1] => Art




reply via email to

[Prev in Thread] Current Thread [Next in Thread]