Completed
Push — master ( d22395...24bd03 )
by Andreas
13:45
created

dialog::get_button()   A

Complexity

Conditions 5
Paths 6

Size

Total Lines 17
Code Lines 11

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 11
CRAP Score 5.0144

Importance

Changes 0
Metric Value
cc 5
eloc 11
nc 6
nop 2
dl 0
loc 17
ccs 11
cts 12
cp 0.9167
crap 5.0144
rs 9.6111
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package midcom.workflow
4
 * @author CONTENT CONTROL http://www.contentcontrol-berlin.de/
5
 * @copyright CONTENT CONTROL http://www.contentcontrol-berlin.de/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
namespace midcom\workflow;
10
11
use Symfony\Component\OptionsResolver\OptionsResolver;
12
use midcom;
13
use Symfony\Component\HttpFoundation\Request;
14
use Symfony\Component\HttpFoundation\Response;
15
16
/**
17
 * Workflow base class
18
 *
19
 * @package midcom.workflow
20
 */
21
abstract class dialog
22
{
23
    const INACTIVE = 'inactive';
24
25
    /**
26
     * @var string
27
     */
28
    protected $state = self::INACTIVE;
29
30 210
    public function __construct(array $options = [])
31
    {
32 210
        $resolver = new OptionsResolver();
33 210
        $this->configure($resolver);
34
35 210
        foreach ($resolver->resolve($options) as $key => $value) {
36 209
            $this->$key = $value;
37
        }
38 210
    }
39
40
    /**
41
     *
42
     * @param OptionsResolver $resolver
43
     */
44 53
    public function configure(OptionsResolver $resolver)
45
    {
46 53
    }
47
48 139
    public static function add_head_elements()
49
    {
50 139
        $head = midcom::get()->head;
51 139
        $head->enable_jquery_ui(['mouse', 'draggable', 'resizable', 'button', 'dialog']);
52 139
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.js');
53 139
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
54 139
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.css');
55 139
    }
56
57 71
    protected function add_dialog_js()
58
    {
59 71
        midcom::get()->head->enable_jquery_ui(['button']);
60 71
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
61 71
    }
62
63
    /**
64
     * @return string
65
     */
66 26
    public function get_state()
67
    {
68 26
        return $this->state;
69
    }
70
71
    /**
72
     *
73
     * @param string $url
74
     * @param array $options
75
     * @return array button config in midcom_helper_toolbar format
76
     */
77 138
    public function get_button($url, array $options = [])
78
    {
79 138
        static::add_head_elements();
80 138
        $button_config = $this->get_button_config();
81 138
        if (!empty($options[MIDCOM_TOOLBAR_ICON])) {
82
            unset($button_config[MIDCOM_TOOLBAR_GLYPHICON]);
83
        }
84 138
        $button_config[MIDCOM_TOOLBAR_URL] = $url;
85
        //The constants are numeric, so array_merge won't work...
86 138
        foreach ($options as $key => $value) {
87 136
            if (   is_array($value)
88 136
                && !empty($button_config[$key])) {
89 4
                $value = array_merge($button_config[$key], $value);
90
            }
91 136
            $button_config[$key] = $value;
92
        }
93 138
        return $button_config;
94
    }
95
96
    /**
97
     *
98
     * @return string
99
     */
100 7
    public function render_attributes()
101
    {
102 7
        $button_config = $this->get_button_config();
103
104 7
        $output = ' title="' . htmlspecialchars($button_config[MIDCOM_TOOLBAR_LABEL]) . '"';
105
106 7
        foreach ($button_config[MIDCOM_TOOLBAR_OPTIONS] as $key => $val) {
107 7
            $output .= ' ' . $key . '="' . htmlspecialchars($val) . '"';
108
        }
109
110 7
        return $output;
111
    }
112
113
    /**
114
     * @return array
115
     */
116
    abstract public function get_button_config();
117
118
    /**
119
     * @return Response
120
     */
121
    abstract public function run(Request $request);
122
}
123