Example #1
0
 /**
  * save data to database
  *
  * @param   string  $integration_key  integration key
  * @param   array   $options          options: skip_captcha, ...
  * @param   array   $data             form data, leave null to use posted data
  *
  * @return boolean|stdclass
  */
 public function apisaveform($integration_key = '', $options = array(), $data = null)
 {
     $app = JFactory::getApplication();
     $db = $this->_db;
     $result = new stdclass();
     // create a new class for this ?
     $result->posts = array();
     /* Default values */
     $answer = '';
     $return = false;
     $redcompetition = false;
     $redevent = false;
     // Check the token
     $token = JSession::getFormToken();
     // Get data from post if not specified
     if (!$data) {
         $data = array_merge(JRequest::get('post'), JRequest::get('files'));
         if (!isset($data['form_id'])) {
             $data['form_id'] = $app->input->getInt('form_id');
         }
         if (!isset($data['submit_key'])) {
             $data['submit_key'] = $app->input->getCmd('submit_key', false);
         }
         if (!isset($data['curform'])) {
             $data['curform'] = $app->input->getInt('curform', 1);
         }
     }
     if (!isset($data[$token])) {
         $this->setError('Form integrity check failed');
         return false;
     }
     $check_captcha = JFactory::getSession()->get('checkcaptcha' . $data[$token], 0);
     if (!isset($data['submit_key']) || !$data['submit_key']) {
         $submit_key = uniqid();
     } else {
         $submit_key = $data['submit_key'];
     }
     $this->setSubmitKey($submit_key);
     $result->submit_key = $submit_key;
     /* Get the form details */
     $form = $this->getForm($data['form_id']);
     /* Load the fields */
     $fieldlist = $this->getfields($form->id);
     // number of submitted active forms (min is 1)
     $totalforms = isset($data['curform']) ? $data['curform'] : 1;
     $allanswers = array();
     /* Loop through the different forms */
     for ($signup = 1; $signup <= $totalforms; $signup++) {
         // new answers object
         $answers = new rfanswers();
         $answers->setFormId($form->id);
         if (isset($options['baseprice'])) {
             if (is_array($options['baseprice'])) {
                 $answers->initPrice(isset($options['baseprice'][$signup - 1]) ? $options['baseprice'][$signup - 1] : 0);
             } else {
                 $answers->initPrice($options['baseprice']);
             }
         }
         /* Create an array of values to store */
         $postvalues = array();
         // remove the _X parts, where X is the form (signup) number
         foreach ($data as $key => $value) {
             if (strpos($key, 'field') === 0 && strpos($key, '_' . $signup, 5) > 0) {
                 $postvalues[str_replace('_' . $signup, '', $key)] = $value;
             } else {
                 $postvalues[$key] = $value;
             }
         }
         if (isset($data['submitter_id' . $signup])) {
             $postvalues['sid'] = intval($data['submitter_id' . $signup]);
         }
         $postvalues['form_id'] = $data['form_id'];
         $postvalues['submit_key'] = $submit_key;
         $postvalues['integration'] = $integration_key;
         /* Get the raw form data */
         $postvalues['rawformdata'] = serialize($data);
         /* Build up field list */
         foreach ($fieldlist as $key => $field) {
             if (isset($postvalues['field' . $key])) {
                 /* Get the answers */
                 try {
                     $answers->addPostAnswer($field, $postvalues['field' . $key]);
                 } catch (Exception $e) {
                     $this->setError($e->getMessage());
                     return false;
                 }
             }
         }
         $allanswers[] = $answers;
     }
     /* End multi-user signup */
     $this->_answers = $allanswers;
     // Save to session in case we need to display form again
     $sessiondata = array();
     foreach ($allanswers as $a) {
         $sessiondata[] = $a->toSession();
     }
     $app->setUserState('formdata' . $data['form_id'], $sessiondata);
     // Captcha verification
     if ($check_captcha) {
         JPluginHelper::importPlugin('redform_captcha');
         $res = true;
         $dispatcher = JDispatcher::getInstance();
         $results = $dispatcher->trigger('onCheckCaptcha', array(&$res));
         if (count($results) && $res == false) {
             // Save to session
             $sessiondata = array();
             foreach ($allanswers as $a) {
                 $sessiondata[] = $a->toSession();
             }
             $app->setUserState($submit_key, $sessiondata);
             $this->setError(JText::_('COM_REDFORM_CAPTCHA_WRONG'));
             return false;
         }
     }
     // Savetosession: data is saved to session using the submit key
     if (isset($options['savetosession'])) {
         $sessiondata = array();
         foreach ($allanswers as $a) {
             $sessiondata[] = $a->toSession();
         }
         $app->setUserState($submit_key, $sessiondata);
         return $result;
     }
     // Else save to db !
     foreach ($allanswers as $answers) {
         $res = $answers->savedata($postvalues);
         if (!$res) {
             $this->setError(JText::_('COM_REDFORM_SAVE_ANSWERS_FAILED'));
             return false;
         } else {
             // Delete session data
             $app->setUserState('formdata' . $form->id, null);
             $result->posts[] = array('sid' => $res);
         }
         if ($answers->isNew()) {
             $this->updateMailingList($answers);
         }
     }
     // send email to maintainers
     $this->notifymaintainer($allanswers, $answers->isNew());
     /* Send a submission mail to the submitters if set */
     if ($answers->isNew() && $form->submitterinform) {
         foreach ($allanswers as $answers) {
             $this->notifysubmitter($answers, $form);
         }
     }
     return $result;
 }