private function send_invitations($sid, &$contacts, $remaining = 0) { if (!empty($contacts)) { $app = JFactory::getApplication(); $user = JFactory::getUser(); $model = $this->getModel('survey'); $params = JComponentHelper::getParams(S_APP_NAME); $itemid = CJFunctions::get_active_menu_id(true, 'index.php?option=' . S_APP_NAME . '&view=survey'); $editor = $user->authorise('core.wysiwyg', S_APP_NAME) ? $params->get('default_editor', 'bbcode') : 'none'; $subject = $app->input->getString('invitation-subject', JText::_('TXT_INVITE_DEFAULT_SUB')); $default_body = $editor == 'wysiwyg' ? str_replace("\n", '<br>', JText::_('TXT_INVITE_DEFAULT_BODY')) : JText::_('TXT_INVITE_DEFAULT_BODY'); $body = CJFunctions::get_clean_var('invitation-body', true, $default_body); $body = CJFunctions::process_html($body, $editor == 'bbcode'); $messageid = $app->input->getInt('messageid', 0); $count = count($contacts); $keys = $model->create_survey_keys($sid, $count); if (!empty($keys) && count($keys) > 0) { $emails = array(); foreach ($keys as $i => $key) { $link = JRoute::_('index.php?option=' . S_APP_NAME . '&view=survey&task=take_survey&key=' . $key . $itemid, false, -1); $link = '<a href="' . $link . '">' . $link . '</a>'; $email = new stdClass(); $email->name = $contacts[$i]->name; $email->subid = 0; $email->link = $link; $email->email = $contacts[$i]->email; $emails[] = $email; $contacts[$i]->key = $key; $i++; } $template = $params->get('mail-tpl-newanswer', 'mail-blue.tpl'); $sent = $model->add_messages_to_queue($sid, $subject, $body, $emails, $template, $messageid); $model->update_key_userids($sid, $contacts); if ($sent === false) { echo json_encode(array('error' => JText::_('MSG_ERROR_PROCESSING') . (S_DEBUG_ENABLED ? $model->getError() : ''))); } else { echo json_encode(array('message' => JText::sprintf('MSG_INVITATIONS_ADDED_TO_QUEUE', $sent), 'remaining' => $remaining)); } } else { echo json_encode(array('error' => JText::_('MSG_NO_CREDITS'))); } } else { echo json_encode(array('error' => JText::_('MSG_NO_CONTACTS_SELECTED'))); } }
function save_response($sid, $pid, $rid, $ignore_error = false, $skip_validations = false) { $app = JFactory::getApplication(); $user = JFactory::getUser(); $questions = $this->get_questions($sid, $pid); $config = JComponentHelper::getParams(S_APP_NAME); $html_allowed = $user->authorise('core.wysiwyg', S_APP_NAME) && $config->get('default_editor', 'bbcode') == 'wysiwyg'; // validate if legimate user if (!$user->guest && !$user->authorise('core.manage', S_APP_NAME)) { $query = 'select created_by from #__survey_responses where id = ' . $rid; $this->_db->setQuery($query); $created_by = (int) $this->_db->loadResult(); if ($created_by > 0 && $created_by != $user->id) { if (!$ignore_error) { CJFunctions::throw_error(JText::_('MSG_UNAUTHORIZED'), 401); } else { $this->setError(JText::_('MSG_UNAUTHORIZED') . '| Error: 1'); } return false; } } $rules = $this->get_conditional_rules($sid, $pid, null, true); $return = new stdClass(); $return->page_id = $return->finalize = 0; if (!empty($questions)) { $answers = array(); foreach ($questions as $question) { $free_text = null; switch ($question->question_type) { case 2: // Choice - Radio // Choice - Radio case 4: // Choice - Select box // Choice - Select box case 11: // Image - Radio $answer_id = $app->input->post->getInt('answer-' . $question->id, 0); $free_text = $app->input->post->getString('free-text-' . $question->id, null); if ($answer_id) { $answer = array(); $answer['question_id'] = $question->id; $answer['answer_id'] = $answer_id; $answer['column_id'] = 0; $answer['free_text'] = null; array_push($answers, $answer); } if (!empty($rules) && empty($return->finalize) && empty($return->page_id)) { $this->validate_rules($question->id, array($answer_id), $rules, $return, 1); } break; case 3: // Choice - Checkbox // Choice - Checkbox case 12: // Image - Checkbox $answer_ids = $app->input->post->getArray(array('answer-' . $question->id => 'array')); $free_text = $app->input->post->getString('free-text-' . $question->id, null); $answer_ids = $answer_ids['answer-' . $question->id]; JArrayHelper::toInteger($answer_ids); if (!empty($answer_ids)) { foreach ($answer_ids as $answer_id) { $answer = array(); $answer['question_id'] = $question->id; $answer['answer_id'] = $answer_id; $answer['column_id'] = 0; $answer['free_text'] = null; array_push($answers, $answer); } } if (!empty($rules) && empty($return->finalize) && empty($return->page_id)) { $this->validate_rules($question->id, $answer_ids, $rules, $return, 1); } break; case 5: // Grid - Radio $rows = array(); $columns = array(); $grid_answers = array(); foreach ($question->answers as $answer) { if ($answer->answer_type == 'x') { $rows[] = $answer; } else { if ($answer->answer_type == 'y') { $columns[] = $answer; } } } $free_text = $app->input->post->getString('free-text-' . $question->id, null); foreach ($rows as $row) { $column_id = $app->input->post->getInt('answer-' . $question->id . '-' . $row->id, 0); if ($column_id) { $answer = array(); $answer['question_id'] = $question->id; $answer['answer_id'] = $row->id; $answer['column_id'] = $column_id; $answer['free_text'] = null; array_push($grid_answers, array($row->id => $column_id)); array_push($answers, $answer); } } if (!empty($rules) && empty($return->finalize) && empty($return->page_id)) { $this->validate_rules($question->id, $grid_answers, $rules, $return, 2); } break; case 6: // Grid - Checkbox $rows = array(); $columns = array(); $grid_answers = array(); foreach ($question->answers as $answer) { if ($answer->answer_type == 'x') { $rows[] = $answer; } else { if ($answer->answer_type == 'y') { $columns[] = $answer; } } } $free_text = $app->input->post->getString('free-text-' . $question->id, null); foreach ($rows as $row) { $column_ids = $app->input->post->getArray(array('answer-' . $question->id . '-' . $row->id => 'array')); $column_ids = $column_ids['answer-' . $question->id . '-' . $row->id]; JArrayHelper::toInteger($column_ids); if (!empty($column_ids)) { foreach ($column_ids as $column_id) { $answer = array(); $answer['question_id'] = $question->id; $answer['answer_id'] = $row->id; $answer['column_id'] = $column_id; $answer['free_text'] = null; array_push($grid_answers, array($row->id => $column_id)); array_push($answers, $answer); } } } if (!empty($rules) && empty($return->finalize) && empty($return->page_id)) { $this->validate_rules($question->id, $grid_answers, $rules, $return, 2); } break; case 7: // Freetext - Singleline // Freetext - Singleline case 8: // Freetext - Multiline // Freetext - Multiline case 9: // Freetext - Password // Freetext - Password case 14: // Special - Email // Special - Email case 15: // Special - Calendar $free_text = $app->input->post->getString('free-text-' . $question->id, null); if (!empty($rules) && empty($return->finalize) && empty($return->page_id)) { $this->validate_rules($question->id, array($free_text), $rules, $return, 3); } break; case 10: // Freetext - Rich text $free_text = CJFunctions::get_clean_var('free-text-' . $question->id, $html_allowed); if (!empty($rules) && empty($return->finalize) && empty($return->page_id)) { $text = strip_tags($free_text); $this->validate_rules($question->id, array($text), $rules, $return, 3); } break; case 13: // Special - Name $names = $app->input->getArray(array('user-name-' . $question->id => 'array')); if (count($names['user-name-' . $question->id]) == 3) { $free_text = implode('|', $names['user-name-' . $question->id]); $tmp_text = str_replace('|', '', $free_text); if (empty($tmp_text)) { $free_text = ''; } $this->validate_rules($question->id, array($free_text), $rules, $return, 3); } break; case 16: // Special - Address $addr_name = $app->input->post->getString('address-name-' . $question->id, ''); $addr_line1 = $app->input->post->getString('address-line1-' . $question->id, ''); $addr_line2 = $app->input->post->getString('address-line2-' . $question->id, ''); $addr_city = $app->input->post->getString('address-city-' . $question->id, ''); $addr_state = $app->input->post->getString('address-state-' . $question->id, ''); $addr_country = $app->input->post->getString('address-country-' . $question->id, ''); $addr_zip = $app->input->post->getString('address-zip-' . $question->id, ''); if (!empty($addr_name) && !empty($addr_line1) && !empty($addr_city) && !empty($addr_state) && !empty($addr_country) && !empty($addr_zip)) { $free_text = $addr_name . '|||' . $addr_line1 . '|||' . $addr_line2 . '|||' . $addr_city . '|||' . $addr_state . '|||' . $addr_country . '|||' . $addr_zip; } break; } if ($free_text) { $answer = array(); $answer['question_id'] = $question->id; $answer['answer_id'] = 0; $answer['column_id'] = 0; $answer['free_text'] = $free_text; array_push($answers, $answer); } } $query = ' delete from #__survey_response_details where response_id=' . $rid . ' and question_id in (select id from #__survey_questions where survey_id=' . $sid . ' and page_number=' . $pid . ')'; $this->_db->setQuery($query); if ($this->_db->query()) { $query = ''; foreach ($answers as $answer) { if (empty($answer['free_text'])) { $answer['free_text'] = 'null'; } else { $answer['free_text'] = $this->_db->quote($answer['free_text']); } $query = $query . '(' . $rid . ',' . $answer['question_id'] . ',' . $answer['answer_id'] . ',' . $answer['column_id'] . ',' . $answer['free_text'] . '),'; } if (!empty($query)) { $query = 'insert into #__survey_response_details (response_id, question_id, answer_id, column_id, free_text) values ' . $query; $query = substr($query, 0, -1); $this->_db->setQuery($query); if ($this->_db->query()) { return $return; } } else { return $return; } } } $this->setError($this->_db->getErrorMsg()); return false; }