View source
<?php
module_load_include('inc', 'botcha', 'model/botcha_form.model');
module_load_include('inc', 'botcha', 'model/botcha_recipe.model');
module_load_include('inc', 'botcha', 'model/botcha_recipebook.model');
define('BOTCHA_MODEL_TYPE_SELECT', 'SELECT');
define('BOTCHA_MODEL_OP_IN', 'IN');
class BotchaModel {
public static function getFormsRecipebooks($parameters = array()) {
$parameters['mode'] = !empty($parameters['mode']) ? $parameters['mode'] : 'recipebook';
return self::getRecipebooksForms($parameters);
}
public static function getRecipebooksForms($parameters = array()) {
$key = '';
switch ($parameters['mode']) {
case 'form':
$key = 'form_id';
break;
case 'recipebook':
default:
$key = 'rbid';
break;
}
$pars = array();
$pars['fields'] = array();
if (!empty($parameters['recipebooks'])) {
$pars['conditions'][BOTCHA_MODEL_OP_IN]['rbid'] = (array) $parameters['recipebooks'];
}
if (!empty($parameters['forms'])) {
$pars['conditions'][BOTCHA_MODEL_OP_IN]['form_id'] = (array) $parameters['forms'];
}
$query = BotchaModel::buildQuery(BOTCHA_MODEL_TYPE_SELECT, 'botcha_recipebook_form', $pars);
$result_query = BotchaModel::getQueryResult($query['query'], $query['query_subs']);
$result = BotchaModel::fetchQueryResult($result_query, $key);
return $result;
}
public static function getRecipesRecipebooks($parameters = array()) {
$parameters['mode'] = !empty($parameters['mode']) ? $parameters['mode'] : 'recipebook';
return self::getRecipebooksRecipes($parameters);
}
public static function getRecipebooksRecipes($parameters = array()) {
$key = '';
switch ($parameters['mode']) {
case 'recipe':
$key = 'recipe_id';
break;
case 'recipebook':
default:
$key = 'rbid';
break;
}
$pars = array();
$pars['fields'] = array();
if (!empty($parameters['recipebooks'])) {
$pars['conditions'][BOTCHA_MODEL_OP_IN]['rbid'] = (array) $parameters['recipebooks'];
}
if (!empty($parameters['recipes'])) {
$pars['conditions'][BOTCHA_MODEL_OP_IN]['recipe_id'] = (array) $parameters['recipes'];
}
$query = BotchaModel::buildQuery(BOTCHA_MODEL_TYPE_SELECT, 'botcha_recipebook_recipe', $pars);
$result_query = BotchaModel::getQueryResult($query['query'], $query['query_subs']);
$result = BotchaModel::fetchQueryResult($result_query, $key);
return $result;
}
public static function buildQuery($type, $table, $parameters) {
$query = '';
$query_subs = array();
switch ($type) {
case BOTCHA_MODEL_TYPE_SELECT:
default:
$query = 'SELECT ';
$query .= !empty($parameters['fields']) ? implode(', ', (array) $parameters['fields']) . ' ' : '* ';
$query .= "FROM {{$table}}";
if (!empty($parameters['conditions'])) {
foreach ((array) $parameters['conditions'] as $op => $conditions) {
switch ($op) {
case BOTCHA_MODEL_OP_IN:
default:
$where = '';
foreach ($conditions as $field => $value) {
$query .= empty($where) ? $where = " WHERE " : " AND ";
$query .= "{$field} {$op} ('" . implode("', '", array_fill(0, count((array) $value), '%s')) . "')";
$query_subs = array_merge($query_subs, (array) $value);
}
break;
}
}
}
break;
}
return array(
'query' => $query,
'query_subs' => $query_subs,
);
}
public static function getQueryResult($query, $query_subs = array(), $error_message = 'Please perform an update via update.php or reinstall the BOTCHA module', $error_message_subs = array()) {
$result_query = db_query($query, $query_subs);
if (!$result_query) {
drupal_set_message($error_message, 'warning');
watchdog('BOTCHA', $error_message, $error_message_subs, WATCHDOG_WARNING);
}
return $result_query;
}
public static function fetchQueryResult($result_query, $key = 'id') {
$result = array();
while ($result_row = db_fetch_object($result_query)) {
$result[$result_row->{$key}] = $result_row;
}
return $result;
}
}