From dc68edab467fbaa99a2997660ca564308f7d4849 Mon Sep 17 00:00:00 2001 From: Ralf Becker Date: Fri, 14 Apr 2006 09:50:39 +0000 Subject: [PATCH] fixed csv import: - no more caching in the cat_id function, as it's done in the categories class in the API no - categories::name2id no longer caches not found entries, as they might be added in this request - contacts_sql::add set's all unset telefon numbers to '', to avoid the stupid db-default '+1 (000) 000-0000', which I dont want to change in the stable release --- addressbook/csv_import.php | 56 +++++++++---------------- phpgwapi/inc/class.categories.inc.php | 4 +- phpgwapi/inc/class.contacts_sql.inc.php | 7 ++-- 3 files changed, 27 insertions(+), 40 deletions(-) diff --git a/addressbook/csv_import.php b/addressbook/csv_import.php index 50029dcaed..1f889df24c 100644 --- a/addressbook/csv_import.php +++ b/addressbook/csv_import.php @@ -36,7 +36,7 @@ $GLOBALS['egw']->redirect_link('/addressbook/index.php'); } $GLOBALS['egw_info']['flags']['app_header'] = lang('Import CSV-File into Addressbook'); - $GLOBALS['egw']->common->phpgw_header(); + $GLOBALS['egw']->common->egw_header(); $GLOBALS['egw']->contacts = createobject('phpgwapi.contacts'); @@ -81,51 +81,36 @@ return False; } - $cat2id = array(); - function cat_id($cats) { if(!$cats) { return ''; } - + if(!is_object($GLOBALS['egw']->categories)) + { + $GLOBALS['egw']->categories =& CreateObject('phpgwapi.categories'); + } $ids = array(); foreach(split(' *[,;] *',$cats) as $cat) { - if(isset($cat2id[$cat])) + if (is_numeric($cat) && $GLOBALS['egw']->categories->id2name($cat) != '--') { - $ids[$cat] = $cat2id[$cat]; // cat is in cache + $id = (int) $cat; + } + elseif ($id = $GLOBALS['egw']->categories->name2id(addslashes($cat))) + { + // cat exists } else - { - if(!is_object($GLOBALS['egw']->categories)) - { - $GLOBALS['egw']->categories = createobject('phpgwapi.categories'); - } - if (is_numeric($cat) && $GLOBALS['egw']->categories->id2name($cat) != '--') - { - $cat2id[$cat] = $ids[$cat] = $cat; - } - elseif ($id = $GLOBALS['egw']->categories->name2id(addslashes($cat))) - { // cat exists - $cat2id[$cat] = $ids[$cat] = $id; - } - else - { // create new cat - $GLOBALS['egw']->categories->add(array('name' => $cat,'descr' => $cat)); - $cat2id[$cat] = $ids[$cat] = $GLOBALS['egw']->categories->name2id(addslashes($cat)); - } + { // create new cat + $id = $GLOBALS['egw']->categories->add(array('name' => $cat,'descr' => $cat)); } + $ids[$id] = $id; // we use the $id as index to not ass a cat twice } - $id_str = implode(',',$ids); - - if(count($ids) > 1) // multiple cats need to be in ',' - { - $id_str = ",$id_str,"; - } - return $id_str; + return implode(',',$ids); } + if (!is_object($GLOBALS['egw']->html)) { $GLOBALS['egw']->html = CreateObject('phpgwapi.html'); @@ -410,7 +395,7 @@ $empty = !count($values); // convert the category name to an id - if ($values['cat_id'] && !is_numeric($values['cat_id']) && $values['cat_id'][0] != ',') + if ($values['cat_id']) { $values['cat_id'] = cat_id($values['cat_id']); } @@ -438,9 +423,8 @@ } if(!$_POST['debug'] && !$empty) // dont import empty contacts { - $GLOBALS['egw']->contacts->add( $values['owner'] ? $values['owner'] : $GLOBALS['egw_info']['user']['account_id'], - $values); - // echo "

adding: ".print_r($values)."

\n"; + $GLOBALS['egw']->contacts->add( $values['owner'] ? $values['owner'] : $GLOBALS['egw_info']['user']['account_id'],$values); + //echo "

adding: ".print_r($values,true)."

\n"; } } $log .= "\t\n\n"; @@ -459,5 +443,5 @@ $GLOBALS['egw']->template->set_var('hiddenvars',str_replace('{','{',$hiddenvars)); $GLOBALS['egw']->template->pfp('out','import',True); - $GLOBALS['egw']->common->phpgw_footer(); + $GLOBALS['egw']->common->egw_footer(); ?> diff --git a/phpgwapi/inc/class.categories.inc.php b/phpgwapi/inc/class.categories.inc.php index f3402c219d..7ae0194f83 100644 --- a/phpgwapi/inc/class.categories.inc.php +++ b/phpgwapi/inc/class.categories.inc.php @@ -607,7 +607,9 @@ '(cat_owner = '.(int)$this->account_id.' OR cat_owner = -1)', ),__LINE__,__FILE__); - return $cache[$cat_name] = $this->db->next_record() ? $this->db->f('cat_id') : 0; + if (!$this->db->next_record()) return 0; // cat not found, dont cache it, as it might be created in this request + + return $cache[$cat_name] = (int) $this->db->f('cat_id'); } /** diff --git a/phpgwapi/inc/class.contacts_sql.inc.php b/phpgwapi/inc/class.contacts_sql.inc.php index 8f1ac89684..7af6f64dcb 100644 --- a/phpgwapi/inc/class.contacts_sql.inc.php +++ b/phpgwapi/inc/class.contacts_sql.inc.php @@ -610,10 +610,11 @@ { $fields['tid'] = 'n'; } - // setting the following fields to empty if unset, as the addressbook does not seem to set them anymore and I dont want to change the db-default atm. - foreach(array('tel_voice','tel_bbs','tel_modem') as $name) + // setting the telephone numbers to empty if unset, as otherwise the db-default adds '+1 (000) 000-0000' + // I dont want to change the default in the stable release + foreach($this->stock_contact_fields as $name) { - if (!isset($fields[$name])) $fields[$name] = ''; + if (substr($name,0,4) == 'tel_' && !isset($fields[$name])) $fields[$name] = ''; } list($stock_fields,$stock_fieldnames,$extra_fields) = $this->split_stock_and_extras($fields);