Changeset 19d5973 in github


Ignore:
Timestamp:
Apr 23, 2012 2:38:26 PM (13 months ago)
Author:
alecpl <alec@…>
Branches:
master, HEAD, courier-fix, dev-browser-capabilities, pdo
Children:
988a80a
Parents:
91cb9df
Message:
  • Add vCard import from multiple files at once (#1488015)
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • CHANGELOG

    r55dce68 r19d5973  
    22=========================== 
    33 
     4- Add vCard import from multiple files at once (#1488015) 
    45- Roundcube Framework: 
    56    Add possibility to replace IMAP driver with custom class 
  • program/steps/addressbook/import.inc

    r9336ba2 r19d5973  
    3636  $writable_books = $RCMAIL->get_address_sources(true); 
    3737 
    38   $upload = new html_inputfield(array('type' => 'file', 'name' => '_file', 'id' => 'rcmimportfile', 'size' => 40)); 
     38  $upload = new html_inputfield(array( 
     39    'type' => 'file', 
     40    'name' => '_file[]', 
     41    'id' => 'rcmimportfile', 
     42    'size' => 40, 
     43    'multiple' => 'multiple', 
     44  )); 
    3945  $form = html::p(null, html::label('rcmimportfile', rcube_label('importfromfile')) . $upload->show()); 
    4046 
     
    136142$importstep = 'rcmail_import_form'; 
    137143 
    138 if ($_FILES['_file']['tmp_name'] && is_uploaded_file($_FILES['_file']['tmp_name'])) { 
    139   $replace = (bool)get_input_value('_replace', RCUBE_INPUT_GPC); 
    140   $target = get_input_value('_target', RCUBE_INPUT_GPC); 
    141   $CONTACTS = $RCMAIL->get_address_book($target, true); 
    142  
    143   // let rcube_vcard do the hard work :-) 
    144   $vcard_o = new rcube_vcard(); 
    145   $vcard_o->extend_fieldmap($CONTACTS->vcard_map); 
    146  
    147   $vcards = $vcard_o->import(file_get_contents($_FILES['_file']['tmp_name'])); 
    148  
    149   // no vcards detected 
    150   if (!count($vcards)) { 
    151     $OUTPUT->show_message('importerror', 'error'); 
    152   } 
    153   else if ($CONTACTS->readonly) { 
    154     $OUTPUT->show_message('addresswriterror', 'error'); 
    155   } 
    156   else { 
    157     $IMPORT_STATS = new stdClass; 
    158     $IMPORT_STATS->names = array(); 
    159     $IMPORT_STATS->skipped_names = array(); 
    160     $IMPORT_STATS->count = count($vcards); 
    161     $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0; 
    162  
    163     if ($replace) 
    164       $CONTACTS->delete_all(); 
    165  
    166     foreach ($vcards as $vcard) { 
    167       $email    = $vcard->email[0]; 
    168       $a_record = $vcard->get_assoc(); 
    169  
    170       // skip entries without an e-mail address or invalid 
    171       if (empty($email) || !$CONTACTS->validate($a_record, true)) { 
    172         $IMPORT_STATS->nomail++; 
    173         continue; 
    174       } 
    175  
    176       // We're using UTF8 internally 
    177       $email = rcube_idn_to_utf8($email); 
    178  
    179       if (!$replace && $email) { 
    180         // compare e-mail address 
    181         $existing = $CONTACTS->search('email', $email, 1, false); 
    182         if (!$existing->count && $vcard->displayname) {  // compare display name 
    183           $existing = $CONTACTS->search('name', $vcard->displayname, 1, false); 
    184         } 
    185         if ($existing->count) { 
    186           $IMPORT_STATS->skipped++; 
    187           $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email; 
    188           continue; 
    189         } 
    190       } 
    191  
    192       $a_record['vcard'] = $vcard->export(); 
    193  
    194       $plugin = $RCMAIL->plugins->exec_hook('contact_create', array('record' => $a_record, 'source' => null)); 
    195       $a_record = $plugin['record']; 
    196  
    197       // insert record and send response 
    198       if (!$plugin['abort']) 
    199         $success = $CONTACTS->insert($a_record); 
    200       else 
    201         $success = $plugin['result']; 
    202  
    203       if ($success) { 
    204         $IMPORT_STATS->inserted++; 
    205         $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email; 
    206       } else { 
    207         $IMPORT_STATS->errors++; 
    208       } 
    209     } 
    210  
    211     $importstep = 'rcmail_import_confirm'; 
    212   } 
    213 } 
    214 else if ($err = $_FILES['_file']['error']) { 
    215   if ($err == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { 
    216     $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))); 
    217   } else { 
    218     $OUTPUT->show_message('fileuploaderror', 'error'); 
    219   } 
     144if (is_array($_FILES['_file'])) { 
     145    $replace  = (bool)get_input_value('_replace', RCUBE_INPUT_GPC); 
     146    $target   = get_input_value('_target', RCUBE_INPUT_GPC); 
     147 
     148    $vcards       = array(); 
     149    $upload_error = null; 
     150 
     151    $CONTACTS = $RCMAIL->get_address_book($target, true); 
     152 
     153    if ($CONTACTS->readonly) { 
     154        $OUTPUT->show_message('addresswriterror', 'error'); 
     155    } 
     156    else { 
     157        foreach ((array)$_FILES['_file']['tmp_name'] as $i => $filepath) { 
     158            // Process uploaded file if there is no error 
     159            $err = $_FILES['_file']['error'][$i]; 
     160 
     161            if ($err) { 
     162                $upload_error = $err; 
     163            } 
     164            else { 
     165                // let rcube_vcard do the hard work :-) 
     166                $vcard_o = new rcube_vcard(); 
     167                $vcard_o->extend_fieldmap($CONTACTS->vcard_map); 
     168 
     169                $v_list = $vcard_o->import(file_get_contents($filepath)); 
     170 
     171                if (!empty($v_list)) { 
     172                    $vcards = array_merge($vcards, $v_list); 
     173                } 
     174            } 
     175        } 
     176    } 
     177 
     178    // no vcards detected 
     179    if (!count($vcards)) { 
     180        if ($upload_error == UPLOAD_ERR_INI_SIZE || $err == UPLOAD_ERR_FORM_SIZE) { 
     181            $OUTPUT->show_message('filesizeerror', 'error', array('size' => show_bytes(parse_bytes(ini_get('upload_max_filesize'))))); 
     182        } 
     183        else if ($upload_error) { 
     184            $OUTPUT->show_message('fileuploaderror', 'error'); 
     185        } 
     186        else { 
     187            $OUTPUT->show_message('importerror', 'error'); 
     188        } 
     189    } 
     190    else { 
     191        $IMPORT_STATS = new stdClass; 
     192        $IMPORT_STATS->names = array(); 
     193        $IMPORT_STATS->skipped_names = array(); 
     194        $IMPORT_STATS->count = count($vcards); 
     195        $IMPORT_STATS->inserted = $IMPORT_STATS->skipped = $IMPORT_STATS->nomail = $IMPORT_STATS->errors = 0; 
     196 
     197        if ($replace) { 
     198            $CONTACTS->delete_all(); 
     199        } 
     200 
     201        foreach ($vcards as $vcard) { 
     202            $email    = $vcard->email[0]; 
     203            $a_record = $vcard->get_assoc(); 
     204 
     205            // skip entries without an e-mail address or invalid 
     206            if (empty($email) || !$CONTACTS->validate($a_record, true)) { 
     207                $IMPORT_STATS->nomail++; 
     208                continue; 
     209            } 
     210 
     211            // We're using UTF8 internally 
     212            $email = rcube_idn_to_utf8($email); 
     213 
     214            if (!$replace && $email) { 
     215                // compare e-mail address 
     216                $existing = $CONTACTS->search('email', $email, 1, false); 
     217                if (!$existing->count && $vcard->displayname) {  // compare display name 
     218                    $existing = $CONTACTS->search('name', $vcard->displayname, 1, false); 
     219                } 
     220                if ($existing->count) { 
     221                    $IMPORT_STATS->skipped++; 
     222                    $IMPORT_STATS->skipped_names[] = $vcard->displayname ? $vcard->displayname : $email; 
     223                    continue; 
     224                } 
     225            } 
     226 
     227            $a_record['vcard'] = $vcard->export(); 
     228 
     229            $plugin = $RCMAIL->plugins->exec_hook('contact_create', 
     230                array('record' => $a_record, 'source' => null)); 
     231            $a_record = $plugin['record']; 
     232 
     233            // insert record and send response 
     234            if (!$plugin['abort']) 
     235                $success = $CONTACTS->insert($a_record); 
     236            else 
     237                $success = $plugin['result']; 
     238 
     239            if ($success) { 
     240                $IMPORT_STATS->inserted++; 
     241                $IMPORT_STATS->names[] = $vcard->displayname ? $vcard->displayname : $email; 
     242            } 
     243            else { 
     244                $IMPORT_STATS->errors++; 
     245            } 
     246        } 
     247 
     248        $importstep = 'rcmail_import_confirm'; 
     249    } 
    220250} 
    221251 
Note: See TracChangeset for help on using the changeset viewer.