fmsystem-commits
[Top][All Lists]
Advanced

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

[Fmsystem-commits] [11442] property: generic import


From: Sigurd Nes
Subject: [Fmsystem-commits] [11442] property: generic import
Date: Sun, 10 Nov 2013 18:46:34 +0000

Revision: 11442
          http://svn.sv.gnu.org/viewvc/?view=rev&root=fmsystem&revision=11442
Author:   sigurdne
Date:     2013-11-10 18:46:33 +0000 (Sun, 10 Nov 2013)
Log Message:
-----------
property: generic import

Modified Paths:
--------------
    trunk/property/inc/class.soadmin_entity.inc.php
    trunk/property/inc/class.uiimport.inc.php
    trunk/property/inc/import/import_update_generic.php

Modified: trunk/property/inc/class.soadmin_entity.inc.php
===================================================================
--- trunk/property/inc/class.soadmin_entity.inc.php     2013-11-08 15:09:21 UTC 
(rev 11441)
+++ trunk/property/inc/class.soadmin_entity.inc.php     2013-11-10 18:46:33 UTC 
(rev 11442)
@@ -258,8 +258,9 @@
                        {
                                $this->category_tree[$category['id']] = array
                                (
-                                       'id'    => $category['id'],
-                                       'name'  => $category['name']
+                                       'id'                    => 
$category['id'],
+                                       'name'                  => 
$category['name'],
+                                       'location_id'   => 
$category['location_id']
                                );
                                $this->get_children2($entity_id, 
$category['id'], 1);
                        }
@@ -487,6 +488,7 @@
                                $category = array
                                (
                                        'id'                                    
        => $this->db->f('id'),
+                                       'entity_id'                             
        => $this->db->f('entity_id'),
                                        'name'                                  
        => $this->db->f('name',true),
                                        'descr'                                 
        => $this->db->f('descr',true),
                                        'prefix'                                
        => $this->db->f('prefix',true),

Modified: trunk/property/inc/class.uiimport.inc.php
===================================================================
--- trunk/property/inc/class.uiimport.inc.php   2013-11-08 15:09:21 UTC (rev 
11441)
+++ trunk/property/inc/class.uiimport.inc.php   2013-11-10 18:46:33 UTC (rev 
11442)
@@ -9,12 +9,12 @@
 
                const DELIMITER = ";";
                const ENCLOSING = "'";
-               
+
                // List of messages, warnings and errors to be displayed to the 
user after the import
                protected $messages = array();
                protected $warnings = array();
                protected $errors = array();
-               
+
                // File system path to import folder on server
                protected $file;
                protected $district;
@@ -26,12 +26,12 @@
                protected $fields = array();
                protected $table;
                protected $debug;
-               
+
                // Label on the import button. Changes as we step through the 
import process.
                protected $import_button_label;
-               
+
                protected $defalt_values;
-               
+
                public function __construct()
                {
                        if ( !$GLOBALS['phpgw']->acl->check('run', 
phpgwapi_acl::READ, 'admin')
@@ -47,8 +47,8 @@
                        $this->db           = & $GLOBALS['phpgw']->db;
                        $this->table            = phpgw::get_var('table');
                }
-               
 
+
                /**
                 * Public method. 
                 * 
@@ -85,7 +85,7 @@
                                        }
 
                                        $file = PHPGW_SERVER_ROOT . 
"/property/inc/import/{$GLOBALS['phpgw_info']['user']['domain']}/{$this->conv_type}";
-       
+
                                        if ( is_file($file) )
                                        {
                                                require_once $file;
@@ -96,7 +96,8 @@
                                        require_once PHPGW_SERVER_ROOT . 
"/property/inc/import/import_update_generic.php";
                                }
 
-                               $this->import_conversion = new 
import_conversion;
+                               $location_id = phpgw::get_var('location_id', 
'int');
+                               $this->import_conversion = new 
import_conversion($location_id);
                                $this->debug = phpgw::get_var('debug', 'bool');
                                $this->import_conversion->debug = $this->debug;
 
@@ -110,7 +111,7 @@
                                                'name'  => 
$_FILES['file']['tmp_name'],
                                                'type'  => 
$_FILES['file']['type']
                                        );
-                                       
+
                                }
                                else
                                {
@@ -140,7 +141,7 @@
                                                        $valid_type = true;
                                                        break;
                                        }
-                                       
+
                                        if($valid_type)
                                        {
                                                $result = $this->import();
@@ -158,7 +159,7 @@
                                $difference = ($end_time - $start_time) / 60;
                                $end = date("G:i:s",$end_time);
                                echo "<h3>Import ended at: {$end}. Import 
lasted {$difference} minutes.";
-                               
+
                                if ($this->errors)
                                { 
                                        echo "<ul>";
@@ -166,10 +167,10 @@
                                        {
                                                echo '<li class="error">Error: 
' . $error . '</li>';
                                        }
-               
+
                                        echo "</ul>";
                                }
-               
+
                                if ($this->warnings)
                                { 
                                        echo "<ul>";
@@ -179,11 +180,11 @@
                                        }
                                        echo "</ul>";
                                }
-               
+
                                if ($this->messages)
                                {
                                        echo "<ul>";
-               
+
                                        foreach ($this->messages as $message)
                                        {
                                                echo '<li class="info">Message: 
' . $message . '</li>';
@@ -201,7 +202,7 @@
 
                                phpgwapi_cache::session_clear('property', 
'import_message');
                                $conv_list                      = 
$this->get_import_conv($import_settings['conv_type']);
-                               
+
                                $conv_option = '<option value="">' . lang('none 
selected') . '</option>' . "\n";
                                foreach ( $conv_list as $conv)
                                {
@@ -214,9 +215,13 @@
                                        $conv_option .=  <<<HTML
                                        <option 
value='{$conv['id']}'{$selected}>{$conv['name']}</option>
 HTML;
-                               }                       
+                               }
 
-                               $tables = $this->db->table_names();
+               //              $tables = $this->db->table_names();
+                               $tables = array
+                               (
+                                       'fm_vendor'
+                               );
                                sort($tables);
 
                                $table_option = '<option value="">' . 
lang('none selected') . '</option>' . "\n";
@@ -226,9 +231,24 @@
                                        $table_option .=  <<<HTML
                                        <option 
value='{$table}'{$selected}>{$table}</option>
 HTML;
-                               }                       
+                               }
 
+                               $entity                 = 
CreateObject('property.soadmin_entity');
+                               $entity_list    = $entity->read(array('allrows' 
=> true));
+                               $category_option = '<option value="">' . 
lang('none selected') . '</option>' . "\n";
+                               foreach($entity_list as $entry)
+                               {
+                                       $category_list = 
$entity->read_category_tree2($entry['id']);
 
+                                       foreach($category_list as $category)
+                                       {
+                                               $selected = 
$import_settings['category'] == $category ? 'selected =  "selected"' : '';
+                                               $category_option .=  <<<HTML
+                                               <option 
value="{$category['location_id']}"{$selected}>{$category['name']}</option>
+HTML;
+                                       }
+                               }
+
                                $home =  $GLOBALS['phpgw']->link('/home.php');
                                $action =  
$GLOBALS['phpgw']->link('/index.php', 
array('menuaction'=>'property.uiimport.index'));
 
@@ -258,6 +278,12 @@
                                                        {$table_option}
                                                </select>
                                                </p>
+                                               <p>
+                                                       <label 
for="category">Choose category:</label>
+                                                       <select 
name="location_id" id="category">
+                                                       {$category_option}
+                                               </select>
+                                               </p>
 
                                                <p>
                                                        <label 
for="debug">Debug:</label>
@@ -273,7 +299,7 @@
                                echo $html;
                        }
                }
-               
+
                /**
                 * Import Facilit data to Portico Estate's rental module
                 * The function assumes CSV files have been uploaded to a 
location on the server reachable by the
@@ -286,7 +312,7 @@
                public function import()
                {
                        $this->steps++;
-                       
+
                        /* Import logic:
                         * 
                         * 1. Do step logic if the session variable is not set
@@ -300,7 +326,7 @@
                        $this->log_messages($this->steps);
                        return $this->steps;
                }
-               
+
                protected function import_data()
                {
                        $metadata = array();
@@ -312,25 +338,44 @@
                                {
                                        _debug_array($metadata);
                                }
-                               
+
+
                                foreach($this->fields as $field)
                                {
-                                       if(!isset($metadata[$field]))
+                                       if($field && !isset($metadata[$field]))
                                        {
-                                               throw new Exception("Feltet 
'{$field}' finnes ikke i tabellen '{$this->table}'");
+                                               $this->messages[] = "Feltet 
'{$field}' finnes ikke i tabellen '{$this->table}'";
                                        }
                                }
-//                             _debug_array($metadata);die();
+
+                               
$this->import_conversion->set_table($this->table);
+                               
$this->import_conversion->set_metadata($metadata);
                        }
-                       
-                       $this->import_conversion->table = $this->table;
-                       $this->import_conversion->fields = $this->fields;
-                       $this->import_conversion->metadata = $metadata;
 
+                       if($this->fields)
+                       {
+                               $found_field = false;
+
+                               foreach($this->fields as $field)
+                               {
+                                       if($field && !$found_field)
+                                       {
+                                               $found_field = true;
+                                       }
+                               }
+
+                               if(!$found_field)
+                               {
+                                       throw new Exception("Felter er ikke 
definert");
+                               }
+                               $this->import_conversion->fields = 
$this->fields;
+                       }
+
+
                        $start_time = time();
-                       
+
                        $datalines = $this->csvdata;
-                       
+
                        $ok = true;
                        $_ok = false;
                        $this->db->transaction_begin();
@@ -338,12 +383,14 @@
                        //Do your magic...
                        foreach ($datalines as $data)
                        {
-                               if(!$_ok = $this->import_conversion->add($data))
+                               $_ok = $this->import_conversion->add($data);
+
+                               if( !$_ok )
                                {
                                        $ok = false;
                                }
                        }
-                       
+
                        if($ok)
                        {
                                $this->messages[] = "Imported data. (" . 
(time() - $start_time) . " seconds)";
@@ -371,22 +418,22 @@
                {
                        // Open the csv file
                        $handle = fopen($path, "r");
-                       
+
                        if ($skipfirstline)
                        {
                                // 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)
                        {
                                $result[] = $data;
                        }
-                       
+
                        fclose($handle);
-                       
+
                        $this->messages[] = "Read '{$path}' file in " . (time() 
- $start_time) . " seconds";
                        $this->messages[] = "'{$path}' contained " . 
count($result) . " lines";
 
@@ -409,7 +456,7 @@
                        {
                                $this->fields = array_values($data[1]);
                        }
-                       
+
                        $rows = count($data)+1;
 
                        for ($i=$start; $i<$rows; $i++ )
@@ -430,7 +477,7 @@
                        return $result;
                }
 
-               
+
                /**
                 * Read the next line from the given file handle and parse it 
to CSV according to the rules set up
                 * in the class constants DELIMITER and ENCLOSING.  Returns 
FALSE like getcsv on EOF.
@@ -442,14 +489,14 @@
                {
                        return fgetcsv($handle, 1000, self::DELIMITER, 
self::ENCLOSING);
                }
-               
 
+
                private function log_messages($step)
         {
         //     sort($this->errors);
         //     sort($this->warnings);
         //     sort($this->messages);
-               
+        
             $msgs = array_merge(
                array('----------------Errors--------------------'),
                $this->errors,
@@ -506,7 +553,7 @@
                        }
 
                        $mime_magic = createObject('phpgwapi.mime_magic');
-                       
+
                        $file_list = array();
                        $dir = new DirectoryIterator($dirname); 
                        if ( is_object($dir) )

Modified: trunk/property/inc/import/import_update_generic.php
===================================================================
--- trunk/property/inc/import/import_update_generic.php 2013-11-08 15:09:21 UTC 
(rev 11441)
+++ trunk/property/inc/import/import_update_generic.php 2013-11-10 18:46:33 UTC 
(rev 11442)
@@ -6,19 +6,96 @@
                public $warnings = array();
                public $errors = array();
                public $debug = true;
-               public $fields = array();
-               public $table;
-               public $metadata = array();
+               protected $is_eav;
+               protected $location_id;
+               protected $bim_type_id = 0;
+               protected $table;
+               protected $entity_id;
+               protected $cat_id;
+               protected $metadata = array();
 
-               public function __construct()
+               public function __construct($location_id)
                {
+                       $location_id = (int) $location_id;
                        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;
+                       $this->join                     = $this->db->join;
+
+                       if(! $category = 
execMethod('property.soadmin_entity.get_single_category', $location_id ))
+                       {
+                               throw new Exception("Not a valid location for 
{$location_id}");
+                       }
+
+                       $this->is_eav = !!$category['is_eav'];
+                       $this->location_id = $location_id;
+
+                       $this->entity_id = $category['entity_id'];
+                       $this->cat_id = $category['id'];
+
+                       
+                       if ($this->is_eav)
+                       {
+                               $this->table = 'fm_bim_item';
+                               $sql = "SELECT fm_bim_type.id FROM fm_bim_type 
WHERE location_id = {$location_id}";
+                               $this->db->query($sql,__LINE__,__FILE__);
+                               $this->db->next_record();
+                               $this->bim_type_id = $this->db->f('id');
+                               $custom                 = 
createObject('property.custom_fields');
+                               $attributes     = $custom->find2($location_id, 
0, '', 'ASC', 'attrib_sort', true, true);
+                               $this->metadata['id'] = array('primary_key' => 
true);
+                               $this->metadata['location_id'] = 
array('primary_key' => true);
+                               $this->metadata['model'] = array();
+                               $this->metadata['p_location_id'] = array();
+                               $this->metadata['p_id'] = array();
+                               $this->metadata['location_code'] = array();
+                               $this->metadata['loc1'] = array();
+                               $this->metadata['address'] = array();
+                               $this->metadata['entry_date'] = array();
+                               $this->metadata['user_id'] = array();
+
+                               foreach($attributes as $attribute)
+                               {
+                                       
$this->metadata[$attribute['column_name']] = array();
+                               }
+
+                       }
+                       else
+                       {
+                               $this->table = 
"fm_entity_{$category['entity_id']}_{$category['id']}";
+                               $this->metadata = 
$this->db->metadata($this->table);
+                       }
+
+
                }
 
+               public function set_table($table)
+               {
+                       $this->table = $table;
+               }
+               public function set_metadata($metadata)
+               {
+                       $this->metadata = $metadata;
+               }
+
                public function add($data)
                {
+                       if ($this->is_eav)
+                       {
+                               $ok = $this->_add_eav($data);
+                       }
+                       else
+                       {
+                               $ok = $this->_add_sql($data);
+                       }
+                       return $ok;
+               }
+               
+               private function _add_eav($data)
+               {
+                       static $count_records = 0;
+// -------- produce data_set
+
                        $error = false;
                        $table = $this->table;
                        $fields =  $this->fields;
@@ -28,6 +105,199 @@
                                throw new Exception("Tabell er ikke angitt");
                        }
 
+                       $remove_keys = array();
+                       foreach($this->metadata as $key => $info)
+                       {
+                               if(isset($info['primary_key']) && 
$info['primary_key'])
+                               {
+                                       $_value = $data[array_search($key, 
$fields)];
+                                       if(!array_search($key, $fields) || 
!$_value)
+                                       {
+                                               if(array_search($key, $fields) 
=== 0 && $_value)
+                                               {
+                                                       break;
+                                               }
+                                               if($count_records === 0)// 
first one
+                                               {
+                                                       throw new 
Exception("Fant ikke verdi for feltet 'primary key' $key");
+                                               }
+                                               else
+                                               {
+                                                       $found_data = false;
+                                                       foreach($data as $value)
+                                                       {
+                                                               if($value && 
!$found_data)
+                                                               {
+                                                                       
$found_data = true;
+                                                               }
+                                                       }
+                                                       if($found_data)
+                                                       {
+                                                               throw new 
Exception("Fant ikke verdi for feltet 'primary key' $key");
+                                                       }
+                                                       else
+                                                       {
+                                                               
$this->warnings[] = "Fant ikke verdi for feltet 'primary key' $key";
+                                                               return true;
+                                                       }
+                                               }
+                                       }
+                                       $remove_keys[] = $key;
+                               }
+                       }
+                       $count_records ++;
+                       unset($key);
+                       unset($info);
+                       unset($_value);
+
+                       $value_set = array();
+                       foreach ($fields as $key => $field)
+                       {
+                               if(isset($this->metadata[$field]))
+                               {
+                                       $value_set[$field]      = 
$this->validate_value($data[$key], $field);
+                               }
+                       }
+
+                       $id = (int) $value_set['id'];
+                       $filtermethod = "location_id = {$this->location_id} AND 
id = {$id}";
+
+//---------produce data_set
+
+                       $location_id = $this->location_id;
+                       $sql = "SELECT fm_bim_item.id FROM fm_bim_item WHERE 
{$filtermethod}";
+                       $this->db->query($sql,__LINE__,__FILE__);
+
+                       $type = (int)$this->bim_type_id;
+
+                       $location_name = 
"_entity_{$this->entity_id}_{$this->cat_id}";
+               
+                       if($this->db->next_record())
+                       {
+                               $this->warnings[] = "ID finnes fra før: {$id}, 
oppdaterer";
+
+                               foreach ($remove_keys as $remove_key)
+                               {
+                                       unset($value_set[$remove_key]);         
        
+                               }
+
+                               phpgw::import_class('phpgwapi.xmlhelper');
+
+                               $xmldata = 
phpgwapi_xmlhelper::toXML($value_set, $location_name);
+                               $doc = new DOMDocument;
+                               $doc->preserveWhiteSpace = true;
+                               $doc->loadXML( $xmldata );
+                               $domElement = 
$doc->getElementsByTagName($location_name)->item(0);
+                               $domAttribute = 
$doc->createAttribute('appname');
+                               $domAttribute->value = 'property';
+
+                               // Don't forget to append it to the element
+                               $domElement->appendChild($domAttribute);
+
+                               // Append it to the document itself
+                               $doc->appendChild($domElement);
+
+                               $doc->formatOutput = true;
+                               $xml = $doc->saveXML();
+
+                               $_value_set = array
+                               (
+                                       'xml_representation'    => 
$this->db->db_addslashes($xml),
+                                       'p_location_id'                 => 
isset($value_set['p_location_id']) && $value_set['p_location_id'] ? 
$value_set['p_location_id'] : '',
+                                       'p_id'                                  
=> isset($value_set['p_id']) && $value_set['p_id'] ? $value_set['p_id'] : '',
+                                       'location_code'                 => 
$value_set['location_code'],
+                                       'loc1'                                  
=> $value_set['loc1'],
+                                       'address'                               
=> $value_set['address'],
+                               );
+
+                               $_value_set     = 
$this->db->validate_update($_value_set);
+
+                               $sql = "UPDATE fm_bim_item SET $_value_set 
WHERE id = $id AND location_id = {$location_id}";
+                       }
+                       else
+                       {
+                               $this->warnings[] = "Denne er ny: {$id}, legger 
til";
+
+                               phpgw::import_class('phpgwapi.xmlhelper');
+                               $xmldata = 
phpgwapi_xmlhelper::toXML($value_set, $location_name);
+                               $doc = new DOMDocument;
+                               $doc->preserveWhiteSpace = true;
+                               $doc->loadXML( $xmldata );
+                               $domElement = 
$doc->getElementsByTagName($location_name)->item(0);
+                               $domAttribute = 
$doc->createAttribute('appname');
+                               $domAttribute->value = 'property';
+
+                               // Don't forget to append it to the element
+                               $domElement->appendChild($domAttribute);
+
+                               // Append it to the document itself
+                               $doc->appendChild($domElement);
+                               $doc->formatOutput = true;
+                       
+                               $xml = $doc->saveXML();
+
+                               if (function_exists('com_create_guid') === true)
+                               {
+                                       $guid = trim(com_create_guid(), '{}');
+                               }
+                               else
+                               {
+                                       $guid = 
sprintf('%04X%04X-%04X-%04X-%04X-%04X%04X%04X', mt_rand(0, 65535), mt_rand(0, 
65535), mt_rand(0, 65535), mt_rand(16384, 20479), mt_rand(32768, 49151), 
mt_rand(0, 65535), mt_rand(0, 65535), mt_rand(0, 65535));
+                               }
+
+                               $values_insert = array
+                               (
+                                       'id'                                    
=> $id,
+                                       'type'                                  
=> $type,
+                                       'location_id'                   => 
$location_id,
+                                       'guid'                                  
=> $guid,
+                                       'xml_representation'    => 
$this->db->db_addslashes($xml),
+                                       'model'                                 
=> 0,
+                                       'p_location_id'                 => 
isset($value_set['p_location_id']) && $value_set['p_location_id'] ? 
$value_set['p_location_id'] : '',
+                                       'p_id'                                  
=> isset($value_set['p_id']) && $value_set['p_id'] ? $value_set['p_id'] : '',
+                                       'location_code'                 => 
$value_set['location_code'],
+                                       'loc1'                                  
=> $value_set['loc1'],
+                                       'address'                               
=> $value_set['address'],
+                                       'entry_date'                    => 
time(),
+                                       'user_id'                               
=> $this->account
+                               );
+
+                               $sql = "INSERT INTO fm_bim_item (" . 
implode(',',array_keys($values_insert)) . ') VALUES ('
+                                . 
$this->db->validate_insert(array_values($values_insert)) . ')';
+                       }
+
+                       $ok = false;
+                       if($this->debug)
+                       {
+                               _debug_array($sql);
+                       }
+                       else
+                       {
+                               $ok = $this->db->query($sql,__LINE__,__FILE__);
+                       }
+
+                       if($ok)
+                       {
+                               $this->messages[] = "Successfully imported 
record: id ({$id})";
+                       }
+                       else
+                       {
+                               $this->errors[] = "Error importing record: id 
({$id})";
+                       }
+                       return $ok;
+               }
+
+               private function _add_sql($data)
+               {
+                       $error = false;
+                       $table = $this->table;
+                       $fields =  $this->fields;
+
+                       if(!$table)
+                       {
+                               throw new Exception("Tabell er ikke angitt");
+                       }
+
                        if(!$fields)
                        {
                                throw new Exception("Felter er ikke definert");
@@ -57,7 +327,10 @@
                        $value_set = array();
                        foreach ($fields as $key => $field)
                        {
-                               $value_set[$field]      = 
$this->validate_value($data[$key], $field);
+                               if(isset($this->metadata[$field]))
+                               {
+                                       $value_set[$field]      = 
$this->validate_value($data[$key], $field);
+                               }
                        }
 
                        $this->db->query("SELECT count(*) as cnt FROM {$table} 
WHERE {$filtermethod}",__LINE__,__FILE__);
@@ -91,35 +364,26 @@
                                $request_ok = 
$this->db->query($sql,__LINE__,__FILE__);
                        }
 
-                       if(!$error)
+                       if(!$request_ok)
                        {
-                               $this->messages[] = "Successfully updated 
entry: id ($filtermethod)";
+                               $this->messages[] = "Successfully imported 
record: id ({$id})";
                                $ok = true;
                        }
                        else
                        {
-                               $this->errors[] = "Error updating location: id 
({$filtermethod})";
+                               $this->errors[] = "Error importing record: id 
({$id})";
                                $ok = false;
                        }
                        return $ok;
                }
 
-
-               /**
-                * Test a value for null according to several formats that can 
exist in the export.
-                * Returns true if the value is null according to these rules, 
false otherwise.
-                * 
-                * @param string $value The value to test
-                * @return bool
-                */
-               protected function is_null($value)
-               {
-                       return ((trim($value) == "") || ($data == "<NULL>") || 
($data == "''"));
-               }
-
                protected function validate_value($value,$field)
                {
-                       $datatype = $this->metadata[$field]->type;
+                       if($value == '#N/A')
+                       {
+                               return '';
+                       }
+                       $datatype = $this->metadata[$field]['type'];
                        switch ($datatype)
                        {
                                case 'char':




reply via email to

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