Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
10
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Open sidebar
project
checklistapi
Commits
33dfec8c
Commit
33dfec8c
authored
Jun 12, 2012
by
TravisCarden
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
By TravisCarden: Cleaned up and simplified code.
parent
7e9b5771
Changes
5
Hide whitespace changes
Inline
Side-by-side
Showing
5 changed files
with
58 additions
and
66 deletions
+58
-66
checklistapi.admin.inc
checklistapi.admin.inc
+4
-4
checklistapi.module
checklistapi.module
+44
-46
checklistapi.pages.inc
checklistapi.pages.inc
+3
-8
lib/Drupal/checklistapi/ChecklistapiChecklist.php
lib/Drupal/checklistapi/ChecklistapiChecklist.php
+6
-7
tests/checklistapi.test
tests/checklistapi.test
+1
-1
No files found.
checklistapi.admin.inc
View file @
33dfec8c
...
...
@@ -20,11 +20,11 @@ function checklistapi_report_form() {
t
(
'Last updated by'
),
t
(
'Operations'
),
);
$
checklist
s
=
checklistapi_get_checklist_info
();
if
(
count
(
$
checklist
s
))
{
$
definition
s
=
checklistapi_get_checklist_info
();
if
(
count
(
$
definition
s
))
{
$rows
=
array
();
foreach
(
$
checklist
s
as
$
key
=>
$
value
)
{
$checklist
=
checklistapi_checklist_load
(
$
key
);
foreach
(
$
definition
s
as
$
id
=>
$
definition
)
{
$checklist
=
checklistapi_checklist_load
(
$
id
);
$row
=
array
();
$row
[]
=
array
(
'data'
=>
(
$checklist
->
userHasAccess
())
?
l
(
$checklist
->
title
,
$checklist
->
path
)
:
drupal_placeholder
(
$checklist
->
title
),
...
...
checklistapi.module
View file @
33dfec8c
...
...
@@ -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
)
{
$
checklist
s
=
&
drupal_static
(
__FUNCTION__
);
if
(
!
isset
(
$
checklist
s
))
{
$
checklist
s
=
module_invoke_all
(
'checklistapi_checklist_info'
);
$
checklist
s
=
checklistapi_sort_array
(
$
checklist
s
);
drupal_alter
(
'checklistapi_checklist_info'
,
$
checklist
s
);
$
checklist
s
=
checklistapi_sort_array
(
$
checklist
s
);
foreach
(
$
checklist
s
as
$key
=>
$value
)
{
$
checklist
s
[
$key
]
=
array
(
'#id'
=>
$key
)
+
$
checklist
s
[
$key
];
$
definition
s
=
&
drupal_static
(
__FUNCTION__
);
if
(
!
isset
(
$
definition
s
))
{
$
definition
s
=
module_invoke_all
(
'checklistapi_checklist_info'
);
$
definition
s
=
checklistapi_sort_array
(
$
definition
s
);
drupal_alter
(
'checklistapi_checklist_info'
,
$
definition
s
);
$
definition
s
=
checklistapi_sort_array
(
$
definition
s
);
foreach
(
$
definition
s
as
$key
=>
$value
)
{
$
definition
s
[
$key
]
=
array
(
'#id'
=>
$key
)
+
$
definition
s
[
$key
];
}
}
if
(
!
empty
(
$id
))
{
return
(
!
empty
(
$
checklist
s
[
$id
]))
?
$
checklist
s
[
$id
]
:
FALSE
;
return
(
!
empty
(
$
definition
s
[
$id
]))
?
$
definition
s
[
$id
]
:
FALSE
;
}
return
$
checklist
s
;
return
$
definition
s
;
}
/**
* 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
);
}
checklistapi.pages.inc
View file @
33dfec8c
...
...
@@ -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>'
;
//
Bu
il
d
the list item.
//
Comp
il
e
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
;
}
...
...
lib/Drupal/checklistapi/ChecklistapiChecklist.php
View file @
33dfec8c
...
...
@@ -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_
str
tolowercamel
(
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
++
;
}
}
...
...
tests/checklistapi.test
View file @
33dfec8c
...
...
@@ -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
resett
ing saved progress.
* @todo Add tests for
clear
ing saved progress.
*/
class
ChecklistapiWebTestCase
extends
DrupalWebTestCase
{
protected
$privilegedUser
;
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment