I use this function in my wordpress site to add genres to an album: What is it that makes this code so bulky? Is it have for the server or it just looks like?
/**
* Creates a genere. If one already exists it will return an error
* @author Junaid Qadir Baloch <[email protected]>
* @global type $wpdb
* @param type $genreName
* @param bool $returnFalse Return False if genre already exists insted of
* returning setatus and message. Defaults to FALSE
* @return type
*/
function createGenre($genreName, $returnFalse = FALSE) {
$funtionName = __FUNCTION__;
global $wpdb;
$genreName = trim($genreName);
if (empty($genreName)) {
$status = ReturnValue::STATUS_ERROR;
$value = "";
$message = 'Genere name cannot be empty';
return ReturnValue::doReturn($funtionName, $status, $value, $message);
} elseif (!is_string($genreName)) {
$status = ReturnValue::STATUS_ERROR;
$value = "";
$message = 'Genere name must be a text string';
return ReturnValue::doReturn($funtionName, $status, $value, $message);
}
$genreName = ucwords($genreName);
$genreSlug = text2Slug($genreName);
$query = "SELECT COUNT (*) as count FROM `genres` WHERE `name`='" . $genreName . "' AND `slug`='" . $genreSlug . "' ";
$result = $wpdb->get_results($query);
if ($result[0]->count <= 0) {
$formData = array(
'name' => $genreName,
'slug' => $genreSlug
);
$result = $wpdb->insert('genres', $formData);
if ($result) {
$status = ReturnValue::STATUS_SUCCESS;
$value = "";
$message = 'Genere ' . $genreName . ' has been created.';
return ReturnValue::doReturn($funtionName, $status, $value, $message);
} else {
$status = ReturnValue::STATUS_ERROR;
$value = "";
$message = 'Failed to create a genere as ' . $genreName . '.';
return ReturnValue::doReturn($funtionName, $status, $value, $message);
}
} else {
if ($returnFalse) {
return FALSE;
}
$status = ReturnValue::STATUS_ERROR;
$value = "";
$message = 'Genere ' . $genreName . ' already exists.';
return ReturnValue::doReturn($funtionName, $status, $value, $message);
}
}
This a static class to handle return values. It provides a unified way for other functions to return values.
/**
* ReturnValue is an helper class to handle
* return values, status codes and messages
* returned from different functions
*
* @author JQ
*/
class ReturnValue {
const STATUS_SUCCESS = "SUCCESS";
const STATUS_FAIL = "FAILED";
const STATUS_OK = "OK";
const STATUS_ERROR = "ERROR";
public static function doReturn($funtionName, $status = NULL, $value = NULL, $message = NULL) {
if ($status == NULL) {
$status = self::STATUS_FAIL;
}
$return = array(
action => $funtionName,
status => $status,
message => $message,
returnValue => $value
);
return (object) $return;
}
}