SugarFieldImage::save()   C
last analyzed

Complexity

Conditions 16
Paths 84

Size

Total Lines 69
Code Lines 43

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 15
CRAP Score 84.0252
Metric Value
cc 16
eloc 43
nc 84
nop 5
dl 0
loc 69
ccs 15
cts 42
cp 0.357
crap 84.0252
rs 5.6565

How to fix   Long Method    Complexity   

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
/*********************************************************************************
3
 * SugarCRM Community Edition is a customer relationship management program developed by
4
 * SugarCRM, Inc. Copyright (C) 2004-2013 SugarCRM Inc.
5
6
 * SuiteCRM is an extension to SugarCRM Community Edition developed by Salesagility Ltd.
7
 * Copyright (C) 2011 - 2014 Salesagility Ltd.
8
 *
9
 * This program is free software; you can redistribute it and/or modify it under
10
 * the terms of the GNU Affero General Public License version 3 as published by the
11
 * Free Software Foundation with the addition of the following permission added
12
 * to Section 15 as permitted in Section 7(a): FOR ANY PART OF THE COVERED WORK
13
 * IN WHICH THE COPYRIGHT IS OWNED BY SUGARCRM, SUGARCRM DISCLAIMS THE WARRANTY
14
 * OF NON INFRINGEMENT OF THIRD PARTY RIGHTS.
15
 *
16
 * This program is distributed in the hope that it will be useful, but WITHOUT
17
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
18
 * FOR A PARTICULAR PURPOSE.  See the GNU Affero General Public License for more
19
 * details.
20
 *
21
 * You should have received a copy of the GNU Affero General Public License along with
22
 * this program; if not, see http://www.gnu.org/licenses or write to the Free
23
 * Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
24
 * 02110-1301 USA.
25
 *
26
 * You can contact SugarCRM, Inc. headquarters at 10050 North Wolfe Road,
27
 * SW2-130, Cupertino, CA 95014, USA. or at email address [email protected].
28
 *
29
 * The interactive user interfaces in modified source and object code versions
30
 * of this program must display Appropriate Legal Notices, as required under
31
 * Section 5 of the GNU Affero General Public License version 3.
32
 *
33
 * In accordance with Section 7(b) of the GNU Affero General Public License version 3,
34
 * these Appropriate Legal Notices must retain the display of the "Powered by
35
 * SugarCRM" logo and "Supercharged by SuiteCRM" logo. If the display of the logos is not
36
 * reasonably feasible for  technical reasons, the Appropriate Legal Notices must
37
 * display the words  "Powered by SugarCRM" and "Supercharged by SuiteCRM".
38
 ********************************************************************************/
39
40 1
require_once('include/SugarFields/Fields/File/SugarFieldFile.php');
41
42
class SugarFieldImage extends SugarFieldFile
43
{
44
45 1
    public function save(&$bean, $params, $field, $vardef, $prefix = '')
46
    {
47
48 1
        $fakeDisplayParams = array();
49 1
        $this->fillInOptions($vardef, $fakeDisplayParams);
50
51 1
        require_once('include/upload_file.php');
52 1
        $upload_file = new UploadFile($prefix . $field . '_file');
53
        //remove file
54 1
        if (isset($_REQUEST['remove_file_' . $field]) && $params['remove_file_' . $field] == 1) {
55
            $upload_file->unlink_file($bean->$field);
56
            $bean->$field = "";
57
        }
58
59 1
        $move = false;
60 1
        if (isset($_FILES[$prefix . $field . '_file']) && $upload_file->confirm_upload()) {
61
            if($this->verify_image($upload_file)) {
62
                $bean->$field = $upload_file->get_stored_file_name();
63
                $move = true;
64
            }else{
65
                //not valid image.
66
                $GLOBALS['log']->fatal("Image Field : Not a Valid Image.");
67
                $temp = $vardef['vname'];
68
                $temp = translate($temp, $bean->module_name);
69
                SugarApplication::appendErrorMessage($temp . " Field :  Not a valid image format.");
70
            }
71
72
        }
73
74 1
        if (empty($bean->id)) {
75 1
            $bean->id = create_guid();
76 1
            $bean->new_with_id = true;
77
        }
78
79 1
        if ($move) {
80
            $upload_file->final_move($bean->id . '_' . $field); //BEAN ID IS THE FILE NAME IN THE INSTANCE.
81
82
            $upload_file->upload_doc($bean, $bean->id, $params[$prefix . $vardef['docType']], $bean->$field, $upload_file->mime_type);
83 1
        } else if (!empty($old_id)) {
0 ignored issues
show
Bug introduced by
The variable $old_id seems to never exist, and therefore empty should always return true. Did you maybe rename this variable?

This check looks for calls to isset(...) or empty() on variables that are yet undefined. These calls will always produce the same result and can be removed.

This is most likely caused by the renaming of a variable or the removal of a function/method parameter.

Loading history...
84
            // It's a duplicate, I think
85
86
            if (empty($params[$prefix . $vardef['docUrl']])) {
87
                $upload_file->duplicate_file($old_id, $bean->id, $bean->$field);
88
            } else {
89
                $docType = $vardef['docType'];
90
                $bean->$docType = $params[$prefix . $field . '_old_doctype'];
91
            }
92 1
        } else if (!empty($params[$prefix . $field . '_remoteName'])) {
93
            // We aren't moving, we might need to do some remote linking
94
            $displayParams = array();
95
            $this->fillInOptions($vardef, $displayParams);
96
97
            if (isset($params[$prefix . $vardef['docId']])
98
                && !empty($params[$prefix . $vardef['docId']])
99
                && isset($params[$prefix . $vardef['docType']])
100
                && !empty($params[$prefix . $vardef['docType']])
101
            ) {
102
                $bean->$field = $params[$prefix . $field . '_remoteName'];
103
104
                require_once('include/utils/file_utils.php');
105
                $extension = get_file_extension($bean->$field);
106
                if (!empty($extension)) {
107
                    $bean->file_ext = $extension;
108
                    $bean->file_mime_type = get_mime_content_type_from_filename($bean->$field);
109
                }
110
            }
111
        }
112
113 1
    }
114
115
    public function verify_image($upload_file){
116
        global $sugar_config;
117
118
        $valid_ext = isset($sugar_config['image_ext']) ? $sugar_config['image_ext'] : array("image/jpeg","image/png");
119
120
        $img_size = getimagesize($upload_file->temp_file_location);
121
        $filetype = $img_size['mime'];
122
        if( in_array($filetype, array_values($valid_ext)) ) {
123
            return true;
124
        }
125
}
126 1
    private function fillInOptions(&$vardef,&$displayParams) {
0 ignored issues
show
Bug introduced by
Consider using a different method name as you override a private method of the parent class.

Overwriting private methods is generally fine as long as you also use private visibility. It might still be preferable for understandability to use a different method name.

Loading history...
127 1
        if ( isset($vardef['allowEapm']) && $vardef['allowEapm'] == true ) {
128
            if ( empty($vardef['docType']) ) {
129
                $vardef['docType'] = 'doc_type';
130
            }
131
            if ( empty($vardef['docId']) ) {
132
                $vardef['docId'] = 'doc_id';
133
            }
134
            if ( empty($vardef['docUrl']) ) {
135
                $vardef['docUrl'] = 'doc_url';
136
            }
137
        } else {
138 1
            $vardef['allowEapm'] = false;
139
        }
140
141
        // Override the default module
142 1
        if ( isset($vardef['linkModuleOverride']) ) {
143
            $vardef['linkModule'] = $vardef['linkModuleOverride'];
144
        } else {
145 1
            $vardef['linkModule'] = '{$module}';
146
        }
147
148
        // This is needed because these aren't always filled out in the edit/detailview defs
149 1
        if ( !isset($vardef['fileId']) ) {
150 1
            if ( isset($displayParams['id']) ) {
151
                $vardef['fileId'] = $displayParams['id'];
152
            } else {
153 1
                $vardef['fileId'] = 'id';
154
            }
155
        }
156 1
    }
157
}
158
159
?>