Completed
Push — master ( 3fae1e...4e7cc0 )
by Andreas
29:46 queued 13:59
created

dialog::get_state()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 3
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 2
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 1
nc 1
nop 0
dl 0
loc 3
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
31 222
    public function __construct(array $options = [])
32
    {
33 222
        $resolver = new OptionsResolver();
34 222
        $this->configure($resolver);
35
36 222
        foreach ($resolver->resolve($options) as $key => $value) {
37 221
            $this->$key = $value;
38
        }
39 222
    }
40
41
    /**
42
     *
43
     * @param OptionsResolver $resolver
44
     */
45 128
    public function configure(OptionsResolver $resolver)
46
    {
47 128
    }
48
49 148
    public static function add_head_elements()
50
    {
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 148
    }
57
58 72
    public static function add_dialog_js()
59
    {
60 72
        midcom::get()->head->enable_jquery_ui(['button']);
61 72
        midcom::get()->head->add_jsfile(MIDCOM_STATIC_URL . '/midcom.workflow/dialog.js');
62 72
    }
63
64 72
    public static function response(\midcom_core_context $context) : midcom_response_styled
65
    {
66 72
        self::add_dialog_js();
67 72
        midcom::get()->style->append_styledir(__DIR__ . '/style');
68 72
        return new midcom_response_styled($context, 'POPUP');
69
    }
70
71
    /**
72
     * @return string
73
     */
74 27
    public function get_state() : string
75
    {
76 27
        return $this->state;
77
    }
78
79
    /**
80
     *
81
     * @param string $url
82
     * @param array $options
83
     * @return array button config in midcom_helper_toolbar format
84
     */
85 147
    public function get_button($url, array $options = []) : array
86
    {
87 147
        static::add_head_elements();
88 147
        $button_config = $this->get_button_config();
89 147
        if (!empty($options[MIDCOM_TOOLBAR_ICON])) {
90
            unset($button_config[MIDCOM_TOOLBAR_GLYPHICON]);
91
        }
92 147
        $button_config[MIDCOM_TOOLBAR_URL] = $url;
93
        //The constants are numeric, so array_merge won't work...
94 147
        foreach ($options as $key => $value) {
95 145
            if (   is_array($value)
96 145
                && !empty($button_config[$key])) {
97 4
                $value = array_merge($button_config[$key], $value);
98
            }
99 145
            $button_config[$key] = $value;
100
        }
101 147
        return $button_config;
102
    }
103
104
    /**
105
     *
106
     * @return string
107
     */
108 7
    public function render_attributes() : string
109
    {
110 7
        $button_config = $this->get_button_config();
111
112 7
        $output = ' title="' . htmlspecialchars($button_config[MIDCOM_TOOLBAR_LABEL]) . '"';
113
114 7
        foreach ($button_config[MIDCOM_TOOLBAR_OPTIONS] as $key => $val) {
115 7
            $output .= ' ' . $key . '="' . htmlspecialchars($val) . '"';
116
        }
117
118 7
        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