Commit 33dfec8c authored by TravisCarden's avatar TravisCarden

By TravisCarden: Cleaned up and simplified code.

parent 7e9b5771
......@@ -20,11 +20,11 @@ function checklistapi_report_form() {
t('Last updated by'),
t('Operations'),
);
$checklists = checklistapi_get_checklist_info();
if (count($checklists)) {
$definitions = checklistapi_get_checklist_info();
if (count($definitions)) {
$rows = array();
foreach ($checklists as $key => $value) {
$checklist = checklistapi_checklist_load($key);
foreach ($definitions as $id => $definition) {
$checklist = checklistapi_checklist_load($id);
$row = array();
$row[] = array(
'data' => ($checklist->userHasAccess()) ? l($checklist->title, $checklist->path) : drupal_placeholder($checklist->title),
......
......@@ -64,57 +64,40 @@ function checklistapi_checklist_load($id) {
return ($definition) ? new ChecklistapiChecklist($definition) : FALSE;
}
/**
* Converts a string to lowerCamel case, suitably for a class property name.
*
* @param string $string
* The input string.
*
* @return string
* The input string converted to camelCase.
*/
function checklistapi_convert_string_to_lower_camel($string) {
$string = str_replace('_', ' ', $string);
$string = ucwords($string);
$string = str_replace(' ', '', $string);
return lcfirst($string);
}
/**
* Gets checklist definitions.
*
* @param string $id
* (optional) A checklist ID.
* (optional) A checklist ID. Defaults to NULL.
*
* @return array|false
* The definition of the specified checklist, or FALSE if no such checklist
* exists, or an array of all checklist definitions if none is specified.
*/
function checklistapi_get_checklist_info($id = NULL) {
$checklists = &drupal_static(__FUNCTION__);
if (!isset($checklists)) {
$checklists = module_invoke_all('checklistapi_checklist_info');
$checklists = checklistapi_sort_array($checklists);
drupal_alter('checklistapi_checklist_info', $checklists);
$checklists = checklistapi_sort_array($checklists);
foreach ($checklists as $key => $value) {
$checklists[$key] = array('#id' => $key) + $checklists[$key];
$definitions = &drupal_static(__FUNCTION__);
if (!isset($definitions)) {
$definitions = module_invoke_all('checklistapi_checklist_info');
$definitions = checklistapi_sort_array($definitions);
drupal_alter('checklistapi_checklist_info', $definitions);
$definitions = checklistapi_sort_array($definitions);
foreach ($definitions as $key => $value) {
$definitions[$key] = array('#id' => $key) + $definitions[$key];
}
}
if (!empty($id)) {
return (!empty($checklists[$id])) ? $checklists[$id] : FALSE;
return (!empty($definitions[$id])) ? $definitions[$id] : FALSE;
}
return $checklists;
return $definitions;
}
/**
* Implements hook_help().
*/
function checklistapi_help($path, $arg) {
$checklists = checklistapi_get_checklist_info();
foreach ($checklists as $checklist) {
if ($checklist['#path'] == $path && !empty($checklist['#help'])) {
return $checklist['#help'];
foreach (checklistapi_get_checklist_info() as $definition) {
if ($definition['#path'] == $path && !empty($definition['#help'])) {
return $definition['#help'];
}
}
}
......@@ -133,27 +116,26 @@ function checklistapi_menu() {
'file' => 'checklistapi.admin.inc',
);
// Individual checklists.
foreach (checklistapi_get_checklist_info() as $checklist_id => $checklist) {
if (!empty($checklist['#path']) && !empty($checklist['#title'])) {
$checklist_path = $checklist['#path'];
foreach (checklistapi_get_checklist_info() as $id => $definition) {
if (!empty($definition['#path']) && !empty($definition['#title'])) {
// View/edit checklist.
$items[$checklist_path] = array(
'title' => $checklist['#title'],
'description' => (!empty($checklist['#description'])) ? $checklist['#description'] : '',
$items[$definition['#path']] = array(
'title' => $definition['#title'],
'description' => (!empty($definition['#description'])) ? $definition['#description'] : '',
'page callback' => 'drupal_get_form',
'page arguments' => array('checklistapi_checklist_form', $checklist_id),
'page arguments' => array('checklistapi_checklist_form', $id),
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($checklist_id),
'access arguments' => array($id),
'file' => 'checklistapi.pages.inc',
'menu_name' => (!empty($checklist['#menu_name'])) ? $checklist['#menu_name'] : '',
'menu_name' => (!empty($definition['#menu_name'])) ? $definition['#menu_name'] : '',
);
// Clear saved progress.
$items[$checklist_path . '/clear'] = array(
$items[$definition['#path'] . '/clear'] = array(
'title' => 'Clear',
'page callback' => 'drupal_get_form',
'page arguments' => array('checklistapi_checklist_clear_confirm', $checklist_id),
'page arguments' => array('checklistapi_checklist_clear_confirm', $id),
'access callback' => 'checklistapi_checklist_access',
'access arguments' => array($checklist_id, 'edit'),
'access arguments' => array($id, 'edit'),
'file' => 'checklistapi.pages.inc',
);
}
......@@ -182,19 +164,19 @@ function checklistapi_permission() {
'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 => $checklist) {
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($checklist['#title'], $checklist['#path']) : drupal_placeholder($checklist['#title']))
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($checklist['#title'], $checklist['#path']) : drupal_placeholder($checklist['#title']))
array('!name' => (checklistapi_checklist_access($id)) ? l($definition['#title'], $definition['#path']) : drupal_placeholder($definition['#title']))
),
'description' => $edit_checklist_perm_description,
);
......@@ -246,3 +228,19 @@ function checklistapi_sort_array(array $array) {
}
return $array;
}
/**
* Converts a string to lowerCamel case, suitably for a class property name.
*
* @param string $string
* The input string.
*
* @return string
* The input string converted to camelCase.
*/
function checklistapi_strtolowercamel($string) {
$string = str_replace('_', ' ', $string);
$string = ucwords($string);
$string = str_replace(' ', '', $string);
return lcfirst($string);
}
......@@ -52,7 +52,7 @@ function checklistapi_checklist_clear_confirm_submit($form, &$form_state) {
* @ingroup forms
*/
function checklistapi_checklist_form($form, &$form_state, $id) {
$checklist = checklistapi_checklist_load($id);
$form['#checklist'] = $checklist = checklistapi_checklist_load($id);
$actions = array(
'#type' => 'actions',
'#access' => $user_has_edit_access = $checklist->userHasAccess('edit'),
......@@ -68,8 +68,6 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
'#access' => $checklist->hasSavedProgress(),
),
);
$form['#checklist'] = $checklist;
$form['actions_top'] = $actions + array(
'#weight' => -100,
'#attributes' => array('class' => array('js-hide')),
......@@ -84,7 +82,6 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
);
// Loop through groups.
$groups = $checklist->items;
foreach (element_children($groups) as $group_key) {
$group = &$groups[$group_key];
$form['checklistapi'][$group_key] = array(
......@@ -98,9 +95,8 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
$item_keys = element_children($group);
foreach ($item_keys as $item_key) {
$item = &$group[$item_key];
// Get saved state.
$saved_item = !empty($checklist->savedProgress[$group_key][$item_key]) ? $checklist->savedProgress[$group_key][$item_key] : 0;
// Set default value.
$saved_item = !empty($checklist->savedProgress[$group_key][$item_key]) ? $checklist->savedProgress[$group_key][$item_key] : 0;
$default_value = FALSE;
if ($saved_item) {
$default_value = TRUE;
......@@ -143,7 +139,7 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
));
}
$description .= '<div class="links">' . implode(' | ', $links) . '</div>';
// Build the list item.
// Compile the list item.
$form['checklistapi'][$group_key][$item_key] = array(
'#type' => 'checkbox',
'#title' => filter_xss($item['#title']),
......@@ -154,7 +150,6 @@ function checklistapi_checklist_form($form, &$form_state, $id) {
}
}
$form['actions_bottom'] = $actions + array('#weight' => 100);
return $form;
}
......
......@@ -93,7 +93,7 @@ class ChecklistapiChecklist {
unset($definition[$group_key]);
}
foreach ($definition as $key => $value) {
$property_name = checklistapi_convert_string_to_lower_camel(substr($key, 1));
$property_name = checklistapi_strtolowercamel(drupal_substr($key, 1));
$this->$property_name = $value;
}
$this->savedProgress = variable_get($this->getSavedProgressVariableName(), array());
......@@ -149,27 +149,26 @@ class ChecklistapiChecklist {
foreach ($group as $item_key => $item) {
$old_item = (!empty($this->savedProgress[$group_key][$item_key])) ? $this->savedProgress[$group_key][$item_key] : 0;
$new_item = &$values[$group_key][$item_key];
// Item is checked.
if ($item == 1) {
// Item is checked.
$completed_items_counter++;
// Item was previously checked. Use saved value.
if ($old_item) {
// Item was previously checked. Use saved value.
$new_item = $old_item;
}
// Item is newly checked. Set new value.
else {
// Item is newly checked. Set new value.
$new_item = array(
'#completed' => $time,
'#uid' => $user->uid,
);
// Increment changed items counter.
$changed_items_counter++;
}
}
// Item is unchecked.
else {
// Item was previously checked off. Increment changed items counter.
// Item is unchecked.
if ($old_item) {
// Item was previously checked.
$changed_items_counter++;
}
}
......
......@@ -58,7 +58,7 @@ class ChecklistapiUnitTestCase extends DrupalUnitTestCase {
*
* @todo Add tests for vertical tabs progress indicators.
* @todo Add tests for saving and retrieving checklist progress.
* @todo Add tests for resetting saved progress.
* @todo Add tests for clearing saved progress.
*/
class ChecklistapiWebTestCase extends DrupalWebTestCase {
protected $privilegedUser;
......
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