MarkdownTextareaField::FieldHolder()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 13
Code Lines 8

Duplication

Lines 0
Ratio 0 %

Importance

Changes 0
Metric Value
dl 0
loc 13
rs 9.4285
c 0
b 0
f 0
cc 1
eloc 8
nc 1
nop 1
1
<?php
2
/**
3
 * Configure the input field for markdown
4
 */
5
class MarkdownTextareaField extends TextareaField
0 ignored issues
show
Coding Style Compatibility introduced by
PSR1 recommends that each class must be in a namespace of at least one level to avoid collisions.

You can fix this by adding a namespace to your class:

namespace YourVendor;

class YourClass { }

When choosing a vendor namespace, try to pick something that is not too generic to avoid conflicts with other libraries.

Loading history...
6
{
7
    /**
8
     * Define the actions allowed on this field
9
     * @var array
10
     */
11
    private static $allowed_actions = array(
0 ignored issues
show
Comprehensibility introduced by
Consider using a different property name as you override a private property of the parent class.
Loading history...
Unused Code introduced by
The property $allowed_actions is not used and could be removed.

This check marks private properties in classes that are never used. Those properties can be removed.

Loading history...
12
        'preview',
13
        'parse'
14
    );
15
16
    /**
17
     * @var int Visible number of text lines.
18
     * Default on TextareaField is too small
19
     */
20
    protected $rows = 15;
21
22
    /**
23
     * Toggle rendering markdown with extra syntax enabled.
24
     * @link http://michelf.ca/projects/php-markdown/extra
25
     * @var boolean
26
     */
27
    protected $enable_extra = false;
28
29
    /**
30
     * Returns the field holder used by templates
31
     *
32
     * @param  array $properties
33
     * @return string            HTML to be used
34
     */
35
    public function FieldHolder($properties = array())
36
    {
37
        $this->extraClasses['stacked'] = 'stacked';
38
39
        Requirements::css(MARKDOWN_DIR . '/templates/css/styles.css');
40
41
        Requirements::javascript(THIRDPARTY_DIR . '/jquery/jquery.js');
42
        Requirements::javascript(THIRDPARTY_DIR . '/jquery-entwine/dist/jquery.entwine-dist.js');
43
        Requirements::javascript(MARKDOWN_DIR . '/thirdparty/textinputs_jquery.js');
44
        Requirements::javascript(MARKDOWN_DIR . '/templates/javascript/script.js');
45
46
        return parent::FieldHolder($properties);
47
    }
48
49
    /**
50
     * Turn on extra syntax support
51
     * @return MarkdownTextareaField
52
     */
53
    public function enableExtra()
54
    {
55
        $this->enable_extra = true;
56
        return $this;
57
    }
58
59
    /**
60
     * Body for the preview iframe with just the typography styles included
61
     * @return string html
0 ignored issues
show
Documentation introduced by
Should the return type not be HTMLText?

This check compares the return type specified in the @return annotation of a function or method doc comment with the types returned by the function and raises an issue if they mismatch.

Loading history...
62
     */
63
    public function preview()
64
    {
65
        Requirements::clear();
66
        // Should contain text styles of the page by Silverstripe theme conventions.
67
        Requirements::css('themes/' . Config::inst()->get('SSViewer', 'theme') . '/css/editor.css');
68
        return $this->renderWith('PreviewFrame');
69
    }
70
71
    /**
72
     * Parse markdown into html
73
     * @return string html
74
     */
75
    public function parse()
76
    {
77
        $parser = new MarkdownParser($this->request['markdown']);
78
79
        return ($this->enable_extra) ? $parser->parseExtra() : $parser->parse();
80
    }
81
82
    /**
83
     * Get buttons described in buttons.yml and wrap them in ViewableData
84
     * @return ArrayList list of buttons and theyr configurations
85
     */
86
    public function ToolbarButtons()
87
    {
88
        $buttons = new ArrayList();
89
90
        foreach ($this->config()->get('buttons') as $button) {
91
            $buttons->push(new ArrayData($button));
92
        }
93
94
        return $buttons;
95
    }
96
}
97