<?php

include dirname(__FILE__) . '/../../../../lib/AbstractXmlPhonebookApp.php';

abstract class AbstractGigasetXmlPhonebookApp extends AbstractXmlPhonebookApp
{

    protected static $limit = 16;
    protected $offset = 0;
    public $searchAttributes = array();
    public $requestKeys = array(
        'ln' => 'n_family',
        'fn' => 'n_given',
        'ct' => 'adr_one_locality',
        'zc' => 'adr_one_postalcode',
        'st' => 'adr_one_street',
        'cpn' => 'org_name',
        'em' => 'contact_email',
        'hm' => 'tel_work',
        'mb' => 'tel_cell'
    );

    protected function setAttributes()
    {
        $this->mPhone = $this->getPhoneModel();

        if ($this->mPhone != null) {
            $sipAccount = $this->mPhone->getSipAccount();
            if (isset($_REQUEST['sipid'])) {
                $deviceName = substr($sipAccount->device_name, 0, strpos($sipAccount->device_name, '+')) . '*'; // HOTFIX: [PDEV-351] Hotfix because of "query to long/wrong"-bug
                $sipAccount = SipAccounts::model()->findByAttributes(array('device_name' => $deviceName, 'authname' => $_REQUEST['sipid']));
            }
            $this->mSipAccount = $sipAccount;
            if ($this->mSipAccount) {
                if ($this->mSipAccount->getUser() != null) {
                    $this->mUser = $this->mSipAccount->getUser();
                    Yii::app()->user->setId($this->mUser->id);
                    Yii::app()->user->setState('sudoUser', $this->mUser->id);
                }
            }
        }

        $pbID = $this->mPhone->group_phonebook;
        if (isset($_REQUEST['type']) && $_REQUEST['type'] == 'pr') {
            $pbID = $this->mPhone->privat_phonebook;
        }

        $this->pbID = $pbID;
        $this->setSearchAttributes();
    }

    protected function setSearchAttributes()
    {
        $requestKeys = $this->requestKeys;
        $searchRequest = $_REQUEST;
        if (isset($searchRequest['set_first'])) {
            $searchRequest['ln'] = $searchRequest['set_first'];
        }

        foreach ($searchRequest as $key => $value) {
            if ($value != '*' && isset($requestKeys[$key])) {
                $this->searchAttributes[$key] = trim(str_replace('*', '', $value));
            }
        }
    }

    public function run($controller)
    {
        $this->setAttributes();
        if ($this->mUser == null) {
            die("not allowed.");
        }

        if ($this->pbID) {
            return $this->showAddresses();
        }
    }

    protected function showAddresses()
    {
        // selected phonebook is now known!
        //check permission
        if ($this->mUser->hasPermission($this->pbID, "read")) {
            $book = Addressbook::model()->findByPk($this->pbID);
            if ($book) {
                if ($book->type == AddressbookType::personal) {
                    $search = array('contact_tid' => $book->id, 'contact_owner' => $this->mUser->id);
                    $addresses = Addresses::model()->find($this->getSearchArray($search), 'n_family', AbstractGigasetXmlPhonebookApp::$limit, $this->offset);
                } else {
                    $search = array('contact_tid' => $book->id);
                    $addresses = Addresses::model()->find($this->getSearchArray($search), 'n_family', AbstractGigasetXmlPhonebookApp::$limit, $this->offset);
                }
                $addressCount = count($addresses);
                $this->renderPhoneAdresses($addresses, $addressCount);
            }
        } else {
            die("not allowed.");
        }
    }

    protected function getSearchArray($search)
    {
        foreach ($this->searchAttributes as $searchKey => $searchAttribute) {
            $search[$this->requestKeys[$searchKey]] = implode('*|', permute($searchAttribute)) . '*';
        }
        return $search;
    }
}
