Completed
Push — master ( 1dd31e...35fb2b )
by Tim
13:18
created

AbstractRendering::parseBody()   B

Complexity

Conditions 3
Paths 4

Size

Total Lines 24
Code Lines 14

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 24
rs 8.9713
cc 3
eloc 14
nc 4
nop 2
1
<?php
2
/**
3
 * Abstract rendering helper
4
 *
5
 * @author  Tim Lochmüller
6
 */
7
8
namespace FRUIT\Ink\Rendering;
9
10
use FRUIT\Ink\Configuration;
11
use TYPO3\CMS\Core\Utility\MailUtility;
12
use TYPO3\CMS\Frontend\ContentObject\ContentObjectRenderer;
13
14
/**
15
 * Abstract rendering helper
16
 */
17
abstract class AbstractRendering implements RenderingInterface
18
{
19
20
    /**
21
     * Content object
22
     *
23
     * @var ContentObjectRenderer
24
     */
25
    protected $contentObject;
26
27
    /**
28
     * Configuration
29
     *
30
     * @var array
31
     */
32
    protected $configuration;
33
34
    /**
35
     * Set the content object and configuration
36
     * Call the renderInternal after preparation
37
     *
38
     * @param ContentObjectRenderer $contentObject
39
     * @param array                 $configuration
40
     *
41
     * @return array
42
     */
43
    public function render($contentObject, $configuration)
44
    {
45
        $this->contentObject = $contentObject;
46
        $this->configuration = $configuration;
47
        return $this->renderInternal();
48
    }
49
50
    /**
51
     * Parsing the bodytext field content, removing typical entities and <br /> tags.
52
     *
53
     * @param    string $str     : Field content from "bodytext" or other text field
54
     * @param    string $altConf : Altername conf name (especially when bodyext field in other table then tt_content)
55
     *
56
     * @return    string        Processed content
57
     */
58
    function parseBody($str, $altConf = 'bodytext')
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
59
    {
60
        if ($this->configuration[$altConf . '.']['doubleLF']) {
61
            $str = preg_replace("/\n/", "\n\n", $str);
62
        }
63
        // Regular parsing:
64
        $str = preg_replace('/<br\s*\/?>/i', chr(10), $str);
65
        $str = $this->contentObject->stdWrap($str, $this->configuration[$altConf . '.']['stdWrap.']);
66
67
        // Then all a-tags:
68
        $aConf = array();
69
        $aConf['parseFunc.']['tags.']['a'] = 'USER';
70
        // check direct mail usage @todo
71
        $aConf['parseFunc.']['tags.']['a.']['userFunc'] = 'FRUIT\\Ink\\PlainRenderer->atag_to_http';
72
        $aConf['parseFunc.']['tags.']['a.']['siteUrl'] = 'http://www.google.de';
73
        $str = $this->contentObject->stdWrap($str, $aConf);
74
        $str = str_replace('&nbsp;', ' ', htmlspecialchars_decode($str));
75
76
        if ($this->configuration[$altConf . '.']['header']) {
77
            $str = $this->configuration[$altConf . '.']['header'] . LF . $str;
78
        }
79
80
        return chr(10) . $str;
81
    }
82
83
    /**
84
     * Function used to wrap the bodytext field content (or image caption) into lines of a max length of
85
     *
86
     * @param        string $str : The content to break
87
     *
88
     * @return        string                Processed value.
89
     * @see main_plaintext(), breakLines()
90
     */
91
    function breakContent($str)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
92
    {
93
        $cParts = explode(chr(10), $str);
94
        $lines = array();
95
        foreach ($cParts as $substrs) {
96
            $lines[] = $this->breakLines($substrs, LF);
97
        }
98
        return implode(chr(10), $lines);
99
    }
100
101
    /**
102
     * Returns a typolink URL based on input.
103
     *
104
     * @param    string $ll : Parameter to typolink
105
     *
106
     * @return    string        The URL returned from $this->cObj->getTypoLink_URL(); - possibly it prefixed with the URL of the site if not present already
107
     */
108
    function getLink($ll)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
109
    {
110
        return $this->contentObject->getTypoLink_URL($ll);
111
    }
112
113
    /**
114
     * Breaking lines into fixed length lines, using GeneralUtility::breakLinesForEmail()
115
     *
116
     * @param        string  $str       : The string to break
117
     * @param        string  $implChar  : Line break character
118
     * @param        integer $charWidth : Length of lines, default is $this->charWidth
119
     *
120
     * @return        string                Processed string
121
     */
122
    function breakLines($str, $implChar = LF, $charWidth = false)
0 ignored issues
show
Best Practice introduced by
It is generally recommended to explicitly declare the visibility for methods.

Adding explicit visibility (private, protected, or public) is generally recommend to communicate to other developers how, and from where this method is intended to be used.

Loading history...
123
    {
124
        $charWidth = $charWidth === false ? Configuration::getPlainTextWith() : (int)$charWidth;
125
        return MailUtility::breakLinesForEmail($str, $implChar, $charWidth);
126
    }
127
}
128