From 8e6f54f9ded3dd43a053ea4b68022fce5c208456 Mon Sep 17 00:00:00 2001 From: Christian Binder Date: Wed, 11 Nov 2009 08:44:51 +0000 Subject: [PATCH] new addressbook admin function to cleanup addressbook fields on all contacts - useful if synchronisatioin creates duplicates --- addressbook/inc/class.addressbook_bo.inc.php | 88 +++++++++++++++++++- addressbook/inc/class.addressbook_ui.inc.php | 25 ++++++ addressbook/setup/egw_de.lang | 1 + addressbook/setup/egw_en.lang | 1 + addressbook/templates/default/config.tpl | 6 ++ 5 files changed, 120 insertions(+), 1 deletion(-) diff --git a/addressbook/inc/class.addressbook_bo.inc.php b/addressbook/inc/class.addressbook_bo.inc.php index a4f211ac1d..5f7c8cee66 100755 --- a/addressbook/inc/class.addressbook_bo.inc.php +++ b/addressbook/inc/class.addressbook_bo.inc.php @@ -433,7 +433,93 @@ class addressbook_bo extends addressbook_so } return $updated; } - + + /** + * Cleanup all contacts db fields of all users (called by Admin >> Addressbook >> Site configuration (Admin only) + * + * Cleanup means to truncate all unnecessary chars like whitespaces or tabs, + * remove unneeded carriage returns or set empty fields to NULL + * + * @param int &$errors=null on return number of errors + * @return int|boolean number of contacts updated + */ + function set_all_cleanup(&$errors=null,$ignore_acl=false) + { + if ($ignore_acl) + { + unset($this->somain->grants); // to NOT limit search to contacts readable by current user + } + + // fields that must not be touched + $fields_exclude = array( + 'id' => true, + 'tid' => true, + 'owner' => true, + 'private' => true, + 'created' => true, + 'creator' => true, + 'modified' => true, + 'modifier' => true, + 'account_id' => true, + 'etag' => true, + 'uid' => true, + 'freebusy_uri' => true, + 'calendar_uri' => true, + 'photo' => true, + ); + + // to be able to work on huge contact repositories we read the contacts in chunks of 100 + for($n = $updated = $errors = 0; ($contacts = parent::search(array(),false,'','','',false,'OR',array($n*100,100))); ++$n) + { + foreach($contacts as $contact) + { + $fields_to_update = array(); + foreach($contact as $field_name => $field_value) + { + if($fields_exclude[$field_name] === true) continue; // dont touch specified field + + if (is_string($field_value) && $field_name != 'pubkey' && $field_name != 'jpegphoto') + { + // check if field has to be trimmed + if (strlen($field_value) != strlen(trim($field_value))) + { + $fields_to_update[$field_name] = $field_value = trim($field_value); + } + // check if field contains a carriage return - exclude notes + if ($field_name != 'note' && strpos($field_value,"\x0D\x0A") !== false) + { + $fields_to_update[$field_name] = $field_value = str_replace("\x0D\x0A"," ",$field_value);; + } + } + // check if a field contains an empty string + if (is_string($field_value) && strlen($field_value) == 0) + { + $fields_to_update[$field_name] = $field_value = null; + } + } + + if(count($fields_to_update) > 0) + { + $contact_to_save = array( + 'id' => $contact['id'], + 'owner' => $contact['owner'], + 'private' => $contact['private'], + 'account_id' => $contact['account_id'], + 'uid' => $contact['uid']) + $fields_to_update; + + if ($this->save($contact_to_save,$ignore_acl)) + { + $updated++; + } + else + { + $errors++; + } + } + } + } + return $updated; + } /** * get full name from the name-parts diff --git a/addressbook/inc/class.addressbook_ui.inc.php b/addressbook/inc/class.addressbook_ui.inc.php index c6761af108..d88b2f32de 100644 --- a/addressbook/inc/class.addressbook_ui.inc.php +++ b/addressbook/inc/class.addressbook_ui.inc.php @@ -26,6 +26,7 @@ class addressbook_ui extends addressbook_bo 'emailpopup'=> True, 'migrate2ldap' => True, 'admin_set_fileas' => True, + 'admin_set_all_cleanup' => True, 'cat_add' => True, ); /** @@ -2001,6 +2002,30 @@ $readonlys['button[vcard]'] = true; } $GLOBALS['egw']->common->egw_footer(); } + + /** + * Cleanup all contacts of all users (called by Admin >> Addressbook >> Site configuration (Admin only) + * + */ + function admin_set_all_cleanup() + { + translation::add_app('admin'); + $GLOBALS['egw_info']['flags']['app_header'] = lang('Addressbook').' - '.lang('Contact maintenance'); + $GLOBALS['egw']->common->egw_header(); + parse_navbar(); + + // check if user has admin rights (Security) + if (!$this->is_admin()) + { + echo '

'.lang('Permission denied !!!')."

\n"; + } + else + { + $updated = parent::set_all_cleanup($errors,true); // true = ignore acl + echo '

'.lang('%1 contacts updated (%2 errors).',$updated,$errors)."

\n"; + } + $GLOBALS['egw']->common->egw_footer(); + } /** * Download a document with inserted contact(s) diff --git a/addressbook/setup/egw_de.lang b/addressbook/setup/egw_de.lang index 95d7f801af..4d5854367f 100644 --- a/addressbook/setup/egw_de.lang +++ b/addressbook/setup/egw_de.lang @@ -88,6 +88,7 @@ choose owner of imported data addressbook de Wählen Sie den Besitzer der import choose seperator and charset addressbook de Wählen Sie ein Trennzeichen und den Zeichensatz chosse an etemplate for this contact type admin de Wählen Sie ein eTemplate für diesen Kontakt Typ city common de Stadt +cleanup addressbook fields (apply if synchronization creates duplicates) addressbook de Bereinige Adressbuch Felder (anwenden wenn die Synchronisation Duplikate erzeugt) company common de Firma company name addressbook de Firmenname configuration common de Konfiguration diff --git a/addressbook/setup/egw_en.lang b/addressbook/setup/egw_en.lang index 8a0b0d5920..39bcdee07c 100644 --- a/addressbook/setup/egw_en.lang +++ b/addressbook/setup/egw_en.lang @@ -88,6 +88,7 @@ choose owner of imported data addressbook en Choose owner of imported data choose seperator and charset addressbook en Choose seperator and charset chosse an etemplate for this contact type admin en Chosse an eTemplate for this contact type city common en City +cleanup addressbook fields (apply if synchronization creates duplicates) addressbook en Cleanup addressbook fields (apply if synchronization creates duplicates) company common en Company company name addressbook en company name configuration common en Configuration diff --git a/addressbook/templates/default/config.tpl b/addressbook/templates/default/config.tpl index 38a7b97db8..4b9decd916 100644 --- a/addressbook/templates/default/config.tpl +++ b/addressbook/templates/default/config.tpl @@ -77,6 +77,12 @@ + + +  {lang_Cleanup_addressbook_fields_(apply_if_synchronization_creates_duplicates)}: + + +  {lang_Contact_repository}