View source
<?php
function computing_code($group = 'all', $name_only = FALSE) {
$code = array(
'status' => array(
'OKOK' => array(
'name' => t('Successful'),
'description' => t('Command runs successfully.'),
),
'FAIL' => array(
'name' => t('Failed'),
'description' => t('Command failed.'),
),
'RUNG' => array(
'name' => t('Running'),
'description' => t('Command is running.'),
),
'STOP' => array(
'name' => t('Stopped'),
'description' => t('Command is canceled, or forced to stop.'),
),
'NRCG' => array(
'name' => t('Unrecognizable'),
'description' => t('Command is not recognizable in this application.'),
),
'INTR' => array(
'name' => t('Interrupted'),
'description' => t('Unexpected internal error, or program problem.'),
),
),
'control' => array(
'REDY' => array(
'name' => t('Ready'),
'description' => t('Command is ready to be executed, pulled by non-PHP program.'),
),
'CNCL' => array(
'name' => t('Cancel'),
'description' => t('Command should be canceled.'),
),
'REMT' => array(
'name' => t('Remote'),
'description' => t('Command to be executed by an external remote program.'),
),
'CMLN' => array(
'name' => t('CLI'),
'description' => t('Command created from the command line running mode.'),
),
'CODE' => array(
'name' => t('Code'),
'description' => t('Command created from direct program call.'),
),
),
);
if (!$name_only) {
if ($group == 'all') {
return $code;
}
else {
return $code[$group];
}
}
else {
$name_only = array();
foreach ($code as $group_name => $group_value) {
foreach ($group_value as $code_name => $code_value) {
$name_only[$group_name][$code_name] = $code_value['name'];
}
}
if ($group == 'all') {
return $name_only;
}
else {
return $name_only[$group];
}
}
}
function _computing_query_records($conditions = array(), $limit = 0) {
$conditions = computing_validate_fields($conditions);
$query = db_select('computing_record', 'c');
$query
->fields('c');
foreach ($conditions as $field => $value) {
$query
->condition($field, $value);
}
if ($limit > 0) {
$query
->range(0, $limit);
}
return $query
->execute();
}
function computing_query_active_records($app) {
$result = _computing_query_records(array(
'app' => $app,
'status' => NULL,
));
$records = array();
foreach ($result as $r) {
$records[] = $r;
}
return $records;
}
function computing_get_last_command($app, $command, $success = TRUE, $uid = NULL, $nid = NULL) {
$query = db_select('computing_record', 'c');
$query
->fields('c', array(
'id',
))
->condition('app', $app)
->condition('command', $command)
->orderBy('updated', 'DESC')
->orderBy('id', 'DESC')
->range(0, 1);
if ($success) {
$query
->condition('status', 'OKOK');
}
if ($uid !== NULL) {
$query
->condition('uid', $uid);
}
if ($nid !== NULL) {
$query
->condition('nid', $nid);
}
return $query
->execute()
->fetchField();
}
function computing_create_record($app, $command, $description = NULL, $fields = array()) {
$created = isset($fields['created']) ? $fields['created'] : FALSE;
$fields = array_merge($fields, compact('app', 'command', 'description'));
$fields = array_merge(array(
'control' => 'REDY',
'weight' => 0,
), $fields);
$fields = computing_validate_fields($fields, TRUE);
unset($fields['created']);
$result = _computing_query_records($fields, 1);
if (($dup = $result
->fetchObject()) != FALSE) {
watchdog('computing', "Request to create duplicate command record {$dup->id}");
return $dup->id;
}
$fields['created'] = $created == FALSE ? time() : $created;
$insert = db_insert('computing_record');
$insert
->fields($fields);
$id = $insert
->execute();
return $id;
}
function computing_load_record($id, $fields = NULL) {
if ($fields == NULL) {
$fields = array();
}
else {
if (!is_array($fields)) {
$fields = array(
$fields,
);
}
$schema = drupal_get_schema('computing_record');
$valid_fields = array_keys($schema['fields']);
$fields = array_intersect($fields, $valid_fields);
}
return db_select('computing_record', 'c')
->fields('c', $fields)
->condition('id', $id)
->execute()
->fetchObject();
}
function computing_update_record($id, $fields) {
$fields = computing_validate_fields($fields);
$fields['updated'] = time();
unset($fields['id']);
$updated = FALSE;
if (!empty($fields)) {
$updated = db_update('computing_record')
->fields($fields)
->condition('id', $id)
->execute();
}
return $updated;
}
function computing_update_record_field($id, $field, $value) {
$fields = computing_validate_fields(array(
$field => $value,
));
return computing_update_record($id, $fields);
}
function computing_validate_fields($fields, $insert_default = FALSE) {
$valid_fields = array();
$schema = drupal_get_schema_unprocessed('computing', 'computing_record');
if (is_array($fields)) {
$valid_fields = array_intersect_key($fields, $schema['fields']);
}
if ($insert_default) {
foreach ($schema['fields'] as $field_name => $field_value) {
if ($field_name != 'id' && !array_key_exists($field_name, $valid_fields)) {
$valid_fields[$field_name] = isset($field_value['default']) ? $field_value['default'] : NULL;
}
}
}
return $valid_fields;
}
function computing_save_input_json($id, $input) {
return computing_update_record_field($id, 'inputjson', drupal_json_encode($input));
}
function computing_load_output_json($id) {
$record = computing_load_record($id, 'outputjson');
return drupal_json_decode($record->outputjson);
}
function computing_views_api() {
return array(
'api' => 3,
);
}
function computing_cron() {
variable_set('computing_drupal_version', VERSION);
}
function computing_permission() {
return array(
'administer computing record' => array(
'title' => t('Administer computing records'),
'description' => t('Access and change computing records.'),
),
);
}
function computing_services_resources() {
}