1<?php
2/* Copyright (c) 1998-2009 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4
5/**
6 * See bug discussion 24472
7 *
8 * Do not use this class yet. This might need a general factory interface first.
9 *
10 * This could be moved to $DIC->object() service asap.
11 *
12 *
13 * @author <killing@leifos.de>
14 *
15 */
16class ilObjectGUIFactory
17{
18    /**
19     * @var ilObjectDefinition
20     */
21    protected $obj_definition;
22
23    /**
24     * @var ilDBInterface
25     */
26    protected $db;
27
28    /**
29     * Constructor
30     * @param ilObjectDefinition|null $obj_definition
31     * @param ilDBInterface|null $db
32     */
33    public function __construct(ilObjectDefinition $obj_definition = null, ilDBInterface $db = null)
34    {
35        global $DIC;
36
37        if (is_null($obj_definition)) {
38            $obj_definition = $DIC["objDefinition"];
39        }
40        $this->obj_definition = $obj_definition;
41
42        if (is_null($db)) {
43            $db = $DIC->database();
44        }
45        $this->db = $db;
46    }
47
48
49    /**
50     * Get ilObj...GUI instance by reference id
51     *
52     * @param $a_ref_id
53     * @return mixed
54     * @throws ilObjectException
55     * @throws ilObjectNotFoundException
56     */
57    public function getInstanceByRefId($a_ref_id)
58    {
59        $obj_definition = $this->obj_definition;
60        $db = $this->db;
61
62        // check reference id
63        if (!isset($a_ref_id)) {
64            throw new ilObjectNotFoundException("ilObjectGUIFactory::getInstanceByRefId(): No ref_id given!");
65        }
66
67        // check if object exists
68        $set = $db->queryF(
69            "SELECT * FROM object_data,object_reference " .
70            "WHERE object_reference.obj_id = object_data.obj_id " .
71            "AND object_reference.ref_id = %s ",
72            array("integer"),
73            array($a_ref_id)
74            );
75        if (!($rec = $db->fetchAssoc($set))) {
76            throw new ilObjectNotFoundException("ilObjectGUIFactory::getInstanceByRefId(): Object with ref_id " . $a_ref_id . " not found!");
77        }
78
79        // check class name
80        $class_name = "ilObj" . $obj_definition->getClassName($rec["type"]) . "GUI";
81        if ($class_name == "ilObjGUI") {
82            throw new ilObjectException("ilObjectGUIFactory::getInstanceByRefId(): Not able to determine object " .
83                "class for type" . $rec["type"] . ".");
84        }
85
86        // create instance
87        $location = $obj_definition->getLocation($rec["type"]);
88        include_once($location . "/class." . $class_name . ".php");
89        $gui_obj = new $class_name("", $a_ref_id, true, false);
90
91        return $gui_obj;
92    }
93}
94