1<?php
2/**
3 * This class handles the db schema.
4 *
5 * PHP version 5
6 *
7 * @category Kolab
8 * @package  Kolab_Server
9 * @author   Gunnar Wrobel <wrobel@pardus.de>
10 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
11 * @link     http://pear.horde.org/index.php?package=Kolab_Server
12 */
13
14/**
15 * This class handles the db schema.
16 *
17 * Copyright 2008-2016 Horde LLC (http://www.horde.org/)
18 *
19 * See the enclosed file COPYING for license information (LGPL). If you
20 * did not receive this file, see http://www.horde.org/licenses/lgpl21.
21 *
22 * @category Kolab
23 * @package  Kolab_Server
24 * @author   Gunnar Wrobel <wrobel@pardus.de>
25 * @license  http://www.horde.org/licenses/lgpl21 LGPL 2.1
26 * @link     http://pear.horde.org/index.php?package=Kolab_Server
27 */
28class Horde_Kolab_Server_Schema_Base
29implements Horde_Kolab_Server_Schema_Interface
30{
31    /**
32     * A link to the composite server handler.
33     *
34     * @var Horde_Kolab_Server_Composite
35     */
36    private $_composite;
37
38    /**
39     * Set the composite server reference for this object.
40     *
41     * @param Horde_Kolab_Server_Composite $composite A link to the composite
42     *                                                server handler.
43     *
44     * @return NULL
45     */
46    public function setComposite(
47        Horde_Kolab_Server_Composite $composite
48    ) {
49        $this->_composite = $composite;
50    }
51
52    /**
53     * Return the schema for the given objectClass.
54     *
55     * @param string $objectclass Fetch the schema for this objectClass.
56     *
57     * @return array The schema for the given objectClass.
58     *
59     * @throws Horde_Kolab_Server_Exception If retrieval of the schema failed.
60     */
61    public function getObjectclassSchema($objectclass)
62    {
63        if (!empty($this->_config['schema_support'])) {
64            $schema = $this->_getSchema();
65            $info = $schema->get('objectclass', $objectclass);
66            $this->_handleError($info, Horde_Kolab_Server_Exception::SYSTEM);
67            return $info;
68        }
69        return parent::getObjectclassSchema($objectclass);
70    }
71
72    /**
73     * Return the schema for the given attribute.
74     *
75     * @param string $attribute Fetch the schema for this attribute.
76     *
77     * @return array The schema for the given attribute.
78     *
79     * @throws Horde_Kolab_Server_Exception If retrieval of the schema failed.
80     */
81    public function getAttributeSchema($attribute)
82    {
83        if (!empty($this->_config['schema_support'])) {
84            $schema = $this->_getSchema();
85            $info = $schema->get('attribute', $attribute);
86            $this->_handleError($info, Horde_Kolab_Server_Exception::SYSTEM);
87            return $info;
88        }
89        return parent::getAttributeSchema($attribute);
90    }
91
92    /**
93     * Return the attributes supported by the given object class.
94     *
95     * @param string $class Determine the attributes for this class.
96     *
97     * @return array The supported attributes.
98     *
99     * @throws Horde_Kolab_Server_Exception If the schema analysis fails.
100     */
101    public function getExternalAttributes($class)
102    {
103        $childclass = get_class($class);
104        $classes    = array();
105        $level      = 0;
106        while ($childclass != 'Horde_Kolab_Server_Object_Top'
107               && $level < self::MAX_HIERARCHY) {
108            $classes[]  = $childclass;
109            $childclass = get_parent_class($childclass);
110            $level++;
111        }
112
113        /** Finally add the basic object class */
114        $classes[] = $childclass;
115
116        //@todo: Throw exception here
117        if ($level == self::MAX_HIERARCHY) {
118            if (isset($this->logger)) {
119                $logger->err(sprintf('The maximal level of the object hierarchy has been exceeded for class \"%s\"!',
120                                     $class));
121            }
122        }
123
124        /**
125         * Collect attributes from bottom to top.
126         */
127        $classes = array_reverse($classes);
128
129        $attributes = array();
130
131        foreach ($classes as $childclass) {
132            $vars = get_class_vars($childclass);
133            if (isset($vars['attributes'])) {
134                /**
135                 * If the user wishes to adhere to the schema
136                 * information from the server we will skip the
137                 * attributes defined within the object class here.
138                 */
139                if (!empty($this->params['schema_override'])) {
140                    continue;
141                }
142                $attributes = array_merge($vars['attributes'], $attributes);
143            }
144        }
145
146/*         $attrs = array(); */
147
148/*         foreach ($object_classes as $object_class) { */
149/*             $info = $this->getObjectclassSchema($object_class); */
150/*             if (isset($info['may'])) { */
151/*                 $defined = array_merge($defined, $info['may']); */
152/*             } */
153/*             if (isset($info['must'])) { */
154/*                 $defined  = array_merge($defined, $info['must']); */
155/*                 $required = array_merge($required, $info['must']); */
156/*             } */
157/*             foreach ($defined as $attribute) { */
158/*                 try { */
159/*                     $attrs[$attribute] = $this->getAttributeSchema($attribute); */
160/*                 } catch (Horde_Kolab_Server_Exception $e) { */
161/*                     /\** */
162/*                      * If the server considers the attribute to be */
163/*                      * invalid we mark it. */
164/*                      *\/ */
165/*                     $attrs[$attribute] = array('invalid' => true); */
166/*                 } */
167/*             } */
168/*             foreach ($required as $attribute) { */
169/*                 $attrs[$attribute]['required'] = true; */
170/*             } */
171/*             foreach ($locked as $attribute) { */
172/*                 $attrs[$attribute]['locked'] = true; */
173/*             } */
174/*             foreach ($defaults as $attribute => $default) { */
175/*                 $attrs[$attribute]['default'] = $default; */
176/*             } */
177/*             $attrs[Horde_Kolab_Server_Object::ATTRIBUTE_OC]['default'] = $object_classes; */
178/*         } */
179/*         foreach ($derived as $key => $attributes) { */
180/*             $supported = true; */
181/*             if (isset($attributes['base'])) { */
182/*                 foreach ($attributes['base'] as $attribute) { */
183/*                     /\** */
184/*                      * Usually derived attribute are determined on basis */
185/*                      * of one or more attributes. If any of these is not */
186/*                      * supported the derived attribute should not be */
187/*                      * included into the set of supported attributes. */
188/*                      *\/ */
189/*                     if (!isset($attrs[$attribute])) { */
190/*                         unset($derived[$attribute]); */
191/*                         $supported = false; */
192/*                         break; */
193/*                     } */
194/*                 } */
195/*             } */
196/*             if ($supported) { */
197/*                 $attrs[$key] = $attributes; */
198/*             } */
199/*         } */
200/*         $check_collapsed = $collapsed; */
201/*         foreach ($check_collapsed as $key => $attributes) { */
202/*             if (isset($attributes['base'])) { */
203/*                 foreach ($attributes['base'] as $attribute) { */
204/*                     /\** */
205/*                      * Usually collapsed attribute are determined on basis */
206/*                      * of one or more attributes. If any of these is not */
207/*                      * supported the collapsed attribute should not be */
208/*                      * included into the set of supported attributes. */
209/*                      *\/ */
210/*                     if (!isset($attrs[$attribute])) { */
211/*                         unset($collapsed[$attribute]); */
212/*                     } */
213/*                 } */
214/*             } */
215/*         } */
216/*         $this->attributes[$class] = array($attrs, */
217/*                                           array( */
218/*                                               'derived'   => array_keys($derived), */
219/*                                               'collapsed' => $collapsed, */
220/*                                               'locked'    => $locked, */
221/*                                               'required'  => $required)); */
222        return $attributes;
223    }
224
225    /**
226     * Stores the attribute definitions in the cache.
227     *
228     * @return Horde_Kolab_Server The concrete Horde_Kolab_Server reference.
229     */
230    public function shutdown()
231    {
232        if (isset($this->attributes)) {
233            if (isset($this->cache)) {
234                foreach ($this->attributes as $key => $value) {
235                    $this->cache->set('attributes_' . $key, @serialize($value));
236                }
237            }
238        }
239    }
240
241
242}