Skip to content

Commit

Permalink
Merge pull request #36 from spekulatius/adding-completed-class
Browse files Browse the repository at this point in the history
Highlighting the completed steps with a CSS class
  • Loading branch information
dhensby committed Nov 23, 2015
2 parents 6197740 + 78f98a5 commit d2780c4
Show file tree
Hide file tree
Showing 5 changed files with 73 additions and 6 deletions.
2 changes: 0 additions & 2 deletions .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,6 @@ matrix:
include:
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3
- php: 5.6
env: DB=MYSQL CORE_RELEASE=3.1
- php: 5.6
env: DB=PGSQL CORE_RELEASE=3.2
allow_failures:
Expand Down
2 changes: 1 addition & 1 deletion code/extensions/MultiFormObjectDecorator.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ class MultiFormObjectDecorator extends DataExtension {
'MultiFormSession' => 'MultiFormSession',
);

public function augmentSQL(SQLSelect $query) {
public function augmentSQL(SQLQuery &$query) {
// If you're querying by ID, ignore the sub-site - this is a bit ugly...
if(
strpos($query->where[0], ".`ID` = ") === false
Expand Down
24 changes: 23 additions & 1 deletion code/model/MultiForm.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ abstract class MultiForm extends Form {

/**
* The current encrypted MultiFormSession identification.
*
* @var string
*/
protected $currentSessionHash;
Expand Down Expand Up @@ -82,6 +83,13 @@ abstract class MultiForm extends Form {
*/
protected $displayLink;

/**
* Flag which is being used in getAllStepsRecursive() to allow adding the completed flag on the steps
*
* @var boolean
*/
protected $currentStepHasBeenFound = false;

/**
* Start the MultiForm instance.
*
Expand Down Expand Up @@ -566,9 +574,13 @@ public function getAllStepsLinear() {

$firstStep = DataObject::get_one(static::$start_step, "\"SessionID\" = {$this->session->ID}");
$firstStep->LinkingMode = ($firstStep->ID == $this->getCurrentStep()->ID) ? 'current' : 'link';
$firstStep->addExtraClass('completed');
$firstStep->setForm($this);
$stepsFound->push($firstStep);

// mark the further steps as non-completed if the first step is the current
if ($firstStep->ID == $this->getCurrentStep()->ID) $this->currentStepHasBeenFound = true;

$this->getAllStepsRecursive($firstStep, $stepsFound);

return $stepsFound;
Expand All @@ -593,11 +605,21 @@ protected function getAllStepsRecursive($step, &$stepsFound) {
// Is this step in the DB? If it is, we use that
$nextStep = $step->getNextStepFromDatabase();
if(!$nextStep) {
// If it's not in the DB, we use a singleton instance of it instead - this step hasn't been accessed yet
// If it's not in the DB, we use a singleton instance of it instead -
// - this step hasn't been accessed yet
$nextStep = singleton($step->getNextStep());
}

// once the current steps has been found we won't add the completed class anymore.
if ($nextStep->ID == $this->getCurrentStep()->ID) $this->currentStepHasBeenFound = true;

$nextStep->LinkingMode = ($nextStep->ID == $this->getCurrentStep()->ID) ? 'current' : 'link';

// add the completed class
if (!$this->currentStepHasBeenFound) $nextStep->addExtraClass('completed');

$nextStep->setForm($this);

// Add the array data, and do a callback
$stepsFound->push($nextStep);
$this->getAllStepsRecursive($nextStep, $stepsFound);
Expand Down
47 changes: 47 additions & 0 deletions code/model/MultiFormStep.php
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,13 @@ class MultiFormStep extends DataObject {
*/
protected $form;

/**
* List of additional CSS classes for this step
*
* @var array $extraClasses
*/
protected $extraClasses = array();

/**
* Form fields to be rendered with this step.
* (Form object is created in {@link MultiForm}.
Expand Down Expand Up @@ -331,6 +338,8 @@ public function getForm() {
// ##################### Utility ####################

/**
* Determines whether the user is able to go back using the "action_back"
* Determines whether the user is able to go back using the "action_back"
* Determines whether the user is able to go back using the "action_back"
* form action, based on the boolean value of $can_go_back.
*
Expand Down Expand Up @@ -362,4 +371,42 @@ public function isCurrentStep() {
return ($this->class == $this->Session()->CurrentStep()->class) ? true : false;
}

/**
* Add a CSS-class to the step. If needed, multiple classes can be added by delimiting a string with spaces.
*
* @param string $class A string containing a classname or several class names delimited by a space.
* @return MultiFormStep
*/
public function addExtraClass($class) {
// split at white space
$classes = preg_split('/\s+/', $class);
foreach($classes as $class) {
// add classes one by one
$this->extraClasses[$class] = $class;
}
return $this;
}

/**
* Remove a CSS-class from the step. Multiple classes names can be passed through as a space delimited string.
*
* @param string $class
* @return MultiFormStep
*/
public function removeExtraClass($class) {
// split at white space
$classes = preg_split('/\s+/', $class);
foreach ($classes as $class) {
// unset one by one
unset($this->extraClasses[$class]);
}
return $this;
}

/**
* @return string
*/
public function getExtraClasses() {
return join(' ', array_keys($this->extraClasses));
}
}
4 changes: 2 additions & 2 deletions templates/Includes/MultiFormProgressList.ss
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
<ul class="stepIndicator current-$CurrentStep.class">
<% loop AllStepsLinear %>
<li class="$ClassName<% if LinkingMode %> $LinkingMode<% end_if %><% if FirstLast %> $FirstLast<% end_if %>">
<li class="$ClassName<% if LinkingMode %> $LinkingMode<% end_if %><% if FirstLast %> $FirstLast<% end_if %><% if $ExtraClasses %> $ExtraClasses<% end_if %>">
<% if LinkingMode = current %><% else %><% if ID %><a href="{$Top.URLSegment}/?MultiFormSessionID={$SessionID}&amp;StepID={$ID}"><% end_if %><% end_if %>
<% if Title %>$Title<% else %>$ClassName<% end_if %>
<% if LinkingMode = current %><% else %><% if ID %></a><% end_if %><% end_if %>
</li>
<% end_loop %>
</ul>
</ul>

0 comments on commit d2780c4

Please sign in to comment.