Passed
Push — master ( 5fb799...1c50e6 )
by Robbie
03:48
created
code/DocumentImportInnerField.php 2 patches
Spacing   +56 added lines, -56 removed lines patch added patch discarded remove patch
@@ -34,11 +34,11 @@  discard block
 block discarded – undo
34 34
 	 * Process the document immediately upon upload.
35 35
 	 */
36 36
 	public function upload(SS_HTTPRequest $request) {
37
-		if($this->isDisabled() || $this->isReadonly()) return $this->httpError(403);
37
+		if ($this->isDisabled() || $this->isReadonly()) return $this->httpError(403);
38 38
 
39 39
 		// Protect against CSRF on destructive action
40 40
 		$token = $this->getForm()->getSecurityToken();
41
-		if(!$token->checkRequest($request)) return $this->httpError(400);
41
+		if (!$token->checkRequest($request)) return $this->httpError(400);
42 42
 
43 43
 		$name = $this->getName();
44 44
 		$tmpfile = $request->postVar($name);
@@ -57,11 +57,11 @@  discard block
 block discarded – undo
57 57
 
58 58
 		if (!$return['error']) {
59 59
 			// Get options for this import.
60
-			$splitHeader = (int)$request->postVar('SplitHeader');
61
-			$keepSource = (bool)$request->postVar('KeepSource');
62
-			$chosenFolderID = (int)$request->postVar('ChosenFolderID');
63
-			$publishPages = (bool)$request->postVar('PublishPages');
64
-			$includeTOC = (bool)$request->postVar('IncludeTOC');
60
+			$splitHeader = (int) $request->postVar('SplitHeader');
61
+			$keepSource = (bool) $request->postVar('KeepSource');
62
+			$chosenFolderID = (int) $request->postVar('ChosenFolderID');
63
+			$publishPages = (bool) $request->postVar('PublishPages');
64
+			$includeTOC = (bool) $request->postVar('IncludeTOC');
65 65
 
66 66
 			// Process the document and write the page.
67 67
 			$preservedDocument = null;
@@ -110,42 +110,42 @@  discard block
 block discarded – undo
110 110
 		$page = $this->form->getRecord();
111 111
 		$content = '<ul>';
112 112
 
113
-		if($page) {
114
-			if($page->Children()->Count() > 0) {
115
-				foreach($page->Children() as $child) {
116
-					$content .= '<li><a href="' . $child->Link() . '">' . $child->Title . '</a></li>';
113
+		if ($page) {
114
+			if ($page->Children()->Count() > 0) {
115
+				foreach ($page->Children() as $child) {
116
+					$content .= '<li><a href="'.$child->Link().'">'.$child->Title.'</a></li>';
117 117
 				}
118
-				$page->Content = $content . '</ul>';
119
-			}  else {
118
+				$page->Content = $content.'</ul>';
119
+			} else {
120 120
 				$doc = new DOMDocument();
121 121
 				$doc->loadHTML($page->Content);
122 122
 				$body = $doc->getElementsByTagName('body')->item(0);
123 123
 				$node = $body->firstChild;
124 124
 				$h1 = $h2 = 1;
125
-				while($node) {
126
-					if($node instanceof DOMElement && $node->tagName == 'h1') {
127
-						$content .= '<li><a href="#h1.' . $h1 . '">'. trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))) . '</a></li>';
125
+				while ($node) {
126
+					if ($node instanceof DOMElement && $node->tagName == 'h1') {
127
+						$content .= '<li><a href="#h1.'.$h1.'">'.trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))).'</a></li>';
128 128
 						$node->setAttributeNode(new DOMAttr("id", "h1.".$h1));
129 129
 						$h1++;
130
-					} elseif($node instanceof DOMElement && $node->tagName == 'h2') {
131
-						$content .= '<li class="menu-h2"><a href="#h2.' . $h2 . '">'. trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))) . '</a></li>';
130
+					} elseif ($node instanceof DOMElement && $node->tagName == 'h2') {
131
+						$content .= '<li class="menu-h2"><a href="#h2.'.$h2.'">'.trim(preg_replace('/\n|\r/', '', Convert::html2raw($node->textContent))).'</a></li>';
132 132
 						$node->setAttributeNode(new DOMAttr("id", "h2.".$h2));
133 133
 						$h2++;
134 134
 					}
135 135
 					$node = $node->nextSibling;
136 136
 				}
137
-				$page->Content = $content . '</ul>' . $doc->saveHTML();
137
+				$page->Content = $content.'</ul>'.$doc->saveHTML();
138 138
 			}
139 139
 
140 140
 			// Add in the link to the original document, if provided.
141
-			if($preservedDocument) {
142
-				$page->Content = '<a href="' . $preservedDocument->Link() . '" title="download original document">download original document (' .
143
-									$preservedDocument->getSize() . ')</a>' . $page->Content;
141
+			if ($preservedDocument) {
142
+				$page->Content = '<a href="'.$preservedDocument->Link().'" title="download original document">download original document ('.
143
+									$preservedDocument->getSize().')</a>'.$page->Content;
144 144
 			}
145 145
 
146 146
 			// Store the result
147 147
 			$page->write();
148
-			if($publishPages) $page->doPublish();
148
+			if ($publishPages) $page->doPublish();
149 149
 		}
150 150
 	}
151 151
 
@@ -179,28 +179,28 @@  discard block
 block discarded – undo
179 179
 	protected function writeContent($subtitle, $subdoc, $subnode, $sort = null, $publishPages = false) {
180 180
 		$record = $this->form->getRecord();
181 181
 
182
-		if($subtitle) {
182
+		if ($subtitle) {
183 183
 			// Write the chapter page to a subpage.
184 184
 			$page = DataObject::get_one('Page', sprintf('"Title" = \'%s\' AND "ParentID" = %d', $subtitle, $record->ID));
185
-			if(!$page) {
185
+			if (!$page) {
186 186
 				$page = new Page();
187 187
 				$page->ParentID = $record->ID;
188 188
 				$page->Title = $subtitle;
189 189
 			}
190 190
 
191 191
 			unset($this->unusedChildren[$page->ID]);
192
-			file_put_contents(ASSETS_PATH . '/index-' . $sort . '.html', $this->getBodyText($subdoc, $subnode));
192
+			file_put_contents(ASSETS_PATH.'/index-'.$sort.'.html', $this->getBodyText($subdoc, $subnode));
193 193
 
194 194
 			if ($sort) $page->Sort = $sort;
195 195
 			$page->Content = $this->getBodyText($subdoc, $subnode);
196 196
 			$page->write();
197
-			if($publishPages) $page->doPublish();
197
+			if ($publishPages) $page->doPublish();
198 198
 		} else {
199 199
 			// Write to the master page.
200 200
 			$record->Content = $this->getBodyText($subdoc, $subnode);
201 201
 			$record->write();
202 202
 
203
-			if($publishPages) $record->doPublish();
203
+			if ($publishPages) $record->doPublish();
204 204
 		}
205 205
 
206 206
 	}
@@ -237,7 +237,7 @@  discard block
 block discarded – undo
237 237
 		$tidy->cleanRepair();
238 238
 
239 239
 		$fragment = [];
240
-		foreach($tidy->body()->child as $child) {
240
+		foreach ($tidy->body()->child as $child) {
241 241
 			$fragment[] = $child->value;
242 242
 		}
243 243
 
@@ -254,13 +254,13 @@  discard block
 block discarded – undo
254 254
 		// make sure any images are added as Image records with a relative link to assets
255 255
 		$folderName = ($chosenFolderID) ? DataObject::get_by_id('Folder', $chosenFolderID)->Name : '';
256 256
 		$imgs = $xpath->query('//img');
257
-		for($i = 0; $i < $imgs->length; $i++) {
257
+		for ($i = 0; $i < $imgs->length; $i++) {
258 258
 			$img = $imgs->item($i);
259
-			$originalPath = 'assets/' . $folderName . '/' . $img->getAttribute('src');
259
+			$originalPath = 'assets/'.$folderName.'/'.$img->getAttribute('src');
260 260
 			$name = FileNameFilter::create()->filter(basename($originalPath));
261 261
 
262 262
 			$image = Image::get()->filter(array('Name' => $name, 'ParentID' => (int) $chosenFolderID))->first();
263
-			if(!($image && $image->exists())) {
263
+			if (!($image && $image->exists())) {
264 264
 				$image = new Image();
265 265
 				$image->ParentID = (int) $chosenFolderID;
266 266
 				$image->Name = $name;
@@ -279,24 +279,24 @@  discard block
 block discarded – undo
279 279
 			'//font' // Remove any font tags
280 280
 		);
281 281
 
282
-		foreach($remove_rules as $rule => $parenttag) {
283
-			if(is_numeric($rule)) {
282
+		foreach ($remove_rules as $rule => $parenttag) {
283
+			if (is_numeric($rule)) {
284 284
 				$rule = $parenttag;
285 285
 				$parenttag = null;
286 286
 			}
287 287
 
288 288
 			$nodes = array();
289
-			foreach($xpath->query($rule) as $node) $nodes[] = $node;
289
+			foreach ($xpath->query($rule) as $node) $nodes[] = $node;
290 290
 
291
-			foreach($nodes as $node) {
291
+			foreach ($nodes as $node) {
292 292
 				$parent = $node->parentNode;
293 293
 
294
-				if($parenttag) {
294
+				if ($parenttag) {
295 295
 					$parent = $doc->createElement($parenttag);
296 296
 					$node->nextSibling ? $node->parentNode->insertBefore($parent, $node->nextSibling) : $node->parentNode->appendChild($parent);
297 297
 				}
298 298
 
299
-				while($node->firstChild) $parent->appendChild($node->firstChild);
299
+				while ($node->firstChild) $parent->appendChild($node->firstChild);
300 300
 				$node->parentNode->removeChild($node);
301 301
 			}
302 302
 		}
@@ -320,11 +320,11 @@  discard block
 block discarded – undo
320 320
 			'//br' // BR tags
321 321
 		);
322 322
 
323
-		foreach($clean as $query) {
323
+		foreach ($clean as $query) {
324 324
 			// First get all the nodes. Need to build array, as they'll disappear from the nodelist while we're deleteing them, causing the indexing
325 325
 			// to screw up.
326 326
 			$nodes = array();
327
-			foreach($xpath->query($query) as $node) $nodes[] = $node;
327
+			foreach ($xpath->query($query) as $node) $nodes[] = $node;
328 328
 
329 329
 			// Then remove them all
330 330
 			foreach ($nodes as $node) { if ($node->parentNode) $node->parentNode->removeChild($node); }
@@ -334,7 +334,7 @@  discard block
 block discarded – undo
334 334
 		$body = $doc->getElementsByTagName('body')->item(0);
335 335
 
336 336
 		$this->unusedChildren = array();
337
-		foreach($sourcePage->Children() as $child) {
337
+		foreach ($sourcePage->Children() as $child) {
338 338
 			$this->unusedChildren[$child->ID] = $child;
339 339
 		}
340 340
 
@@ -346,10 +346,10 @@  discard block
 block discarded – undo
346 346
 		$subnode = $subdoc->createElement('body');
347 347
 		$node = $body->firstChild;
348 348
 		$sort = 1;
349
-		if($splitHeader == 1 || $splitHeader == 2) {
350
-			while($node && !$DocumentImportInnerfield_error) {
351
-				if($node instanceof DOMElement && $node->tagName == 'h' . $splitHeader) {
352
-					if($subnode->hasChildNodes()) {
349
+		if ($splitHeader == 1 || $splitHeader == 2) {
350
+			while ($node && !$DocumentImportInnerfield_error) {
351
+				if ($node instanceof DOMElement && $node->tagName == 'h'.$splitHeader) {
352
+					if ($subnode->hasChildNodes()) {
353 353
 						$this->writeContent($subtitle, $subdoc, $subnode, $sort, $publishPages);
354 354
 						$sort++;
355 355
 					}
@@ -367,7 +367,7 @@  discard block
 block discarded – undo
367 367
 			$this->writeContent($subtitle, $subdoc, $body, null, $publishPages);
368 368
 		}
369 369
 
370
-		if($subnode->hasChildNodes() && !$DocumentImportInnerfield_error) {
370
+		if ($subnode->hasChildNodes() && !$DocumentImportInnerfield_error) {
371 371
 			$this->writeContent($subtitle, $subdoc, $subnode, null, $publishPages);
372 372
 		}
373 373
 
@@ -376,7 +376,7 @@  discard block
 block discarded – undo
376 376
 			return array('error' => $DocumentImportInnerfield_error);
377 377
 		}
378 378
 
379
-		foreach($this->unusedChildren as $child) {
379
+		foreach ($this->unusedChildren as $child) {
380 380
 			$origStage = Versioned::current_stage();
381 381
 
382 382
 			Versioned::reading_stage('Stage');
@@ -415,7 +415,7 @@  discard block
 block discarded – undo
415 415
 
416 416
 	protected static $docvert_url;
417 417
 
418
-	public static function set_docvert_username($username = null)  {
418
+	public static function set_docvert_username($username = null) {
419 419
 		self::$docvert_username = $username;
420 420
 	}
421 421
 
@@ -448,14 +448,14 @@  discard block
 block discarded – undo
448 448
 		$ch = curl_init();
449 449
 
450 450
 		// PHP 5.5+ introduced CURLFile which makes the '@/path/to/file' syntax deprecated.
451
-		if(class_exists('CURLFile')) {
451
+		if (class_exists('CURLFile')) {
452 452
 			$file = new CURLFile(
453 453
 				$this->fileDescriptor['path'],
454 454
 				$this->fileDescriptor['mimeType'],
455 455
 				$this->fileDescriptor['name']
456 456
 			);
457 457
 		} else {
458
-			$file = '@' . $this->fileDescriptor['path'];
458
+			$file = '@'.$this->fileDescriptor['path'];
459 459
 		}
460 460
 
461 461
 		curl_setopt_array($ch, array(
@@ -469,7 +469,7 @@  discard block
 block discarded – undo
469 469
 
470 470
 		$folderName = ($this->chosenFolderID) ? '/'.DataObject::get_by_id('Folder', $this->chosenFolderID)->Name : '';
471 471
 		$outname = tempnam(ASSETS_PATH, 'convert');
472
-		$outzip = $outname . '.zip';
472
+		$outzip = $outname.'.zip';
473 473
 
474 474
 		$out = fopen($outzip, 'w');
475 475
 		curl_setopt($ch, CURLOPT_FILE, $out);
@@ -491,15 +491,15 @@  discard block
 block discarded – undo
491 491
 		// you need php zip, i.e. port install php5-zip
492 492
 		$zip = new ZipArchive();
493 493
 
494
-		if($zip->open($outzip)) {
495
-			$zip->extractTo(ASSETS_PATH .$folderName);
494
+		if ($zip->open($outzip)) {
495
+			$zip->extractTo(ASSETS_PATH.$folderName);
496 496
 		}
497 497
 
498 498
 		// remove temporary files
499 499
 		unlink($outname);
500 500
 		unlink($outzip);
501 501
 
502
-		if (!file_exists(ASSETS_PATH . $folderName . '/index.html')) {
502
+		if (!file_exists(ASSETS_PATH.$folderName.'/index.html')) {
503 503
 			return array('error' =>  _t(
504 504
 				'DocumentConverter.PROCESSFAILED',
505 505
 				'Could not process document, please double-check you uploaded a .doc or .docx format.',
@@ -507,9 +507,9 @@  discard block
 block discarded – undo
507 507
 			));
508 508
 		}
509 509
 
510
-		$content = file_get_contents(ASSETS_PATH . $folderName . '/index.html');
510
+		$content = file_get_contents(ASSETS_PATH.$folderName.'/index.html');
511 511
 
512
-		unlink(ASSETS_PATH . $folderName . '/index.html');
512
+		unlink(ASSETS_PATH.$folderName.'/index.html');
513 513
 
514 514
 		return $content;
515 515
 	}
Please login to merge, or discard this patch.
Braces   +35 added lines, -12 removed lines patch added patch discarded remove patch
@@ -34,11 +34,15 @@  discard block
 block discarded – undo
34 34
 	 * Process the document immediately upon upload.
35 35
 	 */
36 36
 	public function upload(SS_HTTPRequest $request) {
37
-		if($this->isDisabled() || $this->isReadonly()) return $this->httpError(403);
37
+		if($this->isDisabled() || $this->isReadonly()) {
38
+			return $this->httpError(403);
39
+		}
38 40
 
39 41
 		// Protect against CSRF on destructive action
40 42
 		$token = $this->getForm()->getSecurityToken();
41
-		if(!$token->checkRequest($request)) return $this->httpError(400);
43
+		if(!$token->checkRequest($request)) {
44
+			return $this->httpError(400);
45
+		}
42 46
 
43 47
 		$name = $this->getName();
44 48
 		$tmpfile = $request->postVar($name);
@@ -65,7 +69,9 @@  discard block
 block discarded – undo
65 69
 
66 70
 			// Process the document and write the page.
67 71
 			$preservedDocument = null;
68
-			if ($keepSource) $preservedDocument = $this->preserveSourceDocument($tmpfile, $chosenFolderID);
72
+			if ($keepSource) {
73
+				$preservedDocument = $this->preserveSourceDocument($tmpfile, $chosenFolderID);
74
+			}
69 75
 
70 76
 			$importResult = $this->importFromPOST($tmpfile, $splitHeader, $publishPages, $chosenFolderID);
71 77
 			if (is_array($importResult) && isset($importResult['error'])) {
@@ -116,7 +122,7 @@  discard block
 block discarded – undo
116 122
 					$content .= '<li><a href="' . $child->Link() . '">' . $child->Title . '</a></li>';
117 123
 				}
118 124
 				$page->Content = $content . '</ul>';
119
-			}  else {
125
+			} else {
120 126
 				$doc = new DOMDocument();
121 127
 				$doc->loadHTML($page->Content);
122 128
 				$body = $doc->getElementsByTagName('body')->item(0);
@@ -145,7 +151,9 @@  discard block
 block discarded – undo
145 151
 
146 152
 			// Store the result
147 153
 			$page->write();
148
-			if($publishPages) $page->doPublish();
154
+			if($publishPages) {
155
+				$page->doPublish();
156
+			}
149 157
 		}
150 158
 	}
151 159
 
@@ -191,16 +199,22 @@  discard block
 block discarded – undo
191 199
 			unset($this->unusedChildren[$page->ID]);
192 200
 			file_put_contents(ASSETS_PATH . '/index-' . $sort . '.html', $this->getBodyText($subdoc, $subnode));
193 201
 
194
-			if ($sort) $page->Sort = $sort;
202
+			if ($sort) {
203
+				$page->Sort = $sort;
204
+			}
195 205
 			$page->Content = $this->getBodyText($subdoc, $subnode);
196 206
 			$page->write();
197
-			if($publishPages) $page->doPublish();
207
+			if($publishPages) {
208
+				$page->doPublish();
209
+			}
198 210
 		} else {
199 211
 			// Write to the master page.
200 212
 			$record->Content = $this->getBodyText($subdoc, $subnode);
201 213
 			$record->write();
202 214
 
203
-			if($publishPages) $record->doPublish();
215
+			if($publishPages) {
216
+				$record->doPublish();
217
+			}
204 218
 		}
205 219
 
206 220
 	}
@@ -286,7 +300,9 @@  discard block
 block discarded – undo
286 300
 			}
287 301
 
288 302
 			$nodes = array();
289
-			foreach($xpath->query($rule) as $node) $nodes[] = $node;
303
+			foreach($xpath->query($rule) as $node) {
304
+				$nodes[] = $node;
305
+			}
290 306
 
291 307
 			foreach($nodes as $node) {
292 308
 				$parent = $node->parentNode;
@@ -296,7 +312,9 @@  discard block
 block discarded – undo
296 312
 					$node->nextSibling ? $node->parentNode->insertBefore($parent, $node->nextSibling) : $node->parentNode->appendChild($parent);
297 313
 				}
298 314
 
299
-				while($node->firstChild) $parent->appendChild($node->firstChild);
315
+				while($node->firstChild) {
316
+					$parent->appendChild($node->firstChild);
317
+				}
300 318
 				$node->parentNode->removeChild($node);
301 319
 			}
302 320
 		}
@@ -324,10 +342,15 @@  discard block
 block discarded – undo
324 342
 			// First get all the nodes. Need to build array, as they'll disappear from the nodelist while we're deleteing them, causing the indexing
325 343
 			// to screw up.
326 344
 			$nodes = array();
327
-			foreach($xpath->query($query) as $node) $nodes[] = $node;
345
+			foreach($xpath->query($query) as $node) {
346
+				$nodes[] = $node;
347
+			}
328 348
 
329 349
 			// Then remove them all
330
-			foreach ($nodes as $node) { if ($node->parentNode) $node->parentNode->removeChild($node); }
350
+			foreach ($nodes as $node) { if ($node->parentNode) {
351
+				$node->parentNode->removeChild($node);
352
+			}
353
+			}
331 354
 		}
332 355
 
333 356
 		// Now split the document into portions by H1
Please login to merge, or discard this patch.
code/DocumentImportField.php 2 patches
Spacing   +4 added lines, -4 removed lines patch added patch discarded remove patch
@@ -38,9 +38,9 @@  discard block
 block discarded – undo
38 38
 					'Split document into pages'
39 39
 				), 
40 40
 				array(
41
-					0 => _t('DocumentImportField.No','no'), 
42
-					1 => _t('DocumentImportField.EachH1','for each heading 1'), 
43
-					2 => _t('DocumentImportField.EachH2','for each heading 2')
41
+					0 => _t('DocumentImportField.No', 'no'), 
42
+					1 => _t('DocumentImportField.EachH1', 'for each heading 1'), 
43
+					2 => _t('DocumentImportField.EachH2', 'for each heading 2')
44 44
 				)
45 45
 			),
46 46
 			$keepSource = new CheckboxField(
@@ -68,7 +68,7 @@  discard block
 block discarded – undo
68 68
 			),
69 69
 			$this->innerField = new DocumentImportInnerField(
70 70
 				'ImportedFromFile', 
71
-				_t('DocumentImportField.ImportedFromFile','Import content from a word document')
71
+				_t('DocumentImportField.ImportedFromFile', 'Import content from a word document')
72 72
 			),
73 73
 		));
74 74
 
Please login to merge, or discard this patch.
Braces   +6 added lines, -2 removed lines patch added patch discarded remove patch
@@ -16,8 +16,12 @@
 block discarded – undo
16 16
 	 * @param $children FieldSet/array Any additional children.
17 17
 	 */
18 18
 	public function __construct($children = null) {
19
-		if (is_string($children)) throw new InvalidArgumentException('DocumentImportField::__construct does not accept a name as its parameter, it defaults to "ImportedFromFile" instead. Use DocumentImportField::getInnerField()->setName() if you want to change it.');
20
-		if ($children) throw new InvalidArgumentException('DocumentImportField::__construct provides its own fields and does not accept additional children.');
19
+		if (is_string($children)) {
20
+			throw new InvalidArgumentException('DocumentImportField::__construct does not accept a name as its parameter, it defaults to "ImportedFromFile" instead. Use DocumentImportField::getInnerField()->setName() if you want to change it.');
21
+		}
22
+		if ($children) {
23
+			throw new InvalidArgumentException('DocumentImportField::__construct provides its own fields and does not accept additional children.');
24
+		}
21 25
 
22 26
 		// Add JS specific to this field.
23 27
 		Requirements::javascript('documentconverter/javascript/DocumentImportField.js');
Please login to merge, or discard this patch.