/**
  * @Route("/document")
  * @Route("/document/")
  */
 public function documentAction(Request $request)
 {
     if (Remote::isOptionsRequest()) {
         return Remote::getCORSSymfonyResponse();
     }
     $rowData = $this->cleanData(Remote::getRequestVariables());
     $secret = $request->get('identifier');
     $em = $this->getDoctrine()->getManager();
     /** @var Doc $doc */
     $doc = $em->getRepository('AppShedExtensionsSpreadsheetBundle:Doc')->findOneBy(['itemsecret' => $secret]);
     if (!$doc) {
         $screen = new Screen('Error');
         $screen->addChild(new HTML('You must setup the extension before using it'));
         return (new Remote($screen))->getSymfonyResponse();
     }
     try {
         $existingTitles = $this->getColumnTitles($doc->getKey());
         $store = false;
         foreach ($rowData as $titleName => $value) {
             if (!in_array($titleName, $existingTitles)) {
                 $store = true;
                 $this->addTitle($titleName, $doc->getKey());
                 $existingTitles[] = $titleName;
             }
         }
         if ($store) {
             $doc->setTitles(array_unique($existingTitles));
             $em->flush();
         }
         foreach ($existingTitles as $titleName) {
             if (!isset($rowData[$titleName])) {
                 $rowData[$titleName] = '';
             }
         }
         if (count($rowData) > 0) {
             $this->getSpreadsheets()->insertRow($rowData, $doc->getKey(), 1);
         }
     } catch (\Exception $e) {
         $screen = new Screen('Error');
         $screen->addChild(new HTML('There was an error storing'));
         $screen->addChild(new Text($e->getMessage()));
         $this->logger->error('Problem accessing a spreadsheet', ['exception' => $e, 'rowData' => $rowData]);
         return (new Remote($screen))->getSymfonyResponse();
     }
     $screen = new Screen('Saved');
     $screen->addChild(new HTML("Your record has been saved"));
     return (new Remote($screen))->getSymfonyResponse();
 }
 /**
  * @Route("/write", name="write")
  * @Method({"GET", "POST"})
  */
 public function appshedAction(Request $request)
 {
     $view = $this->getView($request);
     if (!$view->getId()) {
         $screen = new Screen("Error");
         $screen->addChild(new Text("You must setup the view first"));
         return (new Remote($screen))->getSymfonyResponse();
     }
     $store = $view->getStore();
     $data = Remote::getRequestVariables();
     $systemFields = $this->container->getParameter('data_system_fields');
     foreach (array_keys($data) as $key) {
         if (in_array($key, $systemFields)) {
             unset($data[$key]);
             continue;
         }
         if (is_array($data[$key])) {
             $data[$key] = '[' . implode(', ', $data[$key]) . ']';
         }
     }
     $cols = array_keys($data);
     if (!count($cols)) {
         $screen = new Screen("Error");
         $screen->addChild(new Text("You must send some data"));
         return (new Remote($screen))->getSymfonyResponse();
     }
     //Add any new columns to the store
     $newColumns = array_diff($cols, $store->getColumns());
     if (count($newColumns)) {
         $store->setColumns(array_merge($store->getColumns(), $newColumns));
     }
     //Create new data obj
     $dataO = new Data();
     $dataO->setColumns($cols);
     $dataO->setData($data);
     $dataO->setStore($store);
     $em = $this->getDoctrine()->getManager();
     $em->persist($dataO);
     $em->flush();
     $screen = new Screen($view->getTitle() ?: "Thank you");
     $screen->addChild(new Text($view->getMessage() ?: "Your entry has been saved"));
     return (new Remote($screen))->getSymfonyResponse();
 }