Completed
Push — master ( b0bf1d...042f37 )
by Andreas
14:04
created

org_openpsa_relatedto_plugin::add_header_files()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Code Coverage

Tests 3
CRAP Score 1

Importance

Changes 0
Metric Value
cc 1
eloc 2
nc 1
nop 0
dl 0
loc 4
ccs 3
cts 3
cp 1
crap 1
rs 10
c 0
b 0
f 0
1
<?php
2
/**
3
 * @package org.openpsa.relatedto
4
 * @author Nemein Oy, http://www.nemein.com/
5
 * @copyright Nemein Oy, http://www.nemein.com/
6
 * @license http://www.gnu.org/licenses/gpl.html GNU General Public License
7
 */
8
9
/**
10
 * Class for handling "related to" information
11
 *
12
 * @package org.openpsa.relatedto
13
 */
14
class org_openpsa_relatedto_plugin extends midcom_baseclasses_components_plugin
15
{
16
    /**
17
     * Shorthand for creating a relatedto object.
18
     *
19
     * The <i>from</i> object is something that is related to the <em>to</em>
20
     * object.
21
     * For example, if a task is created under a sales project, that task is
22
     * the from object, and the sales project the to object.
23
     *
24
     * @param object $from_obj The from object
25
     * @param string $from_component The from component name
26
     * @param object $to_obj The to object
27
     * @param string $to_component The to component name
28
     * @param int $status The status of the relation
29
     * @param array $extra Array with the possible extra-properties
30
     * @return mixed The newly-created relatedto object or false on failure
31
     */
32 11
    public static function create($from_obj, $from_component, $to_obj, $to_component, $status = null, array $extra = [])
33
    {
34 11
        if (   !is_object($from_obj)
35 11
            || !is_object($to_obj)) {
36 1
            return false;
37
        }
38
39 11
        if (!$status) {
0 ignored issues
show
Bug Best Practice introduced by
The expression $status of type integer|null is loosely compared to false; this is ambiguous if the integer can be 0. You might want to explicitly use === null instead.

In PHP, under loose comparison (like ==, or !=, or switch conditions), values of different types might be equal.

For integer values, zero is a special case, in particular the following results might be unexpected:

0   == false // true
0   == null  // true
123 == false // false
123 == null  // false

// It is often better to use strict comparison
0 === false // false
0 === null  // false
Loading history...
40 11
            $status = org_openpsa_relatedto_dba::CONFIRMED;
41
        }
42
43 11
        $rel = new org_openpsa_relatedto_dba();
44 11
        $rel->fromClass = get_class($from_obj);
45 11
        $rel->toClass = get_class($to_obj);
46 11
        $rel->fromGuid = $from_obj->guid;
47 11
        $rel->toGuid = $to_obj->guid;
48 11
        $rel->fromComponent = $from_component;
49 11
        $rel->toComponent = $to_component;
50 11
        $rel->status = $status;
51
52 11
        if ($guid = $rel->check_db(false)) {
53 1
            $db_rel = new org_openpsa_relatedto_dba($guid);
54 1
            debug_add("A relation from {$rel->fromClass} #{$rel->fromGuid} to {$rel->toClass} #{$rel->toGuid} already exists, returning this one instead");
55 1
            if ($db_rel->status < $rel->status) {
56 1
                $db_rel->status = $rel->status;
57 1
                $db_rel->update();
58
            }
59 1
            return $db_rel;
60
        }
61
62 11
        foreach ($extra as $extra_key => $extra_value) {
63
            $rel->$extra_key = $extra_value;
64
        }
65
66 11
        if (!$rel->create()) {
67
            debug_add("failed to create link from {$rel->fromClass} #{$rel->fromGuid} to {$rel->toClass} #{$rel->toGuid}, errstr: " . midcom_connection::get_error_string(), MIDCOM_LOG_WARN);
68
            return false;
69
        }
70
71 11
        return $rel;
72
    }
73
74
    /**
75
     * Parses relatedto information from request, returning either
76
     * existing matching relatedtos or prefilled new ones for creation
77
     */
78 30
    public static function get2relatedto()
79
    {
80 30
        $ret = [];
81 30
        if (!array_key_exists('org_openpsa_relatedto', $_REQUEST)) {
82 30
            return $ret;
83
        }
84
        foreach ($_REQUEST['org_openpsa_relatedto'] as $rel_array) {
85
            $rel = new org_openpsa_relatedto_dba();
86
            foreach ($rel_array as $k => $v) {
87
                $rel->$k = $v;
88
            }
89
            if ($guid = $rel->check_db()) {
90
                $rel = new org_openpsa_relatedto_dba($guid);
91
            }
92
            $ret[] = $rel;
93
        }
94
        return $ret;
95
    }
96
97
    /**
98
     * Serializes an array or relatedto objects into GET parameters
99
     *
100
     * NOTE: does not prefix the ? for the first parameter in case this needs
101
     * to be used with some other GET parameters.
102
     */
103 4
    public static function relatedto2get(array $array)
104
    {
105 4
        $ret = ['org_openpsa_relatedto' => []];
106 4
        foreach ($array as $rel) {
107 4
            if (!midcom::get()->dbfactory->is_a($rel, org_openpsa_relatedto_dba::class)) { //Matches also 'org_openpsa_relatedto'
108
                //Wrong type of object found in array, cruelly abort the whole procedure
109
                return false;
110
            }
111
            $entry = [
112 4
                'toGuid' => $rel->toGuid,
113 4
                'toComponent' => $rel->toComponent,
114 4
                'toClass' => $rel->toClass
115
            ];
116
117
            //To save GET space we only append these if they have values
118 4
            foreach (['status', 'fromComponent', 'fromClass', 'fromGuid'] as $key) {
119 4
                if ($rel->$key) {
120 4
                    $entry[$key] = $rel->$key;
121
                }
122
            }
123
124 4
            $ret['org_openpsa_relatedto'][] = $entry;
125
        }
126 4
        return http_build_query($ret, '', '&');
127
    }
128
129 4
    public static function add_button(midcom_helper_toolbar $toolbar, $guid)
130
    {
131 4
        $toolbar->add_item([
132 4
            MIDCOM_TOOLBAR_URL => "__mfa/org.openpsa.relatedto/render/{$guid}/both/",
133 4
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view related information', 'org.openpsa.relatedto'),
134 4
            MIDCOM_TOOLBAR_GLYPHICON => 'paperclip',
135
        ]);
136 4
    }
137
138 5
    private static function common_node_toolbar_buttons_sanitycheck(array &$data, $button_component, $bind_object, $calling_component)
139
    {
140 5
        if (!midcom::get()->componentloader->load_graceful($button_component)) {
141
            //For some reason the component is and can not (be) loaded
142
            debug_add("component {$button_component} could not be loaded", MIDCOM_LOG_ERROR);
143
            return false;
144
        }
145 5
        if (empty($data['node'])) {
146 5
            debug_add("data['node'] not given, trying with siteconfig");
147 5
            $siteconfig = org_openpsa_core_siteconfig::get_instance();
148 5
            $node_guid = $siteconfig->get_node_guid($button_component);
149 5
            if (!$node_guid) {
150
                debug_add("data['node'] not given, and {$button_component} could not be found in siteconfig", MIDCOM_LOG_INFO);
151
                return false;
152
            }
153
154 5
            $nap = new midcom_helper_nav();
155 5
            $data['node'] = $nap->resolve_guid($node_guid);
156 5
            if (empty($data['node'])) {
157
                //Invalid node given/found
158 5
                debug_add("data['node'] is invalid", MIDCOM_LOG_ERROR);
159 5
                return false;
160
            }
161
        }
162
163 3
        $related_to = new org_openpsa_relatedto_dba();
164 3
        $related_to->toGuid = $bind_object->guid;
165 3
        $related_to->toClass = get_class($bind_object);
166 3
        $related_to->toComponent = $calling_component;
167 3
        $related_to->fromComponent = $button_component;
168 3
        $related_to->status = org_openpsa_relatedto_dba::CONFIRMED;
169
170 3
        return $related_to;
171
    }
172
173 3
    public static function common_toolbar_buttons_defaults()
174
    {
175
        return [
176
            'event' => [
177
                'node' => false,
178
                'component' => 'org.openpsa.calendar'
179 3
            ],
180
            'task'  => [
181
                'node' => false,
182
                'component' => 'org.openpsa.projects'
183
            ],
184
            'wikinote' => [
185
                'node' => false,
186
                'component' => 'net.nemein.wiki',
187
                'wikiword'  => false, //Calling component MUST define this key to get a wikinote button
188
            ],
189
            'document' => [
190
                'node' => false,
191
                'component' => 'org.openpsa.documents'
192
            ],
193
        ];
194
    }
195
196 5
    public static function common_node_toolbar_buttons(midcom_helper_toolbar $toolbar, $bind_object, $calling_component, array $buttons)
197
    {
198 5
        $workflow = new midcom\workflow\datamanager;
199 5
        $toolbar_buttons = [];
200 5
        foreach ($buttons as $mode => $data) {
201 5
            debug_print_r("processing button '{$mode}' with data:", $data);
202 5
            if ($data === false) {
203
                //In case somebody didn't unset() a button from the defaults, just marked it as false
204
                debug_add('data marked as false, skipping (the correct way is to unset() the key)', MIDCOM_LOG_WARN);
205
                continue;
206
            }
207
208 5
            $related_to = self::common_node_toolbar_buttons_sanitycheck($data, $data['component'], $bind_object, $calling_component);
209 5
            if (!$related_to) {
210 5
                debug_add("sanitycheck returned false, skipping", MIDCOM_LOG_WARN);
211 5
                continue;
212
            }
213
            //Remember that switch is also a for statement in PHPs mind, use "continue 2"
214
            switch ($mode) {
215 3
                case 'event':
216 3
                    $toolbar_buttons[] = org_openpsa_calendar_interface::get_create_button($data['node'], '?' . self::relatedto2get([$related_to]));
0 ignored issues
show
Bug introduced by
Are you sure self::relatedto2get(array($related_to)) of type false|string can be used in concatenation? ( Ignorable by Annotation )

If this is a false-positive, you can also ignore this issue in your code via the ignore-type  annotation

216
                    $toolbar_buttons[] = org_openpsa_calendar_interface::get_create_button($data['node'], '?' . /** @scrutinizer ignore-type */ self::relatedto2get([$related_to]));
Loading history...
217 3
                    break;
218 2
                case 'task':
219 2
                    if (midcom::get()->auth->can_user_do('midgard:create', null, org_openpsa_projects_task_dba::class)) {
220 2
                        $toolbar_buttons[] = $workflow->get_button("{$data['node'][MIDCOM_NAV_ABSOLUTEURL]}task/new/?" . self::relatedto2get([$related_to]), [
221 2
                            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('create task', $data['component']),
222 2
                            MIDCOM_TOOLBAR_GLYPHICON => 'calendar-check-o',
223
                            MIDCOM_TOOLBAR_OPTIONS => ['data-refresh-opener' => 'true'],
224
                        ]);
225
                    }
226 2
                    break;
227
                case 'wikinote':
228
                    if (empty($data['wikiword'])) {
229
                        //Wikiword to use not given
230
                        debug_add("data['wikiword'] not given, skipping", MIDCOM_LOG_WARN);
231
                        continue 2;
232
                    }
233
234
                    if (!net_nemein_wiki_interface::node_wikiword_is_free($data['node'], $data['wikiword'])) {
235
                        //Wikiword is already reserved
236
                        //PONDER: append number or something and check again ??
237
                        debug_add("node_wikiword_is_free returned false for '{$data['wikiword']}'", MIDCOM_LOG_WARN);
238
                        continue 2;
239
                    }
240
241
                    $data['wikiword_encoded'] = rawurlencode($data['wikiword']);
242
                    $toolbar_buttons[] = $workflow->get_button("{$data['node'][MIDCOM_NAV_ABSOLUTEURL]}create/?wikiword={$data['wikiword_encoded']}&" . self::relatedto2get([$related_to]), [
243
                        MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('create note', $data['component']),
244
                        MIDCOM_TOOLBAR_GLYPHICON => 'sticky-note-o',
245
                        MIDCOM_TOOLBAR_ENABLED => $data['node'][MIDCOM_NAV_OBJECT]->can_do('midgard:create'),
246
                        MIDCOM_TOOLBAR_OPTIONS => ['data-refresh-opener' => 'true'],
247
                    ]);
248
                    break;
249
                case 'document':
250
                    if ($data['node'][MIDCOM_NAV_OBJECT]->can_do('midgard:create')) {
251
                        $toolbar_buttons[] = $workflow->get_button("{$data['node'][MIDCOM_NAV_ABSOLUTEURL]}document/create/?" . self::relatedto2get([$related_to]), [
252
                            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('create document', $data['component']),
253
                            MIDCOM_TOOLBAR_GLYPHICON => 'file-o',
254
                            MIDCOM_TOOLBAR_OPTIONS => ['data-refresh-opener' => 'true'],
255
                        ]);
256
                    }
257
                    break;
258
                default:
259
                    debug_add("given button '{$mode}' not recognized", MIDCOM_LOG_ERROR);
260 3
                    break;
261
            }
262
        }
263 5
        $toolbar->add_items($toolbar_buttons);
264 5
    }
265
266
    /**
267
     * function to add the button for journal_entry to the toolbar
268
     */
269 2
    public static function add_journal_entry_button(midcom_helper_toolbar $toolbar, $guid)
270
    {
271 2
        $toolbar->add_item([
272 2
            MIDCOM_TOOLBAR_URL => "__mfa/org.openpsa.relatedto/journalentry/{$guid}/",
273 2
            MIDCOM_TOOLBAR_LABEL => midcom::get()->i18n->get_string('view journal entries', 'org.openpsa.relatedto'),
274 2
            MIDCOM_TOOLBAR_GLYPHICON => 'list',
275
        ]);
276 2
    }
277
}
278