/**
  * Extracts the WidgetContext from the given $httpRequest.
  * If the request contains an argument "__widgetId" the context is fetched from the session (AjaxWidgetContextHolder).
  * Otherwise the argument "__widgetContext" is expected to contain the serialized WidgetContext (protected by a HMAC suffix)
  *
  * @param Request $httpRequest
  * @return WidgetContext
  */
 protected function extractWidgetContext(Request $httpRequest)
 {
     if ($httpRequest->hasArgument('__widgetId')) {
         return $this->ajaxWidgetContextHolder->get($httpRequest->getArgument('__widgetId'));
     } elseif ($httpRequest->hasArgument('__widgetContext')) {
         $serializedWidgetContextWithHmac = $httpRequest->getArgument('__widgetContext');
         $serializedWidgetContext = $this->hashService->validateAndStripHmac($serializedWidgetContextWithHmac);
         return unserialize(base64_decode($serializedWidgetContext));
     }
     return null;
 }
 /**
  * Get array of node selection properties
  *
  *
  *
  * @param Node $node
  * @return array
  */
 public function prepareNodeSelectionFromNode(Node $node)
 {
     foreach ($this->contentContextFactory->getInstances() as $context) {
         $this->workspace = $context->getWorkspace();
         break;
     }
     $limit = false;
     $limit_param_name = false;
     $offset = false;
     $offset_param_name = false;
     $filter = array();
     $sort = array();
     $nodetype = false;
     $nodetypeisabstract = false;
     $entryNodes = array();
     $nodeParentPath = $node->getParentPath();
     if ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')) {
         // calculate nodetype name
         if ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes') && array_key_exists('nodeType', $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes'))) {
             foreach ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['nodeType'] as $key => $value) {
                 switch ($key) {
                     case 'property':
                         if ($node->getProperty($value)) {
                             $nodetype = $node->getProperty($value);
                         }
                         break;
                     case 'value':
                         $nodetype = $value;
                         break;
                     case 'param':
                         if ($this->httpRequest->hasArgument($value) || $nodetype == false) {
                             $nodetype = addslashes($this->httpRequest->getArgument($value));
                         }
                         break;
                     case 'abstract':
                         $nodetypeisabstract = TRUE;
                         break;
                     default:
                         break;
                 }
             }
         } else {
             throw new IndexedNodesException($node->getNodeData()->getNodeType()->getName() . ' has no nodeType definition.');
         }
         // calculate limit
         if ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes') && array_key_exists('limit', $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes'))) {
             foreach ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['limit'] as $key => $value) {
                 switch ($key) {
                     case 'property':
                         if ($node->getProperty($value)) {
                             $limit = $node->getProperty($value);
                         }
                         break;
                     case 'value':
                         $limit = $value;
                         break;
                     case 'param':
                         if ($this->httpRequest->hasArgument($value) || $limit == false) {
                             $limit = addslashes($this->httpRequest->getArgument($value));
                         }
                         $limit_param_name = $value;
                         if (strlen($limit) == 0) {
                             $limit = false;
                         }
                         break;
                     default:
                         break;
                 }
             }
         }
         if (!$limit) {
             // fetch default limit from internal params
             $value = "_limit-" . $node->getIdentifier();
             if ($this->httpRequest->hasArgument($value)) {
                 $limit = addslashes($this->httpRequest->getArgument($value));
             }
             if (!$limit_param_name) {
                 $limit_param_name = $value;
             }
         }
         // calculate limit offset, if limit isset
         if ($limit && $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes') && array_key_exists('offset', $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes'))) {
             foreach ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['offset'] as $key => $value) {
                 switch ($key) {
                     case 'property':
                         if ($node->getProperty($value)) {
                             $offset = $node->getProperty($value);
                         }
                         break;
                     case 'value':
                         $offset = $value;
                         break;
                     case 'param':
                         if ($this->httpRequest->hasArgument($value) || $offset == false) {
                             $offset = addslashes($this->httpRequest->getArgument($value));
                         }
                         $offset_param_name = $value;
                         if (strlen($offset) == 0) {
                             $offset = false;
                         }
                         break;
                     default:
                         break;
                 }
             }
         }
         if (!$offset) {
             // fetch default offset from internal params
             $value = "_offset-" . $node->getIdentifier();
             if ($this->httpRequest->hasArgument($value)) {
                 $offset = addslashes($this->httpRequest->getArgument($value));
             }
             if (!$offset_param_name) {
                 $offset_param_name = $value;
             }
             if (strlen($offset) == 0) {
                 $offset = 0;
             }
         }
         // calculate filters
         if ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes') && $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['filter']) {
             foreach ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['filter'] as $property => $arguments) {
                 foreach ($arguments as $arg => $filterValues) {
                     switch ($arg) {
                         case 'type':
                             $filter[$property]['type'] = $filterValues;
                             break;
                         case 'operand':
                             foreach ($filterValues as $key => $value) {
                                 switch ($key) {
                                     case 'property':
                                         if ($node->getProperty($value)) {
                                             $filter[$property]['operand'] = $node->getProperty($value);
                                         }
                                         break;
                                     case 'value':
                                         $filter[$property]['operand'] = $value;
                                         break;
                                     case 'param':
                                         if ($this->httpRequest->hasArgument($value) || isset($filter[$property]['operand']) == false) {
                                             $filter[$property]['operand'] = addslashes($this->httpRequest->getArgument($value));
                                         }
                                         break;
                                     default:
                                         break;
                                 }
                             }
                             break;
                         case 'operator':
                             foreach ($filterValues as $key => $value) {
                                 switch ($key) {
                                     case 'property':
                                         if ($node->getProperty($value)) {
                                             $filter[$property]['operator'] = $node->getProperty($value);
                                         }
                                         break;
                                     case 'value':
                                         $filter[$property]['operator'] = $value;
                                         break;
                                     case 'param':
                                         if ($this->httpRequest->hasArgument($value) || isset($filter[$property]['operator']) == false) {
                                             $filter[$property]['operator'] = addslashes($this->httpRequest->getArgument($value));
                                         }
                                         break;
                                     default:
                                         break;
                                 }
                             }
                             break;
                     }
                 }
                 if (isset($filter[$property]['type']) == false) {
                     $targetNodeType = $this->nodeTypeManager->getNodeType($nodetype);
                     // get sorting type by property definition
                     if (isset($targetNodeType->getConfiguration('properties')['text'])) {
                         $filter[$property]['type'] = $targetNodeType->getConfiguration('properties')['text']['type'];
                     } else {
                         $filter[$property]['type'] = 'string';
                     }
                 }
             }
         }
         // calculate entry nodes
         if ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes') && array_key_exists('entryNodes', $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes'))) {
             foreach ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['entryNodes'] as $property => $filterValues) {
                 foreach ($filterValues as $key => $value) {
                     switch ($key) {
                         case 'property':
                             if ($node->getProperty($value)) {
                                 $entryNodes[$property]['value'] = $node->getProperty($value);
                             }
                             break;
                         case 'value':
                             $entryNodes[$property]['value'] = $value;
                             break;
                         case 'param':
                             if ($this->httpRequest->hasArgument($value) || isset($entryNodes[$property]['value']) == false) {
                                 $entryNodes[$property]['value'] = addslashes($this->httpRequest->getArgument($value));
                             }
                             break;
                         case 'recursive':
                             $entryNodes[$property]['recursive'] = $value;
                             break;
                         case 'childNodePath':
                             $entryNodes[$property]['childNodePath'] = $value;
                             break;
                     }
                     if (isset($entryNodes[$property]['recursive']) == false) {
                         $entryNodes[$property]['recursive'] = TRUE;
                     }
                     if (isset($entryNodes[$property]['childNodePath']) == false) {
                         $entryNodes[$property]['childNodePath'] = FALSE;
                     }
                     if (isset($entryNodes[$property]['value']) && is_array($entryNodes[$property]['value']) == false) {
                         $targetNode = $this->nodeDataRepository->findOneByIdentifier($entryNodes[$property]['value'], $this->workspace);
                         if ($targetNode) {
                             $entryNodes[$property]['path'] = $targetNode->getParentPath();
                         }
                     }
                 }
                 if (isset($entryNodes[$property]['value']) && is_array($entryNodes[$property]['value'])) {
                     $t = $entryNodes[$property];
                     unset($entryNodes[$property]);
                     foreach ($t['value'] as $key => $val) {
                         $entryNodes[$property . $key] = array('path' => $val->getPath(), 'childNodePath' => $t['childNodePath'], 'parentPath' => $val->getParentPath(), 'childNodes' => $val->getNodeType()->getChildNodes(), 'recursive' => $t['recursive']);
                     }
                 }
             }
         } else {
             // set reference to self node
             $entryNodes['self'] = array('path' => '/');
         }
         // calculate sorting
         if ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes') && array_key_exists('sort', $node->getNodeData()->getNodeType()->getConfiguration('indexedNodes'))) {
             foreach ($node->getNodeData()->getNodeType()->getConfiguration('indexedNodes')['sort'] as $nullkey => $sortValues) {
                 foreach ($sortValues as $key => $value) {
                     switch ($key) {
                         case 'property':
                             if ($node->getProperty($value)) {
                                 $sort[$property]['value'] = $node->getProperty($value);
                             }
                             break;
                         case 'value':
                             $sort[$property]['value'] = $value;
                             break;
                         case 'param':
                             if ($this->httpRequest->hasArgument($value) || isset($sort[$property]) == false) {
                                 $sort[$property]['value'] = addslashes($this->httpRequest->getArgument($value));
                             }
                             break;
                         case 'type':
                             $sort[$property]['type'] = $value;
                             break;
                         case 'direction':
                             foreach ($value as $k => $v) {
                                 switch ($k) {
                                     case 'property':
                                         if ($node->getProperty($v)) {
                                             $sort[$property]['direction'] = $node->getProperty($v);
                                         }
                                         break;
                                     case 'value':
                                         $sort[$property]['direction'] = $v;
                                         break;
                                     case 'param':
                                         if ($this->httpRequest->hasArgument($v) || isset($sort[$property]['direction']) == false) {
                                             $sort[$property]['direction'] = addslashes($this->httpRequest->getArgument($v));
                                         }
                                         break;
                                 }
                             }
                             break;
                         default:
                             break;
                     }
                     if (isset($sort[$property]['type']) == false) {
                         $targetNodeType = $this->nodeTypeManager->getNodeType($nodetype);
                         // get sorting type by property definition
                         if (isset($targetNodeType->getConfiguration('properties')['text'])) {
                             $sort[$property]['type'] = $targetNodeType->getConfiguration('properties')['text']['type'];
                         } else {
                             $sort[$property]['type'] = 'string';
                         }
                     }
                 }
             }
         }
     }
     return array('limit' => $limit, 'limit_param_name' => $limit_param_name, 'offset' => $offset, 'offset_param_name' => $offset_param_name, 'filter' => $filter, 'sort' => $sort, 'nodetype' => $nodetype, 'nodetypeisabstract' => $nodetypeisabstract, 'entryNodes' => $entryNodes, 'workspace' => $this->workspace);
 }