Completed
Push — dev ( 35c279...79fff2 )
by Nicolas
01:54
created

ERFXSLTUTilities::getXmlParams()   A

Complexity

Conditions 2
Paths 2

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %
Metric Value
dl 0
loc 7
rs 9.4285
cc 2
eloc 5
nc 2
nop 1
1
<?php
2
	/*
3
	Copyright: Deux Huit Huit 2016
4
	LICENCE: MIT http://deuxhuithuit.mit-license.org;
5
	*/
6
	
7
	if(!defined("__IN_SYMPHONY__")) die("<h2>Error</h2><p>You cannot directly access this file</p>");
8
	
9
	class ERFXSLTUTilities {
10
		public static function entryToXml($parentField, $entry, $entrySectionHandle, $entryFields)
11
		{
12
			$date = new DateTime();
13
			$params = array(
14
				'today' => $date->format('Y-m-d'),
15
				'current-time' => $date->format('H:i'),
16
				'this-year' => $date->format('Y'),
17
				'this-month' => $date->format('m'),
18
				'this-day' => $date->format('d'),
19
				'timezone' => $date->format('P'),
20
				'website-name' => Symphony::Configuration()->get('sitename', 'general'),
21
				'root' => URL,
22
				'workspace' => URL . '/workspace',
23
				'http-host' => HTTP_HOST
24
			);
25
			$entryData = $entry->getData();
26
			$includedElements = FieldEntry_relationship::parseElements($parentField);
27
			
28
			$xslFilePath = WORKSPACE . '/er-templates/' . $entrySectionHandle . '.xsl';
29
			if (!empty($entryData) && !!@file_exists($xslFilePath)) {
30
				$xmlData = new XMLElement('data');
31
				$xmlData->setIncludeHeader(true);
32
				$xml = new XMLElement('entry');
33
				$xml->setAttribute('id', $entryId);
0 ignored issues
show
Bug introduced by
The variable $entryId does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
34
				$xmlData->appendChild(self::getXmlParams($params));
35
				$xmlData->appendChild($xml);
36
				foreach ($entryData as $fieldId => $data) {
37
					$filteredData = array_filter($data, function ($value) {
38
						return $value != null;
39
					});
40
					if (empty($filteredData)) {
41
						continue;
42
					}
43
					$field = $entryFields[$fieldId];
44
					$fieldName = $field->get('element_name');
45
					$fieldIncludedElement = $includedElements[$entrySectionHandle];
46
					
47
					try {
48
						if (FieldEntry_relationship::isFieldIncluded($fieldName, $fieldIncludedElement)) {
49
							$parentIncludableElement = FieldEntry_relationship::getSectionElementName($fieldName, $fieldIncludedElement);
50
							$parentIncludableElementMode = FieldEntry_relationship::extractMode($fieldName, $parentIncludableElement);
51
							
52
							// Special treatments for ERF
53
							if ($field instanceof FieldEntry_relationship) {
54
								// Increment recursive level
55
								$field->recursiveLevel = $recursiveLevel + 1;
0 ignored issues
show
Bug introduced by
The variable $recursiveLevel does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The property recursiveLevel cannot be accessed from this context as it is declared protected in class FieldEntry_relationship.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
56
								$field->recursiveDeepness = $deepness;
0 ignored issues
show
Bug introduced by
The variable $deepness does not exist. Did you forget to declare it?

This check marks access to variables or properties that have not been declared yet. While PHP has no explicit notion of declaring a variable, accessing it before a value is assigned to it is most likely a bug.

Loading history...
Bug introduced by
The property recursiveDeepness cannot be accessed from this context as it is declared protected in class FieldEntry_relationship.

This check looks for access to properties that are not accessible from the current context.

If you need to make a property accessible to another context you can either raise its visibility level or provide an accessible getter in the defining class.

Loading history...
57
							}
58
							
59 View Code Duplication
							if ($parentIncludableElementMode == null) {
0 ignored issues
show
Duplication introduced by
This code seems to be duplicated across your project.

Duplicated code is one of the most pungent code smells. If you need to duplicate the same code in three or more different places, we strongly encourage you to look into extracting the code into a single class or operation.

You can also find more detailed suggestions in the “Code” section of your repository.

Loading history...
60
								$submodes = array_map(function ($fieldIncludableElement) use ($fieldName) {
61
									return FieldEntry_relationship::extractMode($fieldName, $fieldIncludableElement);
62
								}, $field->fetchIncludableElements());
63
							}
64
							else {
65
								$submodes = array($parentIncludableElementMode);
66
							}
67
							
68
							foreach ($submodes as $submode) {
69
								$field->appendFormattedElement($xml, $filteredData, false, $submode, $entryId);
70
							}
71
						}
72
					}
73
					catch (Exception $ex) {
74
						$xml->appendChild(new XMLElement('error', $ex->getMessage() . ' on ' . $ex->getLine() . ' of file ' . $ex->getFile()));
75
					}
76
				}
77
				
78
				$indent = false;
79
				$mode = $parentField->get('mode');
80
				if (isset($_REQUEST['debug'])) {
81
					$mode = 'debug';
82
				}
83
				if ($mode == 'debug') {
84
					$indent = true;
85
				}
86
				$xmlMode = empty($mode) ? '' : 'mode="' . $mode . '"';
87
				$xmlString = $xmlData->generate($indent, 0);
88
				$xsl = '<?xml version="1.0" encoding="UTF-8"?>
89
				<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
90
					<xsl:import href="' . str_replace('\\', '/',  $xslFilePath) . '"/>
91
					<xsl:output method="xml" omit-xml-declaration="yes" encoding="UTF-8" indent="no" />
92
					<xsl:template match="/">
93
						<xsl:apply-templates select="/data" ' . $xmlMode . ' />
94
					</xsl:template>
95
					<xsl:template match="/data" ' . $xmlMode . '>
96
						<xsl:apply-templates select="entry" ' . $xmlMode . ' />
97
					</xsl:template>
98
					<xsl:template match="/data" mode="debug">
99
						<xsl:copy-of select="/" />
100
					</xsl:template>
101
				</xsl:stylesheet>';
102
				$xslt = new XsltProcess();
103
				$result = $xslt->process($xmlString, $xsl, $params);
104
				
105
				if ($mode == 'debug') {
106
					$result = '<pre><code>' .
107
						str_replace('<', '&lt;', str_replace('>', '&gt;', $xmlString)) .
108
						'</code></pre>';
109
				}
110
				
111
				if ($xslt->isErrors()) {
112
					$error = $xslt->getError();
113
					$result = $error[1]['message'];
114
				}
115
				
116
				if (General::strlen($result) > 0) {
117
					return new XMLElement('div', $result, array('class' => 'content'));
118
				}
119
			}
120
			return null;
121
		}
122
		
123
		public static function getXmlParams(array $params) {
124
			$xmlparams = new XMLElement('params');
125
			foreach ($params as $key => $value) {
126
				$xmlparams->appendChild(new XMLElement($key, $value));
127
			}
128
			return $xmlparams;
129
		}
130
	}