new addressbook admin function to cleanup addressbook fields on all contacts - useful if synchronisatioin creates duplicates

This commit is contained in:
Christian Binder 2009-11-11 08:44:51 +00:00
parent 5e24cc0c9b
commit 8e6f54f9de
5 changed files with 120 additions and 1 deletions

View File

@ -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

View File

@ -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 '<h1>'.lang('Permission denied !!!')."</h1>\n";
}
else
{
$updated = parent::set_all_cleanup($errors,true); // true = ignore acl
echo '<p style="margin-top: 20px;"><b>'.lang('%1 contacts updated (%2 errors).',$updated,$errors)."</b></p>\n";
}
$GLOBALS['egw']->common->egw_footer();
}
/**
* Download a document with inserted contact(s)

View File

@ -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

View File

@ -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

View File

@ -77,6 +77,12 @@
<input type="button" onclick="document.location.href='index.php?menuaction=addressbook.addressbook_ui.admin_set_fileas&all=1&type='+this.form.fileas.value;" value="{lang_All}" />
<input type="button" onclick="document.location.href='index.php?menuaction=addressbook.addressbook_ui.admin_set_fileas&type='+this.form.fileas.value;" value="{lang_Empty}" />
</td>
</tr>
<tr class="row_on">
<td>&nbsp;{lang_Cleanup_addressbook_fields_(apply_if_synchronization_creates_duplicates)}:</td>
<td>
<input type="button" onclick="document.location.href='index.php?menuaction=addressbook.addressbook_ui.admin_set_all_cleanup'" value="{lang_Start}" />
</td>
</tr>
<tr class="th">
<td colspan="2">&nbsp;<b>{lang_Contact_repository}</b></td>