/** * Get all forms * * @param int $limit * @param int $page * @param string $sort * @param \Pop\Module\Manager $modules * @return array */ public function getAll($limit = null, $page = null, $sort = null, \Pop\Module\Manager $modules = null) { $order = null !== $sort ? $this->getSortOrder($sort, $page) : 'id ASC'; if (null !== $limit) { $page = null !== $page && (int) $page > 1 ? $page * $limit - $limit : null; $rows = Table\Forms::findAll(['offset' => $page, 'limit' => $limit, 'order' => $order])->rows(); } else { $rows = Table\Forms::findAll(['order' => $order])->rows(); } foreach ($rows as $i => $row) { $fieldCount = []; $flds = null; if (null !== $modules && $modules->isRegistered('phire-fields')) { $flds = \Phire\Fields\Table\Fields::findAll(); } if (null !== $flds) { foreach ($flds->rows() as $f) { if (!empty($f->models)) { $models = unserialize($f->models); foreach ($models as $model) { if ($model['model'] == 'Phire\\Forms\\Model\\Form') { if ((null === $model['type_value'] || $row->id == $model['type_value']) && !in_array($row->id, $fieldCount)) { $fieldCount[] = $f->id; } } } } } } $rows[$i]->num_of_fields = count($fieldCount); $rows[$i]->num_of_submissions = Table\FormSubmissions::findBy(['form_id' => $row->id])->count(); } return $rows; }
/** * Get count of form submissions * * @param int $id * @return int */ public function getCount($id) { return Table\FormSubmissions::findBy(['form_id' => $id])->count(); }
/** * Method to process the form * * @return self */ public function process() { $fields = $this->getFields(); $submission = new Table\FormSubmissions(['form_id' => $this->id, 'timestamp' => date('Y-m-d H:i:s'), 'ip_address' => $_SERVER['REMOTE_ADDR']]); $submission->save(); unset($fields['csrf']); unset($fields['captcha']); unset($fields['id']); unset($fields['submit']); $files = []; if ($_FILES) { foreach ($_FILES as $key => $value) { if (isset($value['tmp_name']) && !empty($value['tmp_name']) && class_exists('Phire\\Fields\\Model\\Field')) { $upload = new Upload($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files'); $filename = $upload->checkFilename($value['name'], $_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files'); $fields[$key] = $filename; $files[] = $filename; $upload->upload($value); unset($_FILES[$key]); } } } $fv = new \Phire\Fields\Model\FieldValue(); $values = $fv->save($fields, $submission->id, 'Phire\\Forms\\Model\\FormSubmission'); $form = Table\Forms::findById($this->id); // If the form action is set if (!empty($form->action)) { $scheme = $form->force_ssl ? 'https://' : 'http://'; $action = substr($form->action, 0, 4) == 'http' ? $form->action : $scheme . $_SERVER['HTTP_HOST'] . BASE_PATH . $form->action; if ($form->method == 'post') { $options = [CURLOPT_POST => true, CURLOPT_POSTFIELDS => $values, CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true]; } else { $action .= '?' . http_build_query($values); $options = [CURLOPT_HEADER => false, CURLOPT_RETURNTRANSFER => true]; } $curl = new \Pop\Http\Client\Curl($action, $options); $curl->send(); unset($curl); } // Send the submission if the form "to" field is set if (!empty($form->to)) { $domain = str_replace('www.', '', $_SERVER['HTTP_HOST']); $subject = $form->name . ' : ' . $domain; // Set the recipient $rcpt = ['email' => $form->to]; $message = ''; foreach ($values as $key => $value) { $message .= ucwords(str_replace('_', ' ', $key)) . ': ' . (is_array($value) ? implode(', ', $value) : $value) . PHP_EOL; } // Send form submission $mail = new Mail($subject, $rcpt); if (!empty($form->from)) { if (!empty($form->reply_to)) { $mail->from($form->from, null, false)->replyTo($form->reply_to, null, false); } else { $mail->from($form->from); } } else { if (!empty($form->reply_to)) { $mail->replyTo($form->from); } else { $mail->from('noreply@' . $domain); } } $mail->setText($message); if (count($files) > 0) { foreach ($files as $file) { if (file_exists($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file)) { $mail->attachFile($_SERVER['DOCUMENT_ROOT'] . BASE_PATH . CONTENT_PATH . '/files/' . $file); } } } $mail->send(); } $this->clear(); if (!empty($form->redirect)) { if (substr($form->redirect, 0, 4) == 'http' || substr($form->redirect, 0, 1) == '/') { $this->redirect = true; $redirect = substr($form->redirect, 0, 4) == 'http' ? $form->redirect : BASE_PATH . $form->redirect; header('Location: ' . $redirect); exit; } else { $this->message = $form->redirect; } } return $this; }