Passed
Push — main ( 46cb87...b800f4 )
by Marc
18:17 queued 13:49
created

get_category_delete_object()   A

Complexity

Conditions 1
Paths 1

Size

Total Lines 2
Code Lines 1

Duplication

Lines 0
Ratio 0 %

Importance

Changes 1
Bugs 0 Features 0
Metric Value
cc 1
eloc 1
c 1
b 0
f 0
nc 1
nop 1
dl 0
loc 2
rs 10
1
<?php declare(strict_types=1);
2
3
use html_go\exceptions\InternalException;
4
use html_go\model\Config;
5
6
/**
7
 * Returns an admin content <b>view</b> object for <i>categories</i>.
8
 * @param array<mixed> $args
9
 * @return \stdClass
10
 */
11
function get_category_listview_content_object(array $args): \stdClass {
12
    return get_admin_content_object(
13
        'admin.dashboard.title',
14
        'admin-list.html',
15
        CATEGORY_SECTION,
16
        ADMIN_ACTION_VIEW,
17
        $args,
18
        get_categories(get_pagination_pagenumber(), get_config()->getInt(Config::KEY_ADMIN_ITEMS_PER_PAGE)));
19
}
20
21
/**
22
 * Returns an admin content <b>add</b> object for <i>categories</i>.
23
 * @param array<mixed> $args
24
 * @return \stdClass
25
 */
26
function get_category_add_content_object(array $args = []): \stdClass {
27
    return get_admin_content_object(
28
        'admin.dashboard.title',
29
        'admin-action.html',
30
        CATEGORY_SECTION,
31
        ADMIN_ACTION_ADD,
32
        $args);
33
}
34
35
/**
36
 * Edit wrapper for <code>get_category_editdelete_object</code> function.
37
 * @param array<mixed> $args
38
 * @return \stdClass
39
 */
40
function get_category_edit_object(array $args): \stdClass {
41
    return get_category_editdelete_object(ADMIN_ACTION_EDIT, $args);
42
}
43
44
/**
45
 * Delete wrapper for <code>get_category_editdelete_object</code> function.
46
 * @param array<mixed> $args
47
 * @return \stdClass
48
 */
49
function get_category_delete_object(array $args): \stdClass {
50
    return get_category_editdelete_object(ADMIN_ACTION_DELETE, $args);
51
}
52
53
/**
54
 * Return an admin content <b>edit/delete</b> object for <i>categories</i>.
55
 * @param string $action Must be either <code>edit</code> or <code>delete</code>
56
 * @param array<mixed> $args
57
 * @throws InvalidArgumentException
58
 * @throws InternalException
59
 * @return \stdClass
60
 */
61
function get_category_editdelete_object(string $action, array $args): \stdClass {
62
    if (\strpos($action, ADMIN_ACTION_EDIT) === false && \strpos($action, ADMIN_ACTION_DELETE) === false) {
63
        throw new InvalidArgumentException("Unknown action value [$action]");
64
    }
65
    if (empty($args[ID_STR])) {
66
        throw new InvalidArgumentException("The args array must contain an 'id' key.");
67
    }
68
    $manager = get_index_manager();
69
    if ($manager->elementExists($args[ID_STR]) === false) {
70
        $id = $args[ID_STR];
71
        throw new InternalException("Element does not exist [$id]");
72
    }
73
    $element = get_model_factory()->createContentObject($manager->getElementFromSlugIndex($args[ID_STR]));
74
    $params = [
75
        'title' => get_i18n()->getText('admin.dashboard.title'),
76
        'template' => 'admin-action.html',
77
        'context' => get_config()->getString(Config::KEY_ADMIN_CONTEXT),
78
        'section' => CATEGORY_SECTION,
79
        'list' => [$element],
80
        'action' => $action,
81
    ];
82
    return get_model_factory()->createAdminContentObject(\array_merge($args, $params));
83
}
84
85
/**
86
 * Persist a new category.
87
 * @param array<mixed> $formData Passed by reference, as the form data will be modified
88
 * and should maintain the modifications in case of validation failure.
89
 * @return bool <code>true</code> if the successful, otherwise <code>false</code>.
90
 */
91
function save_category(array &$formData): bool {
92
    $formData['errorKey'] = '';
93
    if (empty($formData['title'])) {
94
        $formData['errorfield'] = 'title';
95
        return false;
96
    }
97
    if (empty($formData['description'])) {
98
        $desc = \substr($formData['body'], 0, get_config()->getInt(Config::KEY_DESCRIPTION_LEN));
99
        $pos = \strpos($desc, '.', -1);
100
        if ($pos === false) {
101
            $pos = get_config()->getInt(Config::KEY_DESCRIPTION_LEN);
102
        }
103
        $formData['description'] = \substr($desc, 0, $pos + 1);
104
    }
105
    if (empty($formData[ADMIN_KEY_STR]) || $formData[ADMIN_KEY_STR] === 'category/') {
106
        $formData[ADMIN_KEY_STR] = 'category/'.URLify::slug($formData['title']);
107
    }
108
    if (get_index_manager()->elementExists( $formData[ADMIN_KEY_STR])) {
109
        $formData['fielderror'] = ADMIN_KEY_STR;
110
        return false;
111
    }
112
113
    save_content(CATEGORY_SECTION, $formData);
114
    return true;
115
}
116
117
function update_category(array &$formData): bool {
0 ignored issues
show
Unused Code introduced by
The parameter $formData is not used and could be removed. ( Ignorable by Annotation )

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

117
function update_category(/** @scrutinizer ignore-unused */ array &$formData): bool {

This check looks for parameters that have been defined for a function or method, but which are not used in the method body.

Loading history...
118
    return true;
119
}
120