Open-Xchange contact migration

Post modules, themes or any other code you want to share with the community.

Moderator: Developers

fischli
Posts: 36
Joined: Tue Apr 22, 2014 10:06 am

Open-Xchange contact migration

Postby fischli » Tue May 13, 2014 12:52 pm

Open-Xchange uses a similar contact structure (addressbook->company->contact) and migrating contacts from OX to GO is possible without loosing information. The following snippet (as commandline tool) may be used as a code-draft for similar systems.

Code: Select all

<?php

require('GO.php');

/**
 * commandline script to copy all addressbooks from Open-Xchange to Group-Office
 */
$oxDbConnector = pg_connect("host=oxmail port=5432 dbname=openxchg user=openxchg password=*****");

$publicRootOxFolders = pg_query($oxDbConnector,
	"SELECT fuid,fname FROM oxfolder_tree" .
	" WHERE parent=2 AND module='contact' AND type='public'" .
	" ORDER BY fname;") or die(pg_errormessage());
/**
 * run through all public folders
 */
while ($publicRootFolder = pg_fetch_object($publicRootOxFolders)) {

	$addressbook = new GO_Addressbook_Model_Addressbook();
	$addressbook->name = $publicRootFolder->fname;
	$addressbook->user_id = 1;
	$addressbook->save(true);
	// internal users will have r+w permissions only
	$addressbook->acl->addGroup(GO::config()->group_internal, GO_Base_Model_Acl::DELETE_PERMISSION);
	$addressbookId = $addressbook->getAttribute("id");

	/**
	 * copy all contacts without company
	 */
	$constraint = " WHERE fid=" . $publicRootFolder->fuid;
	print "Addressbook: " . $publicRootFolder->fname . " ";
	storeOxContacts($oxDbConnector, $constraint, $addressbookId);
	print "\n";

	$publicOxFolders = pg_query($oxDbConnector,
		"SELECT fuid,fname FROM oxfolder_tree WHERE parent=" . $publicRootFolder->fuid . " ORDER BY fname;") or die(pg_errormessage());

	while ($oxAddressbook = pg_fetch_object($publicOxFolders)) {
		$company = new GO_Addressbook_Model_Company();
		$company->name = $oxAddressbook->fname;
		//$company->crn = $oxAddressbook->fuid;
		$company->addressbook_id = $addressbookId;
		$company->user_id = 1;
 		$company->save(true);

		$companyId = $company->id;
		print $company->name;

		/**
		 * copy all contacts related to this company
		 */
		$constraint = " WHERE fid=" . $oxAddressbook->fuid;
		storeOxContacts($oxDbConnector, $constraint, $addressbookId, $companyId);

		print "\n";
	}
}

function storeOxContacts($oxDbConnector, $constraint, $addressbookId, $companyId = null) {
	/**
	 * OX->GO database mapping
	 * TODO: country should be evaluated by phone prefix
	 */
	$oxContacts = pg_query($oxDbConnector,
		"SELECT " .
		"field02 as last_name," .
		"field03 as first_name," .
		"field05 as suffix," .
		"field06 as title," .
		"field19 as department," .
		"field20 as function," .
		"field23 as address," .
		"field24 as zip," .
		"field25 as city," .
		"field34 as comment," .
		"lower(field65) as email," .
		"lower(field66) as email2," .
		"field68 as homepage," .
		"field43 as home_phone," .
		"field44 as work_phone," .
		"field53 as cellular," .
		"field54 as cellular2," .
		"field45 as work_fax" .
		" FROM prg_contacts " .
		$constraint . " ORDER BY field02;") or die(pg_errormessage());

	while ($oxContact = pg_fetch_object($oxContacts)) {
		$contact = new GO_Addressbook_Model_Contact();
		$contact->addressbook_id = $addressbookId;
		if (!is_null($companyId)) {
			$contact->company_id = $companyId;
		}
		$contact->last_name = $oxContact->last_name;
		$contact->first_name = $oxContact->first_name;
		$contact->suffix = $oxContact->suffix;
		$contact->title = $oxContact->title;
		$contact->department = $oxContact->department;
		$contact->function = $oxContact->function;
		$contact->address = $oxContact->address;
		$contact->zip = $oxContact->zip;
		$contact->city = $oxContact->city;
		$contact->comment = $oxContact->comment;
		$contact->email = $oxContact->email;
		$contact->email2 = $oxContact->email2;
		$contact->homepage = $oxContact->homepage;
		$contact->home_phone = $oxContact->home_phone;
		$contact->work_phone = $oxContact->work_phone;
		$contact->cellular = $oxContact->cellular;
		$contact->cellular2 = $oxContact->cellular2;
		$contact->work_fax = $oxContact->work_fax;
		$contact->user_id = 1;
		$contact->save(true);
		print ".";
	}

}
mschering
Site Admin
Site Admin
Posts: 8235
Joined: Tue Apr 20, 2004 1:06 pm
Location: The Netherlands - Den Bosch
Contact:

Re: Open-Xchange contact migration

Postby mschering » Wed May 14, 2014 7:21 am

Thanks for sharing!
Best regards,

Merijn Schering
Intermesh
itandg
Posts: 1
Joined: Wed Sep 09, 2015 4:23 pm

Re: Open-Xchange contact migration

Postby itandg » Sun May 22, 2016 8:55 pm

Hi,
I am trying to do a similar migration and tested a few lines with test.php:
<?php
require('GO.php');
$contact = new GO_Addressbook_Model_Contact();
$contact->first_name='John';
$contact->last_name='Doe';
$contact->addressbook_id=42;
$contact->save();

I am getting:
PHP Fatal error: Class 'GO_Addressbook_Model_Contact' not found in /var/www/html/groupoffice-com-6.1.85/test.php on line 3
Any suggestions?
fischli
Posts: 36
Joined: Tue Apr 22, 2014 10:06 am

Re: Open-Xchange contact migration

Postby fischli » Mon Jun 06, 2016 6:21 am

Group Office uses namespaces since 6.0. The script is for a 5.x version and you have to use backslashes instead of an underline as a delimiter.
Please try:

$contact = new \GO\Addressbook\Model\Contact();

Who is online

Users browsing this forum: No registered users and 2 guests