<?php

class LdapAddressPreview extends AbstractLdapPreview
{

    protected function getLdapConnectedModels($id, $ldapId)
    {
        return Addresses::model()->findByAttributes(array('ldapInterface' => $id, 'ldapId' => $ldapId));
    }

    protected function addContactToPreview($phonalisaContact, $ldapContact)
    {
        $status = $this->getContactStatus($phonalisaContact, $this->filterAttributesForValidation($ldapContact));
        $this->validateContact($phonalisaContact, $ldapContact, $status);
        $this->preview[] = $ldapContact;
    }

    protected function prependContact($contact, $status)
    {
        $address = new Addresses();

        $this->validateContact($address, $contact, $status);
        array_unshift($this->preview, $contact);
    }

    private function validateContact($address, &$contact, $status)
    {
        $address->attributes = $this->filterAttributesForValidation($contact);
        $address->scenario = 'LdapImport';
        if (!$address->validate()) {
            $errors = $address->errors;
            unset($errors['contact_owner']);
            unset($errors['contact_tid']);
            unset($errors['ldapInterface']);
            if (!empty($errors)) {
                $status = 'invalid';
            }
            $contact['errors'] = $this->getErrors($errors);
        }

        $contact['status'] = $status;
    }
}
