Completed
Push — master ( 0b47d3...aa71cd )
by Andreas
14:17
created

dialog::js_response()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 7
Code Lines 5

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 6
CRAP Score 1

Importance

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