Exemple #1
0
 /**
  * Initialize form if the page has one. Also catches form processing if user posts the form.
  */
 public function onPagesInitialized()
 {
     $submitted = false;
     if ($this->isAdmin() && !empty($_POST)) {
         $page = $this->grav['page'];
         if (!$page) {
             return;
         }
         $header = $page->header();
         if (isset($header->form) && is_array($header->form)) {
             $this->active = true;
             // Create form
             $this->form = new Form($page);
             $this->enable(['onFormProcessed' => ['onFormProcessed', 0], 'onFormValidationError' => ['onFormValidationError', 0]]);
             $this->form->post();
         }
     } elseif ($this->forms) {
         $this->enable(['onTwigPageVariables' => ['onTwigVariables', 0], 'onTwigSiteVariables' => ['onTwigVariables', 0], 'onFormFieldTypes' => ['onFormFieldTypes', 0]]);
         // Regenerate list of flat_forms if not already populated
         if (empty($this->flat_forms)) {
             $this->flat_forms = Utils::arrayFlatten($this->forms);
         }
         // Save the current state of the forms to cache
         if ($this->recache_forms) {
             $this->grav['cache']->save($this->cache_id, [$this->forms, $this->flat_forms]);
         }
         $this->active = true;
         // Handle posting if needed.
         if (!empty($_POST)) {
             $this->enable(['onFormProcessed' => ['onFormProcessed', 0], 'onFormValidationError' => ['onFormValidationError', 0]]);
             $current_form_name = $this->getFormName($this->grav['page']);
             $this->json_response = [];
             if ($form = $this->getFormByName($current_form_name)) {
                 if ($this->grav['uri']->extension() === 'json') {
                     $this->json_response = $form->uploadFiles();
                 } else {
                     $form->post();
                     $submitted = true;
                 }
             }
         }
     }
     // Clear flash objects for previously uploaded files
     // whenever the user switches page / reloads
     // ignoring any JSON / extension call
     if (is_null($this->grav['uri']->extension()) && !$submitted) {
         // Discard any previously uploaded files session.
         // and if there were any uploaded file, remove them from the filesystem
         if ($flash = $this->grav['session']->getFlashObject('files-upload')) {
             $flash = new \RecursiveIteratorIterator(new \RecursiveArrayIterator($flash));
             foreach ($flash as $key => $value) {
                 if ($key !== 'tmp_name') {
                     continue;
                 }
                 @unlink($value);
             }
         }
     }
 }
Exemple #2
0
 /**
  * Initialize form if the page has one. Also catches form processing if user posts the form.
  */
 public function onPageInitialized()
 {
     /** @var Page $page */
     $page = $this->grav['page'];
     if (!$page) {
         return;
     }
     $header = $page->header();
     if (isset($header->form) && is_array($header->form)) {
         $this->active = true;
         // Create form.
         require_once __DIR__ . '/classes/form.php';
         $this->form = new Form($page);
         // Handle posting if needed.
         if (!empty($_POST)) {
             $this->form->post();
         }
     }
 }