forked from kraftwagen/kw-itemnames
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkw_itemnames.module
172 lines (149 loc) · 5.11 KB
/
kw_itemnames.module
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
<?php
/**
* Implements hook_hook_info().
*/
function kw_itemnames_hook_info() {
return array(
'kw_itemnames_type_info' => array('group' => 'kw_itemnames'),
);
}
/**
* Load the information about how to handle nameable types.
*
* @param $type
* Optional type for which the information should be loaded.
*
* @return
* The information about handling nameable types. If $type is not given, this
* will be an associative array, with types as keys and the information per
* type as values. If $type is given, the value for that type is returned,
* which is also an associative array.
*/
function kw_itemnames_type_info($type = NULL) {
$type_info = &drupal_static(__FUNCTION__);
// if not statically cached yet
if (!isset($type_info)) {
// first try to get from the cache
if ($cache = cache_get(__FUNCTION__)) {
$type_info = $cache->data;
}
}
if (!isset($type_info)) {
// construct the list of types from the hook implementations
$type_info = array();
$modules = module_implements(__FUNCTION__);
foreach ($modules as $module) {
if ($result = module_invoke($module, __FUNCTION__)) {
foreach ($result as $rtype => $rinfo) {
$type_info[$rtype] = $rinfo + array('type' => $rtype, 'module' => $module);
}
}
}
drupal_alter(__FUNCTION__, $type_info);
// save the list to the cache
cache_set(__FUNCTION__, $type_info);
}
// if type given, return only type specific information
if ($type) {
return !empty($type_info[$type]) ? $type_info[$type] : FALSE;
}
return $type_info;
}
/**
* Ensure that an item exists with a certain name.
*
* @param $type
* The type of name that should be ensured.
* @param $name
* The name that the item should have. This name has no other use than
* uniquely identifying the item. Therefore you should make sure that the
* name is used only once per type in your project. A good strategy is to
* prefix all the names you use with your module name.
* @param $required
* The properties of the item that are required. If the item already
* exists, it will be updated to have these properties set correctly.
* @param $defaults
* The properties of the item that should be set on initial creation, but
* should not be enforced.
*
* @return
* The item, after optionally updating it. This function can return FALSE, if
* deletion prevention is switched off!
*/
function kw_itemnames_ensure($type, $name, $required = NULL, $defaults = NULL) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');
// check if we have an item id registered
if ($item_id = kw_itemnames_name_get_item_id($type, $name)) {
// try to load the item
$item = kw_itemnames_item_load($type, $item_id);
if ($item) {
// update the item we loaded, using the required properties
$item = kw_itemnames_item_update($type, $item, $required);
// make sure the name points to the new item
kw_itemnames_name_update($type, $name, $item);
return $item;
}
}
// create the item, using the required and default properties
$item = kw_itemnames_item_create($type, $required, $defaults);
// make sure the name points to the new item
kw_itemnames_name_update($type, $name, $item);
return $item;
}
/**
* Ensure that no item exists with a certain name.
*
* @param $type
* The type of name that should be removed.
* @param $name
* The name that the item has/had.
*
* @return
* TRUE on success, FALSE on failure. Also TRUE if the item is already gone.
*/
function kw_itemnames_remove($type, $name) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');
if ($item_id = kw_itemnames_name_get_item_id($type, $name)) {
kw_itemnames_name_delete($type, $name);
$item = kw_itemnames_item_load($type, $item_id);
if ($item) {
return kw_itemnames_item_delete($type, $item_id);
}
}
return TRUE;
}
/**
* Load an named item.
*
* Never assume in the code that uses this function that a valid item will be
* returned. Not even the item was ensured (using kw_itemnames_ensure) with
* deletion prevention. We can't prevent deletion completely, so your code
* should not break miserably when the item can't be found.
*
* @param $type
* The type of name to load
* @param $name
* The name to load
* @return
* The loaded item, or if not found FALSE.
*/
function kw_itemnames_load($type, $name) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');
if (!($item_id = kw_itemnames_name_get_item_id($type, $name))) {
return FALSE;
}
return kw_itemnames_item_load($type, $item_id);
}
function kw_itemnames_item_id($type, $name) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');
return kw_itemnames_name_get_item_id($type, $name);
}
function kw_itemnames_name($type, $item) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');
return is_numeric($item) ? kw_itemnames_item_id_get_name($type, $item) : kw_itemnames_item_get_name($type, $item);
}