Skip to content

Commit

Permalink
Complete renaming of all functions and parameters
Browse files Browse the repository at this point in the history
  • Loading branch information
rolfvandekrol committed Oct 22, 2012
1 parent d4d70e9 commit 6bf073c
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 73 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ relation.
module depends on the [Entity API](http://drupal.org/project/entity) for the
CRUD operations on entities.
* **kw_itemnames_deletion_prevention** (To be added) Implements logic to prevent
deletion of aliased items of supported types. There will at least be support
deletion of named items of supported types. There will at least be support
for _nodes_.

## Usage
Expand Down
70 changes: 35 additions & 35 deletions kw_itemnames.inc
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ function kw_itemnames_item_load($type, $item_id) {
}

/**
* Create an aliasable item.
* Create an nameable item.
* @param $type
* The type of item that should be created.
Expand All @@ -38,7 +38,7 @@ function kw_itemnames_item_create($type, $defaults = array(), $required = array(
}

/**
* Update an aliased item.
* Update an named item.
* @param $type
* The type of item that should be updated.
Expand All @@ -58,7 +58,7 @@ function kw_itemnames_item_update($type, $item, $required = array()) {
}

/**
* Deleted an aliased item.
* Deleted an named item.
*
* @param $type
* The type of item that should be deleted.
Expand All @@ -77,7 +77,7 @@ function kw_itemnames_item_delete($type, $item_id) {
}

/**
* Get the ID of an aliased item.
* Get the ID of an named item.
*
* @param $type
* The type of item of which the ID should be returned.
Expand All @@ -94,63 +94,63 @@ function kw_itemnames_item_get_item_id($type, $item) {
}

/**
* Change an alias to point to an item
* Change an name to point to an item
*
* @param $type
* The type of item to which the alias should point.
* @param $alias
* The alias that should point to the item.
* The type of item to which the name should point.
* @param $name
* The name that should point to the item.
* @param $item
* The item to which the alias should point.
* The item to which the name name point.
*
* @return
* TRUE if successfull. FALSE on error.
*/
function kw_itemnames_alias_update($type, $alias, $item) {
function kw_itemnames_name_update($type, $name, $item) {
if (!($item_id = kw_itemnames_item_get_item_id($type, $item))) {
return FALSE;
}
return kw_itemnames_alias_set_item_id($type, $alias, $item_id);
return kw_itemnames_name_set_item_id($type, $name, $item_id);
}

/**
* Get the id of an aliased item.
* Get the id of an named item.
*
* @param $type
* The type of alias to get.
* @param $alias
* The alias to set
* The type of name to get.
* @param $name
* The name to set
*
* @return
* The id of the item, or if not found FALSE.
*/
function kw_itemnames_alias_get_item_id($type, $alias) {
if (isset($type) && isset($alias)) {
return kw_itemnames_mapping($type, $alias);
function kw_itemnames_name_get_item_id($type, $name) {
if (isset($type) && isset($name)) {
return kw_itemnames_mapping($type, $name);
}
return FALSE;
}

/**
* Set the id of an aliased item.
* Set the id of an named item.
*
* @param $type
* The type of alias to set.
* @param $alias
* The alias to set
* The type of name to set.
* @param $name
* The name to set
* @param $item_id
* The id to set
*/
function kw_itemnames_alias_set_item_id($type, $alias, $item_id = null) {
function kw_itemnames_name_set_item_id($type, $name, $item_id = null) {
db_merge('kw_itemnames')
->key(array('type' => $type, 'alias' => $alias))
->key(array('type' => $type, 'name' => $name))
->fields(array('item_id' => $item_id))
->execute();
kw_itemnames_mapping(null, null, true); // drops and rebuilds the alias cache
kw_itemnames_mapping(null, null, true); // drops and rebuilds the name cache
return TRUE;
}

function kw_itemnames_mapping($type = null, $alias = null, $reset = false) {
function kw_itemnames_mapping($type = null, $name = null, $reset = false) {
$mapping = &drupal_static('kw_itemnames');

if ($reset) {
Expand All @@ -166,41 +166,41 @@ function kw_itemnames_mapping($type = null, $alias = null, $reset = false) {
if (!isset($mapping)) {
$mapping = array();
$result = db_select('kw_itemnames', 'ka')
->fields('ka', array('item_id', 'type', 'alias'))
->fields('ka', array('item_id', 'type', 'name'))
->execute();
foreach ($result as $row) {
if (!array_key_exists($row->type, $mapping)) {
$mapping[$row->type] = array();
}
$mapping[$row->type][$row->alias] = $row->item_id;
$mapping[$row->type][$row->name] = $row->item_id;
}

cache_set('kw_itemnames', $mapping);
}

if (isset($type) && isset($alias)) {
return isset($mapping[$type][$alias]) ? $mapping[$type][$alias] : FALSE;
if (isset($type) && isset($name)) {
return isset($mapping[$type][$name]) ? $mapping[$type][$name] : FALSE;
}
if (isset($type)) {
return isset($mapping[$type]) ? $mapping[$type] : FALSE;
}
return $mapping;
}

function kw_itemnames_alias_delete($type, $alias) {
function kw_itemnames_name_delete($type, $name) {
db_delete('kw_itemnames')
->condition('type', $type)
->condition('alias', $alias)
->condition('name', $name)
->execute();
kw_itemnames_mapping(null, null, true); // drops and rebuilds the alias cache
kw_itemnames_mapping(null, null, true); // drops and rebuilds the name cache
return TRUE;
}

/**
* Call a callback for an alias type.
* Call a callback for an name type.
*
* @param $type
* The alias type.
* The name type.
* @param $callback_type
* The calback that should be executed.
* @param $args
Expand Down
14 changes: 7 additions & 7 deletions kw_itemnames.install
Original file line number Diff line number Diff line change
Expand Up @@ -7,33 +7,33 @@ function kw_itemnames_schema() {
$schema = array();

$schema['kw_itemnames'] = array(
'description' => 'Stores the aliases that are assigned to kw_itemnames managed items',
'description' => 'Stores the names that are assigned to kw_itemnames managed items',
'fields' => array(
'type' => array(
'description' => 'The type of item for which we registered the alias',
'description' => 'The type of item for which we registered the name',
'type' => 'varchar',
'length' => 32,
'default' => '',
'not null' => TRUE,
),
'alias' => array(
'description' => 'The alias that is assigned to the item',
'name' => array(
'description' => 'The name that is assigned to the item',
'type' => 'varchar',
'length' => 64, // 64 ought to be enough for everybody
'default' => '',
'not null' => TRUE
),
'item_id' => array(
'description' => 'The internal ID of the item this alias points to',
'description' => 'The internal ID of the item this name points to',
'type' => 'int', // this is not intended for items that already have a unique string ID
'default' => 0,
'not null' => TRUE,
),
),
'unique keys' => array(
'item' => array('type', 'item_id'), // you can't assign two aliases to one item
'item' => array('type', 'item_id'), // you can't assign two names to one item
),
'primary key' => array('type', 'alias'), // you can't assign an alias to two items of the same type
'primary key' => array('type', 'name'), // you can't assign a name to two items of the same type
);

return $schema;
Expand Down
58 changes: 29 additions & 29 deletions kw_itemnames.module
Original file line number Diff line number Diff line change
Expand Up @@ -10,13 +10,13 @@ function kw_itemnames_hook_info() {
}

/**
* Load the information about how to handle aliasable types.
* 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 aliasable types. If $type is not given, this
* 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.
Expand Down Expand Up @@ -58,15 +58,15 @@ function kw_itemnames_type_info($type = NULL) {
}

/**
* Ensure that an item exists with a certain alias.
* Ensure that an item exists with a certain name.
*
* @param $type
* The type of alias that should be ensured.
* @param $alias
* The alias that the item should have. This alias has no other use than
* 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
* alias is used only once per type in your project. A good strategy is to
* prefix all the aliases you use with your module name.
* 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.
Expand All @@ -78,49 +78,49 @@ function kw_itemnames_type_info($type = NULL) {
* The item, after optionally updating it. This function can return FALSE, if
* deletion prevention is switched off!
*/
function kw_itemnames_ensure($type, $alias, $required = NULL, $defaults = NULL) {
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_alias_get_item_id($type, $alias)) {
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 alias points to the new item
kw_itemnames_alias_update($type, $alias, $item);
// 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 alias points to the new item
kw_itemnames_alias_update($type, $alias, $item);
// 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 alias.
* Ensure that no item exists with a certain name.
*
* @param $type
* The type of alias that should be removed.
* @param $alias
* The alias that the item has/had.
* 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, $alias) {
function kw_itemnames_remove($type, $name) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');

if ($item_id = kw_itemnames_alias_get_item_id($type, $alias)) {
kw_itemnames_alias_delete($type, $alias);
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) {
Expand All @@ -132,35 +132,35 @@ function kw_itemnames_remove($type, $alias) {
}

/**
* Load an aliased item.
* 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 alias to load
* @param $alias
* The alias to load
* 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, $alias) {
function kw_itemnames_load($type, $name) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');

if (!($item_id = kw_itemnames_alias_get_item_id($type, $alias))) {
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, $alias) {
function kw_itemnames_item_id($type, $name) {
// make sure all our helper functions exist
module_load_include('inc', 'kw_itemnames');

return kw_itemnames_alias_get_item_id($type, $alias);
return kw_itemnames_name_get_item_id($type, $name);
}
2 changes: 1 addition & 1 deletion kw_itemnames_entity.info
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
name = Kraftwagen Itemnames Entity
description = Provides Kraftwagen Aliases item types for entities
description = Provides Kraftwagen Itemnames item types for entities
package = Kraftwagen

core = 7.x
Expand Down

0 comments on commit 6bf073c

Please sign in to comment.