Conditions | 24 |
Paths | 1220 |
Total Lines | 107 |
Lines | 0 |
Ratio | 0 % |
Changes | 0 |
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:
If many parameters/temporary variables are present:
1 | <?php |
||
68 | public function upload(SS_HTTPRequest $request) |
||
69 | { |
||
70 | if ($recordId = $request->postVar('ID')) { |
||
71 | $this->setRecord(DMSDocumentSet::get()->byId($recordId)); |
||
72 | } |
||
73 | |||
74 | if ($this->isDisabled() || $this->isReadonly()) { |
||
75 | return $this->httpError(403); |
||
76 | } |
||
77 | |||
78 | // Protect against CSRF on destructive action |
||
79 | $token = $this->getForm()->getSecurityToken(); |
||
80 | if (!$token->checkRequest($request)) { |
||
81 | return $this->httpError(400); |
||
82 | } |
||
83 | |||
84 | $name = $this->getName(); |
||
85 | $tmpfile = $request->postVar($name); |
||
86 | $record = $this->getRecord(); |
||
87 | |||
88 | // Check if the file has been uploaded into the temporary storage. |
||
89 | if (!$tmpfile) { |
||
90 | $return = array('error' => _t('UploadField.FIELDNOTSET', 'File information not found')); |
||
91 | } else { |
||
92 | $return = array( |
||
93 | 'name' => $tmpfile['name'], |
||
94 | 'size' => $tmpfile['size'], |
||
95 | 'type' => $tmpfile['type'], |
||
96 | 'error' => $tmpfile['error'] |
||
97 | ); |
||
98 | } |
||
99 | |||
100 | // Check for constraints on the record to which the file will be attached. |
||
101 | if (!$return['error'] && $this->relationAutoSetting && $record && $record->exists()) { |
||
102 | $tooManyFiles = false; |
||
103 | // Some relationships allow many files to be attached. |
||
104 | if ($this->getConfig('allowedMaxFileNumber') && ($record->hasMany($name) || $record->manyMany($name))) { |
||
105 | if (!$record->isInDB()) { |
||
106 | $record->write(); |
||
107 | } |
||
108 | $tooManyFiles = $record->{$name}()->count() >= $this->getConfig('allowedMaxFileNumber'); |
||
109 | // has_one only allows one file at any given time. |
||
110 | } elseif ($record->hasOne($name)) { |
||
111 | $tooManyFiles = $record->{$name}() && $record->{$name}()->exists(); |
||
112 | } |
||
113 | |||
114 | // Report the constraint violation. |
||
115 | if ($tooManyFiles) { |
||
116 | if (!$this->getConfig('allowedMaxFileNumber')) { |
||
117 | $this->setConfig('allowedMaxFileNumber', 1); |
||
118 | } |
||
119 | $return['error'] = _t( |
||
120 | 'UploadField.MAXNUMBEROFFILES', |
||
121 | 'Max number of {count} file(s) exceeded.', |
||
122 | array('count' => $this->getConfig('allowedMaxFileNumber')) |
||
123 | ); |
||
124 | } |
||
125 | } |
||
126 | |||
127 | // Process the uploaded file |
||
128 | if (!$return['error']) { |
||
129 | $fileObject = null; |
||
130 | |||
131 | if ($this->relationAutoSetting) { |
||
132 | // Search for relations that can hold the uploaded files. |
||
133 | if ($relationClass = $this->getRelationAutosetClass()) { |
||
134 | // Create new object explicitly. Otherwise rely on Upload::load to choose the class. |
||
135 | $fileObject = SS_Object::create($relationClass); |
||
136 | } |
||
137 | } |
||
138 | |||
139 | // Get the uploaded file into a new file object. |
||
140 | try { |
||
141 | $this->upload->loadIntoFile($tmpfile, $fileObject, $this->getFolderName()); |
||
142 | } catch (Exception $e) { |
||
143 | // we shouldn't get an error here, but just in case |
||
144 | $return['error'] = $e->getMessage(); |
||
145 | } |
||
146 | |||
147 | if (!$return['error']) { |
||
148 | if ($this->upload->isError()) { |
||
149 | $return['error'] = implode(' ' . PHP_EOL, $this->upload->getErrors()); |
||
150 | } else { |
||
151 | $file = $this->upload->getFile(); |
||
152 | |||
153 | // CUSTOM Attach the file to the related record. |
||
154 | $document = $this->attachFile($file); |
||
155 | |||
156 | // Collect all output data. |
||
157 | $return = array_merge($return, array( |
||
158 | 'id' => $document->ID, |
||
159 | 'name' => $document->getTitle(), |
||
160 | 'thumbnail_url' => $document->Icon($document->getExtension()), |
||
161 | 'edit_url' => $this->getItemHandler($document->ID)->EditLink(), |
||
162 | 'size' => $document->getFileSizeFormatted(), |
||
163 | 'buttons' => (string) $document->renderWith($this->getTemplateFileButtons()), |
||
164 | 'showeditform' => true |
||
165 | )); |
||
166 | |||
167 | // CUSTOM END |
||
168 | } |
||
169 | } |
||
170 | } |
||
171 | $response = new SS_HTTPResponse(Convert::raw2json(array($return))); |
||
172 | $response->addHeader('Content-Type', 'text/plain'); |
||
173 | return $response; |
||
174 | } |
||
175 | |||
322 |