Completed
Push — master ( a833c2...4c7587 )
by Adam
18:52
created

EmailTemplateData.php ➔ handleAttachmentForRemove()   A

Complexity

Conditions 4
Paths 4

Size

Total Lines 9
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
cc 4
eloc 5
nc 4
nop 0
dl 0
loc 9
rs 9.2
c 0
b 0
f 0
1
<?php
2
if (!defined('sugarEntry') || !sugarEntry) die('Not A Valid Entry Point');
3
4
function handleAttachmentForRemove() {
5
    if(!empty($_REQUEST['attachmentsRemove'])) {
6
        foreach($_REQUEST['attachmentsRemove'] as $attachmentIdForRemove) {
7
            if($bean = BeanFactory::getBean('Notes', $attachmentIdForRemove)) {
8
                $bean->mark_deleted($bean->id);
9
            }
10
        }
11
    }
12
}
13
14
$error = false;
15
$msgs = array();
16
$data = array();
17
18
$emailTemplateId = isset($_REQUEST['emailTemplateId']) && $_REQUEST['emailTemplateId'] ? $_REQUEST['emailTemplateId'] : null;
19
$_SESSION['campaignWizard'][$_REQUEST['campaignId']]['defaultSelectedTemplateId'] = $emailTemplateId;
20
21
if(preg_match('/^[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$/', $emailTemplateId) || !$emailTemplateId) {
22
23
    $func = isset($_REQUEST['func']) ? $_REQUEST['func'] : null;
24
25
    $fields = array('body_html', 'subject', 'name');
26
27
    // TODO: validate for email template before save it!
28
29
    include_once 'modules/EmailTemplates/EmailTemplateFormBase.php';
30
31
    switch($func) {
32
33
        case 'update':
34
            $bean = BeanFactory::getBean('EmailTemplates', $emailTemplateId);
35
            foreach($bean as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $bean of type object<SugarBean> is not traversable.
Loading history...
36
                if(in_array($key, $fields)) {
37
                    $bean->$key = $_POST[$key];
38
                }
39
            }
40
            $formBase = new EmailTemplateFormBase();
41
            $bean = $formBase->handleAttachmentsProcessImages($bean, false, true, 'download', true);
42
            if($bean->save()) {
43
                $msgs[] = 'LBL_TEMPLATE_SAVED';
44
            }
45
            //$formBase = new EmailTemplateFormBase();
46
            //$bean = $formBase->handleAttachmentsProcessImages($bean, false, true);
47
            $data['id'] = $bean->id;
48
            $data['name'] = $bean->name;
49
            handleAttachmentForRemove();
50
51
            // update marketing->template_id if we have a selected marketing..
52
            if(!empty($_SESSION['campaignWizard'][$_REQUEST['campaignId']]['defaultSelectedMarketingId']) && !empty($_REQUEST['campaignId'])) {
53
                $marketingId = $_SESSION['campaignWizard'][$_REQUEST['campaignId']]['defaultSelectedMarketingId'];
54
55
                $campaign = BeanFactory::getBean('Campaigns', $_REQUEST['campaignId']);
56
                $campaign->load_relationship('emailmarketing');
57
                $marketings = $campaign->emailmarketing->get();
58
                // just a double check for campaign->marketing relation correct is for e.g the user deleted the marketing record or something may could happened..
59
                if(in_array($marketingId, $marketings)) {
60
                    $marketing = BeanFactory::getBean('EmailMarketing', $marketingId);
61
                    $marketing->template_id = $emailTemplateId;
62
                    $marketing->save();
63
                }
64
                else {
65
                    // TODO something is not OK, the selected campaign isn't related to this marketing!!
66
                    $GLOBALS['log']->debug('Selected marketing not found!');
67
                }
68
            }
69
            break;
70
71
        case 'createCopy':
72
            $bean = BeanFactory::getBean('EmailTemplates', $emailTemplateId);
73
            $newBean = new EmailTemplate();
74
            $fieldsForCopy = array('type', 'description');
75
            foreach($bean as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $bean of type object<SugarBean> is not traversable.
Loading history...
76
                if(in_array($key, $fields)) {
77
                    $newBean->$key = $_POST[$key];
78
                }
79
                else if(in_array($key, $fieldsForCopy)) {
80
                    $newBean->$key = $bean->$key;
81
                }
82
            }
83
            if($newBean->save()) {
84
                $msgs[] = 'LBL_TEMPLATE_SAVED';
85
            }
86
            //$formBase = new EmailTemplateFormBase();
87
            //$newBean = $formBase->handleAttachmentsProcessImages($newBean, false, true);
88
            $data['id'] = $newBean->id;
89
            $data['name'] = $newBean->name;
90
            break;
91
92
        case 'uploadAttachments':
93
            $formBase = new EmailTemplateFormBase();
94
            $focus = BeanFactory::getBean('EmailTemplates', $_REQUEST['attach_to_template_id']);
95
            //$data = $formBase->handleAttachments($focus, false, null);
96
            $data = $formBase->handleAttachmentsProcessImages($focus, false, true, 'download', true);
97
            $redirectUrl = 'index.php?module=Campaigns&action=WizardMarketing&campaign_id=' . $_REQUEST['campaign_id'] . "&jump=2&template_id=" . $_REQUEST['attach_to_template_id']; // . '&marketing_id=' . $_REQUEST['attach_to_marketing_id'] . '&record=' . $_REQUEST['attach_to_marketing_id'];
98
            header('Location: ' . $redirectUrl);
99
            die();
100
            break;
101
102
        default: case 'get':
0 ignored issues
show
Coding Style introduced by
The default body in a switch statement must start on the line following the statement.

According to the PSR-2, the body of a default statement must start on the line immediately following the statement.

switch ($expr) {
    default:
        doSomething(); //right
        break;
}


switch ($expr) {
    default:

        doSomething(); //wrong
        break;
}

To learn more about the PSR-2 coding standard, please refer to the PHP-Fig.

Loading history...
103
            if($bean = BeanFactory::getBean('EmailTemplates', $emailTemplateId)) {
104
                $fields = array('id', 'name', 'body', 'body_html', 'subject');
105
                foreach ($bean as $key => $value) {
0 ignored issues
show
Bug introduced by
The expression $bean of type object<SugarBean> is not traversable.
Loading history...
106
                    if (in_array($key, $fields)) {
107
                        $data[$key] = $bean->$key;
108
                    }
109
                }
110
111
                $data['body_from_html'] = from_html($bean->body_html);
112
                $attachmentBeans = $bean->getAttachments();
113
                if($attachmentBeans) {
114
                    $attachments = array();
115
                    foreach($attachmentBeans as $attachmentBean) {
116
                        $attachments[] = array(
117
                            'id' => $attachmentBean->id,
118
                            'name' => $attachmentBean->name,
119
                            'file_mime_type' => $attachmentBean->file_mime_type,
120
                            'filename' => $attachmentBean->filename,
121
                            'parent_type' => $attachmentBean->parent_type,
122
                            'parent_id' => $attachmentBean->parent_id,
123
                            'description' => $attachmentBean->description,
124
                        );
125
                    }
126
                    $data['attachments'] = $attachments;
127
                }
128
            }
129
            else {
130
                $error = 'Email Template not found.';
131
            }
132
            break;
133
    }
134
135
136
}
137
else {
138
    $error = 'Illegal GUID format.';
139
}
140
141
$results = array(
142
    'error' => $error,
143
    'msgs' => $msgs,
144
    'data' => $data,
145
);
146
147
$results = json_encode($results);
148
if(!$results) {
149
    if(json_last_error()) {
150
        $results = array(
151
            'error' => 'json_encode error: '.json_last_error_msg()
152
        );
153
        $results = json_encode($results);
154
    }
155
}
156
echo $results;
157