* @owner University of Latvia
* @since 29.06.2018
*
* @package Lu\LuContacts\Models
*/
class Person
{
/**
* Pre-set uri builder
*
* @var \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder
*/
private $uriBuilder;
/**
* Settings
*
* @var array
*/
private $settings;
/**
* Person ID
*
* @var
*/
private $personId;
/**
* Unit ID
*
* @var string
*/
private $unit_id;
/**
* Alphabet constructor.
*
* @param null $settings
*/
public function __construct($settings = null)
{
$this->settings = $settings;
}
/**
* Sets uri builder
*
* @param \TYPO3\CMS\Extbase\Mvc\Web\Routing\UriBuilder $uriBuilder
*/
public function setUriBuilder(UriBuilder $uriBuilder)
{
$this->uriBuilder = $uriBuilder;
}
/**
* Set unit id
*
* @param $unit_id
*/
public function setUnitParentId($unit_id)
{
$this->unit_id = $unit_id;
}
/**
* Filter persons array for list view
*
* @param $solr_persons
*
* @return array
*/
public function filterPersonList($solr_persons)
{
// Pre-define variables and models
$newPersons = [];
$sortOrder = [];
// Loop and create new persons array
foreach ($solr_persons as $item) {
$tmp = [
'id' => $item['id'],
'fullname' => $item['name'].' '.$item['surname'],
'email' => $item['email'],
'unit' => [],
];
// Add unit info
if ($item['units']['numFound']) {
foreach ($item['units']['docs'] as $entry) {
$tmp['unitNames'][$entry['id']] = $entry['name'];
}
// Use this check, to fetch person id sort
$sortOrder = $item['units']['docs'][0]['person_ids'];
}
// Add position and phone info
if ($item['_childDocuments_']) {
$k = 0;
foreach ($item['_childDocuments_'] as $children) {
$k++;
// Add positions
if ($children['type'] == 'position') {
$tmp['position'][$k] = $children['name'];
}
if ($children['unit_ids']) {
foreach ($children['unit_ids'] as $keyUnitId) {
if (key_exists($keyUnitId, $tmp['unitNames']) && !key_exists($keyUnitId, $tmp['unit'])) {
$tmp['unit'][$k][] = $tmp['unitNames'][$keyUnitId];
}
}
}
$tmp['unit'][$k] = $this->implode($tmp['unit'][$k]);
// Add phone numbers
if ($children['type'] == 'workplace') {
$tmp['phone'][$k] = $this->implode($children['phone']);
}
}
}
$tmp['unit'] = $this->implode($tmp['unit']);
// If uri builder provided, add urls
if ($this->uriBuilder) {
$arguments = [
'tx_lucontacts_pi1[action]' => 'person',
'tx_lucontacts_pi1[personId]' => $item['id'],
];
$tmp['url'] = $this->uriBuilder->setArguments($arguments)->buildFrontendUri();
}
// Flatten arrays
$tmp['position'] = $this->implode($tmp['position']);
$tmp['phone'] = $this->implode($tmp['phone']);
// Assign with person ID
$newPersons[$item['id']] = $tmp;
}
// Resort by unit person_ids field when unit ID is present
if ($this->unit_id && $sortOrder) {
$newPersons = $this->resort($sortOrder, $newPersons);
}
return array_values($newPersons);
}
/**
* Implode an array
*
* @param $value
* @param string $param
*
* @return string
*/
private function implode($value, $param = "
")
{
if (is_array($value)) {
return implode($param, $value);
} else {
return $value;
}
}
/**
* Resort array
*
* @param array $sortOrder
* @param array $newPersons
*
* @return array
*/
private function resort(array $sortOrder, array $newPersons): array
{
// Resort by sortOrder array
$newPersons = array_merge(array_flip($sortOrder), $newPersons);
// Remove values, that are not anm array
foreach ($newPersons as $key => $item) {
if (!is_array($item)) {
unset($newPersons[$key]);
}
}
// Return new and clean array
return $newPersons;
}
/**
* Set person ID
*
* @param string $personId
*/
public function setPersonId(string $personId)
{
$this->personId = $personId;
}
/**
* Fetch and return user data
*
* @return array
*/
public function getFullData()
{
// Pre-set models and values
$addresses = [];
$luisContacts = new LuisContacts();
$DataHelper = new DataHelper();
// Set request
$jsonData = $luisContacts->getPersonDetails($this->personId);
$details = $DataHelper->APIDecode($jsonData);
// Return if no data received
if (empty($details['data'])) {
return [];
} else {
$details = reset($details['data']);
}
// Set workplace
$workplaces = [];
foreach ($details['units']['docs'] as $unit) {
$fullName = [$unit['name']];
if (!empty($unit['parent_id'])) {
$UnitModel = new Unit();
$fullName = array_reverse($UnitModel->getRecursiveUnitNames($unit['id']));
}
$workplaces[$unit['id']] = [
'id' => $unit['id'],
'name' => $fullName,
'position' => '',
];
}
// Add child document info
foreach ($details['_childDocuments_'] as $child) {
switch ($child['type']) {
case "position":
foreach ($child['unit_ids'] as $unitId) {
if (empty($workplaces[$unitId]['position'])) {
$workplaces[$unitId]['position'] = [];
}
$workplaces[$unitId]['position'][] = $child['name'];
}
break;
case "workplace":
$addresses[$child['id']] = [
'address' => $child['address'],
'room' => $child['room'],
'phone' => $child['phone'] ? implode(",", $child['phone']) : '',
];
break;
}
}
// Reverse workplace position results
$workplaces[$unitId]['position'] = array_reverse($workplaces[$unitId]['position']);
// Build the user array
$user = [
'name' => $details['name'].' '.$details['surname'],
'workplaces' => array_values($workplaces),
'address' => array_values($addresses),
'email' => $details['email'],
'html' => $details['html'],
];
// Add away time
if (!empty($details['absence_from']) || !empty($details['absence_to'])) {
$awayTime = [];
if ($details['absence_from']) {
$tmpDate = new \DateTime($details['absence_from']);
$tmpDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$awayTime[] = $tmpDate->format('d.m.Y');
}
if ($details['absence_to']) {
$tmpDate = new \DateTime($details['absence_to']);
$tmpDate->setTimezone(new \DateTimeZone(date_default_timezone_get()));
$awayTime[] = $tmpDate->format('d.m.Y');
}
if (!empty($awayTime)) {
$user['away_time'] = implode(" - ", $awayTime);
}
}
// Add consultation
if (!empty($details['consultation'])) {
$user['consultation'] = $details['consultation'];
}
// Add photo
if (!empty($details['photo'])) {
$user['photo'] = $this->setUserPhoto(($details['photo']));
}
// Return result
return [
'section' => 'person',
'title' => 'SOME PERSON NAME',
'person' => $user,
'details' => $details,
];
}
/**
* Check if person is under required departeament or structure
*
* @param $personId
* @param $unitId
* @param bool $recursive
*
* @return bool
*/
public function validatePerson($personId, $unitId, $recursive = false): bool
{
// Pre-set models and values
$addresses = [];
$luisContacts = new LuisContacts();
$DataHelper = new DataHelper();
// Set request
$jsonData = $luisContacts->getPersonDetails($personId);
$details = $DataHelper->APIDecode($jsonData);
// Return if no data received
if (empty($details['data'])) {
return false;
} else {
$details = reset($details['data']);
}
// Fetch unit IDs foe person
foreach ($details['unit_ids'] as $userUnitId) {
if ($recursive) {
$UnitModel = new Unit();
$userUnits[] = array_reverse($UnitModel->getRecursiveUnitIds($userUnitId));
} else {
$userUnits[] = $userUnitId;
}
}
// Flatten into onel evel array
if (!empty($userUnits)) {
$userUnits = $this->array_flatten($userUnits);
}
// Return result
return in_array($unitId, $userUnits);
}
/**
* Return output image url
*
* @param $image
*
* @return string
*/
private function setUserPhoto($image)
{
return '
';
}
/**
* Flattens array.
*
* @param array $array
*
* @return array
*/
private function array_flatten(array $array)
{
$flat = []; // initialize return array
$stack = array_values($array); // initialize stack
while ($stack) // process stack until done
{
$value = array_shift($stack);
if (is_array($value)) // a value to further process
{
array_unshift($stack, ...$value);
} else // a value to take
{
$flat[] = $value;
}
}
return $flat;
}
}