1<?php
2/* Copyright (c) 1998-2015 ILIAS open source, Extended GPL, see docs/LICENSE */
3
4/**
5 * Class ilForumCronNotificationDataProvider
6 *
7 * @author Nadia Matuschek <nmatuschek@databay.de>
8 */
9class ilForumCronNotificationDataProvider implements ilForumNotificationMailData
10{
11    /**
12     * @var null
13     */
14    public $notification_type = null;
15
16    /**
17     * @var int $ref_id
18     */
19    protected $ref_id = 0;
20
21    /**
22     * @var int $obj_id
23     */
24    protected $obj_id = 0;
25
26    /**
27     * @var int
28     */
29    protected $forum_id = 0;
30
31    /**
32     * @var string $forum_title
33     */
34    protected $forum_title = '';
35
36    /**
37     * @var int
38     */
39    protected $thread_id = 0;
40
41    /**
42     * @var string $thread_title
43     */
44    protected $thread_title = '';
45
46    /**
47     * @var int
48     */
49    protected $post_id = 0;
50    /**
51     * @var string
52     */
53    protected $post_title = '';
54    /**
55     * @var string
56     */
57    protected $post_message = '';
58    /**
59     * @var null
60     */
61    protected $post_date = null;
62    /**
63     * @var null
64     */
65    protected $post_update = null;
66
67    /**
68     * @var bool
69     */
70    protected $post_censored = false;
71    /**
72     * @var null
73     */
74    protected $post_censored_date = null;
75    /**
76     * @var string
77     */
78    protected $post_censored_comment = '';
79
80    /**
81     * @var string
82     */
83    protected $pos_usr_alias = '';
84    /**
85     * @var int
86     */
87    protected $pos_display_user_id = 0;
88
89    /**
90     * @var bool
91     */
92    protected $is_anonymized = false;
93
94    /**
95     * @var int|string
96     */
97    protected $import_name = '';
98
99    /**
100     * @var array $attachments
101     */
102    protected $attachments = array();
103
104    /**
105     * @var array $cron_recipients user_ids
106     */
107    protected $cron_recipients = array();
108
109    /**
110     * @var ilForumPost|null
111     */
112    public $objPost = null;
113
114    /**
115     * @var int
116     */
117    public $post_update_user_id = 0;
118
119    /**
120     * @var int
121     */
122    public $pos_author_id = 0;
123
124    /**
125     * @var string
126     */
127    public $deleted_by = '';
128
129    /**
130     * @var \ilForumAuthorInformation[]
131     */
132    protected static $authorInformationCache = array();
133
134
135    /** @var string|null $post_user_name */
136    private $post_user_name = null;
137
138    /** @var string|null */
139    private $update_user_name = null;
140
141    /** @var ilForumNotificationCache */
142    private $notificationCache;
143
144    /**
145     * @param $row
146     * @param ilForumNotificationCache|null $notificationCache
147     */
148    public function __construct($row, ilForumNotificationCache $notificationCache = null)
149    {
150        $this->obj_id = $row['obj_id'];
151        $this->ref_id = $row['ref_id'];
152
153        $this->thread_id = $row['thread_id'];
154        $this->thread_title = $row['thr_subject'];
155
156        $this->forum_id = $row['pos_top_fk'];
157        $this->forum_title = $row['top_name'];
158
159        $this->post_id = $row['pos_pk'];
160        $this->post_title = $row['pos_subject'];
161        $this->post_message = $row['pos_message'];
162        $this->post_date = $row['pos_date'];
163        $this->post_update = $row['pos_update'];
164        $this->post_update_user_id = $row['update_user'];
165
166        $this->post_censored = $row['pos_cens'];
167        $this->post_censored_date = $row['pos_cens_date'];
168        $this->post_censored_comment = $row['pos_cens_com'];
169
170        $this->pos_usr_alias = $row['pos_usr_alias'];
171        $this->pos_display_user_id = $row['pos_display_user_id'];
172        $this->pos_author_id = $row['pos_author_id'];
173
174        $this->import_name = strlen($row['import_name']) ? $row['import_name'] : '';
175
176        if ($notificationCache === null) {
177            $notificationCache = new ilForumNotificationCache();
178        }
179        $this->notificationCache = $notificationCache;
180
181        if (isset($row['deleted_by'])) {
182            //cron context
183            $this->deleted_by = $row['deleted_by'];
184        } else {
185            //  fallback
186            global $DIC;
187            $this->deleted_by = $DIC->user()->getLogin();
188        }
189
190        $this->read();
191    }
192
193    /**
194     *
195     */
196    protected function read()
197    {
198        $this->readAttachments();
199    }
200
201    /**
202     *
203     */
204    private function readAttachments()
205    {
206        if (ilForumProperties::isSendAttachmentsByMailEnabled()) {
207            // get attachments
208            $fileDataForum = new ilFileDataForum($this->getObjId(), $this->getPostId());
209            $filesOfPost = $fileDataForum->getFilesOfPost();
210
211            foreach ($filesOfPost as $attachment) {
212                $this->attachments[] = $attachment['name'];
213            }
214        }
215    }
216
217    /**
218     * @param int $user_id
219     */
220    public function addRecipient($user_id)
221    {
222        $this->cron_recipients[] = (int) $user_id;
223    }
224
225    /**
226     * @return array
227     */
228    public function getCronRecipients()
229    {
230        return $this->cron_recipients;
231    }
232
233    /**
234     * @return int
235     */
236    public function getRefId()
237    {
238        return $this->ref_id;
239    }
240
241    /**
242     * @return int
243     */
244    public function getObjId()
245    {
246        return $this->obj_id;
247    }
248
249    /**
250     * @return int frm_data.top_pk
251     */
252    public function getForumId()
253    {
254        return $this->forum_id;
255    }
256
257    /**
258     * @return string frm_data.top_name
259     */
260    public function getForumTitle()
261    {
262        return $this->forum_title;
263    }
264
265    /**
266     * @return int
267     */
268    public function getThreadId()
269    {
270        return $this->thread_id;
271    }
272
273    /**
274     * @return string frm_threads.thr_subject
275     */
276    public function getThreadTitle()
277    {
278        return $this->thread_title;
279    }
280
281    /**
282     * @return int
283     */
284    public function getPostId()
285    {
286        return $this->post_id;
287    }
288
289    /**
290     * @return string frm_posts.pos_subject
291     */
292    public function getPostTitle()
293    {
294        return $this->post_title;
295    }
296
297    /**
298     * @return string frm_posts.pos_message
299     */
300    public function getPostMessage()
301    {
302        return $this->post_message;
303    }
304
305    /**
306     * @return string frm_posts.pos_date
307     */
308    public function getPostDate()
309    {
310        return $this->post_date;
311    }
312
313    /**
314     * @return string frm_posts.pos_update
315     */
316    public function getPostUpdate()
317    {
318        return $this->post_update;
319    }
320
321    /**
322     * @return string frm_posts.pos_cens
323     */
324    public function getPostCensored()
325    {
326        return $this->post_censored;
327    }
328
329    /**
330     * @return string frm_posts.pos_cens_date
331     */
332    public function getPostCensoredDate()
333    {
334        return $this->post_censored_date;
335    }
336
337    /**
338     * @return string
339     */
340    public function getCensorshipComment()
341    {
342        return $this->post_censored_comment;
343    }
344
345    /**
346     * @return array file names
347     */
348    public function getAttachments()
349    {
350        return $this->attachments;
351    }
352
353    /**
354     * @param null $notification_type
355     */
356    public function setNotificationType($notification_type)
357    {
358        $this->notification_type = $notification_type;
359    }
360
361    /**
362     * @return int
363     */
364    public function getPosDisplayUserId()
365    {
366        return $this->pos_display_user_id;
367    }
368
369
370    /**
371     * @return string frm_posts.pos_usr_alias
372     */
373    public function getPosUserAlias()
374    {
375        return $this->pos_usr_alias;
376    }
377
378    /**
379     * @return int
380     */
381    public function getPostUpdateUserId()
382    {
383        return $this->post_update_user_id;
384    }
385
386    /**
387     * @param int $post_update_user_id
388     */
389    public function setPostUpdateUserId($post_update_user_id)
390    {
391        $this->post_update_user_id = $post_update_user_id;
392    }
393
394    public function setPosAuthorId($pos_author_id)
395    {
396        $this->pos_author_id = $pos_author_id;
397    }
398    public function getPosAuthorId()
399    {
400        return $this->pos_author_id;
401    }
402
403    /**
404     * @return bool
405     */
406    public function isAnonymized()
407    {
408        return $this->is_anonymized;
409    }
410
411    /**
412     * @return int
413     */
414    public function getDeletedBy()
415    {
416        return $this->deleted_by;
417    }
418
419    /**
420     * @return string
421     */
422    public function getImportName()
423    {
424        return $this->import_name;
425    }
426
427    /**
428     * @inheritdoc
429     */
430    public function getPostUserName(\ilLanguage $user_lang)
431    {
432        if ($this->post_user_name === null) {
433            $this->post_user_name = $this->getPublicUserInformation(self::getAuthorInformation(
434                $user_lang,
435                (int) $this->getPosAuthorId(),
436                (int) $this->getPosDisplayUserId(),
437                (string) $this->getPosUserAlias(),
438                (string) $this->getImportName()
439            ));
440        }
441
442        return $this->post_user_name;
443    }
444
445    /**
446     * @inheritdoc
447     */
448    public function getPostUpdateUserName(\ilLanguage $user_lang)
449    {
450        if ($this->update_user_name === null) {
451            $this->update_user_name = $this->getPublicUserInformation(self::getAuthorInformation(
452                $user_lang,
453                (int) $this->getPosAuthorId(),
454                (int) $this->getPostUpdateUserId(),
455                (string) $this->getPosUserAlias(),
456                (string) $this->getImportName()
457            ));
458        }
459
460        // Possible Fix for #25432
461        if (
462            $this->getPosUserAlias() && $this->getPosDisplayUserId() == 0 &&
463            $this->getPosAuthorId() == $this->getPostUpdateUserId()
464        ) {
465            return (string) $this->getPosUserAlias();
466        }
467
468        return (string) $this->update_user_name;
469    }
470
471    /**
472     * @param ilForumAuthorInformation $authorinfo
473     * @return string
474     */
475    public function getPublicUserInformation(ilForumAuthorInformation $authorinfo)
476    {
477        $publicName = $authorinfo->getAuthorShortName();
478
479        if ($authorinfo->hasSuffix()) {
480            $publicName = $authorinfo->getAuthorName();
481        } elseif ($authorinfo->getAuthorName() && !$this->isAnonymized()) {
482            $publicName = $authorinfo->getAuthorName();
483        }
484
485        return $publicName;
486    }
487
488    /**
489     * @param ilLanguage $lng
490     * @param            $authorUsrId
491     * @param            $displayUserId
492     * @param            $usrAlias
493     * @param            $importName
494     * @return \ilForumAuthorInformation
495     */
496    private function getAuthorInformation(
497        \ilLanguage $lng,
498        int $authorUsrId,
499        int $displayUserId,
500        string $usrAlias,
501        string $importName
502    ) {
503        $cacheKey = $this->notificationCache->createKeyByValues(array(
504            $lng->getLangKey(),
505            (int) $authorUsrId,
506            (int) $displayUserId,
507            (string) $usrAlias,
508            (string) $importName
509        ));
510
511        if (false === $this->notificationCache->exists($cacheKey)) {
512            $authorInformation = new ilForumAuthorInformation(
513                $authorUsrId,
514                $displayUserId,
515                $usrAlias,
516                $importName,
517                array(),
518                $lng
519            );
520
521            $this->notificationCache->store($cacheKey, $authorInformation);
522        }
523
524        return $this->notificationCache->fetch($cacheKey);
525    }
526}
527