I am building an application, and apart of it is a log of conversation, similar to the way how u see messages in whatsapp. The problem I am facing is that the system cannot adapt to different dates nor showcases those in order like:
2018-03-16
item1
item2
2018-03-11
item3
item4
I am making usage of smarty to pull in my data object to my screen:
The back-end
// Fetch info about the support ticket's conversations
$query = "
SELECT
conversation_content,
posted_at,
is_internal,
IF(emp_id IS NULL, 0, 1) AS belongsToEmployee,
EmployeeTBL.EmpFirstname,
EmployeeTBL.EmpInsertion,
EmployeeTBL.EmpLastname
FROM
ticket_conversation
INNER JOIN tickets ON ticket_conversation.ticket_id=tickets.id
LEFT JOIN EmployeeTBL ON ticket_conversation.emp_id=EmployeeTBL.EmpID
WHERE
ticket_conversation.ticket_id = :id
ORDER BY
ticket_conversation.posted_at
DESC
";
$binds = array(':id' => $_GET['id']);
$ticketConversationList = $db->select($query, $binds);
// Set date container
$ticketDates = [];
// Loop through all the tickets and add their date as a ticket entry
foreach($ticketConversationList as $ticket){
$ticketDate = date('d-m-Y',strtotime($ticket['posted_at']));
$ticketDates[$ticketDate][] = $ticket;
}
//var_dump($ticketDates);
// Count all conversations
$conversationCount = count($ticketConversationList);
// Assign the variables
$smarty->assign("ticketDates", $ticketDates);
$smarty->assign("ticketConversationList", $ticketConversationList);
$smarty->assign("conversationCount", $conversationCount);
In my front-end, I am working with the variables provided, as to loop through my array of objects.
The front-end
<div class="form-group">
<label>{$ticketChatHistoryLabel}</label>
<hr style="border-color: transparent;">
{foreach from=$ticketConversationList key=index item=ticketConversation}
{if $index != 0}
{$index = $index - 1}
{/if}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$dateOfPreviousConversation = date('d-m-Y',strtotime($ticketConversationList[$index].posted_at))}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$timeOfConversation = date('H:i',strtotime($ticketConversation.posted_at))}
{if $dateOfPreviousConversation < $dateOfConversation || $index == 0}
<div>
<div class="btn btn-default" style="margin-left: 44%; margin-bottom: 20px;">{$dateOfConversation}</div>
</div>
{/if}
<div class="holder
{if $ticketConversation.belongsToEmployee == 1}
pull-right
{else}pull-left
{/if}" style="border-radius: 0; border: 1px solid; padding: 5px; width: 90%;
{if $ticketConversation.belongsToEmployee != 0 && $ticketConversation.is_internal == 0}
background-color: #c0eed5;
{elseif $ticketConversation.belongsToEmployee != 0 && $ticketConversation.is_internal == 1}
background-color: #eeeeee;
{/if}">
{$ticketConversation.conversation_content}
<i class="{if $ticketConversation.belongsToEmployee}
pull-right
{else}pull-left
{/if}" style="color: #999999">
{$ticketHistoryPostedAt}{$dateOfConversation} {$atLabel} {$timeOfConversation} {$hoursLabel}
</i>
{if $ticketConversation.belongsToEmployee}
<i class="pull-left" style="color: #999999">
{$ticketHistoryPostAuthor}{$ticketConversation.EmpFirstname} {$ticketConversation.EmpInsertion} {$ticketConversation.EmpLastname}
</i>
{/if}
</div>
<div class="clearfix"></div>
{/foreach}
<div>
<h4 style="text-align: center; color: #afafaf;">- {$endOfTicketConversation} -</h4>
</div>
</div>
At the moment this is the output:
Ticket history as is with current code
Then, I switch
{if $index != 0}
{$index = $index - 1}
{/if}
And I set the green response data as 2018-03-15 (yesterday from time of posting this). The result is as follows:
RESOLVED
Required fake 0 check since we're -1'ing
{foreach from=$ticketConversationList key=index item=ticketConversation}
{$fake = $index != 0}
{if $index != 0}
{$index = $index - 1}
{/if}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$dateOfPreviousConversation = date('d-m-Y',strtotime($ticketConversationList[$index].posted_at))}
{$dateOfConversation = date('d-m-Y',strtotime($ticketConversation.posted_at))}
{$timeOfConversation = date('H:i',strtotime($ticketConversation.posted_at))}
{if (( $index == 0 && !$fake )|| $dateOfPreviousConversation != $dateOfConversation) }
<div>
<div class="btn btn-default" style="margin-left: 44%; margin-bottom: 20px;">{$dateOfConversation}</div>
</div>
{/if}