Passed
Push — master ( 9d50f2...da7c38 )
by Andreas
28:20 queued 07:36
created

dialog::configure()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 0

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 0
CRAP Score 2

Importance

Changes 0
Metric Value
cc 1
eloc 0
nc 1
nop 1
dl 0
loc 2
ccs 0
cts 0
cp 0
crap 2
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
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 229
    public function __construct(array $options = [])
33
    {
34 229
        $resolver = new OptionsResolver();
35 229
        $this->configure($resolver);
36
37 229
        foreach ($resolver->resolve($options) as $key => $value) {
38 228
            $this->$key = $value;
39
        }
40
    }
41
42
    public function configure(OptionsResolver $resolver)
43
    {
44
    }
45
46 149
    public static function add_head_elements()
47
    {
48 149
        $head = midcom::get()->head;
49 149
        $head->enable_jquery_ui(['mouse', 'draggable', 'resizable', 'button', 'dialog']);
50 149
        $head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.js');
51 149
        $head->add_stylesheet(MIDCOM_STATIC_URL . "/stock-icons/font-awesome-4.7.0/css/font-awesome.min.css");
52 149
        $head->add_stylesheet(MIDCOM_STATIC_URL . '/midcom.workflow/workflow.css');
53
    }
54
55 75
    protected function add_dialog_js()
56
    {
57 75
        midcom::get()->head->enable_jquery_ui(['button']);
58 75
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
59
    }
60
61 75
    protected function response(\midcom_core_context $context) : midcom_response_styled
62
    {
63 75
        $this->add_dialog_js();
64 75
        midcom::get()->style->append_styledir(__DIR__ . '/style');
65 75
        return new midcom_response_styled($context, 'POPUP');
66
    }
67
68 29
    public function get_state() : string
69
    {
70 29
        return $this->state;
71
    }
72
73
    /**
74
     * @return array button config in midcom_helper_toolbar format
75
     */
76 148
    public function get_button(string $url, array $options = []) : array
77
    {
78 148
        static::add_head_elements();
79 148
        $button_config = $this->get_button_config();
80 148
        if (!empty($options[MIDCOM_TOOLBAR_ICON])) {
81
            unset($button_config[MIDCOM_TOOLBAR_GLYPHICON]);
82
        }
83 148
        $button_config[MIDCOM_TOOLBAR_URL] = $url;
84
        //The constants are numeric, so array_merge won't work...
85 148
        foreach ($options as $key => $value) {
86 146
            if (   is_array($value)
87 146
                && !empty($button_config[$key])) {
88 2
                $value = array_merge($button_config[$key], $value);
89
            }
90 146
            $button_config[$key] = $value;
91
        }
92 148
        return $button_config;
93
    }
94
95 7
    public function render_attributes() : string
96
    {
97 7
        $button_config = $this->get_button_config();
98
99 7
        $output = ' title="' . htmlspecialchars($button_config[MIDCOM_TOOLBAR_LABEL]) . '"';
100
101 7
        foreach ($button_config[MIDCOM_TOOLBAR_OPTIONS] as $key => $val) {
102 7
            $output .= ' ' . $key . '="' . htmlspecialchars($val) . '"';
103
        }
104
105 7
        return $output;
106
    }
107
108 14
    public function js_response(string $script) : Response
109
    {
110 14
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
111 14
        midcom::get()->head->add_jscript($script);
112 14
        $content = '<!DOCTYPE html><html><head>' . midcom::get()->head->render() . '</head><body></body></html>';
113 14
        return new Response($content);
114
    }
115
116
    abstract public function get_button_config() : array;
117
118
    abstract public function run(Request $request) : Response;
119
}
120