1: <?php
  2:   3:   4:   5:   6:   7:   8:   9:  10:  11:  12: 
 13: 
 14:  15:  16:  17:  18:  19:  20:  21:  22: 
 23: class IMP_Ajax_Imple_VcardImport extends Horde_Core_Ajax_Imple
 24: {
 25:      26: 
 27:     protected $_observe = 'submit';
 28: 
 29:      30:  31:  32:  33: 
 34:     public function __construct(array $params = array())
 35:     {
 36:         
 37:         
 38:         
 39:         $params['id'] = 'vcard_import';
 40:         parent::__construct($params);
 41:     }
 42: 
 43:     
 44:     
 45:     protected function _attach($init)
 46:     {
 47:         return array(
 48:             'mime_id' => $this->_params['mime_id'],
 49:             'muid' => $this->_params['muid']
 50:         );
 51:     }
 52: 
 53:      54:  55:  56:  57:  58:  59:  60: 
 61:     protected function _handle(Horde_Variables $vars)
 62:     {
 63:         global $registry, $injector, $notification;
 64: 
 65:         $iCal = new Horde_Icalendar();
 66:         try {
 67:             $contents = $injector->getInstance('IMP_Factory_Contents')
 68:                 ->create(new IMP_Indices_Mailbox($vars));
 69:             $mime_part = $contents->getMIMEPart($vars->mime_id);
 70:             if (empty($mime_part)) {
 71:                 throw new IMP_Exception(_("Cannot retrieve vCard data from message."));
 72:             } elseif (!$iCal->parsevCalendar($mime_part->getContents(), 'VCALENDAR', $mime_part->getCharset())) {
 73:                 throw new IMP_Exception(_("Error reading the contact data."));
 74:             }
 75:             $components = $iCal->getComponents();
 76:         } catch (Exception $e) {
 77:             $notification->push($e, 'horde.error');
 78:         }
 79: 
 80:         $import = !empty($vars->imple_submit->import)
 81:             ? $vars->imple_submit->import
 82:             : false;
 83:         $source = !empty($vars->imple_submit->source)
 84:             ? $vars->imple_submit->source
 85:             : false;
 86: 
 87:         if ($import && $source && $registry->hasMethod('contacts/import')) {
 88:             $count = 0;
 89:             foreach ($components as $c) {
 90:                 if ($c->getType() == 'vcard') {
 91:                     try {
 92:                         $registry->call('contacts/import', array($c, null, $source));
 93:                         ++$count;
 94:                     } catch (Horde_Exception $e) {
 95:                         $notification->push(Horde_Core_Translation::t("There was an error importing the contact data:") . ' ' . $e->getMessage(), 'horde.error');
 96:                     }
 97:                 }
 98:             }
 99:             $notification->push(sprintf(Horde_Core_Translation::ngettext(
100:                 "%d contact was successfully added to your address book.",
101:                 "%d contacts were successfully added to your address book.",
102:                 $count),
103:                                         $count),
104:                                 'horde.success');
105:         }
106:     }
107: 
108: }
109: