XCL Web Application Platform 2.5.0
The XoopsCube Legacy Project
Loading...
Searching...
No Matches
deleteallAction.class.php
1<?php
12
13if (!defined('XOOPS_ROOT_PATH')) {
14 exit();
15}
16
18{
19 private string $inout = 'inbox';
20
21 public function __construct()
22 {
23 parent::__construct();
24 }
25
26 public function execute()
27 {
28 $isAjax = $this->root->mContext->mRequest->getRequest('ajax') == 1;
29
30 if ('in' == $this->root->mContext->mRequest->getRequest('inout')) {
31 $this->inout = 'inbox';
32 $redirectAction = 'index';
33 } else {
34 $this->inout = 'outbox';
35 $redirectAction = 'send';
36 }
37 // Set default redirect URL for non-AJAX
38 $this->setUrl('index.php?action=' . $redirectAction);
39
40 // Fix: Get the POST data properly for AJAX requests
41 $delid = $this->root->mContext->mRequest->getRequest('delmsg');
42
43 // Debug: Log the received data
44 error_log('Delete All Action - inout: ' . $this->inout . ', isAjax: ' . $isAjax . ', delid: ' . print_r($delid, true));
45
46 // --- Initial Validation ---
47 if (!is_array($delid) || 0 == count($delid)) {
48 if ($isAjax) {
49 header('Content-Type: application/json');
50 echo json_encode(['success' => false, 'error' => _MD_MESSAGE_DELETEMSG2]);
51 exit;
52 }
53 $this->setErr(_MD_MESSAGE_DELETEMSG2);
54 return;
55 }
56
57 // Fix the handler initialization - use xoops_getmodulehandler instead of Xoops::getHandler
58 $modHand = xoops_getmodulehandler($this->inout, _MY_DIRNAME);
59 $deleted_ids = [];
60 $failed_ids = [];
61 $errors = [];
62 $success_messages = []; // Store success messages if needed
63
64 // --- Loop and Delete ---
65 foreach ($delid as $boxid) {
66 $boxid = (int)$boxid; // Ensure integer
67 $modObj = $modHand->get($boxid);
68
69 if (!is_object($modObj)) {
70 $errors[$boxid] = _MD_MESSAGE_ACTIONMSG1; // Record error for this ID
71 $failed_ids[] = $boxid;
72 continue; // Skip to the next ID
73 }
74
75 if ($modObj->get('uid') != $this->root->mContext->mXoopsUser->get('uid')) {
76 $errors[$boxid] = _MD_MESSAGE_ACTIONMSG2; // Record error for this ID
77 $failed_ids[] = $boxid;
78 continue; // Skip to the next ID
79 }
80
81 if ($modHand->delete($modObj)) {
82 $deleted_ids[] = $boxid;
83 // Optional: Store individual success message if needed later
84 // $success_messages[$boxid] = _MD_MESSAGE_ACTIONMSG3;
85 } else {
86 $errors[$boxid] = _MD_MESSAGE_ACTIONMSG4; // Record error for this ID
87 $failed_ids[] = $boxid;
88 }
89 } // End foreach loop
90
91 // --- Process Results ---
92 $final_success = count($failed_ids) === 0 && count($deleted_ids) > 0;
93 $final_message = '';
94
95 if (count($deleted_ids) > 0) {
96 // Use XCube_Utils::formatString instead of sprintf for {0} placeholders
97 $final_message .= XCube_Utils::formatString(_MD_MESSAGE_DELETEMSG_SUCCESS_NUM, count($deleted_ids));
98 }
99 if (count($failed_ids) > 0) {
100 // Check if constant exists and use XCube_Utils::formatString
101 if (defined('_MD_MESSAGE_DELETEMSG_FAIL_NUM')) {
102 $final_message .= XCube_Utils::formatString(_MD_MESSAGE_DELETEMSG_FAIL_NUM, count($failed_ids));
103 } else {
104 // Fallback message if constant doesn't exist
105 $final_message .= count($failed_ids) . " message(s) failed to delete.";
106 }
107 }
108 if (empty($final_message)) {
109 $final_message = _MD_MESSAGE_DELETEMSG2; // No items selected or processed message
110 }
111
112
113 if ($isAjax) {
114 header('Content-Type: application/json');
115 echo json_encode([
116 'success' => $final_success,
117 'message' => trim($final_message),
118 'deleted_count' => count($deleted_ids),
119 'failed_count' => count($failed_ids),
120 'deleted_ids' => $deleted_ids,
121 'failed_ids' => $failed_ids,
122 'errors' => $errors, // Detailed errors per ID
123 'inout' => $this->inout
124 ]);
125 exit;
126 } else {
127 // For non-AJAX, set a general success or error message
128 if ($final_success) {
129 // Use XCube_Utils::formatString here too
130 $this->setErr(XCube_Utils::formatString(_MD_MESSAGE_DELETEMSG_SUCCESS_NUM, count($deleted_ids)));
131 } elseif (count($deleted_ids) > 0) {
132 // Partial success
133 $this->setErr(trim($final_message)); // Show combined message
134 } else {
135 // Complete failure or nothing to delete
136 $this->setErr(count($errors) > 0 ? _MD_MESSAGE_ACTIONMSG4 : _MD_MESSAGE_DELETEMSG2);
137 }
138 // The redirect URL is already set earlier.
139 return; // Proceed to redirect
140 }
141 }
142
143 public function executeView(&$render)
144 {
145 // No view rendering needed for delete all
146 }
147}
static formatString()
[Static] Formats string with special care for international.