Index: class.contacts_sql.inc.php
===================================================================
RCS file: /cvsroot/phpgwapi/phpgwapi/inc/class.contacts_sql.inc.php,v
retrieving revision 1.17.2.2.2.69
diff -u -r1.17.2.2.2.69 class.contacts_sql.inc.php
--- class.contacts_sql.inc.php	1 Sep 2004 13:06:08 -0000	1.17.2.2.2.69
+++ class.contacts_sql.inc.php	13 Sep 2004 21:23:22 -0000
@@ -474,8 +474,8 @@
 						'per_initials',
 						'per_sound',
 						'per_active',
-						'per_createon',
-						'per_createby',
+						'per_creaton',
+						'per_creatby',
 						'per_modby',
 						'per_modon',
 						'key_addr_id',
@@ -964,7 +964,7 @@
 		*/
 		function delete_single_extra_field($id,$field_name)
 		{
-			$this->delete_others($cid);
+			$this->delete_others($cid, PHPGW_SQL_RUN_SQL);
 			return array();
 		}
 
@@ -1774,7 +1774,7 @@
 			$addr_keys = array('work', 'home', 'other', 'other', 'other', 'other');
 
 			list($kind, $key, $name) = explode('_', $field, 3);
-			echo "$kind : $key : $name<br />\n";
+			//echo "$kind : $key : $name<br />\n";
 			switch ($kind)
 			{
 			case 'addr':
@@ -1862,8 +1862,8 @@
 				$contact['initials']		= $record['per_initials'];
 				$contact['sound']		= $record['per_sound'];
 				$contact['active']		= $record['per_active'];
-				$contact['createon']		= $record['per_createon'];
-				$contact['createby']		= $record['per_createby'];
+				$contact['createon']		= $record['per_creaton'];
+				$contact['createby']		= $record['per_creatby'];
 				$contact['modby']		= $record['per_modby'];
 				$contact['modon']		= $record['per_modon'];
 				$contact['account_id']		= $record['account_id'];
@@ -2141,8 +2141,16 @@
 		*/
 		function edit_other($id, $data, $action=PHPGW_SQL_RETURN_SQL)
 		{
-			$comm = CreateObject('phpgwapi.contact_others');
-			return $comm->update($data, sql_criteria::_equal('other_id',sql::integer($id)), $action);
+			$other = CreateObject('phpgwapi.contact_others');
+			if(!isset($data['other_modon']))
+			{
+				$data['other_modon'] = $this->get_mkdate();
+			}
+			if(!isset($data['other_modby']))
+			{
+				$data['other_modby'] = $this->get_user_id();
+			}
+			return $other->update($data, sql_criteria::_equal('other_id',sql::integer($id)), $action);
 		}
 
 		/**
@@ -2156,6 +2164,14 @@
 		function edit_comms_by_contact($id, $data, $action=PHPGW_SQL_RETURN_SQL)
 		{
 			$comm = CreateObject('phpgwapi.contact_comm');
+			if(!isset($data['comm_modon']))
+			{
+				$data['comm_modon'] = $this->get_mkdate();
+			}
+			if(!isset($data['comm_modby']))
+			{
+				$data['comm_modby'] = $this->get_user_id();
+			}
 			return $comm->update($data, sql_criteria::_equal('contact_id',sql::integer($id)), $action);
 		}
 
@@ -2356,9 +2372,9 @@
 				$this->lock_table($this->org->table);
 			}
 			$this->lock_table($this->org->table);
-			$principal['org_creaton'] = $this->get_mkdate();
+			$principal['org_creaton'] = (isset($principal['org_creaton'])?$principal['org_creaton']:$this->get_mkdate());
 			$principal['org_creatby'] = $this->get_user_id();
-			$principal['org_modon'] = $this->get_mkdate();
+			$principal['org_modon'] = (isset($principal['org_modon'])?$principal['org_modon']:$this->get_mkdate());
 			$principal['org_modby'] = $this->get_user_id();
 
 			$execute = $this->_add($principal,'org','org_id',$cid, $action);
@@ -2382,9 +2398,9 @@
 				$this->lock_table($this->person->table);
 			}
 			$this->lock_table($this->person->table);
-			$principal['per_creaton'] = $this->get_mkdate();
+			$principal['per_creaton'] = (isset($principal['per_creaton'])?$principal['per_creaton']:$this->get_mkdate());
 			$principal['per_creatby'] = $this->get_user_id();
-			$principal['per_modon'] = $this->get_mkdate();
+			$principal['per_modon'] = (isset($principal['per_modon'])?$principal['per_modon']:$this->get_mkdate());;
 			$principal['per_modby'] = $this->get_user_id();
 
 			$execute = $this->_add($principal,'person','person_id',$cid, $action);
@@ -2501,10 +2517,11 @@
 			{
 				$location['addr_preferred'] = 'N';
 			}
+
+			$location['addr_creaton'] = (isset($location['addr_creaton'])?$location['addr_creaton']:$this->get_mkdate());
 			$location['addr_creatby'] = $this->get_user_id();
+			$location['addr_modon'] = (isset($location['addr_modon'])?$location['addr_modon']:$this->get_mkdate());;
 			$location['addr_modby'] = $this->get_user_id();
-			$location['addr_creaton'] = $this->get_mkdate();
-			$location['addr_modon'] = $this->get_mkdate();
 
 			unset($location['key_addr_id']);
 
@@ -2527,10 +2544,10 @@
 				$this->lock_table($this->comm->table);
 			}
 
+			$comm['comm_creaton'] = (isset($comm['comm_creaton'])?$comm['comm_creaton']:$this->get_mkdate());
 			$comm['comm_creatby'] = $this->get_user_id();
+			$comm['comm_modon'] = (isset($comm['comm_modon'])?$comm['comm_modon']:$this->get_mkdate());;
 			$comm['comm_modby'] = $this->get_user_id();
-			$comm['comm_creaton'] = $this->get_mkdate();
-			$comm['comm_modon'] = $this->get_mkdate();
 
 			unset($comm['key_comm_id']);
 
@@ -2553,10 +2570,10 @@
 				$this->lock_table($this->note->table);
 			}
 
+			$note['note_creaton'] = (isset($note['note_creaton'])?$note['note_creaton']:$this->get_mkdate());
 			$note['note_creatby'] = $this->get_user_id();
+			$note['note_modon'] = (isset($note['note_modon'])?$note['note_modon']:$this->get_mkdate());;
 			$note['note_modby'] = $this->get_user_id();
-			$note['note_creaton'] = $this->get_mkdate();
-			$note['note_modon'] = $this->get_mkdate();
 
 			unset($comm['key_note_id']);
 
@@ -3790,18 +3807,22 @@
 		*/
 		function display_name($field)
 		{
-			if($this->contact_fields['showable'])
+			if($this->contact_fields['showable'][$field])
 			{
 				return $GLOBALS['phpgw']->lang($this->contact_fields['showable'][$field]);
 			}
-			if($this->contact_fields['retreivable'])
+			if($this->contact_fields['retreivable'][$field])
 			{
 				return $GLOBALS['phpgw']->lang($this->contact_fields['retrievable'][$field]);
 			}
-			if($this->contact_fields['catalogs'])
+			if($this->contact_fields['catalogs'][$field])
 			{
 				return $GLOBALS['phpgw']->lang($this->contact_fields['catalogs'][$field]);
 			}
+			else
+			{
+				return $field;
+			}
 		}
 
 		/**
@@ -4018,6 +4039,7 @@
 			$attributes['objectClass'][] = 'inetOrgPerson';
 			$attributes['cn'][]          = utf8_encode($person[0]['per_full_name']) ? utf8_encode($person[0]['per_full_name']) : ' ';
 			$attributes['sn'][]          = utf8_encode($person[0]['per_last_name']) ? utf8_encode($person[0]['per_last_name']) : ' ';
+
 			/* Optional attributes */
  			$attributes['uid'][]         = utf8_encode($uid);
 			if($person[0]['org_name'])
@@ -4144,7 +4166,7 @@
 			if((isset($fields['first_name']) && !isset($fields['org_name'])) || $type)
 			{
 				$contact['contact_id']        = isset($fields['contact_id'])        ? $fields['contact_id']        : '';
-				$contact['access']            = isset($fields['access'])            ? $fields['access']            : '';
+				$contact['access']            = isset($fields['access'])            ? $fields['access']            : 'private';
 				$contact['owner']             = isset($fields['owner'])             ? $fields['owner']             : $GLOBALS['phpgw_info']['user']['account_id'];
 				$contact['per_first_name']    = isset($fields['first_name'])        ? $fields['first_name']        : '';
 				$contact['per_last_name']     = isset($fields['last_name'])         ? $fields['last_name']         : '';
@@ -4161,17 +4183,8 @@
 				$contact['preferred_org']     = isset($fields['preferred_org'])     ? $fields['preferred_org']     : '';
 				$contact['preferred_address'] = isset($fields['preferred_address']) ? $fields['preferred_address'] : '';
 				$contact['relations']         = isset($fields['organizations'])     ? $fields['organizations']     : array();
-
-				if ($contact['preferred_org'] != '')
-				{
-					$this->request(array('contact_id'));
-					$this->criteria(array('org_name' => $contact['preferred_org']));
-					$result = $this->get_query(PHPGW_SQL_RUN_SQL, __LINE__, __FILE__);
-					if ($result && isset($result[0]))
-					{
-						$contact['relations'][] = $result[0]['contact_id'];
-					}
-				}
+				$contact['per_creaton']       = isset($fields['createon'])          ? $fields['createon']          : null;
+				$contact['per_modon']         = isset($fields['modon'])             ? $fields['modon']             : null;
 
 				unset(	$fields['contact_id'],
 					$fields['first_name'],
@@ -4192,27 +4205,30 @@
 					$fields['access'],
 					$fields['full_name'],
 					$fields['owner'],
-					$fields['createon'],
-					$fields['createby'],
-					$fields['modon'],
-					$fields['modby'],
 					$fields['account_id'],
 					$fields['org_name']);
 			}
 			else
 			{
+				$contact['access']     = isset($fields['access'])   ? $fields['access']   : 'private';
+				$contact['owner']      = isset($fields['owner'])    ? $fields['owner']    : $GLOBALS['phpgw_info']['user']['account_id'];
 				$contact['org_name']   = isset($fields['org_name']) ? $fields['org_name'] : '';
 				$contact['org_active'] = isset($fields['active'])   ? $fields['active']   : '';
 				$contact['org_parent'] = isset($fields['parent'])   ? $fields['parent']   : '';
 				$contact['relations']  = isset($fields['people'])   ? $fields['people']   : '';
+				$contact['org_creaton']= isset($fields['createon']) ? $fields['createon'] : null;
+				$contact['org_creatby']= isset($fields['createby']) ? $fields['createby'] : null;
+				$contact['org_modon']  = isset($fields['modon'])    ? $fields['modon']    : null;
+				$contact['org_modby']  = isset($fields['modby'])    ? $fields['modby']    : null;
 
 				unset(	$fields['org_name'],
 					$fields['active'],
 					$fields['parent'],
-					$fields['people']);
+					$fields['people']
+				);
 			}
 
-			$contact['categories'] = isset($fields['categories']) ? $fields['categories'] : '';
+			$contact['cat_id'] = isset($fields['categories']) ? $fields['categories'] : '';
 			unset($fields['categories'], $fields['access']);
 
 			// Locations info
@@ -4248,6 +4264,10 @@
 					$location['addr_postal_code'] = isset($location_input['postal_code']) ? $location_input['postal_code'] : '';
 					$location['addr_country']     = isset($location_input['country'])     ? $location_input['country']     : '';
 					$location['addr_preferred']   = isset($location_input['preferred'])   ? $location_input['preferred']   : '';
+					$location['addr_createon']   = isset($fields['createon'])   ? $fields['createon']   : '';
+					$location['addr_createby']   = isset($fields['createby'])   ? $fields['createby']   : '';
+					$location['addr_modon']      = isset($fields['modon'])      ? $fields['modon']      : '';
+					$location['addr_modby']      = isset($fields['modby'])      ? $fields['modby']      : '';
 					$locations[] = $location;
 				}
 			}
@@ -4284,6 +4304,11 @@
 						}
 					}
 					$note['note_text'] = $note_input['note'];
+					$note['note_createon'] = isset($fields['createon'])   ? $fields['createon']   : '';
+					$note['note_createby'] = isset($fields['createby'])   ? $fields['createby']   : '';
+					$note['note_modon']    = isset($fields['modon'])      ? $fields['modon']      : '';
+					$note['note_modby']    = isset($fields['modby'])      ? $fields['modby']      : '';
+
 					$notes[] = $note;
 				}
 			}
@@ -4300,6 +4325,10 @@
 					{
 						$comm['comm_descr']	= $description_id;
 						$comm['comm_data']	= $comm_input;
+						$comm['comm_createon'] = isset($fields['createon'])   ? $fields['createon']   : '';
+						$comm['comm_createby'] = isset($fields['createby'])   ? $fields['createby']   : '';
+						$comm['comm_modon']    = isset($fields['modon'])      ? $fields['modon']      : '';
+						$comm['comm_modby']    = isset($fields['modby'])      ? $fields['modby']      : '';
 						$comm_media[]		= $comm;
 					}
 					else
@@ -4320,10 +4349,48 @@
 					$other['other_name']	= $field_name;
 					$other['other_value']	= $field_value;
 					$other['other_owner']	= $GLOBALS['phpgw_info']['user']['account_id'];
+					$other['other_createon'] = isset($fields['createon'])   ? $fields['createon']   : '';
+					$other['other_createby'] = isset($fields['createby'])   ? $fields['createby']   : '';
+					$other['other_modon']    = isset($fields['modon'])      ? $fields['modon']      : '';
+					$other['other_modby']    = isset($fields['modby'])      ? $fields['modby']      : '';
 					$others[]		= $other;
 				}
 			}
 
+
+			if($contact['preferred_org'] != '')
+			{
+				$this->criteria(array('org_name' => $contact['preferred_org'],
+				                      'owner' => $GLOBALS['phpgw_info']['user']['account_id']));
+				$records = $this->get_orgs(array('contact_id'));
+
+				if(is_array($records))
+				{
+					foreach($records as $org)
+					{
+						$contact['relations'][] = $org['contact_id'];
+					}
+				}
+				else
+				{
+					$principal['access']   = $contact['access'];
+					$principal['owner']    = $contact['owner'];
+					$principal['org_name'] = $contact['preferred_org'];
+
+					$principal['org_creaton'] = isset($fields['createon'])   ? $fields['createon']   : '';
+					$principal['org_creatby'] = isset($fields['createby'])   ? $fields['createby']   : '';
+					$principal['org_modon']   = isset($fields['modon'])      ? $fields['modon']      : '';
+					$principal['org_modby']   = isset($fields['modby'])      ? $fields['modby']      : '';
+				}
+			}
+
+			unset(
+				$fields['createon'],
+				$fields['createby'],
+				$fields['modon'],
+				$fields['modby']
+			);
+
 			if (($update == true) && (isset($contact['contact_id']) == true))
 			{
 				$cid = $contact['contact_id'];
@@ -4407,7 +4474,7 @@
 				if(is_array($old_locations) && (count($old_locations) > 0))
 				{
 					// 1. delete old locs
-					$this->delete_locations($cid);
+					$this->delete_locations($cid, PHPGW_SQL_RUN_SQL);
 					if ($GLOBALS['phpgw']->db->Error)
 						$ret_loc['delete_locations'] = false;
 					else
@@ -4427,8 +4494,21 @@
 					}
 				}					
 
-				// todo: updare $contact['relations'] ???
-
+				// update organizations data (delete old and add new org)
+				$this->delete_orgs_by_person($cid, PHPGW_SQL_RUN_SQL);
+				if($contact['preferred_org'])
+				{
+					if(isset($principal))
+					{ // new org
+						$type_org = $this->search_contact_type($this->_contact_org);
+						$contact_relations[] = $cid;
+						$this->add_contact($type_org, $principal, array(), array(), $contact['cat_id'], array(), $contact_relations, array());
+					}
+					else
+					{
+						$this->add_orgs_for_person($contact['relations'], $contact['preferred_org'], $contact['preferred_address'], $cid, PHPGW_SQL_RUN_SQL);
+					}
+				}
 
 				// update other data (delete old and add new others)
 				$ret_other = array();
@@ -4438,7 +4518,7 @@
 				{
 					$ret['other'] = $old_others;
 					// 1. delete old others
-					$this->delete_others($cid);
+					$this->delete_others($cid, PHPGW_SQL_RUN_SQL);
 					if ($GLOBALS['phpgw']->db->Error)
 						$ret_other['delete_others'] = false;
 					else
@@ -4458,11 +4538,18 @@
 					}
 				}					
 
-				return $ret;
+				return true; //$ret;
 			}
 			else
 			{
-				return $this->add_contact($type, $contact, $comm_media, $locations, $contact['categories'], $others, $contact['relations'], $notes);
+				$cid = $this->add_contact($type, $contact, $comm_media, $locations, $contact['cat_id'], $others, $contact['relations'], $notes);
+				if(isset($principal))
+				{
+					$type_org = $this->search_contact_type($this->_contact_org);
+					$contact_relations[] = $cid;
+					$this->add_contact($type_org, $principal, array(), array(), $contact['cat_id'], array(), $contact_relations, array());
+				}
+				return $cid;
 			}
 		}
 	}
