View source
<?php
include_once 'farm_sensor.features.inc';
function farm_sensor_farm_ui_entities() {
return array(
'farm_asset' => array(
'sensor' => array(
'label' => t('Sensor'),
'label_plural' => t('Sensors'),
'view' => 'farm_sensors',
),
),
);
}
function farm_sensor_farm_ui_entity_view_groups() {
$groups = array(
'sensor_data' => array(
'weight' => 80,
),
);
return $groups;
}
function farm_sensor_entity_property_info_alter(&$info) {
$info['farm_asset']['bundles']['sensor']['properties']['sensor_type'] = array(
'type' => 'text',
'label' => t('Sensor Type'),
'description' => t('The type of sensor.'),
'setter callback' => 'entity_property_verbatim_set',
);
$info['farm_asset']['bundles']['sensor']['properties']['sensor_settings'] = array(
'type' => 'unknown',
'label' => t('Sensor Settings'),
'description' => t('Settings for this sensor.'),
'setter callback' => 'entity_property_verbatim_set',
);
}
function farm_sensor_entity_load($entities, $type) {
if ($type != 'farm_asset') {
return;
}
foreach ($entities as $entity) {
if ($entity->type != 'sensor') {
continue;
}
$query = db_select('farm_sensor', 's');
$query
->fields('s');
$query
->condition('id', $entity->id);
$result = $query
->execute();
$sensor_info = $result
->fetchAssoc();
if (empty($sensor_info)) {
continue;
}
$entity->sensor_type = $sensor_info['type'];
$entity->sensor_settings = unserialize($sensor_info['settings']);
}
}
function farm_sensor_entity_insert($entity, $type) {
_farm_sensor_entity_save($entity, $type);
}
function farm_sensor_entity_update($entity, $type) {
_farm_sensor_entity_save($entity, $type);
}
function _farm_sensor_entity_save($entity, $type) {
if ($type != 'farm_asset') {
return;
}
if ($entity->type != 'sensor') {
return;
}
if (empty($entity->sensor_type)) {
return;
}
if (!isset($entity->sensor_settings)) {
$entity->sensor_settings = array();
}
farm_sensor_save($entity->id, $entity->sensor_type, $entity->sensor_settings);
}
function farm_sensor_entity_delete($entity, $type) {
if ($type != 'farm_asset') {
return;
}
if ($entity->type != 'sensor') {
return;
}
farm_sensor_delete($entity->id);
}
function farm_sensor_entity_view_alter(&$build, $type) {
if ($type != 'farm_asset') {
return;
}
if (empty($build['#entity'])) {
return;
}
$asset = $build['#entity'];
if (empty($asset->sensor_type)) {
return;
}
$sensor_types = farm_sensor_types();
if (!empty($sensor_types[$asset->sensor_type])) {
$sensor_type_label = $sensor_types[$asset->sensor_type]['label'];
}
else {
$sensor_type_label = $asset->sensor_type . ' (undefined)';
}
$build['sensor_type'] = array(
'#markup' => '<p><strong>Sensor type:</strong> ' . $sensor_type_label . '</p>',
);
$module_content = module_invoke_all('farm_sensor_view', $asset);
if (!empty($module_content)) {
$build = array_merge_recursive($build, $module_content);
}
}
function farm_sensor_form_alter(&$form, &$form_state, $form_id) {
if ($form_id != 'farm_asset_form') {
return;
}
if ($form['#entity_type'] != 'farm_asset' || $form['#bundle'] != 'sensor') {
return;
}
$farm_asset = $form['farm_asset']['#value'];
$sensor_types = farm_sensor_types();
$options = array();
foreach ($sensor_types as $name => $type) {
$options[$name] = $type['label'];
}
$sensor_type = !empty($farm_asset->sensor_type) ? $farm_asset->sensor_type : NULL;
if (!empty($form_state['values']['sensor_type'])) {
$sensor_type = $form_state['values']['sensor_type'];
}
$form['sensor'] = array(
'#type' => 'fieldset',
'#title' => t('Sensor configuration'),
'#collapsible' => TRUE,
'#collapsed' => TRUE,
'#weight' => 99,
);
$form['sensor']['sensor_type'] = array(
'#type' => 'select',
'#title' => t('Sensor type'),
'#description' => t('What type of sensor is this?'),
'#options' => $options,
'#default_value' => $sensor_type,
'#required' => TRUE,
'#ajax' => array(
'callback' => 'farm_sensor_settings_form_ajax',
'wrapper' => 'farm_sensor_settings',
),
);
$sensor_settings = !empty($farm_asset->sensor_settings) ? $farm_asset->sensor_settings : array();
$form['sensor']['sensor_settings'] = array(
'#type' => 'fieldset',
'#title' => t('Sensor settings'),
'#description' => t('Configure settings for the sensor type selected above.'),
'#prefix' => '<div id="farm_sensor_settings">',
'#suffix' => '</div>',
'#tree' => TRUE,
);
if (!empty($sensor_type) && !empty($sensor_types[$sensor_type]['form']) && function_exists($sensor_types[$sensor_type]['form'])) {
$settings_form = call_user_func_array($sensor_types[$sensor_type]['form'], array(
$farm_asset,
$sensor_settings,
));
$form['sensor']['sensor_settings'] = array_merge($form['sensor']['sensor_settings'], $settings_form);
}
$form['#group_children']['sensor'] = 'group_farm_general';
}
function farm_sensor_settings_form_ajax($form, $form_state) {
return $form['sensor']['sensor_settings'];
}
function farm_sensor_types() {
$sensor_types = module_invoke_all('farm_sensor_type_info');
return $sensor_types;
}
function farm_sensor_save($id, $type, $settings = array()) {
farm_sensor_delete($id);
$record = array(
'id' => $id,
'type' => $type,
'settings' => $settings,
);
drupal_write_record('farm_sensor', $record);
}
function farm_sensor_delete($id) {
db_delete('farm_sensor')
->condition('id', $id)
->execute();
}
function farm_sensor_area_sensors($area_id, $type = '') {
$sensors = array();
$query = db_select('farm_asset', 'fa');
$query
->addField('fa', 'id');
$query
->condition('fa.type', 'sensor');
$subquery = farm_movement_asset_movement_query('fa.id');
$query
->join('log', 'l', 'l.id = (' . $subquery . ')');
$query
->join('field_data_field_farm_move_to', 'fdffmt', 'l.id = fdffmt.entity_id');
$query
->condition('fdffmt.field_farm_move_to_tid', $area_id);
if (!empty($type)) {
$query
->join('farm_sensor', 'fs', 'fa.id = fs.id');
$query
->condition('fs.type', $type);
}
$result = $query
->execute();
while ($asset_id = $result
->fetchField()) {
$sensors[] = farm_asset_load($asset_id);
}
return $sensors;
}
function farm_sensor_farm_sensor_type_info() {
return array(
'none' => array(
'label' => t('None'),
'description' => t('No sensor type.'),
),
);
}
function farm_sensor_farm_log_categories() {
return array(
'Sensors',
);
}