2024-01-04 19:56:11 +01:00
|
|
|
<?php
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Common functions for the 3 analyst objects
|
|
|
|
*/
|
|
|
|
class AnalystDataParentBehavior extends ModelBehavior
|
|
|
|
{
|
|
|
|
private $__currentUser = null;
|
|
|
|
|
|
|
|
public $User;
|
|
|
|
|
|
|
|
|
|
|
|
|
2024-02-06 10:04:20 +01:00
|
|
|
public function attachAnalystData(Model $model, array $object, array $types = ['Note', 'Opinion', 'Relationship'])
|
2024-01-04 19:56:11 +01:00
|
|
|
{
|
|
|
|
// No uuid, nothing to attach
|
|
|
|
if (empty($object['uuid'])) {
|
|
|
|
return $object;
|
|
|
|
}
|
|
|
|
if (empty($this->__currentUser)) {
|
|
|
|
$user_id = Configure::read('CurrentUserId');
|
|
|
|
$this->User = ClassRegistry::init('User');
|
|
|
|
if ($user_id) {
|
|
|
|
$this->__currentUser = $this->User->getAuthUser($user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$data = [];
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$this->{$type} = ClassRegistry::init($type);
|
2024-02-13 11:13:39 +01:00
|
|
|
$this->{$type}->fetchRecursive = !empty($model->includeAnalystDataRecursive);
|
2024-01-04 19:56:11 +01:00
|
|
|
$temp = $this->{$type}->fetchForUuid($object['uuid'], $this->__currentUser);
|
|
|
|
if (!empty($temp)) {
|
|
|
|
foreach ($temp as $k => $temp_element) {
|
|
|
|
$data[$type][] = $temp_element[$type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $data;
|
|
|
|
}
|
2024-01-29 12:00:13 +01:00
|
|
|
|
2024-03-15 08:41:55 +01:00
|
|
|
public function attachAnalystDataBulk(Model $model, array $objects, array $types = ['Note', 'Opinion', 'Relationship'])
|
|
|
|
{
|
|
|
|
$uuids = [];
|
|
|
|
$objects = array_chunk($objects, 10000);
|
|
|
|
if (empty($this->__currentUser)) {
|
|
|
|
$user_id = Configure::read('CurrentUserId');
|
|
|
|
$this->User = ClassRegistry::init('User');
|
|
|
|
if ($user_id) {
|
|
|
|
$this->__currentUser = $this->User->getAuthUser($user_id);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
foreach ($objects as $chunk => $chunked_objects) {
|
|
|
|
foreach ($chunked_objects as $k => $object) {
|
|
|
|
if (!empty($object['uuid'])) {
|
|
|
|
$uuids[] = $object['uuid'];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// No uuids, nothing to attach
|
|
|
|
if (empty($uuids)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
foreach ($types as $type) {
|
|
|
|
$this->{$type} = ClassRegistry::init($type);
|
|
|
|
$this->{$type}->fetchRecursive = !empty($model->includeAnalystDataRecursive);
|
|
|
|
$temp = $this->{$type}->fetchForUuids($uuids, $this->__currentUser);
|
|
|
|
if (!empty($temp)) {
|
|
|
|
foreach ($chunked_objects as $k => $object) {
|
|
|
|
if (!empty($temp[$object['uuid']])) {
|
|
|
|
$objects[$chunk][$k][$type][] = $temp[$object['uuid']][$type];
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
$objects = call_user_func_array('array_merge', $objects);
|
|
|
|
return $objects;
|
|
|
|
}
|
|
|
|
|
2024-01-04 19:56:11 +01:00
|
|
|
public function afterFind(Model $model, $results, $primary = false)
|
|
|
|
{
|
|
|
|
if (!empty($model->includeAnalystData)) {
|
|
|
|
foreach ($results as $k => $item) {
|
|
|
|
if (isset($item[$model->alias])) {
|
2024-01-24 21:48:53 +01:00
|
|
|
$results[$k] = array_merge($results[$k], $this->attachAnalystData($model, $item[$model->alias]));
|
2024-01-04 19:56:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return $results;
|
|
|
|
}
|
2024-01-29 12:00:13 +01:00
|
|
|
|
2024-01-04 19:56:11 +01:00
|
|
|
}
|