1<?php
2/* Copyright (c) 1998-2012 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Factory for importer/exporter implementers
6 *
7 * @author Stefan Meyer <smeyer.ilias@gmx.de>
8 * $Id$
9 */
10class ilImportExportFactory
11{
12    const PLUGINS_DIR = "Plugins";
13
14    public static function getExporterClass($a_type)
15    {
16        /**
17         * @var $objDefinition ilObjectDefinition
18         */
19        global $DIC;
20
21        $objDefinition = $DIC['objDefinition'];
22
23        if ($objDefinition->isPlugin($a_type)) {
24            $classname = 'il' . $objDefinition->getClassName($a_type) . 'Exporter';
25            $location = $objDefinition->getLocation($a_type);
26            if (include_once $location . '/class.' . $classname . '.php') {
27                return $classname;
28            }
29        } else {
30            $comp = $objDefinition->getComponentForType($a_type);
31            $class = array_pop(explode("/", $comp));
32            $class = "il" . $class . "Exporter";
33
34            // page component plugin exporter classes are already included
35            // the component is not registered by ilObjDefinition
36            if (class_exists($class)) {
37                return $class;
38            }
39
40            // the next line had a "@" in front of the include_once
41            // I removed this because it tages ages to track down errors
42            // if the include class contains parse errors.
43            // Alex, 20 Jul 2012
44            if (include_once "./" . $comp . "/classes/class." . $class . ".php") {
45                return $class;
46            }
47        }
48
49        throw new InvalidArgumentException('Invalid exporter type given');
50    }
51
52    public static function getComponentForExport($a_type)
53    {
54        /**
55         * @var $objDefinition ilObjectDefinition
56         */
57        global $DIC;
58
59        $objDefinition = $DIC['objDefinition'];
60
61        if ($objDefinition->isPlugin($a_type)) {
62            return self::PLUGINS_DIR . "/" . $a_type;
63        } else {
64            return $objDefinition->getComponentForType($a_type);
65        }
66    }
67
68    /**
69     * Get the importer class of a component
70     *
71     * @param string $a_component	component
72     * @return string	class name of the importer class (or empty if the importer should be ignored)
73     * @throws	InvalidArgumentException	the importer class is not found but should not be ignored
74     */
75    public static function getImporterClass($a_component)
76    {
77        /**
78         * @var $objDefinition ilObjectDefinition
79         */
80        global $DIC;
81        $objDefinition = $DIC['objDefinition'];
82
83        $parts = explode('/', $a_component);
84        $component_type = $parts[0];
85        $component = $parts[1];
86
87        if ($component_type == self::PLUGINS_DIR &&
88            $objDefinition->isPlugin($component)) {
89            $classname = 'il' . $objDefinition->getClassName($component) . 'Importer';
90            $location = $objDefinition->getLocation($component);
91            if (include_once $location . '/class.' . $classname . '.php') {
92                return $classname;
93            }
94        } else {
95            $class = "il" . $component . "Importer";
96            // treat special case of page component plugins
97            // they are imported with component type PLUGINS_DIR
98            // but are not yet recognized by ilObjDefinition::isPlugin()
99            //
100            // if they are active, then their importer class is already included by ilCOPageImporter::init()
101            if (class_exists($class)) {
102                return $class;
103            }
104            // the page component plugin is not installed or not active
105            // return an empty class name instead of throwing an exception
106            // in this case the import should be continued without treating the page component
107            elseif ($component_type == self::PLUGINS_DIR) {
108                return "";
109            }
110
111            if (is_file("./" . $a_component . "/classes/class." . $class . ".php")) {
112                return $class;
113            }
114        }
115
116        throw new InvalidArgumentException('Invalid importer type given: ' . "./" . $a_component . "/classes/class." . $class . ".php");
117    }
118}
119