diff --git a/app/Controller/EventsController.php b/app/Controller/EventsController.php index 859768a87..9a7cfbd57 100644 --- a/app/Controller/EventsController.php +++ b/app/Controller/EventsController.php @@ -4357,16 +4357,17 @@ class EventsController extends AppController return new CakeResponse(array('body' => json_encode($json), 'status' => 200, 'type' => 'json')); } - private function genDistributionGraph($id, $type = 'event', $extended = 0) { + private function genDistributionGraph($id, $type = 'event', $extended = 0) + { $validTools = array('event'); if (!in_array($type, $validTools)) { throw new MethodNotAllowedException(__('Invalid type.')); } - $this->loadModel('Server'); - $this->loadModel('Organisation'); + App::uses('DistributionGraphTool', 'Tools'); $grapher = new DistributionGraphTool(); + $this->loadModel('Server'); $servers = $this->Server->find('list', array( 'fields' => array('name'), )); diff --git a/app/Lib/Tools/DistributionGraphTool.php b/app/Lib/Tools/DistributionGraphTool.php index ced65fa45..44a7a628a 100644 --- a/app/Lib/Tools/DistributionGraphTool.php +++ b/app/Lib/Tools/DistributionGraphTool.php @@ -1,223 +1,221 @@ __eventModel = $eventModel; + $this->__serverList = $servers; + $this->__organisationModel = $eventModel->Orgc; + $this->__user = $user; + $this->__json = array(); + $this->__extended_view = $extended_view; - public function construct($eventModel, $servers, $user, $extended_view=0) - { - $this->__eventModel = $eventModel; - $this->__serverList = $servers; - $this->__organisationModel = $eventModel->Orgc; - $this->__user = $user; - $this->__json = array(); - $this->__extended_view = $extended_view; + // construct distribution info + $this->__json['distributionInfo'] = array(); + $sgs = $this->__eventModel->SharingGroup->fetchAllAuthorised($this->__user, 'simplified', 1); + $this->__json['allSharingGroup'] = h($sgs); + $distributionLevels = $this->__eventModel->distributionLevels; + foreach ($distributionLevels as $key => $value) { + $this->__json['distributionInfo'][$key] = array('key' => h($value), 'desc' => h($this->__eventModel->distributionDescriptions[$key]['formdesc']), 'value' => h($key)); + } + $this->__json['distributionInfo'][5] = ""; // inherit event. Will be deleted afterward - // construct distribution info - $this->__json['distributionInfo'] = array(); - $sgs = $this->__eventModel->SharingGroup->fetchAllAuthorised($this->__user, 'simplified', 1); - $this->__json['allSharingGroup'] = h($sgs); - $distributionLevels = $this->__eventModel->distributionLevels; - foreach ($distributionLevels as $key => $value) { - $this->__json['distributionInfo'][$key] = array('key' => h($value), 'desc' => h($this->__eventModel->distributionDescriptions[$key]['formdesc']), 'value' => h($key)); + return true; + } + + private function __fetchAndAddDistributionInfo($elem) + { + $distributionLevel = $elem['distribution']; + + if ($distributionLevel == 5) { // inherit -> convert it to the event distribution level + $elem['distribution'] = $this->__eventDistribution; + $this->__fetchAndAddDistributionInfo($elem); + } elseif ($distributionLevel == 4) { // sharing group + if (isset($elem['SharingGroup'])) { + $sg_name = $elem['SharingGroup']['name']; + $this->__addAdditionalDistributionInfo($distributionLevel, $sg_name); + } elseif ($this->__eventDistribution == 4) { // event is distributed for sg + $sg_name = $this->__eventSharingGroupName; + $this->__addAdditionalDistributionInfo($distributionLevel, $sg_name); } - $this->__json['distributionInfo'][5] = ""; // inherit event. Will be deleted afterward + } else { + return false; + } + return true; + } - return true; + private function __addAdditionalDistributionInfo($distributionLevel, $data) + { + if (empty($this->__json['additionalDistributionInfo'][$distributionLevel])) { + $this->__json['additionalDistributionInfo'][$distributionLevel] = array(); + } + $this->__json['additionalDistributionInfo'][$distributionLevel][h($data)] = 0; // set-alike + if ($distributionLevel == 4) { + if (!isset($this->__json['sharingGroupRepartition'][h($data)])) { + $this->__json['sharingGroupRepartition'][h($data)] = 0; + } + $this->__json['sharingGroupRepartition'][h($data)]++; + } + } + + private function __addOtherDistributionInfo() + { + // all comm + $this->__addAdditionalDistributionInfo(3, "This community"); // add current community + $this->__addAdditionalDistributionInfo(3, "All other communities"); // add current community + + // connected + $servers = $this->__serverList; + $this->__addAdditionalDistributionInfo(2, "This community"); // add current community + foreach ($servers as $server) { + $this->__addAdditionalDistributionInfo(2, $server); } - private function __extract_sharing_groups_names($sharingArray) - { - return $sharingArray['name']; - } - - private function __fetchAndAddDistributionInfo($elem) - { - $distributionLevel = $elem['distribution']; - - if ($distributionLevel == 5) { // inherit -> convert it to the event distribution level - $elem['distribution'] = $this->__eventDistribution; - $this->__fetchAndAddDistributionInfo($elem); - } elseif ($distributionLevel == 4) { // sharing group - if (isset($elem['SharingGroup'])) { - $sg_name = $this->__extract_sharing_groups_names($elem['SharingGroup']); - $this->__addAdditionalDistributionInfo($distributionLevel, $sg_name); - } elseif ($this->__eventDistribution == 4) { // event is distributed for sg - $sg_name = $this->__eventSharingGroupName; - $this->__addAdditionalDistributionInfo($distributionLevel, $sg_name); - } - } else { - return false; - } - return true; - } - - private function __addAdditionalDistributionInfo($distributionLevel, $data) - { - if (empty($this->__json['additionalDistributionInfo'][$distributionLevel])) { - $this->__json['additionalDistributionInfo'][$distributionLevel] = array(); - } - $this->__json['additionalDistributionInfo'][$distributionLevel][h($data)] = 0; // set-alike - if ($distributionLevel == 4) { - if (!isset($this->__json['sharingGroupRepartition'][h($data)])) { - $this->__json['sharingGroupRepartition'][h($data)] = 0; - } - $this->__json['sharingGroupRepartition'][h($data)]++; + // community + $orgs = $this->__organisationModel->find('list', array( + 'fields' => array('name'), + 'conditions' => array('local' => true) + )); + $thisOrg = $this->__user['Organisation']['name']; + $this->__addAdditionalDistributionInfo(1, $thisOrg); // add current community + foreach ($orgs as $org) { + if ($thisOrg != $org) { + $this->__addAdditionalDistributionInfo(1, $org); } } - private function __addOtherDistributionInfo() - { - // all comm - $this->__addAdditionalDistributionInfo(3, "This community"); // add current community - $this->__addAdditionalDistributionInfo(3, "All other communities"); // add current community - - // connected - $servers = $this->__serverList; - $this->__addAdditionalDistributionInfo(2, "This community"); // add current community - foreach ($servers as $server) { - $this->__addAdditionalDistributionInfo(2, $server); - } - - // community - $orgs = $this->__organisationModel->find('list', array( - 'fields' => array('name'), - 'conditions' => array('local' => true) - )); - $thisOrg = $this->__user['Organisation']['name']; - $this->__addAdditionalDistributionInfo(1, $thisOrg); // add current community - foreach ($orgs as $org) { - if ($thisOrg != $org) { - $this->__addAdditionalDistributionInfo(1, $org); - } - } - - // org only - $thisOrg = $this->__user['Organisation']['name']; - $this->__addAdditionalDistributionInfo(0, $thisOrg); // add current community - } - - private function __get_event($id) - { - $fullevent = $this->__eventModel->fetchEvent($this->__user, array('eventid' => $id, 'flatten' => 0, 'includeTagRelations' => 1, 'extended' => $this->__extended_view)); - $event = array(); - if (empty($fullevent)) { - return $event; - } - - $fullevent = $fullevent[0]; - if (!empty($fullevent['Object'])) { - $event['Object'] = $fullevent['Object']; - } else { - $event['Object'] = array(); - } - - if (!empty($fullevent['Attribute'])) { - $event['Attribute'] = $fullevent['Attribute']; - } else { - $event['Attribute'] = array(); - } - $event['distribution'] = $fullevent['Event']['distribution']; - - if (isset($fullevent['SharingGroup'])) { - $event['SharingGroupName'] = $fullevent['SharingGroup']['name']; - } else { - $event['SharingGroupName'] = "?"; - } + // org only + $thisOrg = $this->__user['Organisation']['name']; + $this->__addAdditionalDistributionInfo(0, $thisOrg); // add current community + } + /** + * Fetch event containing just 'Attribute', 'Object', 'SharingGroup' and 'distribution' + * @param int $id + * @return array + * @throws Exception + */ + private function __get_event($id) + { + $fullevent = $this->__eventModel->fetchEvent($this->__user, array( + 'eventid' => $id, + 'flatten' => 0, + 'noShadowAttributes' => true, + 'noEventReports' => true, + 'noSightings' => true, + 'extended' => $this->__extended_view, + )); + $event = array(); + if (empty($fullevent)) { return $event; } - public function get_distributions_graph($id) - { - $this->__json['event'] = $this->init_array_distri(); - $this->__json['attribute'] = $this->init_array_distri(); - $this->__json['object'] = $this->init_array_distri(); - $this->__json['obj_attr'] = $this->init_array_distri(); - $this->__json['additionalDistributionInfo'] = $this->init_array_distri(array()); - $this->__json['sharingGroupRepartition'] = array(); + $fullevent = $fullevent[0]; + if (isset($fullevent['Object'])) { + $event['Object'] = $fullevent['Object']; + } else { + $event['Object'] = array(); + } - $this->__addOtherDistributionInfo(); + if (isset($fullevent['Attribute'])) { + $event['Attribute'] = $fullevent['Attribute']; + } else { + $event['Attribute'] = array(); + } + $event['distribution'] = $fullevent['Event']['distribution']; - // transform set into array - foreach (array_keys($this->__json['additionalDistributionInfo']) as $d) { - $this->__json['additionalDistributionInfo'][$d] = array_keys($this->__json['additionalDistributionInfo'][$d]); - } + if (isset($fullevent['SharingGroup'])) { + $event['SharingGroupName'] = $fullevent['SharingGroup']['name']; + } else { + $event['SharingGroupName'] = "?"; + } - if ($id === -1) { - return $this->__json; - } - $event = $this->__get_event($id); - $eventDist = $event['distribution']; - $eventSGName = $event['SharingGroupName']; - $this->__eventDistribution = $eventDist; - $this->__eventSharingGroupName = $eventSGName; + return $event; + } - if (empty($event)) { - return $this->__json; - } + public function get_distributions_graph($id) + { + $this->__json['event'] = $this->init_array_distri(); + $this->__json['attribute'] = $this->init_array_distri(); + $this->__json['object'] = $this->init_array_distri(); + $this->__json['obj_attr'] = $this->init_array_distri(); + $this->__json['additionalDistributionInfo'] = $this->init_array_distri(array()); + $this->__json['sharingGroupRepartition'] = array(); - if (!empty($event['Object'])) { - $object = $event['Object']; - } else { - $object = array(); - } + $this->__addOtherDistributionInfo(); - if (!empty($event['Attribute'])) { - $attribute = $event['Attribute']; - } else { - $attribute = array(); - } - - // extract distribution - foreach ($attribute as $attr) { - $distri = $attr['distribution']; - $this->__json['event'][$distri] += 1; - $this->__json['attribute'][$distri] += 1; - $this->__fetchAndAddDistributionInfo($attr); - } - - foreach ($object as $obj) { - $distri = $obj['distribution']; - $this->__json['event'][$distri] += 1; - $this->__json['object'][$distri] += 1; - $this->__fetchAndAddDistributionInfo($obj); - - $added_value = array(); - if (!empty($obj['Attribute'])) { - foreach ($obj['Attribute'] as $objAttr) { - $distri = $objAttr['distribution']; - $this->__json['event'][$distri] += 1; - $this->__json['obj_attr'][$distri] += 1; - $this->__fetchAndAddDistributionInfo($objAttr); - } - } - } - // distribution 5 is inherit event, apply this fact on values - $this->__json['event'][$eventDist] += $this->__json['event'][5]; - unset($this->__json['event'][5]); - $this->__json['attribute'][$eventDist] += $this->__json['attribute'][5]; - unset($this->__json['attribute'][5]); - $this->__json['object'][$eventDist] += $this->__json['object'][5]; - unset($this->__json['object'][5]); - $this->__json['obj_attr'][$eventDist] += $this->__json['obj_attr'][5]; - unset($this->__json['obj_attr'][5]); - - unset($this->__json['distributionInfo'][5]); // inherit event. - - // transform set into array for SG (others are already done) - $this->__json['additionalDistributionInfo'][4] = array_keys($this->__json['additionalDistributionInfo'][4]); + // transform set into array + foreach (array_keys($this->__json['additionalDistributionInfo']) as $d) { + $this->__json['additionalDistributionInfo'][$d] = array_keys($this->__json['additionalDistributionInfo'][$d]); + } + if ($id === -1) { + return $this->__json; + } + $event = $this->__get_event($id); + if (empty($event)) { return $this->__json; } - public function init_array_distri($default=0) - { - $ret = array(); - foreach ($this->__json['distributionInfo'] as $d => $v) { - $ret[h($d)] = $default; - } - return $ret; + $eventDist = $event['distribution']; + $eventSGName = $event['SharingGroupName']; + $this->__eventDistribution = $eventDist; + $this->__eventSharingGroupName = $eventSGName; + + // extract distribution + foreach ($event['Attribute'] as $attr) { + $distri = $attr['distribution']; + $this->__json['event'][$distri] += 1; + $this->__json['attribute'][$distri] += 1; + $this->__fetchAndAddDistributionInfo($attr); } + + foreach ($event['Object'] as $obj) { + $distri = $obj['distribution']; + $this->__json['event'][$distri] += 1; + $this->__json['object'][$distri] += 1; + $this->__fetchAndAddDistributionInfo($obj); + + if (!empty($obj['Attribute'])) { + foreach ($obj['Attribute'] as $objAttr) { + $distri = $objAttr['distribution']; + $this->__json['event'][$distri] += 1; + $this->__json['obj_attr'][$distri] += 1; + $this->__fetchAndAddDistributionInfo($objAttr); + } + } + } + // distribution 5 is inherit event, apply this fact on values + $this->__json['event'][$eventDist] += $this->__json['event'][5]; + unset($this->__json['event'][5]); + $this->__json['attribute'][$eventDist] += $this->__json['attribute'][5]; + unset($this->__json['attribute'][5]); + $this->__json['object'][$eventDist] += $this->__json['object'][5]; + unset($this->__json['object'][5]); + $this->__json['obj_attr'][$eventDist] += $this->__json['obj_attr'][5]; + unset($this->__json['obj_attr'][5]); + + unset($this->__json['distributionInfo'][5]); // inherit event. + + // transform set into array for SG (others are already done) + $this->__json['additionalDistributionInfo'][4] = array_keys($this->__json['additionalDistributionInfo'][4]); + + return $this->__json; } + + public function init_array_distri($default=0) + { + $ret = array(); + foreach ($this->__json['distributionInfo'] as $d => $v) { + $ret[h($d)] = $default; + } + return $ret; + } +}