GitHub Access Token became invalid

It seems like the GitHub access token used for retrieving details about this repository from GitHub became invalid. This might prevent certain types of inspections from being run (in particular, everything related to pull requests).
Please ask an admin of your repository to re-new the access token on this website.

TbFileUpload::generateCallbackBindJSString()   A
last analyzed

Complexity

Conditions 3
Paths 3

Size

Total Lines 11
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 12

Importance

Changes 0
Metric Value
dl 0
loc 11
ccs 0
cts 11
cp 0
rs 9.4285
c 0
b 0
f 0
cc 3
eloc 8
nc 3
nop 0
crap 12
1
<?php
2
/**
3
 *## TbFileUpload class file
4
 *
5
 * @author AsgarothBelem <[email protected]>
6
 * @link http://blueimp.github.com/jQuery-File-Upload/
7
 * @link https://github.com/Asgaroth/xupload
8
 *
9
 * @author: antonio ramirez <[email protected]>
10
 * Date: 11/5/12
11
 * Time: 12:46 AM
12
 */
13
14
Yii::import('zii.widgets.jui.CJuiInputWidget');
15
16
/**
17
 * Class TbFileUpload
18
 *
19
 * Modified version from the great implementation of XUpload Yii Extension
20
 *
21
 * @package booster.widgets.forms.inputs
22
 */
23
class TbFileUpload extends CJuiInputWidget
24
{
25
	private static $_callbackFnPrefix = 'fileupload';
26
	
27
	/**
28
	 * the url to the upload handler
29
	 * @var string
30
	 */
31
	public $url;
32
33
	/**
34
	 * set to true to use multiple file upload
35
	 * @var boolean
36
	 */
37
	public $multiple = false;
38
39
	/**
40
	 * The upload template id to display files available for upload
41
	 * defaults to null, meaning using the built-in template
42
	 */
43
	public $uploadTemplate;
44
45
	/**
46
	 * The template id to display files available for download
47
	 * defaults to null, meaning using the built-in template
48
	 */
49
	public $downloadTemplate;
50
51
	/**
52
	 * Wheter or not to preview image files before upload
53
	 */
54
	public $previewImages = true;
55
56
	/**
57
	 * Whether or not to add the image processing plugin
58
	 */
59
	public $imageProcessing = true;
60
61
	/**
62
	 * Stores callback functions JS code, reffering do jQuery-File-Upload documentation
63
	 * Use these values for array keys:
64
	 * 	- add, 
65
	 *  - submit, 
66
	 *  - send, 
67
	 *  - done, 
68
	 *  - fail, 
69
	 *  - always, 
70
	 *  - progress, 
71
	 *  - progressall, 
72
	 *  - start, 
73
	 *  - stop
74
	 * 	- change, 
75
	 *  - pase, 
76
	 *  - drop, 
77
	 *  - dragover, 
78
	 *  - chunksend, 
79
	 *  - chunkdone, 
80
	 *  - chunkfail, 
81
	 *  - chunkalways
82
	 *  Another part of callback function name will be added from code.
83
	 * 
84
	 * See link below for more details:
85
	 * @link https://github.com/blueimp/jQuery-File-Upload/wiki/Options#callback-options Callback options
86
	 * @var string[] 
87
	 */
88
	public $callbacks = array( );
89
        
90
	/**
91
	 * @var string name of the form view to be rendered
92
	 */
93
	public $formView = 'booster.views.fileupload.form';
94
95
	/**
96
	 * @var string name of the upload view to be rendered
97
	 */
98
	public $uploadView = 'booster.views.fileupload.upload';
99
100
	/**
101
	 * @var string name of the download view to be rendered
102
	 */
103
	public $downloadView = 'booster.views.fileupload.download';
104
105
	/**
106
	 * @var string name of the view to display images at bootstrap-slideshow
107
	 */
108
	public $previewImagesView = 'booster.views.gallery.preview';
109
110
	/**
111
	 * Widget initialization
112
	 */
113
	public function init()
114
	{
115
		if ($this->uploadTemplate === null) {
116
			$this->uploadTemplate = "#template-upload";
117
		}
118
119
		if ($this->downloadTemplate === null) {
120
			$this->downloadTemplate = "#template-download";
121
		}
122
123
		if (!isset($this->htmlOptions['enctype'])) {
124
			$this->htmlOptions['enctype'] = 'multipart/form-data';
125
		}
126
127
		parent::init();
128
	}
129
130
	/**
131
	 * Generates the required HTML and Javascript
132
	 */
133
	public function run()
134
	{
135
136
		list($name) = $this->resolveNameID();
137
138
		$this->htmlOptions['id'] = $this->id.'-'.($this->hasModel() ? get_class($this->model) : 'fileupload') . '-form';
139
140
		$this->options['url'] = $this->url;
141
142
		// if acceptFileTypes is not set as option, try getting it from models rules
143
		if (!isset($this->options['acceptFileTypes'])) {
144
			$fileTypes = $this->getFileValidatorProperty($this->model, $this->attribute, 'types');
145
			if (isset($fileTypes)) {
146
				$fileTypes = (preg_match(':jpg:', $fileTypes) && !preg_match(':jpe:', $fileTypes) ? preg_replace(
147
					':jpg:',
148
					'jpe?g',
149
					$fileTypes
150
				) : $fileTypes);
151
				$this->options['acceptFileTypes'] = 'js:/(\.)(' . preg_replace(':,:', '|', $fileTypes) . ')$/i';
152
			}
153
		}
154
155
		// if maxFileSize is not set as option, try getting it from models rules
156
		if (!isset($this->options['maxFileSize'])) {
157
			$fileSize = $this->getFileValidatorProperty($this->model, $this->attribute, 'maxSize');
158
			if (isset($fileSize)) {
159
				$this->options['maxFileSize'] = $fileSize;
160
			}
161
		}
162
163
		if ($this->multiple) {
164
			$this->htmlOptions["multiple"] = true;
165
		}
166
167
		$this->render($this->uploadView);
168
		$this->render($this->downloadView);
169
		$this->render($this->formView, array('name' => $name, 'htmlOptions' => $this->htmlOptions));
170
171
		if ($this->previewImages || $this->imageProcessing) {
172
			$this->render($this->previewImagesView);
173
		}
174
175
		$this->registerClientScript($this->htmlOptions['id']);
176
	}
177
178
	/**
179
	 * Registers and publishes required scripts
180
	 *
181
	 * @param string $id
182
	 */
183
	public function registerClientScript($id)
184
	{
185
        $booster = Booster::getBooster();
186
        $booster->registerAssetCss('fileupload/jquery.fileupload-ui.css');
187
188
		// Upgrade widget factory
189
		// @todo remove when jquery.ui 1.9+ is fully integrated into stable Yii versions
190
        $booster->registerAssetJs('fileupload/vendor/jquery.ui.widget.js');
191
		//The Templates plugin is included to render the upload/download listings
192
        $booster->registerAssetJs("fileupload/tmpl.min.js", CClientScript::POS_END);
193
194
		if ($this->previewImages || $this->imageProcessing) {
195
            $booster->registerAssetJs("fileupload/load-image.min.js", CClientScript::POS_END);
196
            $booster->registerAssetJs("fileupload/canvas-to-blob.min.js", CClientScript::POS_END);
197
			// gallery :) and one smile from me ;)
198
            $booster->registerAssetCss("bootstrap-image-gallery.min.css");
199
            $booster->registerAssetJs("bootstrap-image-gallery.min.js", CClientScript::POS_END);
200
		}
201
		//The Iframe Transport is required for browsers without support for XHR file uploads
202
        $booster->registerAssetJs('fileupload/jquery.iframe-transport.js');
203
        $booster->registerAssetJs('fileupload/jquery.fileupload.js');
204
		// The File Upload image processing plugin
205
		if ($this->imageProcessing) {
206
            $booster->registerAssetJs('fileupload/jquery.fileupload-ip.js');
207
		}
208
		// The File Upload file processing plugin
209
		if ($this->previewImages) {
210
            $booster->registerAssetJs('fileupload/jquery.fileupload-fp.js');
211
		}
212
		// locale
213
        $booster->registerAssetJs('fileupload/jquery.fileupload-locale.js');
214
		//The File Upload user interface plugin
215
        $booster->registerAssetJs('fileupload/jquery.fileupload-ui.js');
216
217
		$options = CJavaScript::encode($this->options);
218
		Yii::app()->clientScript->registerScript(__CLASS__ . '#' . $id, "jQuery('#{$id}').fileupload({$options}){$this->generateCallbackBindJSString()};");
219
	}
220
221
	/**
222
	 * Check for a property of CFileValidator
223
	 *
224
	 * @param CModel $model
225
	 * @param string $attribute
226
	 * @param string $property
227
	 *
228
	 * @return string property's value or null
229
	 */
230
	private function getFileValidatorProperty($model = null, $attribute = null, $property = null)
231
	{
232
		if (!isset($model, $attribute, $property)) {
233
			return null;
234
		}
235
236
		foreach ($model->getValidators($attribute) as $validator) {
237
			if ($validator instanceof CFileValidator) {
0 ignored issues
show
Bug introduced by
The class CFileValidator does not exist. Did you forget a USE statement, or did you not list all dependencies?

This error could be the result of:

1. Missing dependencies

PHP Analyzer uses your composer.json file (if available) to determine the dependencies of your project and to determine all the available classes and functions. It expects the composer.json to be in the root folder of your repository.

Are you sure this class is defined by one of your dependencies, or did you maybe not list a dependency in either the require or require-dev section?

2. Missing use statement

PHP does not complain about undefined classes in ìnstanceof checks. For example, the following PHP code will work perfectly fine:

if ($x instanceof DoesNotExist) {
    // Do something.
}

If you have not tested against this specific condition, such errors might go unnoticed.

Loading history...
238
				$ret = $validator->$property;
239
			}
240
		}
241
		return isset($ret) ? $ret : null;
242
	}
243
	
244
	private function generateCallbackBindJSString() {
245
		if ( count($this->callbacks) > 0 ) {
246
			$output = '';
247
			foreach ( $this->callbacks as $callbackName => $fnCode ) {
248
				$output .= '.bind("' . self::$_callbackFnPrefix
249
					. $callbackName . '", ' . $fnCode . ')';
250
                     }
251
			return $output;
252
		}
253
		return '';
254
	}
255
}
256