示例#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;
 }
示例#2
0
 function store()
 {
     echo '<pre>';
     print_r('deprecated ?');
     echo '</pre>';
     exit;
     $mainframe =& JFactory::getApplication();
     $db =& $this->_db;
     /* Default values */
     $answer = '';
     $return = false;
     $redcompetition = false;
     $redevent = false;
     $event_task = JRequest::getVar('event_task');
     $submitter_id = Jrequest::getInt('submitter_id', 0);
     if ($submitter_id) {
         $submitter = $this->getSubmitter($submitter_id);
         $submit_key = $submitter->submit_key;
     } else {
         $submitter = false;
         $submit_key = uniqid();
     }
     /* Get the form details */
     $form = $this->getForm(JRequest::getInt('form_id'));
     /* Load the fields */
     $fieldlist = $this->getfields($form->id);
     /* Load the posted variables */
     $post = JRequest::get('post');
     $files = JRequest::get('files');
     $posted = array_merge($post, $files);
     /* See if we have an event ID */
     if (JRequest::getInt('event_xref', 0)) {
         $redevent = true;
         $posted['xref'] = JRequest::getInt('event_xref', 0);
     } else {
         if (isset($post['integration']) && $post['integration'] == 'redevent') {
             $redevent = true;
             $posted['xref'] = JRequest::getInt('xref', 0);
         } else {
             if (JRequest::getInt('competition_id', 0)) {
                 $redcompetition = true;
                 $posted['xref'] = JRequest::getInt('competition_id', 0);
             } else {
                 $posted['xref'] = 0;
             }
         }
     }
     if ($posted['xref'] && $redevent) {
         $event = $this->getEvent($posted['xref']);
     } else {
         $event = null;
     }
     // new answers object
     $answers = new rfanswers();
     $answers->setFormId($form->id);
     if ($event) {
         $answers->initPrice($event->course_price);
     }
     /* Create an array of values to store */
     $postvalues = array();
     // remove the _X parts, where X is the form (signup) number
     $signup = 1;
     foreach ($posted as $key => $value) {
         if (strpos($key, 'field') === 0 && strpos($key, '_' . $signup, 5) > 0) {
             $postvalues[str_replace('_' . $signup, '', $key)] = $value;
         }
     }
     /* Some default values needed */
     $postvalues['xref'] = $post['xref'];
     $postvalues['form_id'] = $post['form_id'];
     $postvalues['submit_key'] = $submit_key;
     if (isset($post['integration'])) {
         $postvalues['integration'] = $post['integration'];
     }
     /* Get the raw form data */
     $postvalues['rawformdata'] = serialize($posted);
     /* 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;
             }
         }
     }
     if ($submitter) {
         // this 'anwers' were already posted
         $answers->setAnswerId($submitter->answer_id);
     }
     // save answers
     if (!$answers->save($postvalues)) {
         return false;
     }
     // add an attendee in redevent ?
     $uid = JRequest::getInt('uid');
     $this->updateMailingList($answers);
     return true;
 }