Completed
Push — master ( 9e79e0...3fae1e )
by Andreas
38:47
created

dialog::response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 5
Code Lines 3

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 3
nc 1
nop 1
dl 0
loc 5
ccs 2
cts 2
cp 1
crap 1
rs 10
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 midcom_response_styled;
14
use Symfony\Component\HttpFoundation\Request;
15
use Symfony\Component\HttpFoundation\Response;
16
17
/**
18
 * Workflow base class
19
 *
20
 * @package midcom.workflow
21
 */
22
abstract class dialog
23
{
24
    const INACTIVE = 'inactive';
25
26
    /**
27
     * @var string
28
     */
29
    protected $state = self::INACTIVE;
30 221
31
    public function __construct(array $options = [])
32 221
    {
33 221
        $resolver = new OptionsResolver();
34
        $this->configure($resolver);
35 221
36 220
        foreach ($resolver->resolve($options) as $key => $value) {
37
            $this->$key = $value;
38 221
        }
39
    }
40
41
    /**
42
     *
43
     * @param OptionsResolver $resolver
44 128
     */
45
    public function configure(OptionsResolver $resolver)
46 128
    {
47
    }
48 148
49
    public static function add_head_elements()
50 148
    {
51 148
        $head = midcom::get()->head;
52 148
        $head->enable_jquery_ui(['mouse', 'draggable', 'resizable', 'button', 'dialog']);
53 148
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.js');
54 148
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
55 148
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.css');
56
    }
57 72
58
    public static function add_dialog_js()
59 72
    {
60 72
        midcom::get()->head->enable_jquery_ui(['button']);
61 72
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
62
    }
63
64
    public static function response(\midcom_core_context $context) : midcom_response_styled
65
    {
66 26
        self::add_dialog_js();
67
        midcom::get()->style->append_styledir(__DIR__ . '/style');
68 26
        return new midcom_response_styled($context, 'POPUP');
69
    }
70
71
    /**
72
     * @return string
73
     */
74
    public function get_state() : string
75
    {
76
        return $this->state;
77 147
    }
78
79 147
    /**
80 147
     *
81 147
     * @param string $url
82
     * @param array $options
83
     * @return array button config in midcom_helper_toolbar format
84 147
     */
85
    public function get_button($url, array $options = []) : array
86 147
    {
87 145
        static::add_head_elements();
88 145
        $button_config = $this->get_button_config();
89 4
        if (!empty($options[MIDCOM_TOOLBAR_ICON])) {
90
            unset($button_config[MIDCOM_TOOLBAR_GLYPHICON]);
91 145
        }
92
        $button_config[MIDCOM_TOOLBAR_URL] = $url;
93 147
        //The constants are numeric, so array_merge won't work...
94
        foreach ($options as $key => $value) {
95
            if (   is_array($value)
96
                && !empty($button_config[$key])) {
97
                $value = array_merge($button_config[$key], $value);
98
            }
99
            $button_config[$key] = $value;
100 7
        }
101
        return $button_config;
102 7
    }
103
104 7
    /**
105
     *
106 7
     * @return string
107 7
     */
108
    public function render_attributes() : string
109
    {
110 7
        $button_config = $this->get_button_config();
111
112
        $output = ' title="' . htmlspecialchars($button_config[MIDCOM_TOOLBAR_LABEL]) . '"';
113
114
        foreach ($button_config[MIDCOM_TOOLBAR_OPTIONS] as $key => $val) {
115
            $output .= ' ' . $key . '="' . htmlspecialchars($val) . '"';
116
        }
117
118
        return $output;
119
    }
120
121
    /**
122
     * @return array
123
     */
124
    abstract public function get_button_config();
125
126
    /**
127
     * @return Response
128
     */
129
    abstract public function run(Request $request);
130
}
131