コード例 #1
0
 function modelControllerExtension(ModelControllerInterface $modelController)
 {
     $request = $modelController->getRequest();
     $files = $request->getUploadedFiles();
     $uploads = [];
     // Check if extension is applicable to the current request.
     foreach ($files as $fieldName => $file) {
         if (str_endsWith($fieldName, ImageField::FILE_FIELD_SUFFIX)) {
             // Note: slashes are converted to dots, which delimit path segments to nested fields. See the Field component.
             $fieldName = str_replace('/', '.', str_segmentsStripLast($fieldName, '_'));
             $uploads[$fieldName] = $file;
         }
     }
     if ($uploads) {
         $modelController->onSave(-1, function () use($uploads, $modelController) {
             /** @var UploadedFileInterface $file */
             foreach ($uploads as $fieldName => $file) {
                 list($targetModel, $prop) = $modelController->getTarget($fieldName);
                 $err = $file->getError();
                 if ($err == UPLOAD_ERR_OK) {
                     static::newUpload($targetModel, $prop, $file);
                 } else {
                     if ($err == UPLOAD_ERR_NO_FILE) {
                         static::noUpload($targetModel, $prop);
                     } else {
                         throw new FlashMessageException("Error {$err}", FlashType::ERROR, "Error uploading file");
                     }
                 }
             }
         });
     }
 }
コード例 #2
0
 /**
  * Performs the main execution sequence.
  *
  * @param ServerRequestInterface $request
  * @param ResponseInterface      $response
  * @param callable               $next
  * @return ResponseInterface
  * @throws FatalException
  * @throws FileException
  * @throws FlashMessageException
  */
 function __invoke(ServerRequestInterface $request, ResponseInterface $response, callable $next)
 {
     if (!$this->kernelSettings) {
         throw new FatalException("Class <kbd class=type>" . get_class($this) . "</kbd>'s constructor forgot to call <kbd>parent::__construct()</kbd>");
     }
     $this->request = $request;
     $this->response = $response;
     $this->redirection->setRequest($request);
     $this->currentLink = $this->navigation->currentLink();
     $this->navigation->getCurrentTrail();
     if (!$this->indexPage && $this->autoRedirectUp && $this->currentLink && ($parent = $this->currentLink->parent())) {
         $this->indexPage = $parent->url();
     }
     // remove page number parameter
     $this->URI_noPage = preg_replace('#&?' . $this->kernelSettings->pageNumberParam . '=\\d*#', '', $this->request->getUri()->getPath());
     $this->URI_noPage = preg_replace('#\\?$#', '', $this->URI_noPage);
     $this->initialize();
     //custom setup
     $this->modelController->setRequest($request);
     $this->model();
     $this->modelController->handleRequest();
     $this->model = $this->modelController->getModel();
     switch ($this->request->getMethod()) {
         /** @noinspection PhpMissingBreakStatementInspection */
         case 'POST':
             // Perform the requested action.
             $res = $this->doFormAction();
             if ($res) {
                 if (!$res instanceof ResponseInterface) {
                     throw new FatalException(sprintf("Invalid HTTP response type: %s<p>Expected: <kbd>%s</kbd>", Debug::typeInfoOf($res), Debug::formatClassName(ResponseInterface::class)));
                 }
                 $response = $res;
             }
             if (!$this->renderOnAction) {
                 if (!$res) {
                     $response = $this->autoRedirect();
                 }
                 break;
             }
             // Fall through.
         // Fall through.
         case 'GET':
             // Render the component.
             $out = $this->getRendering();
             $response->getBody()->write($out);
     }
     $this->finalize($response);
     return $response;
 }