Commit ec5f7f83 authored by TravisCarden's avatar TravisCarden

By TravisCarden: Made numerous minor code improvements.

parent f20d785b
(function ($) {
"use strict";
/**
* Provides the summary information for the block settings vertical tabs.
*/
Drupal.behaviors.checklistapiFieldsetSummaries = {
attach: function (context) {
// Vertical tabs summaries.
$('#checklistapi-checklist-form .vertical-tabs-panes > fieldset', context).drupalSetSummary(function (context) {
var total = $(':checkbox.checklistapi-item', context).size(), args = {};
if (total) {
......@@ -14,8 +15,14 @@
return Drupal.t('@complete of @total (@percent%) complete', args);
}
});
}
};
// Compact mode link.
/**
* Adds dynamic item descriptions toggling.
*/
Drupal.behaviors.checklistapiCompactModeLink = {
attach: function (context) {
$('#checklistapi-checklist-form .compact-link a', context).click(function () {
$(this).closest('#checklistapi-checklist-form').toggleClass('compact-mode');
var is_compact_mode = $(this).closest('#checklistapi-checklist-form').hasClass('compact-mode');
......@@ -25,7 +32,6 @@
document.cookie = 'Drupal.visitor.checklistapi_compact_mode=' + ((is_compact_mode) ? 1 : 0);
return false;
});
}
};
......
......@@ -30,7 +30,7 @@ function checklistapi_checklist_access($id, $operation = 'any') {
}
else {
throw new Exception(t('No such operation "@operation"', array(
'@operation' => $operation
'@operation' => $operation,
)));
}
}
......@@ -62,10 +62,13 @@ function checklistapi_checklist_load($id) {
function checklistapi_get_checklist_info($id = NULL) {
$definitions = &drupal_static(__FUNCTION__);
if (!isset($definitions)) {
// Get definitions.
$definitions = module_invoke_all('checklistapi_checklist_info');
$definitions = checklistapi_sort_array($definitions);
// Let other modules alter them.
drupal_alter('checklistapi_checklist_info', $definitions);
$definitions = checklistapi_sort_array($definitions);
// Inject checklist IDs.
foreach ($definitions as $key => $value) {
$definitions[$key] = array('#id' => $key) + $definitions[$key];
}
......@@ -103,6 +106,7 @@ function checklistapi_init() {
*/
function checklistapi_menu() {
$items = array();
// Checklists report.
$items['admin/reports/checklistapi'] = array(
'title' => 'Checklists',
......@@ -111,43 +115,49 @@ function checklistapi_menu() {
'description' => 'Get an overview of your installed checklists with progress details.',
'file' => 'checklistapi.admin.inc',
);
// Individual checklists.
foreach (checklistapi_get_checklist_info() as $id => $definition) {
if (!empty($definition['#path']) && !empty($definition['#title'])) {
// View/edit checklist.
$items[$definition['#path']] = array(
'title' => $definition['#title'],
'description' => (!empty($definition['#description'])) ? $definition['#description'] : '',
'page callback' => 'drupal_get_form',
'page arguments' => array('checklistapi_checklist_form', $id),
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($id),
'file' => 'checklistapi.pages.inc',
);
if (!empty($checklist['#menu_name'])) {
$items[$definition['#path']]['menu_name'] = $definition['#menu_name'];
}
// Clear saved progress.
$items[$definition['#path'] . '/clear'] = array(
'title' => 'Clear',
'page callback' => 'drupal_get_form',
'page arguments' => array('checklistapi_checklist_clear_confirm', $id),
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($id, 'edit'),
'file' => 'checklistapi.pages.inc',
'type' => MENU_CALLBACK,
);
// Toggle compact mode.
$items[$definition['#path'] . '/compact'] = array(
'title' => 'Compact mode',
'page callback' => 'checklistapi_compact_page',
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($id),
'file' => 'checklistapi.pages.inc',
'type' => MENU_CALLBACK,
);
if (empty($definition['#path']) || empty($definition['#title'])) {
continue;
}
// View/edit checklist.
$items[$definition['#path']] = array(
'title' => $definition['#title'],
'description' => (!empty($definition['#description'])) ? $definition['#description'] : '',
'page callback' => 'drupal_get_form',
'page arguments' => array('checklistapi_checklist_form', $id),
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($id),
'file' => 'checklistapi.pages.inc',
);
if (!empty($checklist['#menu_name'])) {
$items[$definition['#path']]['menu_name'] = $definition['#menu_name'];
}
// Clear saved progress.
$items[$definition['#path'] . '/clear'] = array(
'title' => 'Clear',
'page callback' => 'drupal_get_form',
'page arguments' => array('checklistapi_checklist_clear_confirm', $id),
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($id, 'edit'),
'file' => 'checklistapi.pages.inc',
'type' => MENU_CALLBACK,
);
// Toggle compact mode.
$items[$definition['#path'] . '/compact'] = array(
'title' => 'Compact mode',
'page callback' => 'checklistapi_compact_page',
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($id),
'file' => 'checklistapi.pages.inc',
'type' => MENU_CALLBACK,
);
}
return $items;
}
......@@ -156,6 +166,7 @@ function checklistapi_menu() {
*/
function checklistapi_permission() {
$perms = array();
// Universal permissions.
$perms['view checklistapi checklists report'] = array(
'title' => t(
......@@ -171,25 +182,28 @@ function checklistapi_permission() {
'title' => t('Edit any checklist'),
'description' => $edit_checklist_perm_description = t('Check and uncheck list items and save changes, or clear saved progress.'),
);
// Per checklist permissions.
foreach (checklistapi_get_checklist_info() as $id => $definition) {
if (!empty($id)) {
$perms['view ' . $id . ' checklistapi checklist'] = array(
'title' => t(
'View the !name checklist',
array('!name' => (checklistapi_checklist_access($id)) ? l($definition['#title'], $definition['#path']) : drupal_placeholder($definition['#title']))
),
'description' => $view_checklist_perm_description,
);
$perms['edit ' . $id . ' checklistapi checklist'] = array(
'title' => t(
'Edit the !name checklist',
array('!name' => (checklistapi_checklist_access($id)) ? l($definition['#title'], $definition['#path']) : drupal_placeholder($definition['#title']))
),
'description' => $edit_checklist_perm_description,
);
if (empty($id)) {
continue;
}
$perms['view ' . $id . ' checklistapi checklist'] = array(
'title' => t(
'View the !name checklist',
array('!name' => (checklistapi_checklist_access($id)) ? l($definition['#title'], $definition['#path']) : drupal_placeholder($definition['#title']))
),
'description' => $view_checklist_perm_description,
);
$perms['edit ' . $id . ' checklistapi checklist'] = array(
'title' => t(
'Edit the !name checklist',
array('!name' => (checklistapi_checklist_access($id)) ? l($definition['#title'], $definition['#path']) : drupal_placeholder($definition['#title']))
),
'description' => $edit_checklist_perm_description,
);
}
return $perms;
}
......@@ -207,33 +221,38 @@ function checklistapi_permission() {
*/
function checklistapi_sort_array(array $array) {
$child_keys = element_children($array);
if (count($child_keys)) {
$incrementer = 0;
$children = array();
foreach ($child_keys as $key) {
// Move child to a temporary array for sorting.
$children[$key] = $array[$key];
unset($array[$key]);
// Supply a default weight if missing or invalid.
if (empty($children[$key]['#weight']) || !is_numeric($children[$key]['#weight'])) {
$children[$key]['#weight'] = 0;
}
// Increase each weight incrementally to preserve the original order when
// not overridden. This accounts for undefined behavior in PHP's uasort()
// function when its comparison callback finds two values equal.
$children[$key]['#weight'] += ($incrementer++ / 1000);
// Descend into child.
$children[$key] = checklistapi_sort_array($children[$key]);
}
// Sort by weight.
uasort($children, 'element_sort');
// Remove incremental weight hack.
foreach ($children as $key => $child) {
$children[$key]['#weight'] = floor($children[$key]['#weight']);
if (!count($child_keys)) {
// No children to sort.
return $array;
}
$incrementer = 0;
$children = array();
foreach ($child_keys as $key) {
// Move child to a temporary array for sorting.
$children[$key] = $array[$key];
unset($array[$key]);
// Supply a default weight if missing or invalid.
if (empty($children[$key]['#weight']) || !is_numeric($children[$key]['#weight'])) {
$children[$key]['#weight'] = 0;
}
// Put children back in the main array.
$array += $children;
// Increase each weight incrementally to preserve the original order when
// not overridden. This accounts for undefined behavior in PHP's uasort()
// function when its comparison callback finds two values equal.
$children[$key]['#weight'] += ($incrementer++ / 1000);
// Descend into child.
$children[$key] = checklistapi_sort_array($children[$key]);
}
// Sort by weight.
uasort($children, 'element_sort');
// Remove incremental weight hack.
foreach ($children as $key => $child) {
$children[$key]['#weight'] = floor($children[$key]['#weight']);
}
// Put children back in the main array.
$array += $children;
return $array;
}
......
......@@ -67,6 +67,7 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
'#tree' => TRUE,
'#type' => 'vertical_tabs',
);
// Loop through groups.
$num_autochecked_items = 0;
$groups = $checklist->items;
......@@ -79,6 +80,7 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
if (!empty($group['#description'])) {
$form['checklistapi'][$group_key]['#description'] = filter_xss_admin($group['#description']);
}
// Loop through items.
foreach (element_children($group) as $item_key) {
$item = &$group[$item_key];
......@@ -129,6 +131,7 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
);
}
}
$form['actions'] = array(
'#access' => $user_has_edit_access,
'#type' => 'actions',
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment