Skip to content

Commit

Permalink
Add a macro for activity changes
Browse files Browse the repository at this point in the history
  • Loading branch information
elkuku committed Aug 18, 2014
1 parent 0709d2f commit 0f531f0
Show file tree
Hide file tree
Showing 3 changed files with 222 additions and 50 deletions.
60 changes: 60 additions & 0 deletions src/JTracker/View/Renderer/TrackerExtension.php
Original file line number Diff line number Diff line change
Expand Up @@ -97,12 +97,16 @@ public function getFunctions()
new \Twig_SimpleFunction('avatar', array($this, 'fetchAvatar')),
new \Twig_SimpleFunction('prioClass', array($this, 'getPrioClass')),
new \Twig_SimpleFunction('priorities', array($this, 'getPriorities')),
new \Twig_SimpleFunction('getPriority', array($this, 'getPriority')),
new \Twig_SimpleFunction('status', array($this, 'getStatus')),
new \Twig_SimpleFunction('getStatuses', array($this, 'getStatuses')),
new \Twig_SimpleFunction('issueLink', array($this, 'issueLink')),
new \Twig_SimpleFunction('getRelTypes', array($this, 'getRelTypes')),
new \Twig_SimpleFunction('getRelType', array($this, 'getRelType')),
new \Twig_SimpleFunction('getTimezones', array($this, 'getTimezones')),
new \Twig_SimpleFunction('renderDiff', array($this, 'renderDiff')),
new \Twig_SimpleFunction('renderLabels', array($this, 'renderLabels')),
new \Twig_SimpleFunction('arrayDiff', array($this, 'arrayDiff')),
);

if (!JDEBUG)
Expand Down Expand Up @@ -226,6 +230,22 @@ public function getPriorities()
];
}

/**
* Get the priority text.
*
* @param integer $id The priority id.
*
* @return string
*
* @since 1.0
*/
public function getPriority($id)
{
$priorities = $this->getPriorities();

return isset($priorities[$id]) ? $priorities[$id] : 'N/A';
}

/**
* Dummy function to prevent throwing exception on dump function in the non-debug mode.
*
Expand Down Expand Up @@ -448,6 +468,28 @@ public function getRelTypes()
return $relTypes;
}

/**
* Get the relation type text.
*
* @param integer $id The relation id.
*
* @return string
*
* @since 1.0
*/
public function getRelType($id)
{
foreach ($this->getRelTypes() as $relType)
{
if ($relType->value == $id)
{
return $relType->text;
}
}

return '';
}

/**
* Generate a localized yes/no message.
*
Expand Down Expand Up @@ -560,4 +602,22 @@ public function renderDiff($old, $new, $showLineNumbers = true, $showHeader = tr

return $diff->Render($renderer);
}

/**
* Get the difference of two comma separated value strings.
*
* @param string $a The "a" string.
* @param string $b The "b" string.
*
* @return string difference values comma separated
*
* @since 1.0
*/
public function arrayDiff($a, $b)
{
$as = explode(',', $a);
$bs = explode(',', $b);

return implode(',', array_diff($as, $bs));
}
}
69 changes: 19 additions & 50 deletions templates/tracker/issue.index.twig
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

{% extends "index.twig" %}

{% import "tpl/activities.twig" as activities %}

{% block title %}{{ project.title }} #{{ item.issue_number }} - {{ item.title }}{% endblock %}

{% block headerText %} - {{ project.short_title }}{% endblock %}
Expand Down Expand Up @@ -315,57 +317,24 @@
{% if "change" == activity.event %}
<table class="table">
{% for change in activity.text|json_decode %}
{% if change.name == 'description_raw' %}
<tr>
<td class="span2">
{{ 'Description'|_ }}
</td>
<td colspan="4">
{% set activitiesCnt = activitiesCnt + 1 %}
<button type="button" class="btn" data-toggle="collapse" data-target="#diff-{{ activitiesCnt }}">
{{ 'Differences'|_ }}
</button>

<div id="diff-{{ activitiesCnt }}" class="collapse">
{{ renderDiff(change.old, change.new)|raw }}
</div>
</td>
</tr>
{% elseif change.name == 'title' %}
<tr>
<td class="span2">
{{ 'Title'|_ }}
</td>
<td colspan="4">
{% set activitiesCnt = activitiesCnt + 1 %}
<button type="button" class="btn" data-toggle="collapse" data-target="#diff-{{ activitiesCnt }}">
{{ 'Differences'|_ }}
</button>

<div id="diff-{{ activitiesCnt }}" class="collapse">
{{ renderDiff(change.old, change.new, false, false)|raw }}
</div>
</td>
</tr>
{% if 'status' == change.name %}
{{ activities.status(change.old, change.new) }}
{% elseif 'description_raw' == change.name%}
{% set activitiesCnt = activitiesCnt + 1 %}
{{ activities.description(change.old, change.new, activitiesCnt) }}
{% elseif 'title' == change.name %}
{% set activitiesCnt = activitiesCnt + 1 %}
{{ activities.title(change.old, change.new, activitiesCnt) }}
{% elseif 'priority' == change.name %}
{{ activities.priority(change.old, change.new) }}
{% elseif 'labels' == change.name %}
{{ activities.labels(change.old, change.new) }}
{% elseif 'easy' == change.name %}
{{ activities.easy(change.old, change.new) }}
{% elseif 'rel_type' == change.name %}
{{ activities.relationType(change.old, change.new) }}
{% else %}
<tr>
<td class="span2">
{{ change.name|title }}
</td>
{% if "status" == change.name %}
<td class="span4 alert-{{ status(change.old).closed ? "error" : "success" }}">
{{ status(change.old).status|_ }}
</td>
<td class="span1">&rArr;</td>
<td class="span4 alert-{{ status(change.new).closed ? "error" : "success" }}">
{{ status(change.new).status|_ }}
</td>
{% else %}
<td class="span4">{{ change.old }}</td>
<td class="span1">&rArr;</td>
<td class="span4">{{ change.new }}</td>
{% endif %}
</tr>
{{ activities.change(change.name, change.old, change.new) }}
{% endif %}
{% endfor %}
</table>
Expand Down
143 changes: 143 additions & 0 deletions templates/tracker/tpl/activities.twig
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
{# Copyright (C) 2012 - 2014 Open Source Matters, Inc. All rights reserved. #}
{# GNU General Public License version 2 or later; see LICENSE.txt#}

{% macro change(name, old, new) %}
<tr>
<td class="span2">
{{ name|title }}
</td>
<td class="span4 center">
{{ old }}
</td>
<td class="span1 center">
&rArr;
</td>
<td class="span4 center">
{{ new }}
</td>
</tr>
{% endmacro %}

{% macro description(old, new, cnt) %}
<tr>
<td class="span2">
{{ 'Description'|_ }}
</td>
<td colspan="3">
<button type="button" class="btn" data-toggle="collapse" data-target="#diff-{{ cnt }}">
{{ 'Differences'|_ }}
</button>

<div id="diff-{{ cnt }}" class="collapse">
{{ renderDiff(old, new)|raw }}
</div>
</td>
</tr>
{% endmacro %}

{% macro title(old, new, cnt) %}
<tr>
<td class="span2">
{{ 'Title'|_ }}
</td>
<td colspan="4">
<button type="button" class="btn" data-toggle="collapse" data-target="#diff-{{ cnt }}">
{{ 'Differences'|_ }}
</button>

<div id="diff-{{ cnt }}" class="collapse">
{{ renderDiff(old, new, false, false)|raw }}
</div>
</td>
</tr>
{% endmacro %}

{% macro status(old, new) %}
<tr>
<td class="span2">
{{ 'Status'|_ }}
</td>
<td class="span4 alert-{{ status(old).closed ? "error" : "success" }} center">
{{ status(old).status|_ }}
</td>
<td class="span1 center">
&rArr;
</td>
<td class="span4 alert-{{ status(new).closed ? "error" : "success" }} center">
{{ status(new).status|_ }}
</td>
</tr>
{% endmacro %}

{% macro easy(old, new) %}
<tr>
<td class="span2">
{{ 'Easy'|_ }}
</td>
<td class="span4 center">
{{ old ? 'Yes' : 'No' }}
</td>
<td class="span1 center">
&rArr;
</td>
<td class="span4 center">
{{ new ? 'Yes' : 'No' }}
</td>
</tr>
{% endmacro %}

{% macro priority(old, new) %}
<tr>
<td class="span2">
{{ 'Priority'|_ }}
</td>
<td class="span4 center">
<span class="badge {{ prioClass(old) }}">
{{ getPriority(old) }}
</span>
</td>
<td class="span1 center">
&rArr;
</td>
<td class="span4 center">
<span class="badge {{ prioClass(new) }}">
{{ getPriority(new) }}
</span>
</td>
</tr>
{% endmacro %}

{% macro labels(old, new) %}
<tr>
<td class="span2">
{{ 'Labels'|_ }}
</td>
<td colspan="3">
{% set added = arrayDiff(new, old) %}
{% if added %}
{{ 'Added' }}: {{ renderLabels(added)|raw }}<br />
{% endif %}
{% set removed = arrayDiff(old, new) %}
{% if removed %}
{{ 'Removed'|_ }}: {{ renderLabels(removed)|raw }}<br />
{% endif %}
</td>
</tr>
{% endmacro %}

{% macro relationType(old, new) %}
<tr>
<td class="span2">
{{ 'Relation Type'|_ }}
</td>
<td class="span4 center">
{{ getRelType(old) }}
</td>
<td class="span1 center">
&rArr;
</td>
<td class="span4 center">
{{ getRelType(new) }}
</td>
</tr>
{% endmacro %}

0 comments on commit 0f531f0

Please sign in to comment.