1<?php
2
3namespace ILIAS\Modules\OrgUnit\ARHelper;
4
5/**
6 * Class BaseForm
7 *
8 * @package ILIAS\Modules\OrgUnit\CtrlHelper
9 */
10abstract class BaseForm extends \ilPropertyFormGUI
11{
12
13    /**
14     * @var \ILIAS\Modules\OrgUnit\ARHelper\BaseCommands
15     */
16    protected $parent_gui;
17    /**
18     * @var \ILIAS\DI\Container
19     */
20    protected $DIC;
21    /**
22     * @var \ActiveRecord
23     */
24    protected $object;
25
26
27    /**
28     * BaseForm constructor.
29     *
30     * @param \ILIAS\Modules\OrgUnit\ARHelper\BaseCommands $parent_gui
31     * @param \ActiveRecord                                $object
32     */
33    public function __construct(BaseCommands $parent_gui, \ActiveRecord $object)
34    {
35        $this->parent_gui = $parent_gui;
36        $this->object = $object;
37        $this->dic()->ctrl()->saveParameter($parent_gui, 'arid');
38        $this->setFormAction($this->dic()->ctrl()->getFormAction($this->parent_gui));
39        $this->initFormElements();
40        $this->initButtons();
41        $this->setTarget('_top');
42        parent::__construct();
43    }
44
45
46    abstract protected function initFormElements();
47
48
49    abstract public function fillForm();
50
51
52    abstract protected function fillObject();
53
54
55    /**
56     * @return int ID of the object
57     */
58    public function saveObject()
59    {
60        if (!$this->fillObject()) {
61            return false;
62        }
63        if ($this->object->getId()) {
64            $this->object->update();
65        } else {
66            $this->object->create();
67        }
68
69        return $this->object->getId();
70    }
71
72
73    protected function initButtons()
74    {
75        if (!$this->object->getId()) {
76            $this->setTitle($this->txt('create'));
77            $this->addCommandButton(BaseCommands::CMD_CREATE, $this->txt(BaseCommands::CMD_CREATE));
78            $this->addCommandButton(BaseCommands::CMD_CANCEL, $this->txt(BaseCommands::CMD_CANCEL));
79        } else {
80            $this->setTitle($this->txt('update'));
81            $this->addCommandButton(BaseCommands::CMD_UPDATE, $this->txt(BaseCommands::CMD_UPDATE));
82            $this->addCommandButton(BaseCommands::CMD_CANCEL, $this->txt(BaseCommands::CMD_CANCEL));
83        }
84    }
85
86
87    /**
88     * @param $key
89     *
90     * @return string
91     */
92    protected function txt($key)
93    {
94        return $this->parent_gui->txt($key);
95    }
96
97
98    /**
99     * @param $key
100     *
101     * @return string
102     */
103    protected function infoTxt($key)
104    {
105        return $this->parent_gui->txt($key . '_info');
106    }
107
108
109    /**
110     * @return \ILIAS\DI\Container
111     */
112    protected function dic()
113    {
114        return $GLOBALS["DIC"];
115    }
116}
117