PageRenderTime 62ms CodeModel.GetById 21ms RepoModel.GetById 0ms app.codeStats 0ms

/tine20/Addressbook/Setup/Import/Egw14.php

https://gitlab.com/rsilveira1987/Expresso
PHP | 213 lines | 124 code | 37 blank | 52 comment | 24 complexity | 262d88f4b33e8aa2e3af5075266fa22e MD5 | raw file
  1. <?php
  2. /**
  3. * Tine 2.0
  4. *
  5. * @package Addressbook
  6. * @subpackage Setup
  7. * @license http://www.gnu.org/licenses/agpl.html AGPL Version 3
  8. * @author Cornelius Weiss <c.weiss@metaways.de>
  9. * @copyright Copyright (c) 2009-2010 Metaways Infosystems GmbH (http://www.metaways.de)
  10. */
  11. /**
  12. * class to import addressbooks/contacts from egw14
  13. *
  14. *
  15. * @package Addressbook
  16. * @subpackage Setup
  17. */
  18. class Addressbook_Setup_Import_Egw14 extends Tinebase_Setup_Import_Egw14_Abstract
  19. {
  20. protected $_appname = 'Addressbook';
  21. protected $_egwTableName = 'egw_addressbook';
  22. protected $_egwOwnerColumn = 'contact_owner';
  23. protected $_defaultContainerConfigProperty = Addressbook_Preference::DEFAULTADDRESSBOOK;
  24. protected $_tineRecordModel = 'Addressbook_Model_Contact';
  25. protected $_tineRecordBackend = NULL;
  26. /**
  27. * country mapping
  28. *
  29. * @var array
  30. */
  31. protected $_countryMapping = array(
  32. "BELGIEN" => "BE",
  33. "BULGARIEN" => "BG",
  34. "DEUTSCHLAND" => "DE",
  35. "FRANKREICH" => "FR",
  36. "GERMANY" => "DE",
  37. "GREAT BRITAIN" => "GB",
  38. "IRELAND" => "IE",
  39. "JAPAN" => "JP",
  40. "LUXEMBURG" => "LU",
  41. "NEW ZEALAND" => "NZ",
  42. "NIEDERLANDE" => "NL",
  43. "ÖSTERREICH" => "AT",
  44. "SCHWEIZ" => "CH",
  45. "SLOVAKEI" => "SK",
  46. "SPANIEN" => "ES",
  47. "SWEDEN" => "SE",
  48. "USA" => "US",
  49. "VEREINIGTE STAATEN VON AMERIKA" => "US",
  50. );
  51. /**
  52. * do the import
  53. */
  54. public function import()
  55. {
  56. $this->_log->NOTICE(__METHOD__ . '::' . __LINE__ . ' starting egw import for Adressbook');
  57. $this->_migrationStartTime = Tinebase_DateTime::now();
  58. $this->_tineRecordBackend = Addressbook_Backend_Factory::factory(Addressbook_Backend_Factory::SQL);
  59. $estimate = $this->_getEgwRecordEstimate();
  60. $this->_log->NOTICE(__METHOD__ . '::' . __LINE__ . " found {$estimate} contacts for migration");
  61. $pageSize = 100;
  62. $numPages = ceil($estimate/$pageSize);
  63. for ($page=1; $page <= $numPages; $page++) {
  64. $this->_log->info(__METHOD__ . '::' . __LINE__ . " starting migration page {$page} of {$numPages}");
  65. Tinebase_Core::setExecutionLifeTime($pageSize*10);
  66. $recordPage = $this->_getRawEgwRecordPage($page, $pageSize);
  67. $this->_migrateEgwRecordPage($recordPage);
  68. }
  69. $this->_log->NOTICE(__METHOD__ . '::' . __LINE__ . ' ' . ($this->_importResult['totalcount'] - $this->_importResult['failcount']) . ' contacts imported sucessfully ' . ($this->_importResult['failcount'] ? " {$this->_importResult['failcount']} contacts skipped with failures" : ""));
  70. }
  71. /**
  72. * @TODO: egw can have groups as owner
  73. * -> map this to shared folder
  74. * -> find appropriate creator / current_user for this
  75. * -> support maps for group => creator and owner => folder?
  76. */
  77. protected function _migrateEgwRecordPage($recordPage)
  78. {
  79. foreach($recordPage as $egwContactData) {
  80. try {
  81. $this->_importResult['totalcount']++;
  82. $currentUser = Tinebase_Core::get(Tinebase_Core::USER);
  83. $owner = $egwContactData['contact_owner'] ? Tinebase_User::getInstance()->getFullUserById($this->mapAccountIdEgw2Tine($egwContactData['contact_owner'])) : $currentUser;
  84. Tinebase_Core::set(Tinebase_Core::USER, $owner);
  85. $contactData = array_merge($egwContactData, array(
  86. 'id' => $egwContactData['contact_id'],
  87. 'creation_time' => $this->convertDate($egwContactData['contact_created']),
  88. 'created_by' => $this->mapAccountIdEgw2Tine($egwContactData['contact_creator'], FALSE),
  89. 'last_modified_time' => $egwContactData['contact_modified'] ? $this->convertDate($egwContactData['contact_modified']) : NULL,
  90. 'last_modified_by' => $egwContactData['contact_modifier'] ? $this->mapAccountIdEgw2Tine($contact['contact_modifier'], FALSE) : NULL,
  91. ));
  92. $contactData['created_by'] = $contactData['created_by'] ?: $owner;
  93. $contactData['$egwContactData'] = $contactData['last_modified_time'] && !$contactData['last_modified_by'] ?: $owner;
  94. // fix mandentory fields
  95. if (! ($egwContactData['org_name'] || $egwContactData['n_family'])) {
  96. $contactData['org_name'] = 'N/A';
  97. }
  98. // add 'http://' if missing
  99. foreach(array('contact_url', 'contact_url_home') as $urlProperty) {
  100. if ( !preg_match("/^http/i", $egwContactData[$urlProperty]) && !empty($egwContactData[$urlProperty]) ) {
  101. $contactData[$urlProperty] = "http://" . $egwContactData[$urlProperty];
  102. }
  103. }
  104. // normalize countynames
  105. $contactData['adr_one_countryname'] = $this->convertCountryname2Iso($egwContactData['adr_one_countryname']);
  106. $contactData['adr_two_countryname'] = $this->convertCountryname2Iso($egwContactData['adr_two_countryname']);
  107. // handle bday
  108. if (array_key_exists('contact_bday', $egwContactData) && $egwContactData['contact_bday']) {
  109. // @TODO evaluate contact_tz
  110. $contactData['bday'] = new Tinebase_DateTime($egwContactData['contact_bday'], $this->_config->birthdayDefaultTimezone);
  111. } else if (array_key_exists('bday', $egwContactData) && $egwContactData['bday']) {
  112. // egw <= 1.4
  113. $contactData['bday'] = $this->convertDate($egwContactData['bday']);
  114. }
  115. // handle tags
  116. $contactData['tags'] = $this->convertCategories($egwContactData['cat_id']);
  117. // @TODO handle photo
  118. // handle container
  119. if ($egwContactData['contact_owner'] && ! $egwContactData['account_id']) {
  120. $contactData['container_id'] = $egwContactData['contact_private'] && $this->_config->setPersonalContainerGrants ?
  121. $this->getPrivateContainer($this->mapAccountIdEgw2Tine($egwContactData['contact_owner']))->getId() :
  122. $this->getPersonalContainer($this->mapAccountIdEgw2Tine($egwContactData['contact_owner']))->getId();
  123. }
  124. // finally create the record
  125. $tineContact = new Addressbook_Model_Contact ($contactData);
  126. $this->saveTineRecord($tineContact);
  127. } catch (Exception $e) {
  128. $this->_importResult['failcount']++;
  129. Tinebase_Core::set(Tinebase_Core::USER, $currentUser);
  130. $this->_log->ERR(__METHOD__ . '::' . __LINE__ . ' could not migrate contact "' . $egwContactData['contact_id'] . '" cause: ' . $e->getMessage());
  131. $this->_log->DEBUG(__METHOD__ . '::' . __LINE__ . ' ' . $e);
  132. }
  133. }
  134. }
  135. /**
  136. * save tine20 record to db
  137. *
  138. * @param Tinebase_Record_Abstract $record
  139. */
  140. public function saveTineRecord(Tinebase_Record_Abstract $record)
  141. {
  142. if (! $record->account_id) {
  143. $savedRecord = $this->_tineRecordBackend->create($record);
  144. }
  145. else if ($this->_config->updateAccountRecords) {
  146. $accountId = $this->mapAccountIdEgw2Tine($record->account_id);
  147. $account = Tinebase_User::getInstance()->getUserById($accountId);
  148. if (! ($account && $account->contact_id)) {
  149. $this->_log->WARN(__METHOD__ . '::' . __LINE__ . " could not migrate account contact for {$record->n_fn} - no contact found");
  150. return;
  151. }
  152. $contact = $this->_tineRecordBackend->get($account->contact_id);
  153. $record->setId($account->contact_id);
  154. $record->container_id = $contact->container_id;
  155. $savedRecord = $this->_tineRecordBackend->update($record);
  156. }
  157. // tags
  158. $this->attachTags($record->tags, $savedRecord->getId());
  159. }
  160. /**
  161. * get iso code of localised country name
  162. *
  163. * @TODO iterate zend_translate
  164. */
  165. public function convertCountryname2Iso($countryname)
  166. {
  167. // normalize empty
  168. if (!$countryname) return NULL;
  169. $countryname = strtoupper(trim($countryname));
  170. if (! array_key_exists($countryname, $this->_countryMapping)) {
  171. $this->_log->WARN(__METHOD__ . '::' . __LINE__ . " could not get coutry code for {$countryname}");
  172. return NULL;
  173. }
  174. return $this->_countryMapping[$countryname];
  175. }
  176. }