farmOS/modules/farm/farm_quantity/farm_quantity.module

183 lines
4.1 KiB
Plaintext
Raw Normal View History

<?php
/**
* @file
* Farm quantity module.
*/
// Include Features code.
include_once 'farm_quantity.features.inc';
/**
* Implements hook_permission().
*/
function farm_quantity_permission() {
$perms = array(
'administer farm_quantity module' => array(
'title' => t('Administer farm quantity module'),
),
);
return $perms;
}
/**
* Implements hook_farm_access_perms().
*/
function farm_quantity_farm_access_perms($role) {
// Assemble a list of entity types provided by this module.
$types = array(
'taxonomy' => array(
'farm_quantity_units',
),
);
// Grant different CRUD permissions based on the role.
$perms = array();
switch ($role) {
// Farm Manager and Worker
case 'Farm Manager':
case 'Farm Worker':
$perms = farm_access_entity_perms($types);
break;
// Farm Viewer
case 'Farm Viewer':
$perms = farm_access_entity_perms($types, array('view'));
break;
}
// If the role is "Farm Manager", grant access to quantity configuration.
if ($role == 'Farm Manager') {
$perms[] = 'administer farm_quantity module';
}
return $perms;
}
/**
* Implements hook_menu().
*/
function farm_quantity_menu() {
// Quantity configuration form.
$items['admin/config/farm/quantity'] = array(
'title' => 'Quantity',
'description' => 'Quantity configuration settings.',
'page callback' => 'drupal_get_form',
'page arguments' => array('farm_quantity_settings_form'),
'access arguments' => array('administer farm_quantity module'),
);
return $items;
}
/**
* Quantity settings form.
*/
function farm_quantity_settings_form($form, &$form_state) {
// Metric or US/Imperial.
$form['farm_quantity_unit_system'] = array(
'#type' => 'radios',
'#title' => t('System of measurement'),
'#description' => t('Select the system of measurement you would like to use in farmOS.'),
'#options' => array(
'metric' => t('Metric'),
'us' => t('US/Imperial'),
),
'#default_value' => farm_quantity_system_of_measurement(),
);
// Return it as a system settings form.
return system_settings_form($form);
}
/**
* Helper function for loading the default system of measurement.
*
* @return string
* Returns either 'metric' or 'us' (defaults to 'metric').
*/
function farm_quantity_system_of_measurement() {
return variable_get('farm_quantity_unit_system', 'metric');
}
/**
* Define information about available quantity measures.
*
* @return array
* Returns an array of measure information.
*/
function farm_quantity_measures() {
return array(
'count' => array(
'label' => t('Count'),
),
'length' => array(
'label' => t('Length'),
),
'weight' => array(
'label' => t('Weight'),
),
2017-11-07 18:52:28 +01:00
'area' => array(
'label' => t('Area'),
),
'volume' => array(
'label' => t('Volume'),
),
'time' => array(
'label' => t('Time'),
),
'temperature' => array(
'label' => t('Temperature'),
),
'water_content' => array(
'label' => t('Water content'),
),
'value' => array(
'label' => t('Value'),
),
'rating' => array(
'label' => t('Rating'),
),
'ratio' => array(
'label' => t('Ratio'),
),
'probability' => array(
'label' => t('Probability'),
),
);
}
/**
* Define available options for the Measure field.
*/
function farm_quantity_measure_options() {
// Start an empty options array.
$options = array();
// Load information about measures.
$measures = farm_quantity_measures();
// Iterate through the measures and build a list of options.
foreach ($measures as $measure => $data) {
$options[$measure] = $data['label'];
}
// Return the array of options.
return $options;
}
/**
* Implements hook_preprocess_field().
*/
function farm_quantity_preprocess_field(&$vars) {
// Add quantity CSS when a Quantity field is displayed.
if (!empty($vars['element']['#field_name']) && $vars['element']['#field_name'] == 'field_farm_quantity') {
drupal_add_css(drupal_get_path('module', 'farm_quantity') . '/farm_quantity.css');
}
}