Passed
Push — master ( 4ed902...760dff )
by Julito
11:06 queued 11s
created

manageForm()   B

Complexity

Conditions 4
Paths 4

Size

Total Lines 100
Code Lines 64

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 64
c 0
b 0
f 0
nc 4
nop 4
dl 0
loc 100
rs 8.7853

How to fix   Long Method   

Long Method

Small methods make your code easier to understand, in particular if combined with a good name. Besides, if your method is small, finding a good name is usually much easier.

For example, if you find yourself adding comments to a method's body, this is usually a good sign to extract the commented part to a new method, and use the comment as a starting point when coming up with a good name for this new method.

Commonly applied refactorings include:

1
<?php
2
/* For licensing terms, see /license.txt */
3
4
$cidReset = true;
5
require_once __DIR__.'/../inc/global.inc.php';
6
7
api_protect_admin_script();
8
9
if ('true' !== api_get_setting('allow_social_tool')) {
10
    api_not_allowed(true);
11
}
12
13
$logInfo = [
14
    'tool' => 'Messages',
15
    'action' => 'add_new_promoted_message',
16
];
17
Event::registerLog($logInfo);
18
19
$allowSocial = 'true' === api_get_setting('allow_social_tool');
20
$nameTools = api_xml_http_response_encode(get_lang('Messages'));
21
22
$htmlHeadXtra[] = '<script>
23
var counter_image = 1;
24
function add_image_form() {
25
    // Multiple filepaths for image form
26
    var filepaths = document.getElementById("file_uploads");
27
    if (document.getElementById("filepath_"+counter_image)) {
28
        counter_image = counter_image + 1;
29
    }  else {
30
        counter_image = counter_image;
31
    }
32
    var elem1 = document.createElement("div");
33
    elem1.setAttribute("id","filepath_"+counter_image);
34
    filepaths.appendChild(elem1);
35
    id_elem1 = "filepath_"+counter_image;
36
    id_elem1 = "\'"+id_elem1+"\'";
37
    document.getElementById("filepath_"+counter_image).innerHTML = "<div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('Files attachments').'</label><input class=\"col-sm-8\" type=\"file\" name=\"attach_"+counter_image+"\" /></div><div class=\"form-group\" ><label class=\"col-sm-4\">'.get_lang('Description').'</label><div class=\"col-sm-8\"><input style=\"width:100%\" type=\"text\" name=\"legend[]\" /></div></div>";
38
    if (filepaths.childNodes.length == 6) {
39
        var link_attach = document.getElementById("link-more-attach");
40
        if (link_attach) {
41
            link_attach.innerHTML="";
42
        }
43
    }
44
}
45
</script>';
46
$nameTools = get_lang('Compose message');
47
$tpl = new Template(get_lang('Compose message'));
48
49
/**
50
 * Shows the compose area + a list of users to select from.
51
 */
52
function show_compose_to_any($tpl)
53
{
54
    $default['user_list'] = 0;
0 ignored issues
show
Comprehensibility Best Practice introduced by
$default was never initialized. Although not strictly required by PHP, it is generally a good practice to add $default = array(); before regardless.
Loading history...
55
    $html = manageForm($default, null, null, $tpl);
56
57
    return $html;
58
}
59
60
function show_compose_reply_to_message($message_id, $receiver_id, $tpl)
61
{
62
    $table = Database::get_main_table(TABLE_MESSAGE);
63
    $receiver_id = (int) $receiver_id;
64
    $message_id = (int) $message_id;
65
66
    $query = "SELECT user_sender_id
67
              FROM $table
68
              WHERE user_receiver_id = ".$receiver_id." AND id = ".$message_id;
69
    $result = Database::query($query);
70
    $row = Database::fetch_array($result, 'ASSOC');
71
    $userInfo = api_get_user_info($row['user_sender_id']);
72
    if (empty($row['user_sender_id']) || empty($userInfo)) {
73
        $html = get_lang('The id of the message to reply to is not valid.');
74
75
        return $html;
76
    }
77
78
    $default['users'] = [$row['user_sender_id']];
0 ignored issues
show
Comprehensibility Best Practice introduced by
$default was never initialized. Although not strictly required by PHP, it is generally a good practice to add $default = array(); before regardless.
Loading history...
79
    $html = manageForm($default, null, $userInfo['complete_name_with_username'], $tpl);
80
81
    return $html;
82
}
83
84
function show_compose_to_user($receiver_id, $tpl)
85
{
86
    $userInfo = api_get_user_info($receiver_id);
87
    $html = get_lang('To').':&nbsp;<strong>'.$userInfo['complete_name'].'</strong>';
88
    $default['title'] = api_xml_http_response_encode(get_lang('Please enter a title'));
0 ignored issues
show
Comprehensibility Best Practice introduced by
$default was never initialized. Although not strictly required by PHP, it is generally a good practice to add $default = array(); before regardless.
Loading history...
89
    $default['users'] = [$receiver_id];
90
    $html .= manageForm($default, null, '', $tpl);
91
92
    return $html;
93
}
94
95
/**
96
 * @param          $default
97
 * @param null     $select_from_user_list
0 ignored issues
show
Documentation Bug introduced by
Are you sure the doc-type for parameter $select_from_user_list is correct as it would always require null to be passed?
Loading history...
98
 * @param string   $sent_to
99
 * @param Template $tpl
100
 *
101
 * @return string
102
 */
103
function manageForm($default, $select_from_user_list = null, $sent_to = '', $tpl = null)
104
{
105
    $form = new FormValidator(
106
        'compose_message',
107
        null,
108
        api_get_self(),
109
        null,
110
        ['enctype' => 'multipart/form-data']
111
    );
112
113
    $form->addText('title', get_lang('Subject'));
114
    $form->addHtmlEditor(
115
        'content',
116
        get_lang('Message'),
117
        false,
118
        false,
119
        ['ToolbarSet' => 'Messages', 'Width' => '100%', 'Height' => '250', 'style' => true]
120
    );
121
122
    $form->addLabel(
123
        '',
124
        '<div id="file_uploads"><div id="filepath_1">
125
            <div id="filepaths" class="form-horizontal">
126
                <div id="paths-file" class="form-group">
127
                <label class="col-sm-4">'.get_lang('Files attachments').'</label>
128
                <input class="col-sm-8" type="file" name="attach_1"/>
129
                </div>
130
            </div>
131
            <div id="paths-description" class="form-group">
132
                <label class="col-sm-4">'.get_lang('Description').'</label>
133
                <div class="col-sm-8">
134
                <input id="file-descrtiption" class="form-control" type="text" name="legend[]" />
135
                </div>
136
            </div>
137
        </div>
138
        </div>'
139
    );
140
141
    $form->addLabel(
142
        '',
143
        '<span id="link-more-attach"><a class="btn btn-default" href="javascript://" onclick="return add_image_form()">'.
144
        get_lang('Add one more file').'</a></span>&nbsp;('.
145
        sprintf(
146
            get_lang('Maximun file size: %s'),
147
            format_file_size(api_get_setting('message_max_upload_filesize'))
148
        ).')'
149
    );
150
151
    $form->addButtonSend(get_lang('Send message'), 'compose');
152
    $form->setRequiredNote('<span class="form_required">*</span> <small>'.get_lang('Required field').'</small>');
153
154
    $form->setDefaults($default);
155
    $html = '';
156
    if ($form->validate()) {
157
        $check = true;
158
        if ($check) {
159
            $file_comments = $_POST['legend'];
160
            $title = $default['title'];
161
            $content = $default['content'];
162
163
            $res = MessageManager::send_message(
164
                api_get_user_id(),
165
                $title,
166
                $content,
167
                $_FILES,
168
                $file_comments,
169
                0,
170
                0,
171
                0,
172
                0,
173
                null,
174
                false,
175
                0,
176
                [],
177
                true,
178
                null,
179
                MESSAGE_STATUS_PROMOTED
180
            );
181
182
            if ($res) {
183
                Display::addFlash(Display::return_message(
184
                    get_lang('Message Sent'),
185
                    'confirmation',
186
                    false
187
                ));
188
            }
189
190
            MessageManager::cleanAudioMessage();
191
        }
192
        Security::clear_token();
193
        header('Location: '.api_get_path(WEB_PATH).'main/social/promoted_messages.php');
194
        exit;
195
    } else {
196
        $token = Security::get_token();
197
        $form->addElement('hidden', 'sec_token');
198
        $form->setConstants(['sec_token' => $token]);
199
        $html .= $form->returnForm();
200
    }
201
202
    return $html;
203
}
204
205
$this_section = SECTION_SOCIAL;
206
$interbreadcrumb[] = [
207
    'url' => api_get_path(WEB_PATH).'main/social/home.php',
208
    'name' => get_lang('Social network'),
209
];
210
211
$interbreadcrumb[] = [
212
    'url' => api_get_path(WEB_PATH).'main/messages/inbox.php',
213
    'name' => get_lang('Messages'),
214
];
215
216
$social_right_content = null;
217
218
// LEFT COLUMN
219
$social_left_content = '';
220
221
// Block Social Menu
222
$social_menu_block = SocialManager::show_social_menu('messages');
223
$social_right_content .= '<div class="row">';
224
$social_right_content .= '<div class="col-md-12">';
225
$social_right_content .= '<div class="actions">';
226
$social_right_content .= '<a href="'.api_get_path(WEB_PATH).'main/messages/inbox.php">'.
227
    Display::return_icon('back.png', get_lang('Back'), [], 32).'</a>';
228
$social_right_content .= '</div>';
229
$social_right_content .= '</div>';
230
$social_right_content .= '<div class="col-md-12">';
231
232
// MAIN CONTENT
233
if (!isset($_POST['compose'])) {
234
    if (isset($_GET['re_id'])) {
235
        $social_right_content .= show_compose_reply_to_message(
236
            $_GET['re_id'],
237
            api_get_user_id(),
238
            $tpl
239
        );
240
    } elseif (isset($_GET['send_to_user'])) {
241
        $social_right_content .= show_compose_to_user($_GET['send_to_user'], $tpl);
242
    } else {
243
        $social_right_content .= show_compose_to_any($tpl);
244
    }
245
} else {
246
    $default['title'] = $_POST['title'];
247
    $default['content'] = $_POST['content'];
248
    $social_right_content .= manageForm($default, null, null, $tpl);
249
}
250
251
$social_right_content .= '</div>';
252
$social_right_content .= '</div>';
253
254
// Block Social Avatar
255
SocialManager::setSocialUserBlock($tpl, api_get_user_id(), 'messages');
256
MessageManager::cleanAudioMessage();
257
258
$tpl->assign('social_menu_block', $social_menu_block);
259
$tpl->assign('social_right_content', $social_right_content);
260
$social_layout = $tpl->get_template('social/inbox.tpl');
261
$tpl->display($social_layout);
262