View source
<?php
function makemeeting_answers_form($form, &$form_state, $item, $instance, $answer = NULL) {
global $user;
$form = [];
$form['#item'] = $item;
$form['#theme'] = 'makemeeting_answers';
form_load_include($form_state, 'inc', 'makemeeting', 'makemeeting.form');
$form['#id'] = 'makemeeting-answers-form';
foreach ([
'field_name',
'entity_type',
'deleted',
'entity_id',
'language',
'delta',
] as $info) {
$form[$info] = [
'#type' => 'value',
'#value' => $instance[$info],
];
}
if ($answer) {
$form['answer_edited'] = [
'#type' => 'value',
'#value' => $answer,
];
}
if (!isset($form_state['ajax_destination'])) {
$form_state['ajax_destination'] = drupal_get_destination();
}
$form['name'] = [
'#type' => 'textfield',
'#size' => 22,
];
if (!user_is_logged_in()) {
$form['name']['#attributes'] = [
'placeholder' => t('Your name (required)'),
];
$form['name']['#required'] = TRUE;
}
else {
$form['name']['#default_value'] = format_username($user);
$form['name']['#disabled'] = TRUE;
}
if (!empty($answer)) {
if ($answer->uid > 0) {
$account = user_load($answer->uid);
$form['name']['#default_value'] = format_username($account);
}
else {
$form['name']['#default_value'] = $answer->name;
}
}
$answers = [];
if ($item['limit'] > 0) {
$select = db_select('makemeeting_answers', 'ma')
->fields('ma', [
'value',
]);
foreach ([
'field_name',
'entity_type',
'deleted',
'entity_id',
'language',
'delta',
] as $info) {
$select
->condition($info, $instance[$info]);
}
if ($answer) {
$select
->condition('answer_id', $answer->answer_id, '!=');
}
$results = $select
->execute();
foreach ($results as $result) {
$_answer = unserialize($result->value);
if (is_array($_answer)) {
foreach ($_answer as $key => $value) {
if ($value) {
$answers[$key] = empty($answers[$key]) ? 1 : $answers[$key] + 1;
}
}
}
elseif (is_string($_answer) && $_answer) {
$answers[$_answer] = empty($answers[$_answer]) ? 1 : $answers[$_answer] + 1;
}
}
}
$form['answers'] = [];
foreach ($item['choices'] as $choice) {
$chdate = _makemeeting_date_timestamp($choice['chdate']);
$count = 0;
foreach ($choice['chsuggestions'] as $id => $text) {
if ($text || !$text && !$count) {
_makemeeting_answer_element($form, $item, $id, $chdate, $text, $answers, $answer);
}
$count++;
}
}
$form['submit'] = [
'#type' => 'submit',
'#value' => t('Submit'),
];
if (!empty($answer)) {
$form['submit']['#ajax'] = [
'callback' => 'makemeeting_answer_js',
'wrapper' => 'makemeeting-answers-form',
'effect' => 'fade',
];
}
return $form;
}
function makemeeting_answers_form_validate($form, &$form_state) {
if (!$form_state['values']['name']) {
drupal_set_message(t('You must enter your name.'), 'error');
}
if (user_is_logged_in() && empty($form_state['values']['answer_edited'])) {
global $user;
$select = db_select('makemeeting_answers', 'ma');
foreach ([
'field_name',
'entity_type',
'deleted',
'entity_id',
'language',
'delta',
] as $info) {
$select
->condition($info, $form_state['values'][$info]);
}
$result = $select
->condition('uid', $user->uid)
->countQuery()
->execute()
->fetchField();
if ($result) {
form_error($form, t('You already voted on this poll.'));
}
}
}
function makemeeting_answers_form_submit($form, $form_state) {
global $user;
if (!empty($form_state['values']['answer_edited'])) {
db_update('makemeeting_answers')
->fields([
'name' => $form_state['values']['name'],
'value' => serialize($form_state['values']['answers']),
])
->condition('answer_id', $form_state['values']['answer_edited']->answer_id)
->execute();
}
else {
$fields = [];
foreach ([
'field_name',
'entity_type',
'deleted',
'entity_id',
'language',
'delta',
'name',
] as $field) {
$fields[$field] = $form_state['values'][$field];
}
db_insert('makemeeting_answers')
->fields($fields + [
'value' => serialize($form_state['values']['answers']),
'uid' => $user->uid,
])
->execute();
}
if (current_path() === 'system/ajax') {
$destination =& drupal_static('drupal_get_destination');
$destination = $form_state['ajax_destination'];
}
_makemeeting_clear_related_entity_cache($form_state['values']['entity_type'], $form_state['values']['entity_id']);
}
function _makemeeting_answer_element(&$form, $item, $id, $originalDate, $text, $answers = [], $answer = NULL) {
$key = $originalDate
->format('d-m-Y') . ':' . $id;
if ($item['limit'] > 0 && isset($answers[$key]) && $answers[$key] >= $item['limit']) {
$form['answers'][$key] = [
'#markup' => t('Unavailable'),
];
}
else {
$dateToParse = $originalDate
->format('d-m-Y') . ' ' . $text;
$tz = new DateTimeZone($item['timezone']);
$userTz = new DateTimeZone(drupal_get_user_timezone());
if ($date = DateTime::createFromFormat('d-m-Y H\\h', $dateToParse, $tz)) {
$title = $date
->setTimezone($userTz)
->format('l j F Y H\\h');
}
elseif ($date = DateTime::createFromFormat('d-m-Y H:i', $dateToParse, $tz)) {
$title = $date
->setTimezone($userTz)
->format('l j F Y H:i');
}
elseif ($date = DateTime::createFromFormat('d-m-Y H a', $dateToParse, $tz)) {
$title = $date
->setTimezone($userTz)
->format('l j F Y H a');
}
else {
$title = $originalDate
->format('l j F Y') . ' ' . $text;
}
$form['answers'][$key] = [
'#type' => $item['one_option'] ? 'radio' : ($item['yesnomaybe'] ? 'radios' : 'checkbox'),
'#attributes' => [
'title' => check_plain($title),
],
'#parents' => [
'answers',
$key,
],
];
if ($item['one_option']) {
$form['answers'][$key]['#parents'] = [
'answers',
];
$form['answers'][$key]['#return_value'] = $key;
}
else {
$form['answers'][$key]['#options'] = _makemeeting_options($item['yesnomaybe']);
}
if ($item['yesnomaybe']) {
$form['answers'][$key]['#default_value'] = MAKEMEETING_NO;
}
if ($answer && !empty($answer->value[$key])) {
$form['answers'][$key]['#default_value'] = $answer->value[$key];
}
}
}