1<?php
2/* Copyright (c) 1998-2018 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Collection of basic placeholder values that can be used
6 * @author  Niels Theen <ntheen@databay.de>
7 */
8class ilDefaultPlaceholderValues implements ilCertificatePlaceholderValues
9{
10    /**
11     * @var array
12     */
13    private $placeholder;
14
15    /**
16     * @var ilCertificateObjectHelper
17     */
18    private $objectHelper;
19
20    /**
21     * @var ilCertificateDateHelper
22     */
23    private $dateHelper;
24
25    /**
26     * @var integer
27     */
28    private $dateFormat;
29
30    /**
31     * @var ilLanguage|null
32     */
33    private $language;
34
35    /**
36     * @var ilCertificateUtilHelper|null
37     */
38    private $utilHelper;
39
40    /**
41     * @var ilUserDefinedFieldsPlaceholderValues|null
42     */
43    private $userDefinedFieldsPlaceholderValues;
44
45    /**
46     * @var int
47     */
48    private $birthdayDateFormat;
49
50    /**
51     * @param ilCertificateObjectHelper                 $objectHelper
52     * @param ilCertificateDateHelper                   $dateHelper
53     * @param int                                       $dateFormat
54     * @param ilLanguage|null                           $language
55     * @param ilCertificateUtilHelper|null              $utilHelper
56     * @param ilUserDefinedFieldsPlaceholderValues|null $userDefinedFieldsPlaceholderValues
57     * @param int                                       $birthdayDateFormat
58     */
59    public function __construct(
60        ilCertificateObjectHelper $objectHelper = null,
61        ilCertificateDateHelper $dateHelper = null,
62        int $dateFormat = null,
63        ilLanguage $language = null,
64        ilCertificateUtilHelper $utilHelper = null,
65        ilUserDefinedFieldsPlaceholderValues $userDefinedFieldsPlaceholderValues = null,
66        $birthdayDateFormat = IL_CAL_DATE
67    ) {
68        if (null === $objectHelper) {
69            $objectHelper = new ilCertificateObjectHelper();
70        }
71        $this->objectHelper = $objectHelper;
72
73        if (null === $dateHelper) {
74            $dateHelper = new ilCertificateDateHelper();
75        }
76        $this->dateHelper = $dateHelper;
77
78        if (null === $dateFormat) {
79            $dateFormat = IL_CAL_UNIX;
80        }
81        $this->dateFormat = $dateFormat;
82
83        if (null === $language) {
84            global $DIC;
85            $language = $DIC->language();
86            $language->loadLanguageModule('certificate');
87        }
88        $this->language = $language;
89
90        if (null === $utilHelper) {
91            $utilHelper = new ilCertificateUtilHelper();
92        }
93        $this->utilHelper = $utilHelper;
94
95        if (null === $userDefinedFieldsPlaceholderValues) {
96            $userDefinedFieldsPlaceholderValues = new ilUserDefinedFieldsPlaceholderValues();
97        }
98        $this->userDefinedFieldsPlaceholderValues = $userDefinedFieldsPlaceholderValues;
99
100        $this->birthdayDateFormat = $birthdayDateFormat;
101
102        $this->placeholder = array(
103            'USER_LOGIN' => '',
104            'USER_FULLNAME' => '',
105            'USER_FIRSTNAME' => '',
106            'USER_LASTNAME' => '',
107            'USER_TITLE' => '',
108            'USER_SALUTATION' => '',
109            'USER_BIRTHDAY' => '',
110            'USER_INSTITUTION' => '',
111            'USER_DEPARTMENT' => '',
112            'USER_STREET' => '',
113            'USER_CITY' => '',
114            'USER_ZIPCODE' => '',
115            'USER_COUNTRY' => '',
116            'USER_MATRICULATION' => '',
117            'DATE' => '',
118            'DATETIME' => '',
119            'DATE_COMPLETED' => '',
120            'DATETIME_COMPLETED' => '',
121        );
122    }
123
124    /**
125     * @param $userId
126     * @param $objId
127     * @return array - Array with a mapping of [placholder_key] => actual value
128     * @throws ilException
129     */
130    public function getPlaceholderValues(int $userId, int $objId) : array
131    {
132        /** @var ilObjUser $user */
133        $user = $this->objectHelper->getInstanceByObjId($userId);
134        if (!$user instanceof ilObjUser) {
135            throw new ilException('The entered id: ' . $userId . ' is not an user object');
136        }
137
138        $placeholder = $this->placeholder;
139
140        $placeholder['USER_LOGIN'] = $this->utilHelper->prepareFormOutput((trim($user->getLogin())));
141        $placeholder['USER_FULLNAME'] = $this->utilHelper->prepareFormOutput((trim($user->getFullname())));
142        $placeholder['USER_FIRSTNAME'] = $this->utilHelper->prepareFormOutput((trim($user->getFirstname())));
143        $placeholder['USER_LASTNAME'] = $this->utilHelper->prepareFormOutput((trim($user->getLastname())));
144        $placeholder['USER_TITLE'] = $this->utilHelper->prepareFormOutput((trim($user->getUTitle())));
145
146        $salutation = '';
147        $gender = $user->getGender();
148        if (is_string($gender) && strlen(trim($gender)) > 0 && strtolower($gender) !== 'n') {
149            $salutation = $this->utilHelper->prepareFormOutput($this->language->txt("salutation_" . trim($gender)));
150        }
151
152        $placeholder['USER_SALUTATION'] = $salutation;
153
154        $birthday = '';
155        $dateObject = $user->getBirthday();
156        if (null !== $dateObject) {
157            $birthday = $this->dateHelper->formatDate($dateObject, $this->birthdayDateFormat);
158        }
159
160        $placeholder['USER_BIRTHDAY'] = $this->utilHelper->prepareFormOutput((trim($birthday)));
161        $placeholder['USER_INSTITUTION'] = $this->utilHelper->prepareFormOutput((trim($user->getInstitution())));
162        $placeholder['USER_DEPARTMENT'] = $this->utilHelper->prepareFormOutput((trim($user->getDepartment())));
163        $placeholder['USER_STREET'] = $this->utilHelper->prepareFormOutput((trim($user->getStreet())));
164        $placeholder['USER_CITY'] = $this->utilHelper->prepareFormOutput((trim($user->getCity())));
165        $placeholder['USER_ZIPCODE'] = $this->utilHelper->prepareFormOutput((trim($user->getZipcode())));
166        $placeholder['USER_COUNTRY'] = $this->utilHelper->prepareFormOutput((trim($user->getCountry())));
167        $placeholder['USER_MATRICULATION'] = $this->utilHelper->prepareFormOutput((trim($user->getMatriculation())));
168        $placeholder['DATE'] = $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDate(time(), $this->dateFormat))));
169        $placeholder['DATETIME'] = $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDatetime(time(), $this->dateFormat))));
170
171        $placeholder = array_merge($placeholder, $this->userDefinedFieldsPlaceholderValues->getPlaceholderValues($userId, $objId));
172
173        return $placeholder;
174    }
175
176    /**
177     * This method is different then the 'getPlaceholderValues' method, this
178     * method is used to create a placeholder value array containing dummy values
179     * that is used to create a preview certificate.
180     * Due the fact that this is a class to create default values
181     * the placeholder values will be identical to the description
182     * @param int $userId
183     * @param int $objId
184     * @return mixed
185     * @throws ilDateTimeException
186     * @throws ilException
187     * @throws ilInvalidCertificateException
188     */
189    public function getPlaceholderValuesForPreview(int $userId, int $objId) : array
190    {
191        $previewPlacholderValues = array(
192            "USER_LOGIN" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_login")),
193            "USER_FULLNAME" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_fullname")),
194            "USER_FIRSTNAME" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_firstname")),
195            "USER_LASTNAME" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_lastname")),
196            "USER_TITLE" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_title")),
197            "USER_SALUTATION" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_salutation")),
198            "USER_BIRTHDAY" => $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDate(time(), $this->dateFormat)))),
199            "USER_INSTITUTION" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_institution")),
200            "USER_DEPARTMENT" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_department")),
201            "USER_STREET" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_street")),
202            "USER_CITY" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_city")),
203            "USER_ZIPCODE" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_zipcode")),
204            "USER_COUNTRY" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_country")),
205            "USER_MATRICULATION" => $this->utilHelper->prepareFormOutput($this->language->txt("certificate_var_user_matriculation")),
206            'DATE' => $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDate(time(), $this->dateFormat)))),
207            'DATETIME' => $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDatetime(time(), $this->dateFormat)))),
208            'DATE_COMPLETED' => $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDate(time(), $this->dateFormat)))),
209            'DATETIME_COMPLETED' => $this->utilHelper->prepareFormOutput((trim($this->dateHelper->formatDatetime(time(), $this->dateFormat))))
210        );
211
212        return array_merge($previewPlacholderValues, $this->userDefinedFieldsPlaceholderValues->getPlaceholderValuesForPreview($userId, $objId));
213    }
214}
215