XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
viewAction.class.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
18{
19 private string $inout = 'inbox';
20 private ?array $msgdata = null;
21 private $mService;
22
23 public function execute()
24 {
25 if ('in' == $this->root->mContext->mRequest->getRequest('inout')) {
26 $this->inout = 'inbox';
27 } else {
28 $this->inout = 'outbox';
29 }
30
31 $boxid = (int)$this->root->mContext->mRequest->getRequest($this->inout);
32 $modHand = xoops_getmodulehandler($this->inout);
33 $modObj = $modHand->get($boxid);
34 if (!is_object($modObj)) {
35 if ($this->root->mContext->mRequest->getRequest('ajax') == 1) {
36 header('Content-Type: application/json');
37 echo json_encode(['error' => _MD_MESSAGE_ACTIONMSG1]);
38 exit;
39 }
40 $this->setErr(_MD_MESSAGE_ACTIONMSG1);
41 return;
42 }
43 if ($modObj->get('uid') != $this->root->mContext->mXoopsUser->get('uid')) {
44 if ($this->root->mContext->mRequest->getRequest('ajax') == 1) {
45 header('Content-Type: application/json');
46 echo json_encode(['error' => _MD_MESSAGE_ACTIONMSG8]);
47 exit;
48 }
49 $this->setErr(_MD_MESSAGE_ACTIONMSG8);
50 return;
51 }
52
53 // start After $modObj is loaded and validated
54 $uid = $this->root->mContext->mXoopsUser->get('uid');
55 $inboxTable = $modHand->mTable;
56
57 $key = ($this->inout === 'inbox') ? 'inbox_id' : 'outbox_id';
58 $this->msgdata['param'] = ($this->inout === 'inbox') ? 'inbox' : 'outbox';
59 $this->msgdata['inout_short'] = ($this->inout === 'inbox') ? 'in' : 'out';
60 $currentId = $modObj->get($key);
61
62 // Previous message (smaller ID)
63 $sqlPrev = "SELECT $key FROM $inboxTable WHERE uid=$uid AND $key < $currentId ORDER BY $key DESC LIMIT 1";
64 $resultPrev = $modHand->db->query($sqlPrev);
65 $prevId = ($row = $modHand->db->fetchArray($resultPrev)) ? $row[$key] : null;
66
67 // Next message (larger ID)
68 $sqlNext = "SELECT $key FROM $inboxTable WHERE uid=$uid AND $key > $currentId ORDER BY $key ASC LIMIT 1";
69 $resultNext = $modHand->db->query($sqlNext);
70 $nextId = ($row = $modHand->db->fetchArray($resultNext)) ? $row[$key] : null;
71
72 // Build navigation and $this->msgdata
73 foreach (array_keys($modObj->gets()) as $var_name) {
74 $this->msgdata[$var_name] = $modObj->getShow($var_name);
75 }
76 if ('inbox' == $this->inout) {
77 $this->msgdata['fromname'] = $this->getLinkUnameFromId($this->msgdata['from_uid'], $this->msgdata['uname']);
78 } else {
79 $this->msgdata['toname'] = $this->getLinkUnameFromId($this->msgdata['to_uid'], $this->root->mContext->mXoopsConfig['anonymous']);
80 }
81 $this->msgdata['prev_id'] = $prevId;
82 $this->msgdata['next_id'] = $nextId;
83 $this->msgdata['key'] = $key;
84 $this->msgdata['utime'] = $modObj->get('utime');
85
86 // --- AJAX block: must be AFTER $this->msgdata is built ---
87 if ($this->root->mContext->mRequest->getRequest('ajax') == 1) {
88 header('Content-Type: application/json');
89
90 // Handle AJAX lock/unlock requests
91 if ('inbox' == $this->inout && 'POST' == $_SERVER['REQUEST_METHOD'] &&
92 'lock' == $this->root->mContext->mRequest->getRequest('cmd')) {
93
94 if (1 == (int)$this->root->mContext->mRequest->getRequest('lock')) {
95 $modObj->set('is_read', 2); // Lock the message
96 } else {
97 $modObj->set('is_read', 1); // Unlock the message
98 }
99 $modHand->insert($modObj);
100
101 // Update the is_read value in msgdata to reflect the change
102 $this->msgdata['is_read'] = $modObj->get('is_read');
103 }
104 // Update message status for inbox messages if unread
105 elseif ('inbox' == $this->inout && 0 == $modObj->get('is_read')) {
106 $modObj->set('is_read', 1);
107 $modHand->insert($modObj, true);
108 // Update the is_read value in msgdata to reflect the change
109 $this->msgdata['is_read'] = 1;
110 }
111
112 // Get the avatar URL using the same function as the Smarty modifier
113 $avatarUrl = '';
114 if ('inbox' == $this->inout && isset($this->msgdata['from_uid'])) {
115 $handler = xoops_gethandler('user');
116 $user = $handler->get(intval($this->msgdata['from_uid']));
117 if (is_object($user) && $user->isActive() && ($user->get('user_avatar') != "blank.gif") && file_exists(XOOPS_UPLOAD_PATH . "/" . $user->get('user_avatar'))) {
118 $avatarUrl = XOOPS_UPLOAD_URL . "/" . $user->getShow('user_avatar');
119 } else {
120 $avatarUrl = XOOPS_URL . "/modules/user/images/no_avatar.gif";
121 }
122 }
123
124 echo json_encode([
125 'subject' => $this->msgdata['title'],
126 'body' => $this->msgdata['message'],
127 'from' => $this->msgdata['fromname'] ?? '',
128 'to' => $this->msgdata['toname'] ?? '',
129 'date' => isset($this->msgdata['utime']) ? date('Y-m-d H:i', $this->msgdata['utime']) : '',
130 'prev_id' => $this->msgdata['prev_id'],
131 'next_id' => $this->msgdata['next_id'],
132 'key' => $this->msgdata['key'],
133 'inout' => $this->inout,
134 'param' => $this->msgdata['param'],
135 'inout_short' => $this->msgdata['inout_short'],
136 'avatar_url' => $avatarUrl,
137 'from_uid' => $this->msgdata['from_uid'] ?? 0,
138 'is_read' => $this->msgdata['is_read'] ?? 1,
139 'success' => true,
140 'message' => 'lock' == $this->root->mContext->mRequest->getRequest('cmd') ?
141 ($this->msgdata['is_read'] == 2 ? 'Message locked' : 'Message unlocked') :
142 'Message loaded'
143 ]);
144 exit;
145 }
146
147 if ('inbox' == $this->inout) {
148 if ('POST' == $_SERVER['REQUEST_METHOD']) {
149 if ('lock' == $this->root->mContext->mRequest->getRequest('cmd')) {
150 if (1 == (int)$this->root->mContext->mRequest->getRequest('lock')) {
151 $modObj->set('is_read', 2);
152 } else {
153 $modObj->set('is_read', 1);
154 }
155 $modHand->insert($modObj);
156 } elseif ('mail' == $this->root->mContext->mRequest->getRequest('cmd')) {
157 $this->send_mail($modObj);
158 }
159 } elseif (0 == $modObj->get('is_read')) {
160 $modObj->set('is_read', 1);
161 $modHand->insert($modObj, true);
162 }
163 }
168 if ('outbox' == $this->inout) {
169 if ('POST' == $_SERVER['REQUEST_METHOD']) {
170 if ('mail' == $this->root->mContext->mRequest->getRequest('cmd')) {
171 $this->send_mail($modObj);
172 }
173 }
174 }
175
176 // service UserSearch
177 $this->mService = $this->root->mServiceManager->getService('UserSearch');
178 }
179
180 private function send_mail(&$obj)
181 {
182 $mailer = $this->getMailer();
183 $mailer->setFromName($this->root->mContext->mXoopsConfig['sitename']);
184 $mailer->setFromEmail($this->root->mContext->mXoopsConfig['adminmail']);
185 $mailer->setToEmails($this->root->mContext->mXoopsUser->get('email'));
186 $mailer->setSubject($obj->get('title'));
187 $mailer->setBody($obj->get('message'));
188 $mailer->send();
189 }
190
191 public function executeView(&$render)
192 {
193 if ('inbox' == $this->inout) {
194 $render->setTemplateName('message_inboxview.html');
195 } else {
196 $render->setTemplateName('message_outboxview.html');
197 }
198 $render->setAttribute('msgdata', $this->msgdata);
199 $render->setAttribute('UserSearch', $this->mService);
200 $render->setAttribute('message_url', XOOPS_URL.'/modules/message/index.php');
201 $render->setAttribute('inout', $this->inout); // Previous and Next urls
202 }
203}