summaryrefslogtreecommitdiff
path: root/kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module
diff options
context:
space:
mode:
Diffstat (limited to 'kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module')
-rw-r--r--kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module219
1 files changed, 115 insertions, 104 deletions
diff --git a/kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module b/kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module
index 3cdd752..c229177 100644
--- a/kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module
+++ b/kolab.org/www/drupal-7.18/sites/all/modules/checklistapi/checklistapi.module
@@ -9,47 +9,30 @@
*/
/**
- * Link should always be shown.
- */
-define('CHECKLISTAPI_LINK_CONTEXT_ANY', 1);
-
-/**
- * Link should only be shown if the item it belongs to has been previously
- * checked.
- */
-define('CHECKLISTAPI_LINK_CONTEXT_ITEM_CHECKED', 2);
-
-/**
- * Link should only be shown if the item it belongs to has not been previously
- * checked.
- */
-define('CHECKLISTAPI_LINK_CONTEXT_ITEM_UNCHECKED', 3);
-
-/**
- * Access callback: Checks the current user's access to a checklist.
+ * Access callback: Checks the current user's access to a given checklist.
*
* @param string $id
* The checklist ID.
* @param string $operation
- * The operation to test access for. Possible values are "view", "edit", and
- * "any". Defaults to "any".
+ * (optional) The operation to test access for. Accepted values are "view",
+ * "edit", and "any". Defaults to "any".
*
* @return bool
* Returns TRUE if the current user has access to perform a given operation on
* the specified checklist, or FALSE if not.
*/
function checklistapi_checklist_access($id, $operation = 'any') {
- $view_access = user_access('view any checklistapi checklist') || user_access('view ' . $id . ' checklistapi checklist');
- $edit_access = user_access('edit any checklistapi checklist') || user_access('edit ' . $id . ' checklistapi checklist');
- switch ($operation) {
- case 'view':
- return $view_access;
- case 'edit':
- return $edit_access;
- case 'any':
- default:
- return $view_access || $edit_access;
+ $all_operations = array('view', 'edit', 'any');
+ if (!in_array($operation, $all_operations)) {
+ throw new Exception(t('No such operation "@operation"', array(
+ '@operation' => $operation,
+ )));
}
+
+ $access['view'] = user_access('view any checklistapi checklist') || user_access('view ' . $id . ' checklistapi checklist');
+ $access['edit'] = user_access('edit any checklistapi checklist') || user_access('edit ' . $id . ' checklistapi checklist');
+ $access['any'] = $access['view'] || $access['edit'];
+ return $access[$operation];
}
/**
@@ -79,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];
}
@@ -120,6 +106,7 @@ function checklistapi_init() {
*/
function checklistapi_menu() {
$items = array();
+
// Checklists report.
$items['admin/reports/checklistapi'] = array(
'title' => 'Checklists',
@@ -128,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($definition['#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;
}
@@ -173,6 +166,7 @@ function checklistapi_menu() {
*/
function checklistapi_permission() {
$perms = array();
+
// Universal permissions.
$perms['view checklistapi checklists report'] = array(
'title' => t(
@@ -188,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;
}
@@ -224,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;
}
@@ -279,8 +281,17 @@ function checklistapi_strtolowercamel($string) {
function checklistapi_theme() {
return array(
'checklistapi_compact_link' => array(
- 'variables' => array('id' => NULL),
'file' => 'checklistapi.pages.inc',
),
+ 'checklistapi_progress_bar' => array(
+ 'path' => drupal_get_path('module', 'checklistapi') . '/templates',
+ 'template' => 'checklistapi-progress-bar',
+ 'variables' => array(
+ 'message' => ' ',
+ 'number_complete' => 0,
+ 'number_of_items' => 0,
+ 'percent_complete' => 0,
+ ),
+ ),
);
}