Commit b31abd6d authored by Peter Droogmans's avatar Peter Droogmans
Browse files

upgrade

parent 4c63877a
......@@ -90,22 +90,28 @@ function clientside_validation_general_settings_form($form_id, $form_state) {
function clientside_validation_settings_form($form, $form_state, $cvsid = NULL, $type = 'default') {
$settings = clientside_validation_settings_load($cvsid, $type);
if (isset($settings['settings']) && is_array($settings['settings'])) {
$settings = $settings['settings'];
}
$form['new'] = array(
'#type' => 'value',
'#value' => FALSE,
);
if (!$settings || $settings['settings'] === FALSE) {
if (!$settings || $settings['settings'] === FALSE || ($type != 'default' && empty($settings['form_id']))) {
$settings = clientside_validation_settings_load_defaults();
drupal_set_title(clientside_validation_settings_title($type, $cvsid, FALSE), PASS_THROUGH);
$form['new'] = array(
'#type' => 'value',
'#value' => TRUE,
);
}
else {
drupal_set_title(clientside_validation_settings_title($type, $settings['form_id'], TRUE), PASS_THROUGH);
}
// Make sure settings aren't nested.
if (isset($settings['settings']) && is_array($settings['settings'])) {
$settings = $settings['settings'];
}
$form['#tree'] = TRUE;
$form['cvsid'] = array(
'#type' => 'value',
......@@ -343,7 +349,7 @@ function clientside_validation_settings_form($form, $form_state, $cvsid = NULL,
'#description' => t('Validate hidden fields as well.'),
'#weight' => 2,
);
$form['include_hidden']['validate_tabs'] = array(
'#type' => 'checkbox',
'#default_value' => $settings['include_hidden']['validate_tabs'],
......@@ -411,7 +417,7 @@ function clientside_validation_settings_form_submit($form, &$form_state) {
unset($values['cvstype']);
unset($values['cvsid']);
unset($values['new']);
if ($type == 'default') {
variable_set('clientside_validation_default_settings', $values);
}
......@@ -447,7 +453,7 @@ function clientside_validation_settings_overview($form, $form_state, $type) {
$destination = array('destination' => 'admin/config/validation/clientside_validation/content-types');
$settinguri = 'admin/config/validation/clientside_validation/content-types/';
$headerlabel = t('Content type');
$content_types = node_type_get_types();
foreach ($content_types as $content_type) {
$definedforms[] = array(
......@@ -456,12 +462,12 @@ function clientside_validation_settings_overview($form, $form_state, $type) {
);
}
break;
case 'webforms':
$destination = array('destination' => 'admin/config/validation/clientside_validation/webforms');
$settinguri = 'admin/config/validation/clientside_validation/webforms/';
$headerlabel = t('Webforms');
$webform_types = webform_variable_get('webform_node_types');
$nodes = array();
if ($webform_types) {
......@@ -470,7 +476,7 @@ function clientside_validation_settings_overview($form, $form_state, $type) {
->condition('n.type', $webform_types, 'IN')
->execute()
->fetchAllAssoc('nid');
foreach ($nodes as $node) {
$definedforms[] = array(
'id' => $node->nid,
......@@ -479,12 +485,12 @@ function clientside_validation_settings_overview($form, $form_state, $type) {
}
}
break;
case 'custom-forms':
$destination = array('destination' => 'admin/config/validation/clientside_validation/custom-forms');
$settinguri = 'admin/config/validation/clientside_validation/custom-forms/';
$headerlabel = t('Form Id');
foreach ($settings as $customform) {
$definedforms[] = array(
'id' => $customform->form_id,
......@@ -513,7 +519,7 @@ function clientside_validation_settings_overview($form, $form_state, $type) {
);
break;
}
$rows = array();
if ($definedforms) {
foreach ($definedforms as $definedform) {
......@@ -594,7 +600,7 @@ function clientside_validation_settings_overview($form, $form_state, $type) {
'#rows' => $rows,
'#empty' => t('No content types available.'),
);
return $form;
}
......@@ -626,13 +632,13 @@ function clientside_validation_settings_confirmation_form($form, &$form_state, $
$info = 'You can always enable it later.';
break;
}
return confirm_form(
$form,
t($question),
'admin/config/validation/clientside_validation', // destination is used
t($info),
t(ucfirst($action)),
t(drupal_ucfirst($action)),
t('Cancel')
);
}
......
......@@ -16,10 +16,13 @@ function clientside_validation_enable() {
->condition('type', 'module')
->condition('name', 'clientside_validation')
->execute();
// @TODO: exclude admin-block-theme-form by default
}
/**
* Implements hook_schema()
* Implements hook_schema().
*/
function clientside_validation_schema() {
$schema = array();
......@@ -87,16 +90,131 @@ function clientside_validation_update_7100() {
* This update will create this table.
*/
function clientside_validation_update_7101() {
if(!db_table_exists('clientside_validation_settings')) {
if (!db_table_exists('clientside_validation_settings')) {
db_create_table('clientside_validation_settings', drupal_get_schema('clientside_validation_settings', TRUE));
}
// @TODO: Convert old settings to new
// 1. fill in clientside_validation_default_settings
$default_settings = array(
'validate_options' => array(
'disable_html5' => variable_get('clientside_validation_disable_html5', 1),
'validate_onsubmit' => variable_get('clientside_validation_validate_onsubmit', 1),
'validate_onblur' => variable_get('clientside_validation_validate_onblur', 1),
'validate_onblur_always' => variable_get('clientside_validation_validate_onblur_always', 0),
'validate_onkeyup' => variable_get('clientside_validation_validate_onkeyup', 1),
'show_messages' => variable_get('clientside_validation_show_messages', 0),
),
'error' => array(
'prefix' => variable_get('clientside_validation_prefix', ''),
'suffix' => variable_get('clientside_validation_suffix', ''),
'scrollto_errormessage' => variable_get('clientside_validation_scrollto_errormessage', 1),
'scroll_speed' => variable_get('clientside_validation_scroll_speed', 1000),
'error_element' => variable_get('clientside_validation_error_element', 'label'),
),
'error_placement' => array(
'error_placement_default' => variable_get('clientside_validation_error_placement_default', CLIENTSIDE_VALIDATION_TOP_OF_FORM),
'jquery_selector' => variable_get('clientside_validation_jquery_selector', ''),
'custom_error_function' => variable_get('clientside_validation_custom_error_function', ''),
),
'include_hidden' => array(
'include_hidden' => variable_get('clientside_validation_include_hidden', ''),
'include_hidden_fields' => FALSE, // New setting
'validate_tabs' => variable_get('clientside_validation_validate_tabs', ''),
),
);
variable_set('clientside_validation_default_settings', $default_settings);
// 2. check clientside_validation_error_placement_after_input
$after_input = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_after_input', ''), -1, PREG_SPLIT_NO_EMPTY);
foreach ($after_input as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_AFTER_INPUT;
_clientside_validation_update_7101_save($formid, $settings);
}
// 3. check clientside_validation_error_placement_after_label
$after_label = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_after_label', ''), -1, PREG_SPLIT_NO_EMPTY);
foreach ($after_label as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_AFTER_LABEL;
_clientside_validation_update_7101_save($formid, $settings);
}
// 4. check clientside_validation_error_placement_before_input
$before_input = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_before_input', ''), -1, PREG_SPLIT_NO_EMPTY);
foreach ($before_input as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_BEFORE_INPUT;
_clientside_validation_update_7101_save($formid, $settings);
}
// 5. check clientside_validation_error_placement_before_label
$before_label = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_before_label', ''), -1, PREG_SPLIT_NO_EMPTY);
foreach ($before_label as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_BEFORE_LABEL;
_clientside_validation_update_7101_save($formid, $settings);
}
// 6. check clientside_validation_error_placement_custom_function
$custom_function = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_custom_function', ''), -1, PREG_SPLIT_NO_EMPTY);
foreach ($custom_function as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_CUSTOM_ERROR_FUNCTION;
_clientside_validation_update_7101_save($formid, $settings);
}
// 7. check clientside_validation_error_placement_jquery
$jquery = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_jquery', ''), -1, PREG_SPLIT_NO_EMPTY);
$jquery_selector = variable_get('clientside_validation_jquery_selector', '');
if (!empty($jquery_selector)) {
foreach ($jquery as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_JQUERY_SELECTOR;
_clientside_validation_update_7101_save($formid, $settings);
}
}
// 8. check clientside_validation_error_placement_top_of_form
$top_of_form = preg_split('/(\r\n?|\n)/', variable_get('clientside_validation_error_placement_top_of_form', ''), -1, PREG_SPLIT_NO_EMPTY);
foreach ($after_input as $formid) {
$settings = $default_settings;
$settings['error_placement_override_default'] = TRUE;
$settings['error_placement']['error_placement_default'] = CLIENTSIDE_VALIDATION_TOP_OF_FORM;
_clientside_validation_update_7101_save($formid, $settings);
}
// 9. delete old vars? @TODO: good idea?
}
function _clientside_validation_update_7101_save($formid, $settings) {
static $contenttypes;
$saved = FALSE;
if (!$contenttypes) {
$contenttypes = node_type_get_types();
}
$formid = str_replace('-', '_', $formid);
if (strpos($formid, 'webform_client_form_') !== FALSE) {
$nid = str_replace('webform_client_form_', '', $formid);
if (is_numeric($nid)) {
clientside_validation_settings_add('webforms', $nid, $settings, TRUE);
$saved = TRUE;
}
}
if (strpos($formid, '_node_form') !== FALSE) {
$type = str_replace('_node_form', '', $formid);
if (is_string($type) && array_key_exists($type, $contenttypes)) {
clientside_validation_settings_add('content-types', $type, $settings, TRUE);
$saved = TRUE;
}
}
if (!$saved) {
clientside_validation_settings_add('custom-forms', $formid, $settings, TRUE);
}
}
\ No newline at end of file
......@@ -114,7 +114,7 @@ function clientside_validation_menu() {
'access arguments' => array('administer site configuration'),
'file' => 'clientside_validation.admin.inc',
);
$items['clientside_validation/ajax'] = array(
'title' => 'Clientside validation ajax callback',
'page callback' => '_clientside_validation_ajax_call',
......@@ -131,12 +131,12 @@ function clientside_validation_menu() {
}
function _clientside_validation_ajax_call() {
$param = $_POST['param'];
$value = $_POST['value'];
$param = check_plain($_POST['param']);
$value = check_plain($_POST['value']);
$result = array();
foreach ($param['expressions'] as $key => $regex) {
if (!(bool) preg_match($regex, (string) $value)) {
$result = array('result' => FALSE, 'message' => $param['messages'][$key]);
$result = array('result' => FALSE, 'message' => $param['messages'][$key]);
break;
}
}
......@@ -147,8 +147,8 @@ function _clientside_validation_ajax_call() {
}
function _clientside_validation_ajax_phone() {
$value = $_POST['value'];
$country_code = $_POST['country_code'];
$value = check_plain($_POST['value']);
$country_code = check_plain($_POST['country_code']);
$result['result'] = valid_phone_number($country_code, $value);
drupal_json_output($result);
}
......@@ -186,7 +186,7 @@ function clientside_validation_form_alter(&$form, &$form_state, $form_id) {
&& in_array($form_id, $validate_specific)) {
return;
}
if (strpos($form_id, 'webform_client_form') !== FALSE) {
$definedsettings = clientside_validation_settings_load(NULL, 'webforms', $form['#node']->nid);
clientside_validation_settings_current_form($definedsettings);
......@@ -358,7 +358,7 @@ function _clientside_validation_add_general_settings(&$settings, $form_id) {
}
$form_settings_added[] = str_replace('-', '_', $form_id);
$settings['clientsideValidation']['forms'][$form_id]['general'] = array(
"errorClass" => "error", // @TODO: option?
"wrapper" => "li", // @TODO: option?
......@@ -1115,18 +1115,20 @@ function clientside_validation_settings_enable($cvsid) {
function clientside_validation_settings_disable($cvsid) {
db_update('clientside_validation_settings')
->fields(array(
'status' => 0,
'status' => 0,
))
->condition('cvsid', $cvsid)
->execute();
}
function clientside_validation_settings_add($type, $cvsid, $settings = array()) {
db_insert('clientside_validation_settings')
->fields(array(
function clientside_validation_settings_add($type, $cvsid, $settings = array(), $enable = FALSE) {
return db_merge('clientside_validation_settings')
->key(array(
'type' => $type,
'status' => 0,
'form_id' => $cvsid,
))
->fields(array(
'status' => $enable ? 1 : 0,
'settings' => serialize($settings),
))
->execute();
......@@ -1141,6 +1143,33 @@ function clientside_validation_settings_update($cvsid, $settings) {
->execute();
}
function clientside_validation_settings_title($type, $form_id, $edit = TRUE) {
$title = 'Edit settings of %formid';
if (!$edit) {
$title = 'Create settings for %formid';
}
switch ($type) {
case 'content-types':
$content_types = node_type_get_types();
if (isset($content_types[$form_id])) {
return t($title, array('%formid' => t($content_types[$form_id]->name)));
}
break;
case 'webforms':
dpm($form_id, $type);
$node = node_load($form_id);
if ($node) {
return t($title, array('%formid' => $node->title));
}
break;
case 'custom-forms':
return t($title, array('%formid' => $form_id));
break;
}
return t($title, array('%formid' => $type));
}
function clientside_validation_settings_current_form($settings = NULL) {
$current = &drupal_static(__FUNCTION__, array());
if (isset($settings)) {
......@@ -1163,7 +1192,6 @@ function clientside_validation_settings_load_defaults() {
function clientside_validation_settings_load($cvsid = NULL, $type = NULL, $form_id = NULL) {
if ($type == 'default') {
$default_settings = array(
'type' => 'default',
'validate_options' => array(
'disable_html5' => 1,
'validate_onsubmit' => 1,
......@@ -1197,7 +1225,7 @@ function clientside_validation_settings_load($cvsid = NULL, $type = NULL, $form_
if (!empty($cvsid)) {
$query->condition('cvsid', $cvsid);
}
if (!empty($type)){
if (!empty($type)) {
$query->condition('type', $type);
}
if (!empty($form_id)) {
......@@ -1205,7 +1233,7 @@ function clientside_validation_settings_load($cvsid = NULL, $type = NULL, $form_
}
$result = $query->execute();
// a single id was given -> one result, so only return one row
if (is_numeric($cvsid) || isset($form_id)) {
$res = $result->fetchAssoc();
......
......@@ -21,7 +21,7 @@ function clientside_validation_field_validation_clientside_validation_form_alter
$field = field_info_field($rule['rule']['field_name']);
$languages = field_available_languages($rule['rule']['entity_type'], $field);
foreach ($languages as $langcode) {
if(isset($element[$langcode])) {
if (isset($element[$langcode])) {
$element = &$element[$langcode];
}
foreach (element_children($element) as $delta) {
......@@ -67,7 +67,7 @@ function clientside_validation_field_validation_after_build_recurse($form_id, &$
$types = array(
'textfield', 'textarea', 'select', 'radio', 'checkbox', 'password', 'file', 'radios', 'checkboxes',
);
if (isset($element['#type']) && in_array($element['#type'], $types) && isset($element['#name']) /*&& strpos($element['#name'], '[value]') !== FALSE*/) {
if (isset($element['#type']) && in_array($element['#type'], $types) && isset($element['#name']) /* && strpos($element['#name'], '[value]') !== FALSE */) {
clientside_validation_field_validation_regular($form_id, $element, $rule, $js_rules);
}
clientside_validation_field_validation_after_build_recurse($form_id, $element, $form_state, $rule, $js_rules);
......
......@@ -370,7 +370,7 @@ function clientside_validation_webform_form_webform_component_edit_form_alter(&$
'#description' => 'Check this option if you do not want to add clientside validation.',
'#default_value' => isset($component['extra']['exclude_cv']) ? $component['extra']['exclude_cv'] : FALSE,
);
array_unshift ($form['#submit'], 'clientside_validation_webform_webform_component_edit_form_submit');
array_unshift($form['#submit'], 'clientside_validation_webform_webform_component_edit_form_submit');
}
function clientside_validation_webform_webform_component_edit_form_submit($form, &$form_state) {
......
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment