Skip to content

Commit

Permalink
Merge branch 'mogic-le-json-export' into v4
Browse files Browse the repository at this point in the history
  • Loading branch information
CybotTM committed Apr 30, 2024
2 parents 68d3ffb + a1b7405 commit fda6423
Show file tree
Hide file tree
Showing 4 changed files with 300 additions and 6 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,12 @@
namespace Netresearch\TimeTrackerBundle\Controller;

use Netresearch\TimeTrackerBundle\Entity\Entry;
use Netresearch\TimeTrackerBundle\Response\Error;
use Netresearch\TimeTrackerBundle\Entity\User;
use Netresearch\TimeTrackerBundle\Helper\TimeHelper;
use Netresearch\TimeTrackerBundle\Model\JsonResponse;
use Netresearch\TimeTrackerBundle\Model\Response;
use Doctrine\ORM\Tools\Pagination\Paginator;
use Symfony\Component\HttpFoundation\Request;

class InterpretationController extends BaseController
Expand Down Expand Up @@ -455,4 +457,116 @@ private function normalizeData(array $data)
return $normalized;
}

/**
* Retrieves filtered time tracker entries based on request parameters.
* Applies pagination.
*
* @param Request $request
* @return \Netresearch\TimeTrackerBundle\Model\JsonResponse|Error
* @throws \Exception
*/
public function getAllEntriesAction(Request $request)
{
if (false === $this->isPl($request)) {
return $this->getFailedAuthorizationResponse();
}

$project = (int) $request->get('project_id');
$datestart = $request->get('datestart');
$dateend = $request->get('dateend');
$customer = (int) $request->get('customer_id');
$activity = (int) $request->get('activity_id');
$maxResults = (int) $request->get('maxResults');
$page = (int) $request->get('page');

//prepare data
if ($page < 0) {
$message = $this->get('translator')->trans('page can not be negative.');
return new Error($message, 400);
}
$maxResults = $maxResults > 0 ? $maxResults : 50; //cant be lower than 0

$searchArray = [
'maxResults' => $maxResults, //default 50
'page' => $page
];
// parameter has to exist in request and has to be different from null -> return true -> set array key
($activity ?? null) ? $searchArray['activity'] = $activity : null;
($project ?? null) ? $searchArray['project'] = $project : null;
($datestart ?? null) ? $searchArray['datestart'] = $datestart : null;
($dateend ?? null) ? $searchArray['dateend'] = $dateend : null;
($customer ?? null) ? $searchArray['customer'] = $customer : null;

$repository = $this->getDoctrine()->getRepository('NetresearchTimeTrackerBundle:Entry');
try {
$paginator = new Paginator($repository->queryByFilterArray($searchArray));
} catch (\Exception $e) {
$response = new Response($this->translate($e->getMessage()));
$response->setStatusCode(406);
}

// get data
$entries = $paginator->getQuery()->getResult();
$entryList = array();
foreach ($entries as $entry) {
$flatEntry = $entry->toArray();
unset($flatEntry['class']);
$flatEntry['date'] = $entry->getDay() ? $entry->getDay()->format('Y-m-d') : null;

// add id suffix to if parameter
$flatEntry['user_id'] = $flatEntry['user'];
$flatEntry['project_id'] = $flatEntry['project'];
$flatEntry['customer_id'] = $flatEntry['customer'];
$flatEntry['activity_id'] = $flatEntry['activity'];
$flatEntry['worklog_id'] = $flatEntry['worklog'];
// unset old keys
unset($flatEntry['user']);
unset($flatEntry['project']);
unset($flatEntry['customer']);
unset($flatEntry['activity']);
unset($flatEntry['worklog']);

// build result
$entryList[] = $flatEntry;
}

// build url
parse_str($request->getQueryString(), $queryString);
unset($queryString['page']);
$queryString = '?' . http_build_query($queryString);
$route = $request->getUriForPath($request->getPathInfo()). $queryString;

// negative firstResult are interpreted as 0
$total = $paginator->count();

//self
$self = $route . '&' . http_build_query(['page' => $page]);

// returns null for empty Paginator, else returns last page for given $maxResults
$lastPage = ceil($total / $maxResults) - 1;
$last = $total
? $route . '&' . http_build_query(['page' => $lastPage])
: null;

// returns the last previous page with data, or null if you are on page 0 or there is no data
$prev = $page && $total
? $route . '&' . http_build_query(['page' => min($page - 1, $lastPage)])
: null;

//null when query would return empty data
$next = $page < $lastPage
? $route . '&' . http_build_query(['page' => $page + 1])
: null;

$links = [
'links' => [
'self' => $self,
'last' => $last,
'prev' => $prev,
'next' => $next,
],
];
$entryList = array_merge($links, ['data' => $entryList]);
return new JsonResponse($entryList);
}
}
41 changes: 35 additions & 6 deletions src/Netresearch/TimeTrackerBundle/Repository/EntryRepository.php
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ public function getEntriesByUser($userId, $days = 3, $showFuture = true)
return $data;
}


/**
* Get array of entries of given user and ticketsystem which should be synced to the ticketsystem.
* Ordered by date, starttime desc
Expand Down Expand Up @@ -486,9 +485,8 @@ public function getWorkByUser($userId, $period = self::PERIOD_DAY)
return $data;
}


/**
* Get array of entries for given filter params
* Get query of entries for given filter params
*
* @param array $arFilter every value is optional
*
Expand All @@ -504,10 +502,10 @@ public function getWorkByUser($userId, $period = self::PERIOD_DAY)
* [maxResults] => int max number of returned datasets
* [visibility_user] => user_id restricts entry visibility by users teams
*
* @return array
* @return \Doctrine\ORM\Query
* @throws \Exception
*/
public function findByFilterArray($arFilter = [])
public function queryByFilterArray($arFilter = [])
{
$queryBuilder = $this->createQueryBuilder('e');

Expand Down Expand Up @@ -573,13 +571,44 @@ public function findByFilterArray($arFilter = [])
->setMaxResults((int) $arFilter['maxResults']);
}

//pagination offset
if (isset($arFilter['page']) && isset($arFilter['maxResults'])) {
$queryBuilder
->setFirstResult((int) $arFilter['page'] * $arFilter['maxResults']);
}

if (isset($arFilter['visibility_user']) && !is_null($arFilter['visibility_user'])) {
$queryBuilder
->andWhere('e.user = :vis_user')
->setParameter('vis_user', (int) $arFilter['visibility_user']);
}

return $queryBuilder->getQuery()->getResult();
return $queryBuilder->getQuery();
}

/**
* Get array of entries for given filter params
*
* @param array $arFilter every value is optional
*
* $arFilter[customer] => int customer_id
* [project] => int project_id
* [user] => int user_id
* [activity] => int activity_id
* [team] => int team_id
* [datestart] => string
* [dateend] => string
* [ticket] => string
* [description] => string
* [maxResults] => int max number of returned datasets
* [visibility_user] => user_id restricts entry visibility by users teams
*
* @return array
* @throws \Exception
*/
public function findByFilterArray($arFilter = [])
{
return $this->queryByFilterArray($arFilter)->getResult();
}

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,10 @@ interpretation_entries:
path: /interpretation/entries
defaults: { _controller: NetresearchTimeTrackerBundle:Interpretation:getLastEntries }

interpretation_all_entries:
path: /interpretation/allEntries
defaults: { _controller: NetresearchTimeTrackerBundle:Interpretation:getAllEntries }

timetracking_save:
path: /tracking/save
defaults: { _controller: NetresearchTimeTrackerBundle:Crud:save }
Expand Down
Loading

0 comments on commit fda6423

Please sign in to comment.