/**
  * transfers user accounts to employee records
  * 
  * @param boolean $_deletePrivateInfo should private information be removed from contacts
  */
 public function transferUserAccounts($_deletePrivateInfo = FALSE, $_feastCalendarId = NULL, $_workingTimeModelId = NULL, $_vacationDays = NULL, $cliCall = FALSE)
 {
     $lastNumber = $this->getLastEmployeeNumber();
     // get all active accounts
     $filter = new Addressbook_Model_ContactFilter(array(array('field' => 'type', 'operator' => 'equals', 'value' => 'user'), array('field' => 'is_deleted', 'operator' => 'equals', 'value' => FALSE)), 'AND');
     $filter->addFilter(new Addressbook_Model_ContactDisabledFilter(1));
     $accounts = Addressbook_Controller_Contact::getInstance()->search($filter);
     $nextNumber = $lastNumber + 1;
     $countCreated = 0;
     foreach ($accounts as $account) {
         $filter = new HumanResources_Model_EmployeeFilter(array(array('field' => 'account_id', 'operator' => 'equals', 'value' => $account->account_id)), 'AND');
         // if not already exists
         if ($lastNumber == 0 || $this->search($filter)->count() === 0) {
             $employee = new HumanResources_Model_Employee(array('number' => $nextNumber, 'account_id' => $account->account_id, 'countryname' => $account->adr_two_countryname, 'locality' => $account->adr_two_locality, 'postalcode' => $account->adr_two_postalcode, 'region' => $account->adr_two_region, 'street' => $account->adr_two_street, 'street2' => $account->adr_two_street2, 'email' => $account->email_home, 'tel_home' => $account->tel_home, 'tel_cell' => $account->tel_cell_private, 'n_given' => $account->n_given, 'n_family' => $account->n_family, 'n_fn' => $account->n_fn, 'bday' => $account->bday, 'bank_account_holder' => $account->n_fn));
             if ($_feastCalendarId && $_workingTimeModelId && $_vacationDays) {
                 $contract = $this->createContractDataForEmployee(array('feastCalendarId' => $_feastCalendarId, 'workingTimeModelId' => $_workingTimeModelId, 'vacationDays' => $_vacationDays), $cliCall);
                 $employee->contracts = array($contract);
             }
             $countCreated++;
             if ($cliCall) {
                 echo 'Creating Employee "' . $account->n_fn . '"' . chr(10);
             }
             $this->create($employee);
             $nextNumber++;
         } else {
             if ($cliCall) {
                 echo 'Employee "' . $account->n_fn . '" already exists. Skipping...' . chr(10);
             }
         }
         if ($_deletePrivateInfo) {
             $account->adr_two_countryname = NULL;
             $account->adr_two_locality = NULL;
             $account->adr_two_postalcode = NULL;
             $account->adr_two_region = NULL;
             $account->adr_two_street = NULL;
             $account->adr_two_street2 = NULL;
             $account->email_home = NULL;
             $account->tel_home = NULL;
             $account->tel_cell_private = NULL;
             // do not remove contact image
             $account->jpegphoto = NULL;
             if ($cliCall) {
                 echo 'Removing private information of employee "' . $account->n_fn . '"' . chr(10);
             }
             Addressbook_Controller_Contact::getInstance()->update($account);
         }
     }
     if ($cliCall) {
         echo 'Created ' . $countCreated . ' employees.' . chr(10);
         echo 'Transfer OK' . chr(10);
     }
 }
 /**
  * synchronizes unassigned connections with contacts and clear old assignments
  * @param Sipgate_Model_Line/Tinebase_Record_RecordSet $_line just work on this line
  */
 public function syncContacts($_line = NULL)
 {
     $lines = NULL;
     if ($_line) {
         if ($_line instanceof Sipgate_Model_Line) {
             $lines = new Tinebase_Record_RecordSet('Sipgate_Model_Line', array($_line->toArray()));
         } elseif ($_line instanceof Tinebase_Record_RecordSet) {
             $lines = $_line;
         } else {
             throw new Tinebase_Exception_InvalidArgument('$_line must be an instance of Tinebase_Record_RecordSet or Sipgate_Model_Line!');
         }
     }
     // remove old assignments
     $filter = new Sipgate_Model_ConnectionFilter(array());
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'contact_id', 'operator' => 'not', 'value' => NULL)));
     if ($lines) {
         $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'line_id', 'operator' => 'in', 'value' => $lines->id)));
     }
     $connections = $this->search($filter);
     $contactIds = array_unique($connections->contact_id);
     $filter = new Addressbook_Model_ContactFilter(array(), 'AND');
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'id', 'operator' => 'in', 'value' => $contactIds)));
     $contacts = Addressbook_Controller_Contact::getInstance()->search($filter);
     foreach ($contactIds as $cid) {
         $contact = $contacts->filter('id', $cid)->getFirstRecord();
         $numbers = array_unique($connections->filter('contact_id', $cid)->remote_number);
         if ($contact->n_fn != $connection->contact_name || !in_array($contact->tel_assistent, $numbers) && !in_array($contact->tel_car, $numbers) && !in_array($contact->tel_cell, $numbers) && !in_array($contact->tel_cell_private, $numbers) && !in_array($contact->tel_fax, $numbers) && !in_array($contact->tel_fax_home, $numbers) && !in_array($contact->tel_home, $numbers) && !in_array($contact->tel_other, $numbers) && !in_array($contact->tel_pager, $numbers) && !in_array($contact->tel_prefer, $numbers) && !in_array($contact->tel_work, $numbers)) {
             $filter = new Sipgate_Model_ConnectionFilter(array());
             $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'contact_id', 'operator' => 'equals', 'value' => $cid)));
             $this->updateMultiple($filter, array('contact_id' => null));
         }
     }
     // add new assignments
     $filter = new Sipgate_Model_ConnectionFilter(array(), 'AND');
     $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'contact_id', 'operator' => 'equals', 'value' => null)));
     if ($lines) {
         $filter->addFilter(new Tinebase_Model_Filter_Text(array('field' => 'line_id', 'operator' => 'in', 'value' => $lines->id)));
     }
     $connections = $this->search($filter);
     $numbers = array_unique($connections->remote_number);
     foreach ($numbers as $number) {
         if ($number != 'anonymous') {
             $filter = new Addressbook_Model_ContactFilter(array(array("field" => "telephone", "operator" => "contains", "value" => $number)));
             $contact = Addressbook_Controller_Contact::getInstance()->search($filter)->getFirstRecord();
             if ($contact) {
                 $ids = $connections->filter('remote_number', $number)->id;
                 $filter = new Sipgate_Model_ConnectionFilter(array(array('field' => 'id', 'operator' => 'in', 'value' => $ids)));
                 $this->updateMultiple($filter, array('contact_id' => $contact->getId(), 'contact_name' => $contact->n_fn));
             }
         }
     }
 }