Changeset 4251 in subversion
- Timestamp:
- Nov 22, 2010 3:37:28 PM (2 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/plugins/kolab_addressbook/rcube_kolab_contacts.php
r4245 r4251 14 14 { 15 15 public $primary_key = 'ID'; 16 public $readonly = true;16 public $readonly = false; 17 17 public $groups = true; 18 18 public $coltypes = array( … … 33 33 'im' => array('limit' => 1), 34 34 'website' => array('limit' => 1, 'subtypes' => null), 35 'address' => array( ),35 'address' => array('limit' => 2, 'subtypes' => array('home','work')), 36 36 'notes' => array(), 37 37 // define additional coltypes … … 40 40 // TODO: define more Kolab-specific fields such as: office-location, profession, manager-name, assistant, spouse-name, children, language, latitude, longitude, pgp-publickey, free-busy-url 41 41 ); 42 42 43 43 private $gid; 44 44 private $imap; … … 54 54 private $imap_folder = 'INBOX/Contacts'; 55 55 private $gender_map = array(0 => 'male', 1 => 'female'); 56 private $fieldmap = array( 57 // kolab => roundcube 58 'full-name' => 'name', 59 'given-name' => 'firstname', 60 'middle-names' => 'middlename', 61 'last-name' => 'surname', 62 'prefix' => 'prefix', 63 'suffix' => 'suffix', 64 'nick-name' => 'nickname', 65 'organization' => 'organization', 66 'department' => 'department', 67 'job-title' => 'jobtitle', 68 'initials' => 'initials', 69 'birthday' => 'birthday', 70 'anniversary' => 'anniversary', 71 'im-address' => 'im:aim', 72 'web-page' => 'website', 73 'body' => 'notes', 74 ); 56 75 57 76 … … 64 83 $format = rcube_kolab::get_format('contact'); 65 84 $this->coltypes['phone']['subtypes'] = $format->_phone_types; 66 $this->coltypes['address']['subtypes'] = $format->_address_types;67 85 $this->coltypes['anniversary']['label'] = rcube_label('anniversary'); 68 86 … … 237 255 * @return array List of assigned groups as ID=>Name pairs 238 256 */ 239 function get_record_groups($id)257 public function get_record_groups($id) 240 258 { 241 259 $out = array(); … … 251 269 return $out; 252 270 } 253 271 272 273 /** 274 * Create a new contact record 275 * 276 * @param array Assoziative array with save data 277 * Keys: Field name with optional section in the form FIELD:SECTION 278 * Values: Field value. Can be either a string or an array of strings for multiple values 279 * @param boolean True to check for duplicates first 280 * @return mixed The created record ID on success, False on error 281 */ 282 public function insert($save_data, $check=false) 283 { 284 if (is_object($save_data) && is_a($save_data, rcube_result_set)) 285 return $this->insert_recset($save_data, $check); 286 287 $insert_id = $existing = false; 288 289 // check for existing records by e-mail comparison 290 if ($check) { 291 foreach ($this->_get_col_values('email', $save_data, true) as $email) { 292 if ($existing = $this->search('email', $email, true, false)) 293 break; 294 } 295 } 296 297 $object = $this->_from_rcube_contact($save_data); 298 var_dump($object); 299 300 // TODO: how to create new Kolab objects? 301 302 303 return $insert_id; 304 } 305 306 /** 307 * Insert new contacts for each row in set 308 * 309 * @see rcube_kolab_contacts::insert() 310 */ 311 private function insert_recset($result, $check=false) 312 { 313 $ids = array(); 314 while ($row = $result->next()) { 315 if ($insert = $this->insert($row, $check)) 316 $ids[] = $insert; 317 } 318 return $ids; 319 } 320 321 322 /** 323 * Update a specific contact record 324 * 325 * @param mixed Record identifier 326 * @param array Assoziative array with save data 327 * Keys: Field name with optional section in the form FIELD:SECTION 328 * Values: Field value. Can be either a string or an array of strings for multiple values 329 * @return boolean True on success, False on error 330 */ 331 public function update($id, $save_data) 332 { 333 $updated = false; 334 $this->_fetch_contacts(); 335 if ($this->contacts[$id] && ($uid = $this->id2uid[$id])) { 336 $old = $this->contactstorage->getObject($uid); 337 $object = array_merge($old, $this->_from_rcube_contact($save_data)); 338 $object['last-modification-date'] = time(); 339 340 $saved = $this->contactstorage->save($object, $uid); 341 if (PEAR::isError($saved)) { 342 raise_error(array( 343 'code' => 600, 'type' => 'php', 344 'file' => __FILE__, 'line' => __LINE__, 345 'message' => "Error saving contact object to Kolab server:" . $saved->getMessage()), 346 true, false); 347 } 348 else { 349 $this->contacts[$id] = $this->_to_rcube_contact($object); 350 $updated = true; 351 } 352 } 353 354 return $updated; 355 } 356 357 /** 358 * Mark one or more contact records as deleted 359 * 360 * @param array Record identifiers 361 */ 362 public function delete($ids) 363 { 364 365 } 366 367 /** 368 * Remove all records from the database 369 */ 370 public function delete_all() 371 { 372 /* empty for read-only address books */ 373 } 374 254 375 255 376 /** … … 257 378 * Called on script shutdown 258 379 */ 259 function close()380 public function close() 260 381 { 261 382 rcube_kolab::shutdown(); … … 336 457 $out = array( 337 458 'ID' => md5($record['uid']), 338 'name' => $record['full-name'],339 'firstname' => $record['given-name'],340 'middlename' => $record['middle-names'],341 'surname' => $record['last-name'],342 'prefix' => $record['prefix'],343 'suffix' => $record['suffix'],344 'nickname' => $record['nick-name'],345 'organization' => $record['organization'],346 'department' => $record['department'],347 'jobtitle' => $record['job-title'],348 'initials' => $record['initials'],349 'birthday' => $record['birthday'],350 'anniversary' => $record['anniversary'],351 459 'email' => array(), 352 460 'phone' => array(), 353 'notes' => $record['body'],354 461 ); 462 463 foreach ($this->fieldmap as $kolab => $rcube) { 464 if (strlen($record[$kolab])) 465 $out[$rcube] = $record[$kolab]; 466 } 355 467 356 468 if (isset($record['gender'])) … … 362 474 foreach ((array)$record['phone'] as $i => $phone) 363 475 $out['phone:'.$phone['type']][] = $phone['number']; 364 365 if ($record['im-address']) 366 $out['im:aim'] = array($record['im-address']); 367 if ($record['web-page']) 368 $out['website'] = array($record['web-page']); 369 370 if ($record['addr-home-type']) { 371 $key = 'address:' . $record['addr-home-type']; 372 $out[$key][] = array( 373 'street' => $record['addr-home-street'], 374 'locality' => $record['addr-home-locality'], 375 'zipcode' => $record['addr-home-postal-code'], 376 'region' => $record['addr-home-region'], 377 'country' => $record['addr-home-country'], 378 ); 476 477 if (is_array($record['address'])) { 478 foreach ($record['address'] as $i => $adr) { 479 $key = 'address:' . $adr['type']; 480 $out[$key][] = array( 481 'street' => $adr['street'], 482 'locality' => $adr['locality'], 483 'zipcode' => $adr['postal-code'], 484 'region' => $adr['region'], 485 'country' => $adr['country'], 486 ); 487 } 379 488 } 380 489 … … 385 494 private function _from_rcube_contact($contact) 386 495 { 387 // TBD. 496 $object = array(); 497 498 foreach (array_flip($this->fieldmap) as $rcube => $kolab) { 499 if (isset($contact[$rcube])) 500 $object[$kolab] = is_array($contact[$rcube]) ? $contact[$rcube][0] : $contact[$rcube]; 501 } 502 503 // format dates 504 if ($object['birthday'] && ($date = @strtotime($object['birthday']))) 505 $object['birthday'] = date('Y-m-d', $date); 506 if ($object['anniversary'] && ($date = @strtotime($object['anniversary']))) 507 $object['anniversary'] = date('Y-m-d', $date); 508 509 $gendermap = array_flip($this->gender_map); 510 if (isset($contact['gender'])) 511 $object['gender'] = $gendermap[$contact['gender']]; 512 513 foreach (($emails = $this->_get_col_values('email', $contact, true)) as $email) 514 $object['email'][] = array('smtp-address' => $email, 'display-name' => $object['full-name']); 515 $object['emails'] = join(', ', $emails); 516 517 foreach ($this->_get_col_values('phone', $contact) as $type => $values) { 518 foreach ((array)$values as $phone) 519 $object['phone'][] = array('number' => $phone, 'type' => $type); 520 } 521 522 foreach ($this->_get_col_values('address', $contact) as $type => $values) { 523 foreach ((array)$values as $adr) { 524 $object['address'][] = array( 525 'type' => $type, 526 'street' => $adr['street'], 527 'locality' => $adr['locality'], 528 'postal-code' => $adr['zipcode'], 529 'region' => $adr['region'], 530 'country' => $adr['country'], 531 ); 532 } 533 } 534 535 return $object; 536 } 537 538 539 private function _get_col_values($col, $data, $flat = false) 540 { 541 $out = array(); 542 foreach ($data as $c => $values) { 543 if (strpos($c, $col) === 0) { 544 if ($flat) { 545 $out = array_merge($out, (array)$values); 546 } 547 else { 548 list($f, $type) = explode(':', $c); 549 $out[$type] = array_merge((array)$out[$type], (array)$values); 550 } 551 } 552 } 553 554 return $out; 388 555 } 389 556
Note: See TracChangeset
for help on using the changeset viewer.
