<?php
/**
 * @package Horde_Kolab
 *
 * $Horde: framework/Kolab/Kolab/XML/distributionlist.php,v 1.1.2.1 2008/04/24 12:11:57 wrobel Exp $
 */

/**
 * Kolab XML handler for distributionlist groupware objects
 *
 * $Horde: framework/Kolab/Kolab/XML/distributionlist.php,v 1.1.2.1 2008/04/24 12:11:57 wrobel Exp $
 *
 * Copyright 2007-2008 The Horde Project (http://www.horde.org/)
 *
 * @author  Thomas Jarosch <thomas.jarosch@intra2net.com>
 * @author  Gunnar Wrobel <wrobel@pardus.de>
 * @author  Mike Gabriel <m.gabriel@das-netzwerkteam.de>
 * @package Horde_Kolab
 */
class Horde_Kolab_XML_distributionlist extends Horde_Kolab_XML {

    /**
     * Specific data fields for the contact object
     *
     * @var array
     */
    var $_fields_specific;

    /**
     * Constructor
     */
    function Horde_Kolab_XML_distributionlist()
    {
        $this->_root_name = "distribution-list";

        /** Specific task fields, in kolab format specification order
         */
        $this->_fields_specific = array(
                'display-name' => array(
                    'type'    => HORDE_KOLAB_XML_TYPE_STRING,
                    'value'   => HORDE_KOLAB_XML_VALUE_NOT_EMPTY
                ),
                'member' => array(
                    'type'    => HORDE_KOLAB_XML_TYPE_MULTIPLE,
                    'value'   => HORDE_KOLAB_XML_VALUE_MAYBE_MISSING,
                    'array'   => $this->_fields_simple_person,
                )
            );

        parent::Horde_Kolab_XML();
    }

    /**
     * Load the groupware object based on the specifc XML values.
     *
     * @param array $children An array of XML nodes
     *
     * @return array Array with data or PEAR error
     */
    function _load(&$children)
    {
        $object = $this->loadArray($children, $this->_fields_specific);

        // Map the display-name of a kolab dist list to horde's lastname attribute
        if (isset($object['display-name'])) {
            $object['last-name'] = $object['display-name'];
            unset($object['display-name']);
        }

        // the mapping from $object['member'] as stored in XML back to Turba_Objects (contacts)
        // must be performed in the Kolab_IMAP storage driver as we need access to the search
        // facilities of the kolab storage driver.

        $object['__type'] = 'Group';
        return $object;
    }

    /**
     * Save the  specifc XML values.
     *
     * @param array $root     The XML document root
     * @param array $object   The resulting data array
     *
     * @return boolean True on success, a PEAR error otherwise
     */
    function _save($root, $object)
    {
        // Map the display-name of a kolab dist list to horde's lastname attribute
        if (isset($object['last-name'])) {
            $object['display-name'] = $object['last-name'];
            unset($object['last-name']);
        }

        $result = $this->saveArray($root, $object, $this->_fields_specific);
        if (is_a($result, 'PEAR_Error')) {
            return $result;
        }
        return true;
    }
}
