private function _add_eav($data)
 {
     static $count_records = 0;
     // -------- produce data_set
     $error = false;
     $table = $this->table;
     $fields = $this->fields;
     if (!$table) {
         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) {
                     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;
 }
Пример #2
0
 protected function _edit_eav($data = array(), $location_id, $location_name, $id)
 {
     $location_id = (int) $location_id;
     $id = (int) $id;
     $this->db->query("SELECT id as type FROM fm_bim_type WHERE location_id = {$location_id}", __LINE__, __FILE__);
     $this->db->next_record();
     $type = (int) $this->db->f('type');
     $location_name = str_replace('.', '_', $location_name);
     phpgw::import_class('phpgwapi.xmlhelper');
     /*
     			// FIXME: not working
     			if($data)
     			{
     				foreach ($data as $key => &$value)
     				{
     					if(preg_match('/[<>&]/', $value)) // or use CDATA..
     					{
     						$value =  str_ireplace ( array('&lt;','&gt;','<','>') , array('[',']', '[', ']') , $value);
     					}
     				}
     			}
     */
     $xmldata = phpgwapi_xmlhelper::toXML($data, $location_name);
     $doc = new DOMDocument();
     $doc->preserveWhiteSpace = true;
     $doc->loadXML($xmldata);
     $domElement = $doc->getElementsByTagName($location_name)->item(0);
     $domAttribute = $doc->createAttribute('appname');
     $domAttribute->value = $this->type_app[$this->type];
     // 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();
     //			_debug_array($xml);
     $value_set = array('xml_representation' => $this->db->db_addslashes($xml), 'p_location_id' => isset($data['p_location_id']) && $data['p_location_id'] ? $data['p_location_id'] : '', 'p_id' => isset($data['p_id']) && $data['p_id'] ? $data['p_id'] : '', 'location_code' => $data['location_code'], 'loc1' => $data['loc1'], 'address' => $data['address'], 'org_unit_id' => $data['org_unit_id'], 'entity_group_id' => $data['entity_group_id'], 'modified_by' => $this->account, 'modified_on' => time());
     $value_set = $this->db->validate_update($value_set);
     return $this->db->query("UPDATE fm_bim_item SET {$value_set} WHERE id = {$id} AND type = {$type}", __LINE__, __FILE__);
 }
 /**
  * Reduserer fra 684 til 265 tabeller for Nordlandssykehuset
  *
  * @return bool true on success
  */
 function convert_to_eav()
 {
     //			die('vent litt med denne');
     phpgw::import_class('phpgwapi.xmlhelper');
     $this->type = 'entity';
     $entity_list = $this->read(array('allrows' => true));
     $this->db->transaction_begin();
     foreach ($entity_list as $entry) {
         $cat_list = $this->read_category(array('allrows' => true, 'entity_id' => $entry['id']));
         foreach ($cat_list as $category) {
             if (!$category['is_eav']) {
                 $location_id = $GLOBALS['phpgw']->locations->get_id('property', ".{$this->type}.{$category['entity_id']}.{$category['id']}");
                 $values_insert = array('location_id' => $location_id, 'name' => ".{$this->type}.{$category['entity_id']}.{$category['id']}::{$category['name']}", 'description' => $category['descr'], 'is_ifc' => 0);
                 $this->db->query('INSERT INTO fm_bim_type (' . implode(',', array_keys($values_insert)) . ') VALUES (' . $this->db->validate_insert(array_values($values_insert)) . ')', __LINE__, __FILE__);
                 $sql = "UPDATE fm_{$this->type}_category SET is_eav = 1 WHERE entity_id =" . (int) $category['entity_id'] . ' AND id = ' . (int) $category['id'];
                 $this->db->query($sql, __LINE__, __FILE__);
                 $sql = "UPDATE phpgw_locations SET c_attrib_table = NULL WHERE location_id = {$location_id}";
                 $this->db->query($sql, __LINE__, __FILE__);
                 $type = $this->db->get_last_insert_id('fm_bim_type', 'id');
                 $sql = "SELECT * FROM fm_{$this->type}_{$category['entity_id']}_{$category['id']}";
                 $this->db->query($sql, __LINE__, __FILE__);
                 while ($this->db->next_record()) {
                     $data = $this->db->Record;
                     $xmldata = phpgwapi_xmlhelper::toXML($data, "_{$this->type}_{$category['entity_id']}_{$category['id']}");
                     $doc = new DOMDocument('1.0', 'utf-8');
                     $doc->loadXML($xmldata);
                     $domElement = $doc->getElementsByTagName("_{$this->type}_{$category['entity_id']}_{$category['id']}")->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->preserveWhiteSpace = true;
                     $doc->formatOutput = true;
                     $xml = $doc->saveXML();
                     $p_location_id = '';
                     if ($data['p_cat_id']) {
                         $p_location_id = $GLOBALS['phpgw']->locations->get_id('property', ".{$this->type}.{$data['p_entity_id']}.{$data['p_cat_id']}");
                     }
                     $p_id = '';
                     if ($data['p_num']) {
                         $p_id = (int) ltrim($data['p_num'], $category['prefix']);
                     }
                     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' => $data['id'], 'location_id' => $location_id, 'type' => $type, 'guid' => $guid, 'xml_representation' => $this->db->db_addslashes($xml), 'model' => 0, 'p_location_id' => $p_location_id, 'p_id' => isset($data['p_num']) && $data['p_num'] ? (int) $data['p_num'] : '', 'location_code' => $data['location_code'], 'loc1' => $data['loc1'], 'address' => $data['address'], 'entry_date' => $data['entry_date'], 'user_id' => $data['user_id']);
                     $this->db->query("INSERT INTO fm_bim_item (" . implode(',', array_keys($values_insert)) . ') VALUES (' . $this->db->validate_insert(array_values($values_insert)) . ')', __LINE__, __FILE__);
                 }
                 $sql = "DROP TABLE fm_{$this->type}_{$category['entity_id']}_{$category['id']}";
                 _debug_array($sql);
                 $this->db->query($sql, __LINE__, __FILE__);
             }
         }
     }
     return $this->db->transaction_commit();
 }
 function xml_parse()
 {
     if (strlen($this->xmldata) == 0) {
         $xmldata = $this->vars;
         /* auto generate xml based on vars */
         foreach ($this->xmlvars as $key => $value) {
             $xmldata[$key] = $value;
         }
         //	$this->xmldata = var2xml('PHPGW', $xmldata);
         //  use simplexml - it's faster.
         $this->xmldata = phpgwapi_xmlhelper::toXML($xmldata, 'PHPGW');
     }
     $debug = false;
     //	$debug = true;
     if ($debug) {
         //$this->xmldata = str_replace("\n",'' ,$this->xmldata);
         $doc = new DOMDocument();
         $doc->preserveWhiteSpace = true;
         $doc->loadXML($this->xmldata);
         $doc->formatOutput = true;
         $xml = $doc->saveXML();
         unset($doc);
         echo "<textarea cols='200' rows='20'>";
         echo $xml;
         echo "</textarea><br>";
     }
     return $this->xmldata;
 }
 function export_ticket($ticket)
 {
     //			_debug_array($ticket);
     //_debug_array($receipt);
     $export_values = array();
     $sql = 'SELECT unitid FROM fm_catch_1_1 WHERE user_ = ' . (int) $ticket['assignedto'] . ' ORDER BY id ASC';
     $this->db->query($sql, __LINE__, __FILE__);
     $this->db->next_record();
     $export_values['unitid'] = $this->db->f('unitid', true);
     $solocation = CreateObject('property.solocation');
     $location = $solocation->read_single($ticket['location_code']);
     $values = $this->so->read_single($ticket['id']);
     //_debug_array($values);
     $export_values['melding_id'] = $ticket['id'];
     $export_values['eiendom_navn'] = $location['loc1_name'];
     $export_values['eiendomid'] = $location['loc1'];
     $export_values['byggid'] = $location['loc2'];
     $export_values['byggnavn'] = $location['loc2_name'];
     $export_values['etasjeid'] = $location['loc3'];
     $export_values['etasjenavn'] = $location['loc3_name'];
     $export_values['bruksenhetid'] = $location['loc4'];
     $export_values['bruksenhet_navn'] = $location['loc4_name'];
     $export_values['rom_id_navn'] = $location['rom_nr_id'];
     $export_values['rom_navn'] = $location['loc5_name'];
     $export_values['romid'] = $location['loc5'];
     $export_values['prioritet'] = $ticket['priority'];
     $export_values['overskrift_melding'] = $ticket['subject'];
     $export_values['detaljer_melding'] = "{$values['user_name']}:: {$values['details']}";
     $export_values['meldingskategori'] = $ticket['priority'];
     $export_values['cat_id'] = $ticket['cat_id'];
     $export_values['kommentarer'] = '';
     $export_values['status_melding'] = $this->status_text[$ticket['status']];
     $export_values['status'] = $ticket['status'];
     $export_values['egne_timer'] = $ticket['billable_hours'];
     $additional_notes = $this->read_additional_notes($ticket['id']);
     foreach ($additional_notes as $additional_note) {
         $export_values['detaljer_melding'] .= "\n{$additional_note['value_user']}::{$additional_note['value_note']}";
     }
     //_debug_array($additional_notes); die();
     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));
     }
     phpgw::import_class('phpgwapi.xmlhelper');
     $xmldata = phpgwapi_xmlhelper::toXML($export_values, 'PPCC');
     $doc = new DOMDocument();
     $doc->preserveWhiteSpace = true;
     $doc->loadXML($xmldata);
     $domElement = $doc->getElementsByTagName('PPCC')->item(0);
     $domAttribute = $doc->createAttribute('UUID');
     $domAttribute->value = $guid;
     // 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();
     //			echo $xml;
     //			_debug_array($this->config);
     $filename = "{$GLOBALS['phpgw_info']['server']['temp_dir']}/{$guid}.xml";
     $fp = fopen($filename, "wb");
     fwrite($fp, $xml);
     if (fclose($fp)) {
         $this->transfer($xml, $filename);
     }
     //_debug_array($filename);
     die;
 }