Commit 40a18695 authored by yched's avatar yched

#112464 - refactor userref and noderef validation steps (plus fix / simplify...

#112464 - refactor userref and noderef validation steps (plus fix / simplify _userreference_potential_references)
parent b6248f7a
......@@ -107,28 +107,12 @@ function nodereference_field_settings($op, $field) {
function nodereference_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
$refs = _nodereference_potential_references($field, TRUE);
foreach ($items as $delta => $item) {
$error_field = $field['field_name'].']['.$delta.'][node_name';
if (!empty($item['node_name'])) {
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
if (!empty($matches)) {
// explicit nid
list(, $title, $nid) = $matches;
$refs = _nodereference_potential_references($field, TRUE);
if (!in_array($nid, array_keys($refs))) {
form_set_error($error_field, t('This post can\'t be referenced.'));
}
elseif (!empty($title) && ($n = $refs[$nid]) && $title != $n->node_title) {
form_set_error($error_field, t('Title mismatch. Please reiterate your selection.'));
}
}
else {
// no explicit nid
$refs = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
if (empty($refs)) {
form_set_error($error_field, t('No post with that title can be referenced.'));
}
}
$error_field = isset($item['error_field']) ? $item['error_field'] : '';
unset($item['error_field']);
if (!in_array($item['nid'], array_keys($refs))) {
form_set_error($error_field, t('This post can\'t be referenced.'));
}
}
return;
......@@ -242,6 +226,9 @@ function nodereference_widget($op, &$node, $field, &$items) {
}
// Remove the widget's data representation so it isn't saved.
unset($items['nids']);
foreach($items as $delta => $item) {
$items[$delta]['error_field'] = $field['field_name'].'][nids';
}
}
}
else {
......@@ -297,6 +284,22 @@ function nodereference_widget($op, &$node, $field, &$items) {
}
return $form;
case 'validate':
foreach ($items as $delta => $item) {
$error_field = $field['field_name'].']['.$delta.'][node_name';
if (!empty($item['node_name'])) {
preg_match('/^(?:\s*|(.*) )?\[\s*nid\s*:\s*(\d+)\s*\]$/', $item['node_name'], $matches);
if (!empty($matches)) {
// explicit nid
list(, $title, $nid) = $matches;
if (!empty($title) && ($n = node_load($nid)) && $title != $n->title) {
form_set_error($error_field, t('Title mismatch. Please check your selection.'));
}
}
}
}
return;
case 'process form values':
foreach ($items as $delta => $item) {
$nid = 0;
......@@ -312,18 +315,19 @@ function nodereference_widget($op, &$node, $field, &$items) {
// the best thing would be to present the user with an additional form,
// allowing the user to choose between valid candidates with the same title
// ATM, we pick the first matching candidate...
$nids = _nodereference_potential_references($field, false, $item['node_name'], true);
$nid = array_shift(array_keys($nids));
$nids = _nodereference_potential_references($field, FALSE, $item['node_name'], TRUE);
$nid = (!empty($nids)) ? array_shift(array_keys($nids)) : 0;
}
}
if (!empty($nid)) {
$items[$delta]['nid'] = $nid;
// Remove the widget's data representation so it isn't saved.
unset($items[$delta]['node_name']);
$items[$delta]['error_field'] = $field['field_name'].']['.$delta.'][node_name';
}
else {
elseif ($delta > 0) {
// Don't save empty fields when they're not the first value (keep '0' otherwise)
if ($delta > 0) {unset($items[$delta]);}
unset($items[$delta]);
}
}
break;
......
......@@ -70,9 +70,10 @@ function userreference_field($op, &$node, $field, &$items, $teaser, $page) {
switch ($op) {
case 'validate':
foreach ($items as $delta => $item) {
$error_field = $field['field_name'].']['.$delta.'][user_name';
if ($item['user_name'] && !in_array($item['user_name'], _userreference_potential_references($field))) {
form_set_error($error_field, t('Invalid user name.'));
$error_field = isset($item['error_field']) ? $item['error_field'] : '';
unset($item['error_field']);
if (!empty($item['uid']) && !in_array($item['uid'], array_keys(_userreference_potential_references($field)))) {
form_set_error($error_field, t('Invalid user.'));
}
}
return;
......@@ -148,6 +149,9 @@ function userreference_widget($op, &$node, $field, &$items) {
$form = array();
$options = _userreference_potential_references($field);
if (!$field['required']) {
$options = array('none' => t('<none>')) + $options;
}
if (empty($items['default uids'])) {
$items['default uids'][] = 'none';
}
......@@ -157,7 +161,7 @@ function userreference_widget($op, &$node, $field, &$items) {
'#title' => t($field['widget']['label']),
'#default_value' => $items['default uids'],
'#multiple' => $field['multiple'],
'#size' => $field['multiple'] ? min(count($options), 6) : 0,
'#size' => $field['multiple'] ? min(count($options), 6) : 0,
'#options' => $options,
'#required' => $field['required'],
'#description' => $field['widget']['description'],
......@@ -181,6 +185,9 @@ function userreference_widget($op, &$node, $field, &$items) {
}
// Remove the widget's data representation so it isn't saved.
unset($items['uids']);
foreach($items as $delta => $item) {
$items[$delta]['error_field'] = $field['field_name'].'][uids';
}
}
}
else {
......@@ -244,6 +251,7 @@ function userreference_widget($op, &$node, $field, &$items) {
// Remove the widget's data representation so it isn't saved.
unset($items[$delta]['user_name']);
$items[$delta]['uid'] = $uid;
$items[$delta]['error_field'] = $field['field_name'].']['.$delta.'][user_name';
// Don't save empty fields except the first value
if (empty($uid) && $delta > 0) {
unset($items[$delta]);
......@@ -257,27 +265,20 @@ function userreference_widget($op, &$node, $field, &$items) {
* Fetch an array of all candidate referenced users, for use in presenting the selection form to the user.
*/
function _userreference_potential_references($field) {
if (!$field['required']) {
$users = array('none' => t('<none>'));
}
else {
$users = array();
}
$roles = array();
if (isset($field['referenceable_roles'])) {
foreach ($field['referenceable_roles'] as $role) {
if ($role) {
$roles[] = $role;
}
}
// filter invalid values that seems to get through sometimes ??
$field['referenceable_roles'] = array_intersect(user_roles(1), $field['referenceable_roles']);
$roles = array_keys(array_filter($field['referenceable_roles']));
}
if (!$roles || !isset($field['referenceable_roles']) || in_array(DRUPAL_AUTHENTICATED_RID, $field['referenceable_roles'])) {
if (empty($roles) || in_array(DRUPAL_AUTHENTICATED_RID, $roles)) {
$result = db_query('SELECT u.name, u.uid FROM {users} u WHERE uid > 0 ORDER BY u.name ASC');
}
else {
$result = db_query('SELECT u.name, u.uid FROM {users} u LEFT JOIN {users_roles} r ON u.uid = r.uid WHERE u.uid > 0 AND r.rid IN ('. implode($roles, ',') .') ORDER BY u.name ASC');
}
$users = array();
while ($user = db_fetch_object($result)) {
$users[$user->uid] = $user->name;
}
......
Markdown is supported
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