/**
  * <p> Retrieve the markers to display on Google Maps and the center position
  * if no items are returned for the Current Project Task Locations</p>
  * <p> This method is called via an AJAX request from the client side. </p>
  * <p> The method receive by inheritance some post data
  * properties JSON object which includes objectTypes as "keys" and nested associative propNames
  * example:
  * "properties": {
  *  "location_obj": {
  *      "objectLatPropName": "location_lat",
  *      "objectLngPropName": "location_long",
  *      "objectActivePropName": "location_active"
  *      }
  *   }
  * For more info:
  * \Applications\PMTool\Helpers\MapHelper::CreateTaskLocationMarkerItems
  * @link
  * </p>
  * <p>
  * @param object $rq <p>
  * The current HttpRequest.
  * </p>
  * @return mixed $result <p>
  * The result is a standard JSON response containing
  * the specific data to the request, e.g.:
  *  - the list markers for a given filter
  *  - the default position used to center the map (this configured in the appsettings.xml)
  */
 public function executeListCurrentProjectTasks(\Library\HttpRequest $rq)
 {
     $result = $this->InitResponseWS();
     $dataPost = $this->dataPost();
     $properties = json_decode($dataPost['properties'], true);
     //get facility location info
     $defaultLocationProperties = $properties['defaultLocation'];
     //unset default location because we don't want to show facility marker
     unset($properties['defaultLocation']);
     //get current sesion project and refresh project's locations then get current session task
     $sessionProject = \Applications\PMTool\Helpers\ProjectHelper::GetCurrentSessionProject($this->app()->user());
     //get project_id and load locations from DB
     $project_id = $sessionProject[\Library\Enums\SessionKeys::ProjectObject]->project_id();
     $sessionProject[\Library\Enums\SessionKeys::ProjectLocations] = $locations = \Applications\PMTool\Helpers\LocationHelper::GetLocationListFromDB($this, $project_id);
     //store updated session
     \Applications\PMTool\Helpers\ProjectHelper::SetUserSessionProject($this->user(), $sessionProject);
     $sessionProject[\Library\Enums\SessionKeys::ProjectLocations] = $this->_GetAndStoreLocationsInSession($sessionProject);
     $sessionTask = \Applications\PMTool\Helpers\TaskHelper::GetCurrentSessionTask($this->app()->user());
     //create two arrays with current project's locations, one for locations linked with the task and other unlinked
     $taskLocations = \Applications\PMTool\Helpers\LocationHelper::GetAndStoreTaskLocations($this, $sessionTask);
     $projectLocations = \Applications\PMTool\Helpers\LocationHelper::GetProjectLocations($this, $sessionProject, $taskLocations);
     $locations = array(\Library\Enums\SessionKeys::TaskLocations => $taskLocations, \Library\Enums\SessionKeys::ProjectLocations => $projectLocations);
     //load marker icons from config
     $icons = \Applications\PMTool\Helpers\MapHelper::GetActiveInactiveIcons($this->app()->relative_path, $this->app()->imageUtil, $this->app()->config());
     if (isset($properties['activeTask']) && $properties['activeTask']) {
         $activeTask = $result["activeTask"] = true;
     } else {
         $activeTask = $result["activeTask"] = false;
     }
     //create google maps marker items
     $projectLocationMarkers = \Applications\PMTool\Helpers\MapHelper::CreateTaskLocationMarkerItems($this, $locations, $properties, $icons, $activeTask);
     $result["noLatLngIcon"] = $icons["taskNoLatLng"];
     $result["activeIcon"] = $icons["task"];
     $result["inactiveIcon"] = $icons["task"];
     $result["items"] = $projectLocationMarkers;
     $result["defaultPosition"] = \Applications\PMTool\Helpers\MapHelper::GetCoordinatesToCenterOverARegion($this->app()->config());
     $result["boundary"] = \Applications\PMTool\Helpers\MapHelper::GetBoundary($sessionProject);
     $result["facility_id"] = $sessionProject[\Library\Enums\SessionKeys::FacilityObject]->facility_id();
     $result["project_id"] = $sessionProject[\Library\Enums\SessionKeys::FacilityObject]->project_id();
     $result["type"] = 'task';
     $noCoordinateMarkers = count(array_filter($projectLocationMarkers, function ($item) {
         return !$item['noLatLng'];
     }));
     if ($noCoordinateMarkers == 0) {
         $defaultLocations = \Applications\PMTool\Helpers\MapHelper::BuildLatAndLongCoordFromGeoObjects(array(\Applications\PMTool\Helpers\CommonHelper::GetValueFromArrayByKey($sessionProject, $defaultLocationProperties['object'])), $defaultLocationProperties['objectLatPropName'], $defaultLocationProperties['objectLngPropName']);
         if (count($defaultLocations) > 0) {
             $result['defaultPosition'] = $defaultLocations[0];
         }
     }
     $result["controls"] = array("markers" => true, "shapes" => true, "ruler" => true);
     $this->SendResponseWS($result, array("resx_file" => \Applications\PMTool\Resources\Enums\ResxFileNameKeys::Map, "resx_key" => $this->action(), "step" => count($projectLocationMarkers) >= 0 ? "success" : "error"));
 }
 /**
  * <p> Retrieve complete marker items based on latitude, longitude and active properties of Facility and Project objects
  * </p>
  * <p> Build as an output an associative array in the Google Maps API format
  * </p>
  * @param array $sessionProjects <p>
  * The array of session projects with both project object and facility object
  * </p>
  * @param string $properties <p>
  * Associative object->property list
  * @return array $markers <p>
  * The array with location info and nested array "marker" in Google Maps API format
  * </p>
  */
 public static function CreateFacilityMarkerItems($sessionProjects, $properties, $icons)
 {
     $markers = array();
     foreach ($sessionProjects as $project) {
         $marker = array();
         foreach ($properties as $objectType => $objectProperties) {
             $currentObject = \Applications\PMTool\Helpers\CommonHelper::GetValueFromArrayByKey($project, $objectType);
             if (isset($objectProperties["objectLatPropName"]) && isset($objectProperties["objectLngPropName"]) && self::CheckCoordinateValue($currentObject->{$objectProperties}["objectLatPropName"]()) && self::CheckCoordinateValue($currentObject->{$objectProperties}["objectLngPropName"]())) {
                 $marker["marker"]["lat"] = $currentObject->{$objectProperties}["objectLatPropName"]();
                 $marker["marker"]["lng"] = $currentObject->{$objectProperties}["objectLngPropName"]();
                 $marker["id"] = $currentObject->{$objectProperties}["objectIdPropName"]();
                 $marker["name"] = $currentObject->{$objectProperties}["objectNamePropName"]();
             } else {
                 if (isset($objectProperties["objectLatPropName"]) && isset($objectProperties["objectLngPropName"]) && self::CheckCoordinateValue($currentObject->{$objectProperties}["objectLatPropName"]()) === false && self::CheckCoordinateValue($currentObject->{$objectProperties}["objectLngPropName"]()) === false) {
                     $marker["noLatLng"] = true;
                     $marker["id"] = $currentObject->{$objectProperties}["objectIdPropName"]();
                     $marker["name"] = $currentObject->{$objectProperties}["objectNamePropName"]();
                 } else {
                     if (isset($objectProperties["objectActivePropName"])) {
                         $marker["marker"]["icon"] = $currentObject->{$objectProperties}["objectActivePropName"]() ? $icons["projectActive"] : $icons["projectInactive"];
                         $marker["active"] = $currentObject->{$objectProperties}["objectActivePropName"]();
                     }
                 }
             }
         }
         if (!isset($marker["marker"]["lat"]) && !isset($marker["marker"]["lng"])) {
             unset($marker["marker"]);
         }
         $markers[] = $marker;
     }
     usort($markers, function ($elem1, $elem2) {
         return strcmp($elem1['name'], $elem2['name']);
     });
     return $markers;
 }