2018-03-02 18:16:15 +01:00
|
|
|
<?php
|
|
|
|
/**
|
|
|
|
* @file
|
|
|
|
* Code for the Farm Quick module.
|
|
|
|
*/
|
|
|
|
|
2018-03-02 19:16:26 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_hook_info().
|
|
|
|
*/
|
|
|
|
function farm_quick_hook_info() {
|
|
|
|
$hooks['farm_quick_forms'] = array(
|
|
|
|
'group' => 'farm_quick',
|
|
|
|
);
|
|
|
|
return $hooks;
|
|
|
|
}
|
|
|
|
|
2018-03-21 17:10:17 +01:00
|
|
|
/**
|
|
|
|
* Implements hook_permission().
|
|
|
|
*/
|
|
|
|
function farm_quick_permission() {
|
|
|
|
return array(
|
|
|
|
'configure farm quick forms' => array(
|
|
|
|
'title' => t('Configure quick forms'),
|
|
|
|
'description' => t('Allow access to the quick forms configuration.'),
|
|
|
|
),
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_farm_access_perms().
|
|
|
|
*/
|
|
|
|
function farm_quick_farm_access_perms($role) {
|
|
|
|
$perms = array();
|
|
|
|
|
2018-12-20 23:28:14 +01:00
|
|
|
// Load the list of farm roles.
|
|
|
|
$roles = farm_access_roles();
|
|
|
|
|
|
|
|
// If this role has 'config' access, allow them to configure quick forms.
|
|
|
|
if (!empty($roles[$role]['access']['config'])) {
|
2018-03-21 17:10:17 +01:00
|
|
|
$perms[] = 'configure farm quick forms';
|
|
|
|
}
|
|
|
|
|
|
|
|
return $perms;
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:16:15 +01:00
|
|
|
/**
|
|
|
|
* Load information about all quick forms provided by other modules.
|
|
|
|
*/
|
|
|
|
function farm_quick_forms() {
|
|
|
|
|
|
|
|
// Ask modules for quick forms.
|
2018-03-02 19:16:26 +01:00
|
|
|
$forms = array();
|
|
|
|
$modules = module_implements('farm_quick_forms');
|
|
|
|
foreach ($modules as $module) {
|
|
|
|
$module_forms = module_invoke($module, 'farm_quick_forms');
|
|
|
|
foreach ($module_forms as &$form) {
|
|
|
|
$form['module'] = $module;
|
|
|
|
}
|
|
|
|
$forms = array_merge($forms, $module_forms);
|
|
|
|
}
|
2018-03-02 18:16:15 +01:00
|
|
|
|
|
|
|
// Sort the quick forms.
|
|
|
|
uasort($forms, 'farm_quick_forms_sort');
|
|
|
|
|
|
|
|
// Return the array of quick forms.
|
|
|
|
return $forms;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Sort function for quick form definitions.
|
|
|
|
*/
|
|
|
|
function farm_quick_forms_sort($a, $b) {
|
|
|
|
|
2018-03-21 14:44:45 +01:00
|
|
|
// Sort alphabetically by the 'label' property.
|
|
|
|
return strcasecmp($a['label'], $b['label']);
|
2018-03-02 18:16:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_menu().
|
|
|
|
*/
|
|
|
|
function farm_quick_menu() {
|
|
|
|
|
|
|
|
// Start with an empty menu items array.
|
|
|
|
$items = array();
|
|
|
|
|
|
|
|
// Ask for quick forms from modules.
|
|
|
|
$forms = farm_quick_forms();
|
|
|
|
|
2018-03-21 14:53:25 +01:00
|
|
|
// Get a list of enabled quick forms from saved variable.
|
2018-03-21 15:36:22 +01:00
|
|
|
$enabled_quick_forms = variable_get('farm_quick_forms_enabled', array());
|
|
|
|
|
|
|
|
// Filter out disabled forms.
|
|
|
|
foreach ($forms as $name => $info) {
|
|
|
|
if (empty($enabled_quick_forms[$name])) {
|
|
|
|
unset($forms[$name]);
|
2018-03-21 14:53:25 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-03-02 18:16:15 +01:00
|
|
|
// Add a menu item for each form.
|
|
|
|
foreach ($forms as $name => $form) {
|
|
|
|
|
2020-06-11 16:48:09 +02:00
|
|
|
// Get access permissions for the quick form.
|
|
|
|
// Convert it to an array if it isn't already.
|
|
|
|
$permissions = array();
|
|
|
|
if (isset($form['permission'])) {
|
|
|
|
if (!is_array($form['permission'])) {
|
|
|
|
$form['permission'] = array($form['permission']);
|
|
|
|
}
|
|
|
|
$permissions = $form['permission'];
|
|
|
|
}
|
|
|
|
|
2018-03-02 19:09:25 +01:00
|
|
|
// Build a menu item definition.
|
|
|
|
$menu_item = array(
|
2018-03-21 14:44:45 +01:00
|
|
|
'title' => $form['label'],
|
2018-10-10 15:57:53 +02:00
|
|
|
'page callback' => 'farm_quick_form_page',
|
|
|
|
'page arguments' => array($form['label'], $form['form']),
|
2020-06-11 16:48:09 +02:00
|
|
|
'access callback' => 'farm_quick_access',
|
2020-07-02 17:32:05 +02:00
|
|
|
'access arguments' => array($permissions),
|
2018-03-02 18:16:15 +01:00
|
|
|
'type' => MENU_LOCAL_TASK,
|
|
|
|
);
|
2018-03-02 19:09:25 +01:00
|
|
|
|
2018-03-02 19:16:26 +01:00
|
|
|
// If the quick form code is in a separate file, add that information
|
|
|
|
// to the menu item so Drupal knows where to look.
|
|
|
|
if (!empty($form['file']) && !empty($form['module'])) {
|
|
|
|
$menu_item['file'] = $form['file'];
|
|
|
|
$menu_item['file path'] = drupal_get_path('module', $form['module']);
|
|
|
|
}
|
|
|
|
|
2018-03-21 15:35:49 +01:00
|
|
|
// Add a menu item.
|
|
|
|
$items['farm/quick/' . $name] = $menu_item;
|
2018-03-02 18:16:15 +01:00
|
|
|
}
|
|
|
|
|
2018-03-21 14:53:25 +01:00
|
|
|
// Add a tab for configuring quick forms.
|
|
|
|
$items['farm/quick/configure'] = array(
|
|
|
|
'title' => t('Configure'),
|
|
|
|
'page callback' => 'drupal_get_form',
|
|
|
|
'page arguments' => array('farm_quick_configure_form'),
|
2018-03-21 17:10:17 +01:00
|
|
|
'access arguments' => array('configure farm quick forms'),
|
2018-03-21 14:53:25 +01:00
|
|
|
'type' => MENU_LOCAL_TASK,
|
|
|
|
'weight' => 100,
|
|
|
|
);
|
|
|
|
|
2018-03-21 15:35:49 +01:00
|
|
|
// Make the first item into the default tab.
|
|
|
|
reset($items);
|
|
|
|
$first = key($items);
|
|
|
|
$items['farm/quick'] = $items[$first];
|
|
|
|
$items['farm/quick']['title'] = 'Quick forms';
|
|
|
|
$items['farm/quick']['type'] = MENU_LOCAL_TASK;
|
|
|
|
$items[$first] = array(
|
|
|
|
'title' => $items[$first]['title'],
|
|
|
|
'type' => MENU_DEFAULT_LOCAL_TASK,
|
|
|
|
);
|
|
|
|
|
2018-03-02 18:16:15 +01:00
|
|
|
// Return menu items.
|
|
|
|
return $items;
|
|
|
|
}
|
2018-03-21 14:53:25 +01:00
|
|
|
|
2020-06-11 16:48:09 +02:00
|
|
|
/**
|
|
|
|
* Quick form access callback.
|
|
|
|
*
|
|
|
|
* @param $permissions
|
|
|
|
* An array of permissions, such as "administer nodes", being checked for.
|
|
|
|
* @param $account
|
|
|
|
* (optional) The account to check, if not given use currently logged in user.
|
|
|
|
*
|
|
|
|
* @return bool
|
|
|
|
* Boolean TRUE if the user has ALL the requested permissions.
|
|
|
|
*/
|
|
|
|
function farm_quick_access($permissions, $account = NULL) {
|
|
|
|
$grant = TRUE;
|
|
|
|
foreach ($permissions as $permission) {
|
|
|
|
if (!user_access($permission, $account)) {
|
|
|
|
$grant = FALSE;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $grant;
|
|
|
|
}
|
|
|
|
|
2018-10-10 15:57:53 +02:00
|
|
|
/**
|
|
|
|
* Quick form page callback.
|
|
|
|
*/
|
|
|
|
function farm_quick_form_page($title, $form_id) {
|
|
|
|
drupal_set_title(t('Quick form') . ': ' . $title);
|
|
|
|
return drupal_get_form($form_id);
|
|
|
|
}
|
|
|
|
|
2018-03-21 14:53:25 +01:00
|
|
|
/**
|
|
|
|
* Form for configuring quick forms.
|
|
|
|
*/
|
|
|
|
function farm_quick_configure_form($form, &$form_state) {
|
|
|
|
|
|
|
|
// Load the list of quick forms.
|
|
|
|
$quick_forms = farm_quick_forms();
|
|
|
|
|
|
|
|
// If there are no forms, bail.
|
|
|
|
if (empty($quick_forms)) {
|
|
|
|
$form['empty'] = array(
|
|
|
|
'#type' => 'markup',
|
|
|
|
'#markup' => 'There are no quick forms available.',
|
|
|
|
);
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Create a set of checkbox options for the forms.
|
|
|
|
$options = array();
|
|
|
|
foreach ($quick_forms as $name => $info) {
|
|
|
|
if (!empty($info['label'])) {
|
|
|
|
$options[$name] = $info['label'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load the list of enabled quick forms from a variable.
|
2018-03-21 15:36:22 +01:00
|
|
|
$enabled_quick_forms = variable_get('farm_quick_forms_enabled', array());
|
2018-03-21 14:53:25 +01:00
|
|
|
|
|
|
|
// Display as a list of checkboxes.
|
|
|
|
$form['farm_quick_forms_enabled'] = array(
|
|
|
|
'#type' => 'checkboxes',
|
|
|
|
'#title' => t('Enable or disable quick forms'),
|
|
|
|
'#options' => $options,
|
|
|
|
'#default_value' => $enabled_quick_forms,
|
|
|
|
);
|
|
|
|
|
|
|
|
// Wrap it in a system settings form.
|
|
|
|
$form = system_settings_form($form);
|
|
|
|
|
|
|
|
// Add a submit function that will rebuild the menu tabs (to run after the
|
|
|
|
// system_settings_form() submit function.
|
|
|
|
$form['#submit'][] = 'farm_quick_configure_form_submit';
|
|
|
|
|
|
|
|
// Return the form.
|
|
|
|
return $form;
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Submit function for rebuilding the menu tabs after configuring quick forms.
|
|
|
|
*/
|
|
|
|
function farm_quick_configure_form_submit(&$form, &$form_state) {
|
|
|
|
menu_rebuild();
|
|
|
|
}
|
2018-09-05 18:58:49 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Link an entity to a quick form.
|
|
|
|
*
|
|
|
|
* @param string $quick_form_id
|
|
|
|
* The quick form ID.
|
|
|
|
* @param string $entity_type
|
|
|
|
* The entity type.
|
|
|
|
* @param $entity
|
|
|
|
* The entity.
|
|
|
|
*/
|
|
|
|
function farm_quick_entity_link($quick_form_id, $entity_type, $entity) {
|
|
|
|
|
|
|
|
// If no quick form ID is provided, bail.
|
|
|
|
if (empty($quick_form_id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get the entity ID.
|
|
|
|
$id = entity_id($entity_type, $entity);
|
|
|
|
|
|
|
|
// If the ID could not be found, bail.
|
|
|
|
if (empty($id)) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Save it to the {farm_quick_entity} table.
|
|
|
|
$record = array(
|
|
|
|
'entity_type' => $entity_type,
|
|
|
|
'entity_id' => $id,
|
|
|
|
'quick_form_id' => $quick_form_id
|
|
|
|
);
|
|
|
|
drupal_write_record('farm_quick_entity', $record);
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Implements hook_entity_delete().
|
|
|
|
*/
|
|
|
|
function farm_quick_entity_delete($entity, $type) {
|
|
|
|
|
|
|
|
// When an entity is deleted, delete associated records in
|
|
|
|
// {farm_quick_entity}.
|
|
|
|
$id = entity_id($type, $entity);
|
2019-10-15 21:35:31 +02:00
|
|
|
if (!empty($id) && is_numeric($id)) {
|
2018-09-05 18:58:49 +02:00
|
|
|
db_query('DELETE FROM {farm_quick_entity} WHERE entity_type = :entity_type AND entity_id = :entity_id', array(':entity_type' => $type, ':entity_id' => $id));
|
|
|
|
}
|
|
|
|
}
|